public double[] ReverseSubstitution(MatrixClass matrix) { int length = matrix.RecieveEquation(0).Array.Length; double[] result = new double[matrix.Length]; for (int i = matrix.Length - 1; i >= 0; i--) { double val = matrix.RecieveEquation(i).Array[length - 1]; for (int x = length - 2; x > i - 1; x--) { if (result.Length <= x || matrix.RecieveEquation(i).Array.Length <= x) { break; } val -= matrix.RecieveEquation(i).Array[x] * result[x]; } result[i] = val / matrix.RecieveEquation(i).Array[i]; if (!Confirmation(result[i])) { throw new Exception("Can not calculate this"); } } return(result); }
private bool PivotProcedure(MatrixClass matrix, int row, int column) { bool swapped = false; for (int z = matrix.Length - 1; z > row; z--) { if (matrix.RecieveEquation(z).Array[row] != 0) { _ = new double[matrix.RecieveEquation(0).Array.Length]; double[] temp = matrix.RecieveEquation(z).Array; matrix.LayoutOfEquation(z, matrix.RecieveEquation(column)); matrix.LayoutOfEquation(column, new LinearEquationClass(temp)); swapped = true; } } return(swapped); }
private static void Main(string[] args) { if (args is null) { throw new ArgumentNullException(nameof(args)); } MainProgram(); void MainProgram() { Console.WriteLine("Type linear equations in augmented matrix notation: a1 a2... aN d,\n where a1..N are coefficients and D is constant\n Type 'END' or 'end' or 'done'when you finish typing the equation"); int index = 1; Matrix = new MatrixClass(); try { while (true) { Console.Write($"Eq #{index++}: "); String input = Console.ReadLine(); if (input.Trim() == "END" || input.Trim() == "end" || input.Trim() == "done") { break; } Matrix.JoinEquation(new LinearEquationClass(input)); } Console.WriteLine("You have entered following equations:"); for (int i = 0; i < Matrix.Length; i++) { Console.Write($"Eq #{i}: {Matrix.RecieveEquation(i)}\n"); } Console.WriteLine("Result is:"); Matrix.ShowResults(); } catch (Exception e) { Console.WriteLine("Error occured: " + e.Message); } LoopOfMainProgram(); } void LoopOfMainProgram() { while (true) { Console.WriteLine("--------------------------------------"); Console.WriteLine("Do you want to calculate another Equation? \n Type 'yes' or 'no'"); string answer = Console.ReadLine(); if (answer == "yes") { Console.Clear(); MainProgram(); break; } else if (answer == "no") { Environment.Exit(0); break; } else { Console.WriteLine("Wrong answer"); } } } }