Linear Equations Solver  1.0
Using Gaussian elimination
Loading...
Searching...
No Matches
utils.cpp
Go to the documentation of this file.
1
12#include "utils.h"
13#include <iostream>
14#include <fstream>
15#include <sstream>
16#include <cmath>
17#include <iomanip>
18
19using namespace std;
20
24bool InitMatrix(vector<vector<double>> &m, const string &filename, int &rows, int &cols)
25{
26 ifstream in(filename);
27 if (!in.is_open())
28 {
29 cerr << "Error: Cannot open file " << filename << endl;
30 return false;
31 }
32
33 // Read the matrix dimensions dynamically
34 string line;
35 rows = 0;
36 cols = 0;
37 vector<vector<double>> temp_matrix;
38 while (getline(in, line))
39 {
40 if (line.empty())
41 continue; // Skip empty lines
42 vector<double> row;
43 double num;
44 istringstream iss(line);
45 while (iss >> num)
46 {
47 row.push_back(num);
48 }
49 if (cols == 0)
50 {
51 cols = row.size();
52 }
53 else if (static_cast<int>(row.size()) != cols)
54 {
55 cerr << "Error: Inconsistent number of columns in the file." << endl;
56 in.close();
57 return false;
58 }
59 temp_matrix.push_back(row);
60 rows++;
61 }
62 in.close();
63
64 if (rows == 0 || cols < 2)
65 {
66 cerr << "Error: The matrix must have at least one equation and one variable." << endl;
67 return false;
68 }
69
70 // Assign to m
71 m = temp_matrix;
72 return true;
73}
74
78void ShowEquations(const vector<vector<double>> &m, int rows, int cols)
79{
80 cout << "The current system of linear equations is:" << endl;
81
82 for (int i = 0; i < rows; i++)
83 {
84 string equation = "";
85 for (int j = 0; j < cols - 1; j++)
86 {
87 // Check if the coefficient is an integer
88 double coeff = round(m[i][j] * 1e12) / 1e12; // Handle floating-point precision
89
90 if (fabs(coeff - round(coeff)) < 1e-12)
91 {
92 equation += to_string(static_cast<long long>(round(coeff))) + " x" + to_string(j + 1);
93 }
94 else
95 {
96 // Set precision for floating-point numbers
97 equation += to_string(round(m[i][j] * 10000) / 10000.0) + " x" + to_string(j + 1);
98 }
99
100 if (j < cols - 2)
101 equation += " + "; // Add space around '+' for better readability
102 }
103
104 // Handle constant term
105 double const_term = round(m[i][cols - 1] * 1e12) / 1e12;
106 if (fabs(const_term - round(const_term)) < 1e-12)
107 {
108 equation += " = " + to_string(static_cast<long long>(round(const_term)));
109 }
110 else
111 {
112 equation += " = " + to_string(round(m[i][cols - 1] * 10000) / 10000.0);
113 }
114
115 cout << equation << endl; // Output the equation
116 }
117 cout << endl; // Add a blank line at the end
118}
119
123bool CheckConsistency(const vector<vector<double>> &m, int rows, int cols)
124{
125 for (int i = 0; i < rows; i++)
126 {
127 bool all_zero = true;
128 for (int j = 0; j < cols - 1; j++)
129 {
130 if (fabs(m[i][j]) > 1e-12)
131 {
132 all_zero = false;
133 break;
134 }
135 }
136 if (all_zero && fabs(m[i][cols - 1]) > 1e-12)
137 {
138 return false;
139 }
140 }
141 return true;
142}
143
147void DisplaySolution(const vector<double> &solution)
148{
149 cout << "The system has a unique solution:" << endl;
150 for (size_t i = 0; i < solution.size(); i++)
151 {
152 cout << "x" << i + 1 << " = " << fixed << setprecision(4) << solution[i] << endl;
153 }
154}
155
156// Timing functions
157chrono::steady_clock::time_point StartTimer()
158{
159 return chrono::steady_clock::now();
160}
161
162void StopTimer(const chrono::steady_clock::time_point &start)
163{
164 auto end = chrono::steady_clock::now();
165 chrono::duration<double> elapsed = end - start;
166 cout << "Time elapsed: " << elapsed.count() << " seconds." << endl;
167}
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.