internal override bool Check(LinearSystem system) { bool check = true; List <List <double> > A = new List <List <double> >(); List <double> B = new List <double>(); PreCalculations(system, out A, out B); int rangA = 0, rangB = 0; for (int i = 0; i < B.Count; i++) { if (A[i][i] != 0.0) { rangA++; } if (B[i] != 0.0) { rangB++; } } if (rangA != rangB) { check = false; } return(check); }
internal override List <double> FinalCalculations(LinearSystem system) { if (Check(system) == true) { List <double> x = new List <double>(); for (int i = 0; i < system.NumberOfUnknowns; i++) { x.Add(0.0); } List <List <double> > A = new List <List <double> >(); List <double> B = new List <double>(); PreCalculations(system, out A, out B); for (int k = system.NumberOfUnknowns - 1; k >= 0; k--) { x[k] = B[k]; for (int i = 0; i < k; i++) { B[i] = B[i] - A[i][k] * x[k]; } } for (int i = 0; i < system.NumberOfUnknowns; i++) { Console.WriteLine($"x{i + 1} = {x[i]}; "); } return(x); } else { Console.WriteLine("System has endless number of solutions or none, please enter another system"); return(null); } }
internal override void PreCalculations(LinearSystem system, out List <List <double> > A, out List <double> B) { A = new List <List <double> >(system.Coefficients.Count); for (int i = 0; i < system.Coefficients.Count; i++) { List <double> row = new List <double>(system.Coefficients[i]); A.Add(row); } B = new List <double>(system.ConstantTerms); int k = 0; double Amax; while (k < system.NumberOfEquations) { Amax = A[k][k]; int p = k; for (int i = k + 1; i < system.NumberOfEquations; i++) { if (Math.Abs(A[i][k]) > Amax) { Amax = Math.Abs(A[i][k]); p = i; } } for (int i = 0; i < system.NumberOfUnknowns; i++) { double tempA = A[k][i]; A[k][i] = A[p][i]; A[p][i] = tempA; } double temp = B[k]; B[k] = B[p]; B[p] = temp; temp = A[k][k]; for (int i = 0; i < system.NumberOfEquations; i++) { A[k][i] = A[k][i] / temp; } B[k] = B[k] / temp; Console.WriteLine("Converted matrixes on current step: "); ShowMatrixes(A, B, system.NumberOfEquations, system.NumberOfUnknowns); Console.WriteLine(""); for (int i = k + 1; i < system.NumberOfEquations; i++) { double M = A[i][k]; for (int j = k; j < system.NumberOfEquations; j++) { A[i][j] = A[i][j] - M * A[k][j]; } B[i] = B[i] - M * B[k]; } Console.WriteLine("Converted matrixes on current step: "); ShowMatrixes(A, B, system.NumberOfEquations, system.NumberOfUnknowns); Console.WriteLine(""); k++; } } //method that convertes matrixes to triagonal form
//class to output linear system to the screen public static void Show(LinearSystem system) { for (int i = 0; i < system.NumberOfEquations; i++) { Console.Write($"{system.Coefficients[i][0]} * x1 "); for (int j = 1; j < system.NumberOfUnknowns; j++) { Console.Write($"+ {system.Coefficients[i][j]} * x{j + 1} "); } Console.WriteLine($"= {system.ConstantTerms[i]}"); } }
internal void OutputResultsToFile(TextWriter w, LinearSystem system, List <double> results, List <double> r) //method to output final results to a file { w.WriteLine("System: "); LinearSystemOperator.FileOutput(system, w); w.WriteLine("Results: "); for (int i = 0; i < system.NumberOfUnknowns; i++) { w.WriteLine($"x{i + 1} = {results[i]}; "); } w.WriteLine("Error: "); for (int i = 0; i < system.NumberOfUnknowns; i++) { w.WriteLine($"Error for x{i + 1} = {r[i]}"); } }
protected internal List <double> R(LinearSystem system, List <double> results) //method to calculate error //b-A*x { List <double> r = new List <double>(); List <double> xA = new List <double>(); Console.WriteLine(" "); for (int i = 0; i < system.NumberOfUnknowns; i++) { xA.Add(0.0); r.Add(0.0); for (int j = 0; j < system.NumberOfUnknowns; j++) { xA[i] += system.Coefficients[i][j] * results[j]; } r[i] = system.ConstantTerms[i] - xA[i]; Console.WriteLine($"Error for x{i + 1} = {r[i]}"); } return(r); }
public static void FileOutput(LinearSystem system, TextWriter w) { try { for (int i = 0; i < system.NumberOfEquations; i++) { w.Write($"{system.Coefficients[i][0]} * x1 "); for (int j = 1; j < system.NumberOfUnknowns; j++) { w.Write($"+ {system.Coefficients[i][j]} * x{j + 1} "); } w.WriteLine($"= {system.ConstantTerms[i]}"); } } catch (Exception ex) { Console.WriteLine($"{ ex.GetType()} says { ex.Message}"); Console.ReadLine(); } }
internal override List <double> FinalCalculations(LinearSystem system) { if (Check(system) == true) { List <double> x = new List <double>(); var A = new List <List <double> >(system.Coefficients.Count); for (int i = 0; i < system.Coefficients.Count; i++) { List <double> row = new List <double>(system.Coefficients[i]); A.Add(row); } var B = new List <double>(system.ConstantTerms); Console.WriteLine("Coefficients matrix (A):"); ShowMatrixes(A, system.NumberOfUnknowns); double determinant = Det(A); Console.WriteLine($"Determinant = {determinant}"); for (int i = 0; i < system.NumberOfUnknowns; i++) { List <List <double> > newA = ReplacingColumn(A, B, i); Console.WriteLine("Converted matrix on current step: "); ShowMatrixes(newA, system.NumberOfUnknowns); Console.WriteLine($"Determinant = {Det(newA)}"); x.Add(Det(newA) / determinant); } for (int i = 0; i < system.NumberOfUnknowns; i++) { Console.WriteLine($"x{i + 1} = {x[i]}; "); } return(x); } else { Console.WriteLine("You can't use this method"); return(null); } }
public static LinearSystem Read() //class to input linear system from a file { try { int n = 0; int m = 0; List <List <double> > A = new List <List <double> >(); List <double> B = new List <double>(); Console.WriteLine("Please, enter number of unknowns in your linear system:"); if (Int32.TryParse(Console.ReadLine(), out n)) { Console.WriteLine("Please, enter number of equations in your linear system:"); if (Int32.TryParse(Console.ReadLine(), out m)) { Console.WriteLine("Please, start entering coefficients, after you see '=' enter constant term:"); for (int i = 0; i < n; i++) { double temp = 0.0; int consolePosition = 0; var row = new List <double>(); if (Double.TryParse(Console.ReadLine(), out temp)) { row.Add(temp); Console.CursorTop--; consolePosition += (temp.ToString()).Length; Console.CursorLeft = consolePosition; Console.Write($" * x1 + "); } else { throw new ArgumentException(); } for (int j = 1; j < m; j++) { if (Double.TryParse(Console.ReadLine(), out temp)) { row.Add(temp); Console.CursorTop--; consolePosition += (temp.ToString()).Length + 8; Console.CursorLeft = consolePosition; if (j + 1 == m) { Console.Write($" * x{j + 1} = "); } else { Console.Write($" * x{j + 1} + "); } } else { throw new ArgumentException(); } } if (Double.TryParse(Console.ReadLine(), out temp)) { B.Add(temp); } else { throw new ArgumentException(); } A.Add(row); } } else { throw new ArgumentException(); } } else { throw new ArgumentException(); } LinearSystem system = new LinearSystem(m, n, A, B); return(system); } catch (ArgumentException) { Console.WriteLine("You entered inappropriate value, please follow the instructions!"); LinearSystem system = Read(); return(system); } catch (Exception ex) { Console.WriteLine($"{ ex.GetType()} says { ex.Message}"); Console.ReadLine(); LinearSystem system = Read(); return(system); } }
internal override void PreCalculations(LinearSystem system, out List <List <double> > A, out List <double> B) { A = new List <List <double> >(system.Coefficients.Count); for (int i = 0; i < system.Coefficients.Count; i++) { List <double> row = new List <double>(system.Coefficients[i]); A.Add(row); } B = new List <double>(system.ConstantTerms); double Amax; int k = 0; int p; while (k < system.NumberOfEquations) { Amax = Math.Abs(A[k][k]); p = k; for (int i = k + 1; i < system.NumberOfEquations; i++) { if (Math.Abs(A[i][k]) > Amax) { Amax = Math.Abs(A[i][k]); p = i; } } for (int j = 0; j < system.NumberOfUnknowns; j++) { double tempA = A[k][j]; A[k][j] = A[p][j]; A[p][j] = tempA; } double tempB = B[k]; B[k] = B[p]; B[p] = tempB; Console.WriteLine("Converted matrixes on current step: "); ShowMatrixes(A, B, system.NumberOfEquations, system.NumberOfUnknowns); Console.WriteLine(""); for (int i = k; i < system.NumberOfEquations; i++) { double temp = A[i][k]; if (Math.Abs(temp) == 0.0) { continue; } for (int j = 0; j < system.NumberOfUnknowns; j++) { A[i][j] = A[i][j] / temp; } B[i] = B[i] / temp; if (i == k) { continue; } for (int j = 0; j < system.NumberOfUnknowns; j++) { A[i][j] = A[i][j] - A[k][j]; } B[i] = B[i] - B[k]; } Console.WriteLine("Converted matrixes on current step: "); ShowMatrixes(A, B, system.NumberOfEquations, system.NumberOfUnknowns); Console.WriteLine(""); k++; } }
internal abstract void PreCalculations(LinearSystem system, out List <List <double> > A, out List <double> B);
abstract internal List <double> FinalCalculations(LinearSystem system); //method that does final calculations to find unknowns abstract internal bool Check(LinearSystem system);
abstract internal List <double> FinalCalculations(LinearSystem system); //method that does final calculations to find unknowns
internal override bool Check(LinearSystem system) => (system.NumberOfEquations == system.NumberOfUnknowns && Det(system.Coefficients) != 0);
public static void RunMenu() { uint key; try { Console.WriteLine("Hello! This program is designed as a calculator for linear system. This program uses direct methods such as Gauss and Cramer's method." + "To start using this program you have to enter linear system of equations. Please follow instractions!"); LinearSystem system = LinearSystemOperator.Read(); do { Console.WriteLine("Press 1 to show linear system."); Console.WriteLine("Press 2 to find results using Gauss Method with main element"); Console.WriteLine("Press 3 to find results using Gauss Method with unit diagonal"); Console.WriteLine("Press 4 to find results using Cramer's Method"); Console.WriteLine("Press 5 to enter new linear system"); Console.WriteLine("Press 0 to exit program"); Console.WriteLine("Enter key: "); key = Convert.ToUInt32(Console.ReadLine()); switch (key) { case 1: { LinearSystemOperator.Show(system); break; } case 2: { LinearSystemOperator.Show(system); MainElementGaussMethod gaussMethod = new MainElementGaussMethod(); List <double> results = gaussMethod.FinalCalculations(system); if (results != null) { List <double> error = gaussMethod.R(system, results); Console.WriteLine("Please, enter 1 if you want to save results to file, press enter to go back to menu: "); if (Int32.TryParse(Console.ReadLine(), out int saveKey) && saveKey == 1) { Console.WriteLine("Please, enter name of file: "); string fileName = Console.ReadLine(); using (StreamWriter w = new StreamWriter(fileName)) { gaussMethod.OutputResultsToFile(w, system, results, error); } } } break; } case 3: { LinearSystemOperator.Show(system); var gaussMethod = new UnitDiagonalGaussMethod(); List <double> results = gaussMethod.FinalCalculations(system); if (results != null) { List <double> error = gaussMethod.R(system, results); Console.WriteLine("Please, enter 1 if you want to save results to file, press enter to go back to menu: "); if (Int32.TryParse(Console.ReadLine(), out int saveKey) && saveKey == 1) { Console.WriteLine("Please, enter name of file: "); string fileName = Console.ReadLine(); using (StreamWriter w = new StreamWriter(fileName)) { gaussMethod.OutputResultsToFile(w, system, results, error); } } } break; } case 4: { LinearSystemOperator.Show(system); var method = new CramersMethod(); List <double> results = method.FinalCalculations(system); if (results != null) { List <double> error = method.R(system, results); Console.WriteLine("Please, enter 1 if you want to save results to file, press enter to go back to menu: "); if (Int32.TryParse(Console.ReadLine(), out int saveKey) && saveKey == 1) { Console.WriteLine("Please, enter name of file: "); string fileName = Console.ReadLine(); using (StreamWriter w = new StreamWriter(fileName)) { method.OutputResultsToFile(w, system, results, error); } } } break; } case 5: { system = LinearSystemOperator.Read(); break; } case 0: break; } } while (key != 0); } catch (ArgumentException) { Console.WriteLine("You entered inappropriate value, the program will restart now, please follow instructions."); Menu.RunMenu(); } catch (Exception ex) { Console.WriteLine("An error occured during while program was running, please follow instructions"); Console.WriteLine($"{ ex.GetType()} says { ex.Message}"); Menu.RunMenu(); } }