47bool Eliminate(vector<vector<double>> &m,
int current_row,
int total_rows,
int total_cols)
49 double pivot = m[current_row][current_row];
50 if (fabs(pivot) < 1e-12)
56 for (
int i = current_row + 1; i < total_rows; i++)
58 double factor = m[i][current_row] / pivot;
59 cout <<
"Eliminating element in row " << i + 1 <<
", column " << current_row + 1 <<
":" << endl;
60 cout <<
"Multiplying row " << current_row + 1 <<
" by " << fixed << setprecision(4) << factor
61 <<
" and subtracting from row " << i + 1 <<
"." << endl;
62 m[i][current_row] = 0.0;
63 for (
int j = current_row + 1; j < total_cols; j++)
65 m[i][j] -= factor * m[current_row][j];
77 int exchange_count = 0;
78 int n = min(rows, cols - 1);
80 for (
int k = 0; k < n; k++)
82 cout <<
"Processing column " << k + 1 <<
"..." << endl;
95 cout <<
"No need to swap rows for column " << k + 1 <<
"." << endl;
99 if (fabs(m[k][k]) < 1e-12)
101 cout <<
"Warning: Pivot element in row " << k + 1 <<
" is close to zero. The matrix may be singular." << endl;
108 cout <<
"Elimination failed for column " << k + 1 <<
"." << endl;
112 cout <<
"Current matrix state:" << endl;
113 for (
int r = 0; r < rows; r++)
115 for (
int c = 0; c < cols; c++)
117 double coeff = round(m[r][c] * 1e12) / 1e12;
118 if (fabs(coeff - round(coeff)) < 1e-12)
120 cout << static_cast<long long>(round(coeff)) <<
"\t";
124 cout << fixed << setprecision(2) << coeff <<
"\t";
129 cout <<
"-------------------------------------" << endl;
131 return exchange_count;
137bool BackSubstitution(
const vector<vector<double>> &m,
int rows,
int cols, vector<double> &solution)
139 solution.assign(cols - 1, 0.0);
140 cout <<
"Starting back-substitution process..." << endl;
141 for (
int i = rows - 1; i >= 0; i--)
145 for (
int j = 0; j < cols - 1; j++)
147 if (fabs(m[i][j]) > 1e-12)
156 if (fabs(m[i][cols - 1]) > 1e-12)
168 double rhs = m[i][cols - 1];
169 cout <<
"Calculating x" << pivot_col + 1 <<
":" << endl;
170 for (
int j = pivot_col + 1; j < cols - 1; j++)
172 cout <<
" " << fixed << setprecision(4) << m[i][j] <<
" * x" << j + 1
173 <<
" = " << m[i][j] * solution[j] << endl;
174 rhs -= m[i][j] * solution[j];
176 cout <<
" RHS after subtraction = " << rhs << endl;
177 solution[pivot_col] = rhs / m[i][pivot_col];
178 cout <<
" x" << pivot_col + 1 <<
" = " << rhs <<
" / " << m[i][pivot_col]
179 <<
" = " << fixed << setprecision(4) << solution[pivot_col] << endl
213 cout <<
"The system has infinitely many solutions." << endl;
214 cout <<
"Solution space dimension: " << (cols - 1 - rank) << endl;
220 vector<int> free_vars;
221 for (
int j = 0; j < cols - 1; j++)
223 if (find(pivots.begin(), pivots.end(), j) == pivots.end())
225 free_vars.push_back(j);
230 int num_free = free_vars.size();
231 vector<string> params;
232 for (
int i = 0; i < num_free; i++)
234 params.push_back(
"t" + to_string(i + 1));
238 vector<double> particular_solution(cols - 1, 0.0);
239 vector<vector<double>> basis_vectors;
242 for (
int i = rows - 1; i >= 0; i--)
246 for (
int j = 0; j < cols - 1; j++)
248 if (fabs(m[i][j]) > 1e-12)
260 double rhs = m[i][cols - 1];
261 for (
int j = pivot_col + 1; j < cols - 1; j++)
263 rhs -= m[i][j] * particular_solution[j];
265 particular_solution[pivot_col] = rhs / m[i][pivot_col];
269 for (
int i = 0; i < num_free; i++)
271 vector<double> basis(cols - 1, 0.0);
272 basis[free_vars[i]] = 1.0;
275 for (
int r = rank - 1; r >= 0; r--)
277 int pivot_col = pivots[r];
279 for (
int j = pivot_col + 1; j < cols - 1; j++)
281 rhs -= m[r][j] * basis[j];
283 basis[pivot_col] = rhs / m[r][pivot_col];
286 basis_vectors.push_back(basis);
290 cout <<
"General solution:" << endl;
292 for (
int j = 0; j < cols - 1; j++)
294 cout << fixed << setprecision(4) << particular_solution[j];
300 for (
int i = 0; i < num_free; i++)
302 cout <<
" + " << params[i] <<
" * [";
303 for (
int j = 0; j < cols - 1; j++)
305 cout << fixed << setprecision(4) << basis_vectors[i][j];
310 if (i < num_free - 1)
vector< int > IdentifyPivots(const vector< vector< double > > &m, int rows, int cols)
Identifies the pivot columns in the matrix.
int Pivoting(const vector< vector< double > > &m, int current_row, int total_rows)
Performs partial pivoting and returns the row index with the maximum pivot.
int DetermineRank(const vector< vector< double > > &m, int rows, int cols)
Determines the rank of the coefficient matrix A (excluding augmented column).
int GaussianElimination(vector< vector< double > > &m, int rows, int cols)
Performs Gaussian elimination on the augmented matrix with partial pivoting.
bool Eliminate(vector< vector< double > > &m, int current_row, int total_rows, int total_cols)
Performs elimination on the matrix to form an upper triangular matrix.
void Exchange(vector< vector< double > > &m, int row1, int row2)
Swaps two rows in the matrix and outputs the action.
bool BackSubstitution(const vector< vector< double > > &m, int rows, int cols, vector< double > &solution)
Performs back-substitution to find the solution vector.
void ShowGeneralSolution(const vector< vector< double > > &m, int rows, int cols, int rank)
Displays the general solution for systems with infinitely many solutions.