Linear Equations Solver  1.0
Using Gaussian elimination
Loading...
Searching...
No Matches
utils.cpp File Reference

Implementation of utility functions for matrix operations. More...

#include "utils.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <iomanip>
+ Include dependency graph for utils.cpp:

Go to the source code of this file.

Functions

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.
 
bool CheckConsistency (const vector< vector< double > > &m, int rows, int cols)
 Checks the consistency of the system of equations.
 
void DisplaySolution (const vector< double > &solution)
 Displays the unique solution.
 
chrono::steady_clock::time_point StartTimer ()
 
void StopTimer (const chrono::steady_clock::time_point &start)
 

Detailed Description

Implementation of utility functions for matrix operations.

This file contains the implementations of functions that handle reading matrices from .in files and displaying the corresponding system of linear equations. These utility functions are essential for the initialization and output of matrix data used in solving linear systems.

Definition in file utils.cpp.

Function Documentation

◆ CheckConsistency()

bool CheckConsistency ( const vector< vector< double > > & m,
int rows,
int cols )

Checks the consistency of the system of equations.

Parameters
mThe matrix representing the system.
rowsNumber of rows in the matrix.
colsNumber of columns in the matrix.
Returns
true If the system is consistent.
false If the system is inconsistent.

Definition at line 123 of file utils.cpp.

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}

◆ DisplaySolution()

void DisplaySolution ( const vector< double > & solution)

Displays the unique solution.

Parameters
solutionThe solution vector.

Definition at line 147 of file utils.cpp.

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}

◆ InitMatrix()

bool InitMatrix ( vector< vector< double > > & m,
const string & filename,
int & rows,
int & cols )

Initializes the matrix by reading from a .in file.

Parameters
mReference to the matrix to be initialized.
filenameName of the input file.
rowsReference to store the number of rows.
colsReference to store the number of columns.
Returns
true If the matrix was successfully initialized.
false If there was an error during initialization.

Definition at line 24 of file utils.cpp.

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}

◆ ShowEquations()

void ShowEquations ( const vector< vector< double > > & m,
int rows,
int cols )

Displays the system of linear equations.

Parameters
mThe matrix representing the system.
rowsNumber of equations.
colsNumber of variables plus one (for constants).

Definition at line 78 of file utils.cpp.

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}

◆ StartTimer()

chrono::steady_clock::time_point StartTimer ( )

Definition at line 157 of file utils.cpp.

158{
159 return chrono::steady_clock::now();
160}

◆ StopTimer()

void StopTimer ( const chrono::steady_clock::time_point & start)

Definition at line 162 of file utils.cpp.

163{
164 auto end = chrono::steady_clock::now();
165 chrono::duration<double> elapsed = end - start;
166 cout << "Time elapsed: " << elapsed.count() << " seconds." << endl;
167}