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