IT++ Logo
det.cpp
Go to the documentation of this file.
1
31
32
33namespace itpp
34{
35
36
37/* Determinant of square matrix.
38 Calculate determinant of inmatrix (Uses LU-factorisation)
39 (See Theorem 3.2.1 p. 97 in Golub & van Loan, "Matrix Computations").
40
41 det(X) = det(P')*det(L)*det(U) = det(P')*1*prod(diag(U))
42*/
43double det(const mat &X)
44{
45 it_assert_debug(X.rows() == X.cols(), "det : Only square matrices");
46
47 mat L, U;
48 ivec p;
49 double s = 1.0;
50 int i;
51
52 lu(X, L, U, p); // calculate LU-factorisation
53
54 double temp = U(0, 0);
55 for (i = 1;i < X.rows();i++) {
56 temp *= U(i, i);
57 }
58
59 // Calculate det(P'). Equal to (-1)^(no row changes)
60 for (i = 0; i < p.size(); i++)
61 if (i != p(i))
62 s *= -1.0;
63
64 return temp*s;
65}
66
67
68/* Determinant of complex square matrix.
69 Calculate determinant of inmatrix (Uses LU-factorisation)
70 (See Theorem 3.2.1 p. 97 in Golub & van Loan, "Matrix Computations").
71
72 det(X) = det(P')*det(L)*det(U) = det(P')*1*prod(diag(U))
73
74 Needs LU-factorization of complex matrices (LAPACK)
75*/
76std::complex<double> det(const cmat &X)
77{
78 it_assert_debug(X.rows() == X.cols(), "det : Only square matrices");
79
80 int i;
81 cmat L, U;
82 ivec p;
83 double s = 1.0;
84
85 lu(X, L, U, p); // calculate LU-factorisation
86
87 std::complex<double> temp = U(0, 0);
88 for (i = 1;i < X.rows();i++) {
89 temp *= U(i, i);
90 }
91
92 // Calculate det(P'). Equal to (-1)^(no row changes)
93 for (i = 0; i < p.size(); i++)
94 if (i != p(i))
95 s *= -1.0;
96
97 return temp*s;
98}
99
100
101} // namespace itpp
Definitions of determinant calculations.
double det(const mat &X)
Determinant of real square matrix.
Definition det.cpp:43
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Definition itassert.h:107
bool lu(const mat &X, mat &L, mat &U, ivec &p)
LU factorisation of real matrix.
Definition lu.cpp:117
Definitions of LU factorisation functions.
itpp namespace
Definition itmex.h:37
SourceForge Logo

Generated on Tue Dec 10 2024 04:49:37 for IT++ by Doxygen 1.12.0