13 std::cout <<
"A has no rows" << std::endl;
15 for(
size_t i = 0; i < AR; i++)
17 for(
size_t j = 0; j < AC; j++)
19 std::cout << A[i][j] <<
' ';
21 std::cout << std::endl;
29 for (std::vector<double>& Ai : A)
30 for (
double& Aij : Ai)
38 for (std::vector<double>& Ai : A)
39 for (
double& Aij : Ai)
55 for (
size_t i = 0; i < AR; i++)
56 for (
size_t j = 0; j < AC; j++)
72 assert(r1 >= 0 && r1 < AR && r2 >= 0 && r2 < AR);
74 for (
size_t j = 0; j < AC; j++)
75 std::swap(A[r1][j], A[r2][j]);
88 assert(c1 >= 0 && c1 < A[0].size() && c2 >= 0 && c2 < A[0].size());
90 for (
size_t i = 0; i < AR; i++)
91 std::swap(A[i][c1], A[i][c2]);
105 for(
size_t i = 0; i < R; i++)
106 for(
size_t j = 0; j < C; j++)
107 B[i][j] = A[i][j] * c;
119 assert(C == A[0].size());
123 for(
size_t i = 0; i < R; i++)
125 for(
size_t j = 0; j < C; j++)
126 b[i] += A[i][j] * x[j];
135 size_t AR = A.size();
137 assert(AR != 0 && B.size() != 0);
139 size_t AC = A[0].size();
140 size_t BC = B[0].size();
142 assert(AC != 0 && BC != 0 && AC == B.size());
150 for(
size_t i = 0; i < CR; i++)
151 for(
size_t j = 0; j < CC; j++)
152 for(
size_t k = 0; k < Cs; k++)
153 C[i][j] += A[i][k] * B[k][j];
162 size_t AR = A.size();
163 size_t BR = A.size();
165 assert(AR != 0 && B.size() != 0);
168 size_t AC = A[0].size();
169 size_t BC = B[0].size();
171 assert(AC != 0 && BC != 0);
176 for(
size_t i = 0; i < AR; i++)
177 for(
size_t j = 0; j < AC; j++)
178 C[i][j] = A[i][j] + B[i][j];
187 size_t AR = A.size();
188 size_t BR = A.size();
190 assert(AR != 0 && B.size() != 0);
193 size_t AC = A[0].size();
194 size_t BC = B[0].size();
196 assert(AC != 0 && BC != 0);
201 for(
size_t i = 0; i < AR; i++)
202 for(
size_t j = 0; j < AC; j++)
203 C[i][j] = B[i][j] - A[i][j];
219 return A[0][0]*A[1][1] - A[0][1]*A[1][0];
223 return A[0][0]*A[1][1]*A[2][2] + A[0][1]*A[1][2]*A[2][0] +
224 A[0][2]*A[1][0]*A[2][1] - A[0][0]*A[1][2]*A[2][1] -
225 A[0][1]*A[1][0]*A[2][2] - A[0][2]*A[1][1]*A[2][0];
230 return A[0][0]*A[1][1]*A[2][2]*A[3][3] + A[0][0]*A[1][2]*A[2][3]*A[3][1] + A[0][0]*A[1][3]*A[2][1]*A[3][2]
231 + A[0][1]*A[1][0]*A[2][3]*A[3][2] + A[0][1]*A[1][2]*A[2][0]*A[3][3] + A[0][1]*A[1][3]*A[2][2]*A[3][0]
232 + A[0][2]*A[1][0]*A[2][1]*A[3][3] + A[0][2]*A[1][1]*A[2][3]*A[3][0] + A[0][2]*A[1][3]*A[2][0]*A[3][1]
233 + A[0][3]*A[1][0]*A[2][2]*A[3][1] + A[0][3]*A[1][1]*A[2][0]*A[3][2] + A[0][3]*A[1][2]*A[2][1]*A[3][0]
234 - A[0][0]*A[1][1]*A[2][3]*A[3][2] - A[0][0]*A[1][2]*A[2][1]*A[3][3] - A[0][0]*A[1][3]*A[2][2]*A[3][1]
235 - A[0][1]*A[1][0]*A[2][2]*A[3][3] - A[0][1]*A[1][2]*A[2][3]*A[3][0] - A[0][1]*A[1][3]*A[2][0]*A[3][2]
236 - A[0][2]*A[1][0]*A[2][3]*A[3][1] - A[0][2]*A[1][1]*A[2][0]*A[3][3] - A[0][2]*A[1][3]*A[2][1]*A[3][0]
237 - A[0][3]*A[1][0]*A[2][1]*A[3][2] - A[0][3]*A[1][1]*A[2][2]*A[3][0] - A[0][3]*A[1][2]*A[2][0]*A[3][1];
242 for (
size_t n = 0; n < R; n++)
244 std::vector<std::vector<double> > M =
SubMatrix(0, n, A);
245 double pm = ((n+1)%2)*2. - 1.;
263 assert( (r >= 0) && (r < R) && (c >= 0) && (c < C) );
266 for (
size_t i = 0, ii = 0; i < R; ++i)
270 for (
size_t j = 0, jj = 0; j < C; ++j)
290 for(
int i = 0;i < n-1;++i)
292 const std::vector<double>& ai = A[i];
294 double factor = 1.0/A[i][i];
295 for(
int j = i+1;j < n;++j)
297 std::vector<double>& aj = A[j];
298 double val = aj[i] * factor;
300 for(
int k = i+1;k < n;++k)
301 aj[k] -= val * ai[k];
306 for(
int i = n-1;i >= 0;--i)
308 const std::vector<double>& ai = A[i];
310 for(
int j = i+1;j < n;++j)
321 assert(A.size() == A[0].size());
323 const unsigned int R = A.size();
325 std::vector<std::vector<double> > M(R,std::vector<double>(R,0.));
327 for(
unsigned int i = 0; i < R; i++)
330 std::vector<std::vector<double> > B = A;
332 for (
unsigned int c = 0; c < R; c++)
335 unsigned int max_row=c;
336 for (
unsigned int r = c; r < R; ++r)
337 if (std::fabs(B[r][c]) > std::fabs(B[max_row][c]))
347 for (
unsigned int r = 0; r < R; r++)
351 double g = B[r][c]/B[c][c];
354 for(
unsigned int k=0; k < R; k++)
356 B[r][k] -= B[c][k]*g;
357 M[r][k] -= M[c][k]*g;
364 double g = 1/B[c][c];
365 for (
unsigned int k = 0; k < R; k++)
382 std::vector<std::vector<double>> M(R,std::vector<double>(R,0.));
408 M[0][0] = A[2][2]*A[1][1]-A[2][1]*A[1][2];
409 M[0][1] = -(A[2][2]*A[0][1]-A[2][1]*A[0][2]);
410 M[0][2] = A[1][2]*A[0][1]-A[1][1]*A[0][2];
411 M[1][0] = -(A[2][2]*A[1][0]-A[2][0]*A[1][2]);
412 M[1][1] = A[2][2]*A[0][0]-A[2][0]*A[0][2];
413 M[1][2] = -(A[1][2]*A[0][0]-A[1][0]*A[0][2]);
414 M[2][0] = A[2][1]*A[1][0]-A[2][0]*A[1][1];
415 M[2][1] = -(A[2][1]*A[0][0]-A[2][0]*A[0][1]);
416 M[2][2] = A[1][1]*A[0][0]-A[1][0]*A[0][1];
422 M[0][0] = A[1][1]*A[2][2]*A[3][3] + A[1][2]*A[2][3]*A[3][1] + A[1][3]*A[2][1]*A[3][2] - A[1][1]*A[2][3]*A[3][2] - A[1][2]*A[2][1]*A[3][3] - A[1][3]*A[2][2]*A[3][1];
423 M[0][1] = A[0][1]*A[2][3]*A[3][2] + A[0][2]*A[2][1]*A[3][3] + A[0][3]*A[2][2]*A[3][1] - A[0][1]*A[2][2]*A[3][3] - A[0][2]*A[2][3]*A[3][1] - A[0][3]*A[2][1]*A[3][2];
424 M[0][2] = A[0][1]*A[1][2]*A[3][3] + A[0][2]*A[1][3]*A[3][1] + A[0][3]*A[1][1]*A[3][2] - A[0][1]*A[1][3]*A[3][2] - A[0][2]*A[1][1]*A[3][3] - A[0][3]*A[1][2]*A[3][1];
425 M[0][3] = A[0][1]*A[1][3]*A[2][2] + A[0][2]*A[1][1]*A[2][3] + A[0][3]*A[1][2]*A[2][1] - A[0][1]*A[1][2]*A[2][3] - A[0][2]*A[1][3]*A[2][1] - A[0][3]*A[1][1]*A[2][2];
427 M[1][0] = A[1][0]*A[2][3]*A[3][2] + A[1][2]*A[2][0]*A[3][3] + A[1][3]*A[2][2]*A[3][0] - A[1][0]*A[2][2]*A[3][3] - A[1][2]*A[2][3]*A[3][0] - A[1][3]*A[2][0]*A[3][2];
428 M[1][1] = A[0][0]*A[2][2]*A[3][3] + A[0][2]*A[2][3]*A[3][0] + A[0][3]*A[2][0]*A[3][2] - A[0][0]*A[2][3]*A[3][2] - A[0][2]*A[2][0]*A[3][3] - A[0][3]*A[2][2]*A[3][0];
429 M[1][2] = A[0][0]*A[1][3]*A[3][2] + A[0][2]*A[1][0]*A[3][3] + A[0][3]*A[1][2]*A[3][0] - A[0][0]*A[1][2]*A[3][3] - A[0][2]*A[1][3]*A[3][0] - A[0][3]*A[1][0]*A[3][2];
430 M[1][3] = A[0][0]*A[1][2]*A[2][3] + A[0][2]*A[1][3]*A[2][0] + A[0][3]*A[1][0]*A[2][2] - A[0][0]*A[1][3]*A[2][2] - A[0][2]*A[1][0]*A[2][3] - A[0][3]*A[1][2]*A[2][0];
432 M[2][0] = A[1][0]*A[2][1]*A[3][3] + A[1][1]*A[2][3]*A[3][0] + A[1][3]*A[2][0]*A[3][1] - A[1][0]*A[2][3]*A[3][1] - A[1][1]*A[2][0]*A[3][3] - A[1][3]*A[2][1]*A[3][0];
433 M[2][1] = A[0][0]*A[2][3]*A[3][1] + A[0][1]*A[2][0]*A[3][3] + A[0][3]*A[2][1]*A[3][0] - A[0][0]*A[2][1]*A[3][3] - A[0][1]*A[2][3]*A[3][0] - A[0][3]*A[2][0]*A[3][1];
434 M[2][2] = A[0][0]*A[1][1]*A[3][3] + A[0][1]*A[1][3]*A[3][0] + A[0][3]*A[1][0]*A[3][1] - A[0][0]*A[1][3]*A[3][1] - A[0][1]*A[1][0]*A[3][3] - A[0][3]*A[1][1]*A[3][0];
435 M[2][3] = A[0][0]*A[1][3]*A[2][1] + A[0][1]*A[1][0]*A[2][3] + A[0][3]*A[1][1]*A[2][0] - A[0][0]*A[1][1]*A[2][3] - A[0][1]*A[1][3]*A[2][0] - A[0][3]*A[1][0]*A[2][1];
437 M[3][0] = A[1][0]*A[2][2]*A[3][1] + A[1][1]*A[2][0]*A[3][2] + A[1][2]*A[2][1]*A[3][0] - A[1][0]*A[2][1]*A[3][2] - A[1][1]*A[2][2]*A[3][0] - A[1][2]*A[2][0]*A[3][1];
438 M[3][1] = A[0][0]*A[2][1]*A[3][2] + A[0][1]*A[2][2]*A[3][0] + A[0][2]*A[2][0]*A[3][1] - A[0][0]*A[2][2]*A[3][1] - A[0][1]*A[2][0]*A[3][2] - A[0][2]*A[2][1]*A[3][0];
439 M[3][2] = A[0][0]*A[1][2]*A[3][1] + A[0][1]*A[1][0]*A[3][2] + A[0][2]*A[1][1]*A[3][0] - A[0][0]*A[1][1]*A[3][2] - A[0][1]*A[1][2]*A[3][0] - A[0][2]*A[1][0]*A[3][1];
440 M[3][3] = A[0][0]*A[1][1]*A[2][2] + A[0][1]*A[1][2]*A[2][0] + A[0][2]*A[1][0]*A[2][1] - A[0][0]*A[1][2]*A[2][1] - A[0][1]*A[1][0]*A[2][2] - A[0][2]*A[1][1]*A[2][0];
458 unsigned int n = A.size();
465 double lambda =
Dot( y, Ay );
471 bool converged =
false;
472 while(!converged && it_counter < max_it)
475 lambda0 = std::fabs(lambda);
478 lambda =
Dot( y, Ay );
482 if (std::fabs(std::fabs(lambda) - lambda0) <= tol)
492 y =
VecMul(Ay, 1./lambda);
495 e_vec = std::move(y);
std::vector< VecDbl > MatDbl
std::vector< double > VecDbl
void PrintMatrix(const MatDbl &A)
double PowerIteration(const MatDbl &A, VecDbl &e_vec, int max_it=2000, double tol=1.0e-13)
void Scale(VecDbl &x, const double &val)
VecDbl VecMul(const VecDbl &x, const double &val)
void Set(VecDbl &x, const double &val)
MatDbl Inverse(const MatDbl &A)
MatDbl SubMatrix(const size_t r, const size_t c, const MatDbl &A)
double Determinant(const MatDbl &A)
void SwapColumn(size_t c1, size_t c2, MatDbl &A)
MatDbl Transpose(const MatDbl &A)
MatDbl MatAdd(const MatDbl &A, const MatDbl &B)
void GaussElimination(MatDbl &A, VecDbl &b, int n)
MatDbl MatMul(const MatDbl &A, const double c)
double Dot(const VecDbl &x, const VecDbl &y)
MatDbl InverseGEPivoting(const MatDbl &A)
MatDbl MatSubtract(const MatDbl &A, const MatDbl &B)
double Vec2Norm(const VecDbl &x)
void SwapRow(size_t r1, size_t r2, MatDbl &A)