blob: 3a41e6fcba3b4e47c429c774579d02202c68db88 [file] [log] [blame]
Brian Silverman72890c22015-09-19 14:37:37 -04001// 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
13namespace Eigen {
14
15namespace internal {
16
17template<typename Derived>
Austin Schuhc55b0172022-02-20 17:52:35 -080018EIGEN_DEVICE_FUNC
Brian Silverman72890c22015-09-19 14:37:37 -040019inline const typename Derived::Scalar bruteforce_det3_helper
20(const MatrixBase<Derived>& matrix, int a, int b, int c)
21{
22 return matrix.coeff(0,a)
23 * (matrix.coeff(1,b) * matrix.coeff(2,c) - matrix.coeff(1,c) * matrix.coeff(2,b));
24}
25
Brian Silverman72890c22015-09-19 14:37:37 -040026template<typename Derived,
27 int DeterminantType = Derived::RowsAtCompileTime
28> struct determinant_impl
29{
30 static inline typename traits<Derived>::Scalar run(const Derived& m)
31 {
32 if(Derived::ColsAtCompileTime==Dynamic && m.rows()==0)
33 return typename traits<Derived>::Scalar(1);
34 return m.partialPivLu().determinant();
35 }
36};
37
38template<typename Derived> struct determinant_impl<Derived, 1>
39{
Austin Schuhc55b0172022-02-20 17:52:35 -080040 static inline EIGEN_DEVICE_FUNC
41 typename traits<Derived>::Scalar run(const Derived& m)
Brian Silverman72890c22015-09-19 14:37:37 -040042 {
43 return m.coeff(0,0);
44 }
45};
46
47template<typename Derived> struct determinant_impl<Derived, 2>
48{
Austin Schuhc55b0172022-02-20 17:52:35 -080049 static inline EIGEN_DEVICE_FUNC
50 typename traits<Derived>::Scalar run(const Derived& m)
Brian Silverman72890c22015-09-19 14:37:37 -040051 {
52 return m.coeff(0,0) * m.coeff(1,1) - m.coeff(1,0) * m.coeff(0,1);
53 }
54};
55
56template<typename Derived> struct determinant_impl<Derived, 3>
57{
Austin Schuhc55b0172022-02-20 17:52:35 -080058 static inline EIGEN_DEVICE_FUNC
59 typename traits<Derived>::Scalar run(const Derived& m)
Brian Silverman72890c22015-09-19 14:37:37 -040060 {
61 return bruteforce_det3_helper(m,0,1,2)
62 - bruteforce_det3_helper(m,1,0,2)
63 + bruteforce_det3_helper(m,2,0,1);
64 }
65};
66
67template<typename Derived> struct determinant_impl<Derived, 4>
68{
Austin Schuhc55b0172022-02-20 17:52:35 -080069 typedef typename traits<Derived>::Scalar Scalar;
70 static EIGEN_DEVICE_FUNC
71 Scalar run(const Derived& m)
Brian Silverman72890c22015-09-19 14:37:37 -040072 {
Austin Schuhc55b0172022-02-20 17:52:35 -080073 Scalar d2_01 = det2(m, 0, 1);
74 Scalar d2_02 = det2(m, 0, 2);
75 Scalar d2_03 = det2(m, 0, 3);
76 Scalar d2_12 = det2(m, 1, 2);
77 Scalar d2_13 = det2(m, 1, 3);
78 Scalar d2_23 = det2(m, 2, 3);
79 Scalar d3_0 = det3(m, 1,d2_23, 2,d2_13, 3,d2_12);
80 Scalar d3_1 = det3(m, 0,d2_23, 2,d2_03, 3,d2_02);
81 Scalar d3_2 = det3(m, 0,d2_13, 1,d2_03, 3,d2_01);
82 Scalar d3_3 = det3(m, 0,d2_12, 1,d2_02, 2,d2_01);
83 return internal::pmadd(-m(0,3),d3_0, m(1,3)*d3_1) +
84 internal::pmadd(-m(2,3),d3_2, m(3,3)*d3_3);
85 }
86protected:
87 static EIGEN_DEVICE_FUNC
88 Scalar det2(const Derived& m, Index i0, Index i1)
89 {
90 return m(i0,0) * m(i1,1) - m(i1,0) * m(i0,1);
91 }
92
93 static EIGEN_DEVICE_FUNC
94 Scalar det3(const Derived& m, Index i0, const Scalar& d0, Index i1, const Scalar& d1, Index i2, const Scalar& d2)
95 {
96 return internal::pmadd(m(i0,2), d0, internal::pmadd(-m(i1,2), d1, m(i2,2)*d2));
Brian Silverman72890c22015-09-19 14:37:37 -040097 }
98};
99
100} // end namespace internal
101
102/** \lu_module
103 *
104 * \returns the determinant of this matrix
105 */
106template<typename Derived>
Austin Schuhc55b0172022-02-20 17:52:35 -0800107EIGEN_DEVICE_FUNC
Brian Silverman72890c22015-09-19 14:37:37 -0400108inline typename internal::traits<Derived>::Scalar MatrixBase<Derived>::determinant() const
109{
110 eigen_assert(rows() == cols());
Austin Schuh189376f2018-12-20 22:11:15 +1100111 typedef typename internal::nested_eval<Derived,Base::RowsAtCompileTime>::type Nested;
Brian Silverman72890c22015-09-19 14:37:37 -0400112 return internal::determinant_impl<typename internal::remove_all<Nested>::type>::run(derived());
113}
114
115} // end namespace Eigen
116
117#endif // EIGEN_DETERMINANT_H