11#ifndef EIGEN_DETERMINANT_H
12#define EIGEN_DETERMINANT_H
15#include "./InternalHeaderCheck.h"
21template <
typename Derived>
22EIGEN_DEVICE_FUNC
inline const typename Derived::Scalar bruteforce_det3_helper(
const MatrixBase<Derived>& matrix,
int a,
24 return matrix.coeff(0, a) * (matrix.coeff(1, b) * matrix.coeff(2, c) - matrix.coeff(1, c) * matrix.coeff(2, b));
27template <
typename Derived,
int DeterminantType = Derived::RowsAtCompileTime>
28struct determinant_impl {
29 static inline typename traits<Derived>::Scalar run(
const Derived& m) {
return internal::partial_lu_determinant(m); }
32template <
typename Derived>
33struct determinant_impl<Derived, 1> {
34 static inline EIGEN_DEVICE_FUNC
typename traits<Derived>::Scalar run(
const Derived& m) {
return m.coeff(0, 0); }
37template <
typename Derived>
38struct determinant_impl<Derived, 2> {
39 static inline EIGEN_DEVICE_FUNC
typename traits<Derived>::Scalar run(
const Derived& m) {
40 return m.coeff(0, 0) * m.coeff(1, 1) - m.coeff(1, 0) * m.coeff(0, 1);
44template <
typename Derived>
45struct determinant_impl<Derived, 3> {
46 static inline EIGEN_DEVICE_FUNC
typename traits<Derived>::Scalar run(
const Derived& m) {
47 return bruteforce_det3_helper(m, 0, 1, 2) - bruteforce_det3_helper(m, 1, 0, 2) + bruteforce_det3_helper(m, 2, 0, 1);
51template <
typename Derived>
52struct determinant_impl<Derived, 4> {
53 using Scalar =
typename traits<Derived>::Scalar;
54 static EIGEN_DEVICE_FUNC Scalar run(
const Derived& m) {
55 Scalar d2_01 = det2(m, 0, 1);
56 Scalar d2_02 = det2(m, 0, 2);
57 Scalar d2_03 = det2(m, 0, 3);
58 Scalar d2_12 = det2(m, 1, 2);
59 Scalar d2_13 = det2(m, 1, 3);
60 Scalar d2_23 = det2(m, 2, 3);
61 Scalar d3_0 = det3(m, 1, d2_23, 2, d2_13, 3, d2_12);
62 Scalar d3_1 = det3(m, 0, d2_23, 2, d2_03, 3, d2_02);
63 Scalar d3_2 = det3(m, 0, d2_13, 1, d2_03, 3, d2_01);
64 Scalar d3_3 = det3(m, 0, d2_12, 1, d2_02, 2, d2_01);
65 return internal::pmadd(
static_cast<Scalar
>(-m(0, 3)), d3_0,
static_cast<Scalar
>(m(1, 3) * d3_1)) +
66 internal::pmadd(
static_cast<Scalar
>(-m(2, 3)), d3_2,
static_cast<Scalar
>(m(3, 3) * d3_3));
70 static EIGEN_DEVICE_FUNC Scalar det2(
const Derived& m, Index i0, Index i1) {
71 return m(i0, 0) * m(i1, 1) - m(i1, 0) * m(i0, 1);
74 static EIGEN_DEVICE_FUNC Scalar det3(
const Derived& m, Index i0,
const Scalar& d0, Index i1,
const Scalar& d1,
75 Index i2,
const Scalar& d2) {
76 return internal::pmadd(m(i0, 2), d0,
77 internal::pmadd(
static_cast<Scalar
>(-m(i1, 2)), d1,
static_cast<Scalar
>(m(i2, 2) * d2)));
87template <
typename Derived>
89 eigen_assert(rows() == cols());
90 using Nested =
typename internal::nested_eval<Derived, Base::RowsAtCompileTime>::type;
91 return internal::determinant_impl<internal::remove_all_t<Nested>>::run(derived());
Scalar determinant() const
Definition Determinant.h:88