IT++ Logo
det.cpp
Go to the documentation of this file.
1 
29 #include <itpp/base/algebra/det.h>
30 #include <itpp/base/algebra/lu.h>
31 
32 
33 namespace 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 */
43 double 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 */
76 std::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
SourceForge Logo

Generated on Sat Jul 6 2013 10:54:19 for IT++ by Doxygen 1.8.2