Eigen  3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)
Determinant.h
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 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_DETERMINANT_H
11 #define EIGEN_DETERMINANT_H
12 
13 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
17 namespace internal {
18 
19 template<typename Derived>
20 EIGEN_DEVICE_FUNC
21 inline const typename Derived::Scalar bruteforce_det3_helper
22 (const MatrixBase<Derived>& matrix, int a, int b, int c)
23 {
24  return matrix.coeff(0,a)
25  * (matrix.coeff(1,b) * matrix.coeff(2,c) - matrix.coeff(1,c) * matrix.coeff(2,b));
26 }
27 
28 template<typename Derived,
29  int DeterminantType = Derived::RowsAtCompileTime
30 > struct determinant_impl
31 {
32  static inline typename traits<Derived>::Scalar run(const Derived& m)
33  {
34  if(Derived::ColsAtCompileTime==Dynamic && m.rows()==0)
35  return typename traits<Derived>::Scalar(1);
36  return m.partialPivLu().determinant();
37  }
38 };
39 
40 template<typename Derived> struct determinant_impl<Derived, 1>
41 {
42  static inline EIGEN_DEVICE_FUNC
43  typename traits<Derived>::Scalar run(const Derived& m)
44  {
45  return m.coeff(0,0);
46  }
47 };
48 
49 template<typename Derived> struct determinant_impl<Derived, 2>
50 {
51  static inline EIGEN_DEVICE_FUNC
52  typename traits<Derived>::Scalar run(const Derived& m)
53  {
54  return m.coeff(0,0) * m.coeff(1,1) - m.coeff(1,0) * m.coeff(0,1);
55  }
56 };
57 
58 template<typename Derived> struct determinant_impl<Derived, 3>
59 {
60  static inline EIGEN_DEVICE_FUNC
61  typename traits<Derived>::Scalar run(const Derived& m)
62  {
63  return bruteforce_det3_helper(m,0,1,2)
64  - bruteforce_det3_helper(m,1,0,2)
65  + bruteforce_det3_helper(m,2,0,1);
66  }
67 };
68 
69 template<typename Derived> struct determinant_impl<Derived, 4>
70 {
71  typedef typename traits<Derived>::Scalar Scalar;
72  static EIGEN_DEVICE_FUNC
73  Scalar run(const Derived& m)
74  {
75  Scalar d2_01 = det2(m, 0, 1);
76  Scalar d2_02 = det2(m, 0, 2);
77  Scalar d2_03 = det2(m, 0, 3);
78  Scalar d2_12 = det2(m, 1, 2);
79  Scalar d2_13 = det2(m, 1, 3);
80  Scalar d2_23 = det2(m, 2, 3);
81  Scalar d3_0 = det3(m, 1,d2_23, 2,d2_13, 3,d2_12);
82  Scalar d3_1 = det3(m, 0,d2_23, 2,d2_03, 3,d2_02);
83  Scalar d3_2 = det3(m, 0,d2_13, 1,d2_03, 3,d2_01);
84  Scalar d3_3 = det3(m, 0,d2_12, 1,d2_02, 2,d2_01);
85  return internal::pmadd(static_cast<Scalar>(-m(0,3)),d3_0, static_cast<Scalar>(m(1,3)*d3_1)) +
86  internal::pmadd(static_cast<Scalar>(-m(2,3)),d3_2, static_cast<Scalar>(m(3,3)*d3_3));
87  }
88 protected:
89  static EIGEN_DEVICE_FUNC
90  Scalar det2(const Derived& m, Index i0, Index i1)
91  {
92  return m(i0,0) * m(i1,1) - m(i1,0) * m(i0,1);
93  }
94 
95  static EIGEN_DEVICE_FUNC
96  Scalar det3(const Derived& m, Index i0, const Scalar& d0, Index i1, const Scalar& d1, Index i2, const Scalar& d2)
97  {
98  return internal::pmadd(m(i0,2), d0, internal::pmadd(static_cast<Scalar>(-m(i1,2)), d1, static_cast<Scalar>(m(i2,2)*d2)));
99  }
100 };
101 
102 } // end namespace internal
103 
108 template<typename Derived>
109 EIGEN_DEVICE_FUNC
110 inline typename internal::traits<Derived>::Scalar MatrixBase<Derived>::determinant() const
111 {
112  eigen_assert(rows() == cols());
113  typedef typename internal::nested_eval<Derived,Base::RowsAtCompileTime>::type Nested;
114  return internal::determinant_impl<internal::remove_all_t<Nested>>::run(derived());
115 }
116 
117 } // end namespace Eigen
118 
119 #endif // EIGEN_DETERMINANT_H
Scalar determinant() const
Definition: Determinant.h:110
Namespace containing all symbols from the Eigen library.
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
const int Dynamic
Definition: Constants.h:24