44 #include "../NumTools.h" 45 #include "../../Exceptions.h" 79 std::vector<size_t>
piv;
84 size_t piv_length =
piv.size();
86 X.
resize(piv_length, j1 - j0 + 1);
88 for (
size_t i = 0; i < piv_length; i++)
90 for (
size_t j = j0; j <= j1; j++)
92 X(i, j - j0) = A(
piv[i], j);
97 static void permuteCopy(
const std::vector<Real>& A,
const std::vector<size_t>&
piv, std::vector<Real>& X)
99 size_t piv_length =
piv.size();
100 if (piv_length != A.size())
103 X.resize(piv_length);
105 for (
size_t i = 0; i < piv_length; i++)
197 L_(A.getNumberOfRows(), A.getNumberOfColumns()),
198 U_(A.getNumberOfColumns(), A.getNumberOfColumns()),
199 m(A.getNumberOfRows()),
200 n(A.getNumberOfColumns()),
202 piv(A.getNumberOfRows())
204 for (
size_t i = 0; i <
m; i++)
209 for (
size_t k = 0; k <
n; k++)
213 for (
size_t i = k + 1; i <
m; i++)
215 if (NumTools::abs<Real>(
LU(i, k)) > NumTools::abs<Real>(
LU(p, k)))
223 for (
size_t j = 0; j <
n; j++)
225 double t =
LU(p, j);
LU(p, j) =
LU(k, j);
LU(k, j) = t;
233 for (
size_t i = k + 1; i <
m; i++)
235 LU(i, k) /=
LU(k, k);
236 for (
size_t j = k + 1; j <
n; j++)
238 LU(i, j) -=
LU(i, k) *
LU(k, j);
252 for (
size_t i = 0; i <
m; i++)
254 for (
size_t j = 0; j <
n; j++)
280 for (
size_t i = 0; i <
n; i++)
282 for (
size_t j = 0; j <
n; j++)
320 for (
size_t j = 0; j <
n; j++)
342 if (B.getNumberOfRows() !=
m)
344 throw BadIntegerException(
"Wrong dimension in LU::solve", static_cast<int>(B.getNumberOfRows()));
347 Real minD = NumTools::abs<Real>(
LU(0, 0));
348 for (
size_t i = 1; i <
m; i++)
350 Real currentValue = NumTools::abs<Real>(
LU(i, i));
351 if (currentValue < minD)
361 size_t nx = B.getNumberOfColumns();
366 for (
size_t k = 0; k <
n; k++)
368 for (
size_t i = k + 1; i <
n; i++)
370 for (
size_t j = 0; j < nx; j++)
372 X(i, j) -= X(k, j) *
LU(i, k);
384 for (
size_t j = 0; j < nx; j++)
388 for (
size_t i = 0; i < k; i++)
390 for (
size_t j = 0; j < nx; j++)
392 X(i, j) -= X(k, j) *
LU(i, k);
421 Real minD = NumTools::abs<Real>(
LU(0, 0));
422 for (
size_t i = 1; i <
m; i++)
424 Real currentValue = NumTools::abs<Real>(
LU(i, i));
425 if (currentValue < minD)
437 for (
size_t k = 0; k <
n; k++)
439 for (
size_t i = k + 1; i <
n; i++)
441 x[i] -= x[k] *
LU(i, k);
452 for (
size_t i = 0; i < k; i++)
454 x[i] -= x[k] *
LU(i, k);
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.
This class allows to perform a correspondence analysis.
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.
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)
std::vector< size_t > getPivot() const
Return pivot permutation vector.