Determinant.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
5 //
6 // Eigen is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3 of the License, or (at your option) any later version.
10 //
11 // Alternatively, you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 2 of
14 // the License, or (at your option) any later version.
15 //
16 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License and a copy of the GNU General Public License along with
23 // Eigen. If not, see <http://www.gnu.org/licenses/>.
24 
25 #ifndef EIGEN_DETERMINANT_H
26 #define EIGEN_DETERMINANT_H
27 
28 namespace Eigen {
29 
30 namespace internal {
31 
32 template<typename Derived>
33 inline const typename Derived::Scalar bruteforce_det3_helper
34 (const MatrixBase<Derived>& matrix, int a, int b, int c)
35 {
36  return matrix.coeff(0,a)
37  * (matrix.coeff(1,b) * matrix.coeff(2,c) - matrix.coeff(1,c) * matrix.coeff(2,b));
38 }
39 
40 template<typename Derived>
41 const typename Derived::Scalar bruteforce_det4_helper
42 (const MatrixBase<Derived>& matrix, int j, int k, int m, int n)
43 {
44  return (matrix.coeff(j,0) * matrix.coeff(k,1) - matrix.coeff(k,0) * matrix.coeff(j,1))
45  * (matrix.coeff(m,2) * matrix.coeff(n,3) - matrix.coeff(n,2) * matrix.coeff(m,3));
46 }
47 
48 template<typename Derived,
49  int DeterminantType = Derived::RowsAtCompileTime
50 > struct determinant_impl
51 {
52  static inline typename traits<Derived>::Scalar run(const Derived& m)
53  {
54  if(Derived::ColsAtCompileTime==Dynamic && m.rows()==0)
55  return typename traits<Derived>::Scalar(1);
56  return m.partialPivLu().determinant();
57  }
58 };
59 
60 template<typename Derived> struct determinant_impl<Derived, 1>
61 {
62  static inline typename traits<Derived>::Scalar run(const Derived& m)
63  {
64  return m.coeff(0,0);
65  }
66 };
67 
68 template<typename Derived> struct determinant_impl<Derived, 2>
69 {
70  static inline typename traits<Derived>::Scalar run(const Derived& m)
71  {
72  return m.coeff(0,0) * m.coeff(1,1) - m.coeff(1,0) * m.coeff(0,1);
73  }
74 };
75 
76 template<typename Derived> struct determinant_impl<Derived, 3>
77 {
78  static inline typename traits<Derived>::Scalar run(const Derived& m)
79  {
80  return bruteforce_det3_helper(m,0,1,2)
81  - bruteforce_det3_helper(m,1,0,2)
82  + bruteforce_det3_helper(m,2,0,1);
83  }
84 };
85 
86 template<typename Derived> struct determinant_impl<Derived, 4>
87 {
88  static typename traits<Derived>::Scalar run(const Derived& m)
89  {
90  // trick by Martin Costabel to compute 4x4 det with only 30 muls
91  return bruteforce_det4_helper(m,0,1,2,3)
92  - bruteforce_det4_helper(m,0,2,1,3)
93  + bruteforce_det4_helper(m,0,3,1,2)
94  + bruteforce_det4_helper(m,1,2,0,3)
95  - bruteforce_det4_helper(m,1,3,0,2)
96  + bruteforce_det4_helper(m,2,3,0,1);
97  }
98 };
99 
100 } // end namespace internal
101 
106 template<typename Derived>
107 inline typename internal::traits<Derived>::Scalar MatrixBase<Derived>::determinant() const
108 {
109  assert(rows() == cols());
110  typedef typename internal::nested<Derived,Base::RowsAtCompileTime>::type Nested;
111  return internal::determinant_impl<typename internal::remove_all<Nested>::type>::run(derived());
112 }
113 
114 } // end namespace Eigen
115 
116 #endif // EIGEN_DETERMINANT_H