Linear Equations Solver  1.0
Using Gaussian elimination
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1
29#include <iostream>
30#include <string>
31#include <vector>
32#include <cmath> // Included for fabs function
33#include <filesystem>
34#include <limits> // For std::numeric_limits
35#include "utils.h"
36#include "methods.h"
37#include "interaction.h"
38
39using namespace std;
40
41int main()
42{
43 char choice;
44 do
45 {
46 string selected_file = SelectInputFile();
47 if (selected_file.empty())
48 {
49 return 1; // File selection failed
50 }
51
52 // Start timer after selecting the file
53 auto start_time = StartTimer();
54
55 vector<vector<double>> matrix;
56 int rows, cols;
57 if (!InitMatrix(matrix, selected_file, rows, cols))
58 {
59 return 1; // Matrix initialization failed
60 }
61
62 ShowEquations(matrix, rows, cols);
63 cout << "Starting Gaussian elimination process..." << endl;
64 int exchange_count = GaussianElimination(matrix, rows, cols);
65 cout << "Gaussian elimination completed." << endl
66 << endl;
67
68 int rank = DetermineRank(matrix, rows, cols);
69 bool consistent = CheckConsistency(matrix, rows, cols);
70
71 if (!consistent)
72 {
73 cout << "The system of equations is inconsistent and has no solution." << endl;
74 }
75 else if (rank < (cols - 1))
76 {
77 ShowGeneralSolution(matrix, rows, cols, rank);
78 }
79 else
80 {
81 vector<double> solution;
82 bool solvable = BackSubstitution(matrix, rows, cols, solution);
83 if (solvable)
84 {
85 DisplaySolution(solution);
86 }
87 else
88 {
89 cout << "The system of equations is inconsistent and has no solution." << endl;
90 }
91 }
92
93 // Stop timer after the solution is displayed
94 StopTimer(start_time);
95 choice = AskRunAgain();
96
97 } while (choice == 'y' || choice == 'Y');
98
100 return 0;
101}
string SelectInputFile()
Allows the user to select an input .in file from the current directory.Returns an empty string if no ...
char AskRunAgain()
Return char The user's choice ('y', 'Y', 'n', 'N').
void WaitForExit()
Waits for the user to press Enter before exiting.
User interaction functions.
int main()
Definition main.cpp:41
int DetermineRank(const vector< vector< double > > &m, int rows, int cols)
Determines the rank of the coefficient matrix A (excluding augmented column).
Definition methods.cpp:188
int GaussianElimination(vector< vector< double > > &m, int rows, int cols)
Performs Gaussian elimination on the augmented matrix with partial pivoting.
Definition methods.cpp:75
bool BackSubstitution(const vector< vector< double > > &m, int rows, int cols, vector< double > &solution)
Performs back-substitution to find the solution vector.
Definition methods.cpp:137
void ShowGeneralSolution(const vector< vector< double > > &m, int rows, int cols, int rank)
Displays the general solution for systems with infinitely many solutions.
Definition methods.cpp:211
Core computational functions for solving linear systems.
void DisplaySolution(const vector< double > &solution)
Displays the unique solution.
Definition utils.cpp:147
void StopTimer(const chrono::steady_clock::time_point &start)
Definition utils.cpp:162
bool InitMatrix(vector< vector< double > > &m, const string &filename, int &rows, int &cols)
Initializes the matrix by reading from a .in file.
Definition utils.cpp:24
void ShowEquations(const vector< vector< double > > &m, int rows, int cols)
Displays the system of linear equations.
Definition utils.cpp:78
chrono::steady_clock::time_point StartTimer()
Definition utils.cpp:157
bool CheckConsistency(const vector< vector< double > > &m, int rows, int cols)
Checks the consistency of the system of equations.
Definition utils.cpp:123
Utility functions for matrix initialization and display.