48 for (
int i = 0; i < num_rows; ++i) {
49 for (
int j = 0; j < num_cols; ++j) {
50 std::cout << matrix[i][j] <<
" ";
52 std::cout << std::endl;
57 const std::vector<std::vector<double>> &mat)
const {
58 uint32_t
rows = mat.size();
59 uint32_t
cols = mat[0].size();
61 for (uint32_t i = 0; i <
rows; ++i) {
62 for (uint32_t j = 0; j <
cols; ++j) {
63 std::cout << mat[i][j] <<
" ";
65 std::cout << std::endl;
72 std::vector<std::vector<double>> temp_mtx = matrix;
74 for (
int i = 0; i < num_rows - 1; ++i) {
77 for (
int k = i + 1; k < num_rows; ++k) {
78 if (std::abs(temp_mtx[k][i]) > std::abs(temp_mtx[pivotRow][i]))
81 std::swap(temp_mtx[i], temp_mtx[pivotRow]);
84 for (
int k = i + 1; k < num_rows; ++k) {
85 double factor = temp_mtx[k][i] / temp_mtx[i][i];
86 for (
int j = i; j < num_cols; ++j) {
87 temp_mtx[k][j] -= factor * temp_mtx[i][j];
93 std::vector<double> solutions(num_rows, 0);
95 for (
int i = num_rows - 1; i >= 0; --i) {
97 for (
int j = i + 1; j < num_cols - 1; ++j) {
98 sum += temp_mtx[i][j] * solutions[j];
100 solutions[i] = (temp_mtx[i][num_cols - 1] - sum) / temp_mtx[i][i];
109 if (num_rows != num_cols) {
110 std::cerr <<
"Error: Determinant is undefined for non-square matrices."
115 std::vector<std::vector<double>> temp_mtx = matrix;
118 for (
int i = 0; i < num_rows - 1; ++i) {
119 for (
int k = i + 1; k < num_rows; ++k) {
120 double factor = temp_mtx[k][i] / temp_mtx[i][i];
121 for (
int j = i; j < num_cols; ++j) {
122 temp_mtx[k][j] -= factor * temp_mtx[i][j];
127 for (
int i = 0; i < num_rows; ++i) {
128 det *= temp_mtx[i][i];
137 if (num_rows != num_cols) {
139 <<
"Error: Matrix inversion is undefined for non-square matrices."
144 std::vector<std::vector<double>> identity(
146 std::vector<double>(num_cols, 0.0));
149 for (
int i = 0; i < num_rows; ++i) {
150 matrix[i].insert(matrix[i].end(),
159 for (
int i = 0; i < num_rows; ++i) {
160 matrix[i].erase(matrix[i].begin(), matrix[i].begin() + num_rows);
164 std::pair<std::vector<std::vector<double>>, std::vector<std::vector<double>>>
166 std::vector<std::vector<double>> L(num_rows,
167 std::vector<double>(num_cols, 0.0));
168 std::vector<std::vector<double>> U(num_rows,
169 std::vector<double>(num_cols, 0.0));
171 for (
int i = 0; i < num_rows; ++i) {
175 for (
int j = i; j < num_cols; ++j) {
176 U[i][j] = matrix[i][j];
177 for (
int k = 0; k < i; ++k) {
178 U[i][j] -= L[i][k] * U[k][j];
182 for (
int j = i + 1; j < num_rows; ++j) {
183 L[j][i] = matrix[j][i];
184 for (
int k = 0; k < i; ++k) {
185 L[j][i] -= L[j][k] * U[k][i];
199 for (
int i = 0; i < num_rows - 1; ++i) {
200 for (
int j = i + 1; j < num_rows; ++j) {
201 matrix[j][num_cols - 1] -= matrix[j][i] * matrix[i][num_cols - 1];
206 for (
int i = num_rows - 1; i >= 0; --i) {
207 for (
int j = i + 1; j < num_cols - 1; ++j) {
208 matrix[i][num_cols - 1] -= matrix[i][j] * matrix[j][num_cols - 1];
210 matrix[i][num_cols - 1] /= matrix[i][i];
216 if (!is_symmetric()) {
217 std::cerr <<
"Error: Cholesky decomposition is only applicable to"
218 "symmetric positive-definite matrices\n";
221 for (
int i = 0; i < num_rows; ++i) {
222 for (
int j = 0; j <= i; ++j) {
225 for (
int k = 0; k < j; ++k) {
226 sum += std::pow(matrix[j][k], 2);
228 matrix[j][j] = std::sqrt(matrix[j][j] - sum);
233 for (
int k = 0; k < j; ++k) {
234 sum += matrix[i][k] * matrix[j][k];
236 matrix[i][j] = (matrix[i][j] - sum) / matrix[j][j];
242 for (
int i = 0; i < num_rows; ++i) {
244 for (
int j = 0; j < i; ++j) {
245 sum += matrix[i][j] * matrix[num_cols - 1][j];
247 matrix[num_cols - 1][i] =
248 (matrix[num_cols - 1][i] - sum) / matrix[i][i];
252 for (
int i = num_rows - 1; i >= 0; --i) {
254 for (
int j = i + 1; j < num_cols - 1; ++j) {
255 sum += matrix[j][i] * matrix[i][num_cols - 1];
257 matrix[i][num_cols - 1] =
258 (matrix[i][num_cols - 1] - sum) / matrix[i][i];
264 std::vector<std::vector<double>> x(num_rows, std::vector<double>(1, 0.0));
265 std::vector<std::vector<double>> xOld = x;
267 for (
int iter = 0; iter < maxIterations; ++iter) {
268 for (
int i = 0; i < num_rows; ++i) {
270 for (
int j = 0; j < num_cols - 1; ++j) {
272 sum += matrix[i][j] * xOld[j][0];
275 x[i][0] = (matrix[i][num_cols - 1] - sum) / matrix[i][i];
279 double maxDiff = 0.0;
280 for (
int i = 0; i < num_rows; ++i) {
281 double diff = std::abs(x[i][0] - xOld[i][0]);
282 if (diff > maxDiff) {
288 if (maxDiff < tolerance) {
296 for (
int i = 0; i < num_rows; ++i) {
297 matrix[i][num_cols - 1] = x[i][0];
304 if (num_rows != num_cols) {
309 for (
int i = 0; i < num_rows; ++i) {
310 for (
int j = 0; j < num_cols; ++j) {
312 if (fabs(matrix[i][j] - matrix[j][i]) >
313 std::numeric_limits<double>::epsilon()) {
320 for (
int i = 0; i < num_rows; ++i) {
321 std::vector<std::vector<double>> submatrix(i + 1,
322 std::vector<double>(i + 1));
323 for (
int j = 0; j <= i; ++j) {
324 for (
int k = 0; k <= i; ++k) {
325 submatrix[j][k] = matrix[j][k];
340 for (
int i = 0; i < num_rows; ++i) {
341 for (
int j = 0; j < num_cols; ++j) {
342 sum += std::pow(matrix[i][j], 2);
345 return std::sqrt(sum);
350 double maxColSum = 0.0;
351 for (
int j = 0; j < num_cols; ++j) {
353 for (
int i = 0; i < num_rows; ++i) {
354 colSum += std::abs(matrix[i][j]);
356 maxColSum = std::max(maxColSum, colSum);
363 double maxRowSum = 0.0;
364 for (
int i = 0; i < num_rows; ++i) {
366 for (
int j = 0; j < num_cols; ++j) {
367 rowSum += std::abs(matrix[i][j]);
369 maxRowSum = std::max(maxRowSum, rowSum);
376 std::vector<std::vector<double>> orthoBasis(
378 std::vector<double>(num_cols, 0.0));
380 for (
int j = 0; j < num_cols; ++j) {
381 for (
int i = 0; i < num_rows; ++i) {
382 double projection = 0.0;
383 for (
int k = 0; k < i; ++k) {
384 projection += (matrix[i][j] * orthoBasis[k][j]) /
385 (orthoBasis[k][j] * orthoBasis[k][j]);
387 orthoBasis[i][j] = matrix[i][j] - projection;
397 for (
int i = 0; i < num_rows; ++i) {
398 double diagonalValue = std::abs(matrix[i][i]);
400 for (
int j = 0; j < num_cols; ++j) {
402 rowSum += std::abs(matrix[i][j]);
405 if (diagonalValue <= rowSum) {
414 for (
int i = 0; i < num_rows; ++i) {
415 bool allZeros =
true;
416 for (
int j = 0; j < num_cols - 1; ++j) {
417 if (fabs(matrix[i][j]) > std::numeric_limits<double>::epsilon()) {
423 if (allZeros && fabs(matrix[i][num_cols - 1]) >
424 std::numeric_limits<double>::epsilon()) {
433 for (
int i = 0; i < num_rows; ++i) {
434 if (fabs(matrix[i][num_cols - 1]) >
435 std::numeric_limits<double>::epsilon()) {
Class for solving linear systems and performing matrix operations.
std::vector< std::vector< double > > matrix
void display_mtx() const
Display the augmented matrix.
bool is_consistent() const
Check if the linear system is consistent.
double inf_norm() const
Calculate the infinity norm of the matrix.
double frobenius_norm() const
Calculate the Frobenius norm of the matrix.
bool diagonally_dominant() const
Check if the matrix is diagonally dominant.
void invert_mtx()
Invert the matrix.
void gram_schmidt()
Perform Gram-Schmidt orthogonalization on the matrix.
void solve_cholesky()
Solve the linear system using Cholesky decomposition.
void solve_jacobi(int maxIterations=100, double tolerance=1e-10)
Solve the linear system using Jacobi iteration.
double determinant() const
Calculate the determinant of the matrix.
void solve_lu()
Solve the linear system using LU decomposition.
LinSys(const std::vector< std::vector< double >> &mat)
Constructor for LinSys class.
std::pair< std::vector< std::vector< double > >, std::vector< std::vector< double > > > lu_decomp()
Perform LU decomposition of the matrix.
std::vector< double > solve_gauss()
Solve the linear system using Gaussian Elimination.
bool is_homogeneous() const
Check if the linear system is homogeneous.
bool is_symmetric() const
Check if the matrix is symmetric.
void display(const std::vector< std::vector< double >> &mat) const
double one_norm() const
Calculate the 1-norm of the matrix.