24bool InitMatrix(vector<vector<double>> &m,
const string &filename,
int &rows,
int &cols)
26 ifstream in(filename);
29 cerr <<
"Error: Cannot open file " << filename << endl;
37 vector<vector<double>> temp_matrix;
38 while (getline(in, line))
44 istringstream iss(line);
53 else if (
static_cast<int>(row.size()) != cols)
55 cerr <<
"Error: Inconsistent number of columns in the file." << endl;
59 temp_matrix.push_back(row);
64 if (rows == 0 || cols < 2)
66 cerr <<
"Error: The matrix must have at least one equation and one variable." << endl;
80 cout <<
"The current system of linear equations is:" << endl;
82 for (
int i = 0; i < rows; i++)
85 for (
int j = 0; j < cols - 1; j++)
88 double coeff = round(m[i][j] * 1e12) / 1e12;
90 if (fabs(coeff - round(coeff)) < 1e-12)
92 equation += to_string(
static_cast<long long>(round(coeff))) +
" x" + to_string(j + 1);
97 equation += to_string(round(m[i][j] * 10000) / 10000.0) +
" x" + to_string(j + 1);
105 double const_term = round(m[i][cols - 1] * 1e12) / 1e12;
106 if (fabs(const_term - round(const_term)) < 1e-12)
108 equation +=
" = " + to_string(
static_cast<long long>(round(const_term)));
112 equation +=
" = " + to_string(round(m[i][cols - 1] * 10000) / 10000.0);
115 cout << equation << endl;
125 for (
int i = 0; i < rows; i++)
127 bool all_zero =
true;
128 for (
int j = 0; j < cols - 1; j++)
130 if (fabs(m[i][j]) > 1e-12)
136 if (all_zero && fabs(m[i][cols - 1]) > 1e-12)
149 cout <<
"The system has a unique solution:" << endl;
150 for (
size_t i = 0; i < solution.size(); i++)
152 cout <<
"x" << i + 1 <<
" = " << fixed << setprecision(4) << solution[i] << endl;
159 return chrono::steady_clock::now();
162void StopTimer(
const chrono::steady_clock::time_point &start)
164 auto end = chrono::steady_clock::now();
165 chrono::duration<double> elapsed = end - start;
166 cout <<
"Time elapsed: " << elapsed.count() <<
" seconds." << endl;
void DisplaySolution(const vector< double > &solution)
Displays the unique solution.
void StopTimer(const chrono::steady_clock::time_point &start)
bool InitMatrix(vector< vector< double > > &m, const string &filename, int &rows, int &cols)
Initializes the matrix by reading from a .in file.
void ShowEquations(const vector< vector< double > > &m, int rows, int cols)
Displays the system of linear equations.
chrono::steady_clock::time_point StartTimer()
bool CheckConsistency(const vector< vector< double > > &m, int rows, int cols)
Checks the consistency of the system of equations.
Utility functions for matrix initialization and display.