Peano
Loading...
Searching...
No Matches
LUDecomposition.cpph
Go to the documentation of this file.
1template <int Rows, typename Scalar>
3 const Matrix<Rows, Rows, Scalar>& R,
5) {
7
8 for (int k = Rows - 1; k >= 0; k--) {
9 x(k) = f(k);
10 for (int i = k + 1; i < Rows; i++) {
11 x(k) -= R(k, i) * x(i);
12 }
13 x(k) = x(k) / R(k, k);
14 }
15
16 return x;
17}
18
19
20template <int Rows, typename Scalar>
22 for (int k = 0; k < Rows; k++) {
23 int maxIndex = k;
24 Scalar max = std::abs(A(k, k));
25 for (int i = k + 1; i < Rows; i++) {
26 Scalar current = std::abs(A(i, k));
27 if (current > max) {
28 maxIndex = i;
29 max = current;
30 }
31 }
32 pivots(k) = maxIndex;
33
34 // Exchange lines
35 for (int j = 0; j < Rows; j++) {
36 Scalar temp = A(k, j);
37 A(k, j) = A(maxIndex, j);
38 A(maxIndex, j) = temp;
39 }
40
41 // Compute scaling elements
42 for (int i = k + 1; i < Rows; i++) {
43 A(i, k) /= A(k, k);
44 }
45
46 // Subtract contributions from each line
47 for (int i = k + 1; i < Rows; i++) {
48 for (int j = k + 1; j < Rows; j++) {
49 A(i, j) -= A(i, k) * A(k, j);
50 }
51 }
52 }
53}
54
55
56template <int Rows, typename Scalar>
58 for (int k = 0; k < Rows; k++) {
59 // Compute scaling elements
60 for (int i = k + 1; i < Rows; i++) {
61 A(i, k) /= A(k, k);
62 }
63
64 // Subtract contributions from each line
65 for (int i = k + 1; i < Rows; i++) {
66 for (int j = k + 1; j < Rows; j++) {
67 A(i, j) -= A(i, k) * A(k, j);
68 }
69 }
70 }
71}
72
73
74template <typename Scalar>
76 return R(0, 0) * R(1, 1) - R(0, 1) * R(1, 0);
77}
78
79
80template <int Rows, typename Scalar>
83) {
84 tarch::la::Matrix<Rows, Rows, Scalar> result(static_cast<Scalar>(0));
86
87 lu(LU);
88
89 for (int j = 0; j < Rows; j++) {
90 for (int i = 0; i < Rows; i++) {
91 result(i, j) = ((i == j) ? static_cast<Scalar>(1) : static_cast<Scalar>(0));
92 for (int k = 0; k < i; k++) {
93 result(i, j) -= LU(i, k) * result(k, j);
94 }
95 }
96
97 for (int i = Rows - 1; i >= 0; i--) {
98 for (int k = i + 1; k < Rows; k++) {
99 result(i, j) -= LU(i, k) * result(k, j);
100 }
101 result(i, j) /= LU(i, i);
102 }
103 }
104 return result;
105}
106
107#ifdef UseLapack
108template <int Rows>
111) {
112 // make a copy
113 [[maybe_unused]] tarch::la::Matrix<Rows, Rows, double> M = R;
114
115 // aux variables
116 int returnCode;
117 int ipiv[Rows];
118
119 // step 1 - compute LU factorisation. Modify M in-place.
120 returnCode = LAPACKE_dgetrf(
121 LAPACK_ROW_MAJOR,
122 Rows,
123 Rows,
124 M.data(),
125 Rows,
126 ipiv
127 );
128 // check return code.
129 assertion(returnCode == 0);
130
131 // step 2 - use the result of the above to compute the inverse. Modify M
132 // in-place.
133 returnCode = LAPACKE_dgetri(LAPACK_ROW_MAJOR, Rows, M.data(), Rows, ipiv);
134 // check return code
135 assertion(returnCode == 0);
136
137 return M;
138}
139#endif
140
141template <typename Scalar>
142tarch::la::Matrix<2, 2, Scalar> tarch::la::invert(const Matrix<2, 2, Scalar>& R
143) {
144 Matrix<2, 2, Scalar> result;
145
146 assertion(det(R) != 0.0);
147 const double scaling = 1.0 / det(R);
148
149 result(0, 0) = R(1, 1) * scaling;
150 result(0, 1) = -R(0, 1) * scaling;
151 result(1, 0) = -R(1, 0) * scaling;
152 result(1, 1) = R(0, 0) * scaling;
153
154 return result;
155}
156
157
158template <typename Scalar>
160 return R(0, 0) * (R(1, 1) * R(2, 2) - R(2, 1) * R(1, 2))
161 - R(0, 1) * (R(1, 0) * R(2, 2) - R(1, 2) * R(2, 0))
162 + R(0, 2) * (R(1, 0) * R(2, 1) - R(1, 1) * R(2, 0));
163}
164
165
166template <typename Scalar>
167tarch::la::Matrix<3, 3, Scalar> tarch::la::invert(const Matrix<3, 3, Scalar>& R
168) {
169 Matrix<3, 3, Scalar> result;
170
171 assertion(det(R) != 0.0);
172 double invdet = 1 / det(R);
173
174 result(0, 0) = (R(1, 1) * R(2, 2) - R(2, 1) * R(1, 2)) * invdet;
175 result(0, 1) = (R(0, 2) * R(2, 1) - R(0, 1) * R(2, 2)) * invdet;
176 result(0, 2) = (R(0, 1) * R(1, 2) - R(0, 2) * R(1, 1)) * invdet;
177 result(1, 0) = (R(1, 2) * R(2, 0) - R(1, 0) * R(2, 2)) * invdet;
178 result(1, 1) = (R(0, 0) * R(2, 2) - R(0, 2) * R(2, 0)) * invdet;
179 result(1, 2) = (R(1, 0) * R(0, 2) - R(0, 0) * R(1, 2)) * invdet;
180 result(2, 0) = (R(1, 0) * R(2, 1) - R(2, 0) * R(1, 1)) * invdet;
181 result(2, 1) = (R(2, 0) * R(0, 1) - R(0, 0) * R(2, 1)) * invdet;
182 result(2, 2) = (R(0, 0) * R(1, 1) - R(1, 0) * R(0, 1)) * invdet;
183
184 return result;
185}
double f(double p4, double p1, double p5, double rho1, double rho5, double gamma)
#define assertion(expr)
const int temp
Definition vec.h:7
Static (i.e.
Definition Matrix.h:42
float M
Definition csv_plot.py:10
CF abs(const CF &cf)
Matrix< Rows, Rows, Scalar > invert(const Matrix< Rows, Rows, Scalar > &M)
Invert matrix with LU decomposition.
Vector< Rows, Scalar > backSubstitution(const Matrix< Rows, Rows, Scalar > &R, const Vector< Rows, Scalar > &f)
Back substitution following LU decomposition.
double det(const Matrix< 2, 2, Scalar > &R)
double max(double a, double b, double c)
I need the maximum of three values all the time, to I decided to write a function for this.
Definition Scalar.cpp:8
void lu(Matrix< Rows, Rows, Scalar > &A, Vector< Rows, int > &pivots)
Performs an in-situ LU-decomposition of the square matrix A.
Simple vector class.
Definition Vector.h:150