8 #include "../Common/Range.hpp" 16 template <
class T,
int Dim, eMatrixOrder Order, eMatrixLayout Layout,
bool Packed>
20 template <
class T2,
int Dim2, eMatrixOrder Order2, eMatrixLayout Layout2,
bool Packed2>
34 return Solve(
L,
U, b);
39 T sum = std::abs(prod);
40 for (
int i = 1; i < Dim; ++i) {
42 sum += std::abs(
L(i, i));
45 return std::abs(prod) / sum > T(1e-6);
57 template <
class T,
int Dim, eMatrixOrder Order, eMatrixLayout Layout,
bool Packed>
72 T sum = std::abs(prod);
73 for (
int i = 1; i < Dim; ++i) {
75 sum += std::abs(
L(i, i));
78 return std::abs(prod) / sum > T(1e-6);
90 template <
class T,
int Dim, eMatrixOrder Order, eMatrixLayout Layout,
bool Packed>
97 constexpr
int n = Dim;
99 for (
int i = 0; i < n; ++i) {
100 for (
int j = i + 1; j < n; ++j) {
103 for (
int j = 0; j <= i; ++j) {
109 for (
int i = 0; i < n; ++i) {
112 for (
int j = 1; j < n; ++j) {
113 U(0, j) = A(0, j) /
L(0, 0);
116 for (
int j = 1; j < n - 1; ++j) {
117 for (
int i = j; i < n; ++i) {
120 for (
int k = 0; k <= j - 1; ++k) {
121 Lij -=
L(i, k) *
U(k, j);
125 for (
int k = j; k < n; ++k) {
128 for (
int i = 0; i <= j - 1; ++i) {
129 Ujk -=
L(j, i) *
U(i, k);
136 L(n - 1, n - 1) = A(n - 1, n - 1);
137 for (
int k = 0; k < n - 1; ++k) {
138 L(n - 1, n - 1) -=
L(n - 1, k) *
U(k, n - 1);
150 template <
class T,
int Dim, eMatrixOrder Order, eMatrixLayout Layout,
bool Packed>
160 for (
int i :
Range(0, n)) {
164 for (
int j :
Range(0, n)) {
168 for (
int i :
Range(j, n)) {
169 if (std::abs(
U(i, j)) > p) {
171 p = std::abs(
U(i, j));
181 std::swap(P(j), P(largest));
182 parity *= (j != largest ? -1 : 1);
183 for (
int i :
Range(0, n)) {
184 std::swap(
U(j, i),
U(largest, i));
188 for (
int i :
Range(j + 1, n)) {
189 U(i, j) =
U(i, j) /
U(j, j);
190 for (
int k :
Range(j + 1, n)) {
191 U(i, k) =
U(i, k) -
U(i, j) *
U(j, k);
197 for (
int j :
Range(0, n)) {
198 for (
int i :
Range(j + 1, n)) {
204 for (
int i :
Range(n)) {
214 template <
class T,
int Dim, eMatrixOrder Order, eMatrixLayout Layout,
bool Packed>
221 template <
class T,
int Dim, eMatrixOrder Order, eMatrixLayout Layout,
bool Packed>
222 Vector<float, Dim, Packed> DecompositionLU<T, Dim, Order, Layout, Packed>::Solve(
const MatrixT&
L,
const MatrixT&
U,
const Vector<T, Dim, Packed>& b) {
227 E.template Submatrix<Dim, Dim>(0, 0) = L;
230 for (
int i = 0; i < Dim - 1; ++i) {
231 for (
int i2 = i + 1; i2 < Dim; ++i2) {
237 E(Dim - 1, Dim) /= E(Dim - 1, Dim - 1);
241 E.template Submatrix<Dim, Dim>(0, 0) = U;
243 for (
int i = Dim - 1; i > 0; --i) {
244 for (
int i2 = i - 1; i2 >= 0; --i2) {
250 E(0, Dim) /= E(0, 0);
257 template <
class T,
int Dim, eMatrixOrder Order, eMatrixLayout Layout,
bool Packed>
261 for (
int i :
Range(0, P.Dimension())) {
DecompositionLU(MatrixT L, MatrixT U)
Definition: DecomposeLU.hpp:27
A utility class that can do common operations with the LU decomposition, i.e. solving equation system...
Definition: DecomposeLU.hpp:17
DecompositionLUP(MatrixT L, MatrixT U, Vector< int, Dim, false > P)
Definition: DecomposeLU.hpp:62
auto Column(int colIdx)
Return the submatrix corresponding to the specified column.
Definition: MatrixImpl.hpp:302
bool Solvable() const
Definition: DecomposeLU.hpp:37
Vector< float, Dim, Packed > Solve(const Vector< T, Dim, Packed > &b) const
Solves the equation system Ax=b, that is LUx=Pb.
Definition: DecomposeLU.hpp:258
MatrixT U
Upper triangular matrix, LU=P'A.
Definition: DecomposeLU.hpp:51
Represents a vector in N-dimensional space.
Definition: Definitions.hpp:57
Vector< float, Dim, Packed > Solve(const Vector< T, Dim, Packed > &b) const
Solves the equation system Ax=b, that is LUx=b.
Definition: DecomposeLU.hpp:33
MatrixT L
Lower triangular matrix, LU=P'A.
Definition: DecomposeLU.hpp:49
std::array< Vector< T, StripeDim, Packed >, StripeCount > stripes
Definition: MatrixImpl.hpp:46
Definition: Approx.hpp:11
bool Solvable()
Definition: DecomposeLU.hpp:70
auto DecomposeLUP(const Matrix< T, Dim, Dim, Order, Layout, Packed > &m, int &parity)
Implements LU decomposition with partial pivoting.
Definition: DecomposeLU.hpp:151
Vector< int, Dim, false > P
Row permutations. LU=P'A, where P' is a matrix whose i-th row's P[i]-th element is one...
Definition: DecomposeLU.hpp:86
A utility class that can do common operations with the LUP decomposition, i.e. solving equation syste...
Definition: DecomposeLU.hpp:58
RangeHelper< T > Range(T first, T last, T step)
Definition: Range.hpp:54
auto DecomposeLU(const Matrix< T, Dim, Dim, Order, Layout, Packed > &m)
Definition: DecomposeLU.hpp:91
MatrixT L
Lower triangular matrix, LU=P'A.
Definition: DecomposeLU.hpp:82
MatrixT U
Upper triangular matrix, LU=P'A.
Definition: DecomposeLU.hpp:84
constexpr int RowCount() const
Returns the number of rows of the matrix.
Definition: MatrixImpl.hpp:31