det.cpp
Go to the documentation of this file.
#include <itpp/base/algebra/det.h>
#include <itpp/base/algebra/lu.h>
namespace itpp
{
/* Determinant of square matrix.
Calculate determinant of inmatrix (Uses LU-factorisation)
(See Theorem 3.2.1 p. 97 in Golub & van Loan, "Matrix Computations").
det(X) = det(P')*det(L)*det(U) = det(P')*1*prod(diag(U))
*/
double det(const mat &X)
{
it_assert_debug(X.rows() == X.cols(), "det : Only square matrices");
mat L, U;
ivec p;
double s = 1.0;
int i;
lu(X, L, U, p); // calculate LU-factorisation
double temp = U(0, 0);
for (i = 1;i < X.rows();i++) {
temp *= U(i, i);
}
// Calculate det(P'). Equal to (-1)^(no row changes)
for (i = 0; i < p.size(); i++)
if (i != p(i))
s *= -1.0;
return temp*s;
}
/* Determinant of complex square matrix.
Calculate determinant of inmatrix (Uses LU-factorisation)
(See Theorem 3.2.1 p. 97 in Golub & van Loan, "Matrix Computations").
det(X) = det(P')*det(L)*det(U) = det(P')*1*prod(diag(U))
Needs LU-factorization of complex matrices (LAPACK)
*/
std::complex<double> det(const cmat &X)
{
it_assert_debug(X.rows() == X.cols(), "det : Only square matrices");
int i;
cmat L, U;
ivec p;
double s = 1.0;
lu(X, L, U, p); // calculate LU-factorisation
std::complex<double> temp = U(0, 0);
for (i = 1;i < X.rows();i++) {
temp *= U(i, i);
}
// Calculate det(P'). Equal to (-1)^(no row changes)
for (i = 0; i < p.size(); i++)
if (i != p(i))
s *= -1.0;
return temp*s;
}
} // namespace itpp
det.h
Definitions of determinant calculations.
itpp::det
double det(const mat &X)
Determinant of real square matrix.
Definition det.cpp:43
it_assert_debug
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Definition itassert.h:107
itpp::lu
bool lu(const mat &X, mat &L, mat &U, ivec &p)
LU factorisation of real matrix.
Definition lu.cpp:117
lu.h
Definitions of LU factorisation functions.
itpp
itpp namespace
Definition itmex.h:37

Generated on Mon Jun 10 2024 11:49:16 for IT++ by Doxygen 1.9.8