5 #ifndef BPP_NUMERIC_MATRIX_LUDECOMPOSITION_H 6 #define BPP_NUMERIC_MATRIX_LUDECOMPOSITION_H 9 #include "../../Exceptions.h" 10 #include "../NumTools.h" 45 std::vector<size_t>
piv;
50 size_t piv_length = piv.size();
52 X.
resize(piv_length, j1 - j0 + 1);
54 for (
size_t i = 0; i < piv_length; i++)
56 for (
size_t j = j0; j <= j1; j++)
58 X(i, j - j0) = A(piv[i], j);
63 static void permuteCopy(
const std::vector<Real>& A,
const std::vector<size_t>& piv, std::vector<Real>& X)
65 size_t piv_length = piv.size();
66 if (piv_length != A.size())
71 for (
size_t i = 0; i < piv_length; i++)
163 L_(A.getNumberOfRows(), A.getNumberOfColumns()),
164 U_(A.getNumberOfColumns(), A.getNumberOfColumns()),
165 m(A.getNumberOfRows()),
166 n(A.getNumberOfColumns()),
168 piv(A.getNumberOfRows())
170 for (
size_t i = 0; i <
m; i++)
175 for (
size_t k = 0; k <
n; k++)
179 for (
size_t i = k + 1; i <
m; i++)
181 if (NumTools::abs<Real>(
LU(i, k)) > NumTools::abs<Real>(
LU(p, k)))
189 for (
size_t j = 0; j <
n; j++)
191 double t =
LU(p, j);
LU(p, j) =
LU(k, j);
LU(k, j) = t;
193 size_t t = piv[p]; piv[p] = piv[k]; piv[k] = t;
199 for (
size_t i = k + 1; i <
m; i++)
201 LU(i, k) /=
LU(k, k);
202 for (
size_t j = k + 1; j <
n; j++)
204 LU(i, j) -=
LU(i, k) *
LU(k, j);
218 for (
size_t i = 0; i <
m; i++)
220 for (
size_t j = 0; j <
n; j++)
246 for (
size_t i = 0; i <
n; i++)
248 for (
size_t j = 0; j <
n; j++)
285 Real d = Real(pivsign);
286 for (
size_t j = 0; j <
n; j++)
313 Real minD = NumTools::abs<Real>(
LU(0, 0));
314 for (
size_t i = 1; i <
m; i++)
316 Real currentValue = NumTools::abs<Real>(
LU(i, i));
317 if (currentValue < minD)
332 for (
size_t k = 0; k <
n; k++)
334 for (
size_t i = k + 1; i <
n; i++)
336 for (
size_t j = 0; j < nx; j++)
338 X(i, j) -= X(k, j) *
LU(i, k);
350 for (
size_t j = 0; j < nx; j++)
354 for (
size_t i = 0; i < k; i++)
356 for (
size_t j = 0; j < nx; j++)
358 X(i, j) -= X(k, j) *
LU(i, k);
378 Real
solve (
const std::vector<Real>& b, std::vector<Real>& x)
const 387 Real minD = NumTools::abs<Real>(
LU(0, 0));
388 for (
size_t i = 1; i <
m; i++)
390 Real currentValue = NumTools::abs<Real>(
LU(i, i));
391 if (currentValue < minD)
403 for (
size_t k = 0; k <
n; k++)
405 for (
size_t i = k + 1; i <
n; i++)
407 x[i] -= x[k] *
LU(i, k);
418 for (
size_t i = 0; i < k; i++)
420 x[i] -= x[k] *
LU(i, k);
429 #endif // BPP_NUMERIC_MATRIX_LUDECOMPOSITION_H
The matrix template interface.
The base class exception for zero division error.
const RowMatrix< Real > & getL()
Return lower triangular factor.
const RowMatrix< Real > & getU()
Return upper triangular factor.
virtual void resize(size_t nRows, size_t nCols)=0
Resize the matrix.
Number exception: integers.
Real solve(const std::vector< Real > &b, std::vector< Real > &x) const
Solve A*x = b, where x and b are vectors of length equal to the number of rows in A...
static void permuteCopy(const std::vector< Real > &A, const std::vector< size_t > &piv, std::vector< Real > &X)
std::vector< size_t > piv
Real det() const
Compute determinant using LU factors.
virtual size_t getNumberOfColumns() const =0
Real solve(const Matrix< Real > &B, Matrix< Real > &X) const
Solve A*X = B.
LUDecomposition(const Matrix< Real > &A)
LU Decomposition.
static void permuteCopy(const Matrix< Real > &A, const std::vector< size_t > &piv, size_t j0, size_t j1, Matrix< Real > &X)
virtual size_t getNumberOfRows() const =0
std::vector< size_t > getPivot() const
Return pivot permutation vector.