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 // 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 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal {
19 
20 template <typename Derived>
22  int b, int c) {
23  return matrix.coeff(0, a) * (matrix.coeff(1, b) * matrix.coeff(2, c) - matrix.coeff(1, c) * matrix.coeff(2, b));
24 }
25 
26 template <typename Derived, int DeterminantType = Derived::RowsAtCompileTime>
28  static inline typename traits<Derived>::Scalar run(const Derived& m) {
29  if (Derived::ColsAtCompileTime == Dynamic && m.rows() == 0) return typename traits<Derived>::Scalar(1);
30  return m.partialPivLu().determinant();
31  }
32 };
33 
34 template <typename Derived>
35 struct determinant_impl<Derived, 1> {
36  static inline EIGEN_DEVICE_FUNC typename traits<Derived>::Scalar run(const Derived& m) { return m.coeff(0, 0); }
37 };
38 
39 template <typename Derived>
40 struct determinant_impl<Derived, 2> {
41  static inline EIGEN_DEVICE_FUNC typename traits<Derived>::Scalar run(const Derived& m) {
42  return m.coeff(0, 0) * m.coeff(1, 1) - m.coeff(1, 0) * m.coeff(0, 1);
43  }
44 };
45 
46 template <typename Derived>
47 struct determinant_impl<Derived, 3> {
48  static inline EIGEN_DEVICE_FUNC typename traits<Derived>::Scalar run(const Derived& m) {
49  return bruteforce_det3_helper(m, 0, 1, 2) - bruteforce_det3_helper(m, 1, 0, 2) + bruteforce_det3_helper(m, 2, 0, 1);
50  }
51 };
52 
53 template <typename Derived>
54 struct determinant_impl<Derived, 4> {
55  typedef typename traits<Derived>::Scalar Scalar;
56  static EIGEN_DEVICE_FUNC Scalar run(const Derived& m) {
57  Scalar d2_01 = det2(m, 0, 1);
58  Scalar d2_02 = det2(m, 0, 2);
59  Scalar d2_03 = det2(m, 0, 3);
60  Scalar d2_12 = det2(m, 1, 2);
61  Scalar d2_13 = det2(m, 1, 3);
62  Scalar d2_23 = det2(m, 2, 3);
63  Scalar d3_0 = det3(m, 1, d2_23, 2, d2_13, 3, d2_12);
64  Scalar d3_1 = det3(m, 0, d2_23, 2, d2_03, 3, d2_02);
65  Scalar d3_2 = det3(m, 0, d2_13, 1, d2_03, 3, d2_01);
66  Scalar d3_3 = det3(m, 0, d2_12, 1, d2_02, 2, d2_01);
67  return internal::pmadd(static_cast<Scalar>(-m(0, 3)), d3_0, static_cast<Scalar>(m(1, 3) * d3_1)) +
68  internal::pmadd(static_cast<Scalar>(-m(2, 3)), d3_2, static_cast<Scalar>(m(3, 3) * d3_3));
69  }
70 
71  protected:
72  static EIGEN_DEVICE_FUNC Scalar det2(const Derived& m, Index i0, Index i1) {
73  return m(i0, 0) * m(i1, 1) - m(i1, 0) * m(i0, 1);
74  }
75 
76  static EIGEN_DEVICE_FUNC Scalar det3(const Derived& m, Index i0, const Scalar& d0, Index i1, const Scalar& d1,
77  Index i2, const Scalar& d2) {
78  return internal::pmadd(m(i0, 2), d0,
79  internal::pmadd(static_cast<Scalar>(-m(i1, 2)), d1, static_cast<Scalar>(m(i2, 2) * d2)));
80  }
81 };
82 
83 } // end namespace internal
84 
89 template <typename Derived>
91  eigen_assert(rows() == cols());
94 }
95 
96 } // end namespace Eigen
97 
98 #endif // EIGEN_DETERMINANT_H
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define eigen_assert(x)
Definition: Macros.h:910
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
Scalar * b
Definition: benchVecAdd.cpp:17
SCALAR Scalar
Definition: bench_gemm.cpp:45
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
EIGEN_DEVICE_FUNC Scalar determinant() const
Definition: Determinant.h:90
Eigen::Map< Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor >, 0, Eigen::OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Definition: common.h:85
const Scalar * a
Definition: level2_cplx_impl.h:32
int * m
Definition: level2_cplx_impl.h:294
EIGEN_STRONG_INLINE Packet4f pmadd(const Packet4f &a, const Packet4f &b, const Packet4f &c)
Definition: AltiVec/PacketMath.h:1218
EIGEN_DEVICE_FUNC const Derived::Scalar bruteforce_det3_helper(const MatrixBase< Derived > &matrix, int a, int b, int c)
Definition: Determinant.h:21
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
auto run(Kernel kernel, Args &&... args) -> decltype(kernel(args...))
Definition: gpu_test_helper.h:414
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
const int Dynamic
Definition: Constants.h:25
int c
Definition: calibrate.py:100
Definition: Eigen_Colamd.h:49
static EIGEN_DEVICE_FUNC traits< Derived >::Scalar run(const Derived &m)
Definition: Determinant.h:36
static EIGEN_DEVICE_FUNC traits< Derived >::Scalar run(const Derived &m)
Definition: Determinant.h:41
static EIGEN_DEVICE_FUNC traits< Derived >::Scalar run(const Derived &m)
Definition: Determinant.h:48
static EIGEN_DEVICE_FUNC Scalar det3(const Derived &m, Index i0, const Scalar &d0, Index i1, const Scalar &d1, Index i2, const Scalar &d2)
Definition: Determinant.h:76
static EIGEN_DEVICE_FUNC Scalar run(const Derived &m)
Definition: Determinant.h:56
traits< Derived >::Scalar Scalar
Definition: Determinant.h:55
static EIGEN_DEVICE_FUNC Scalar det2(const Derived &m, Index i0, Index i1)
Definition: Determinant.h:72
Definition: Determinant.h:27
static traits< Derived >::Scalar run(const Derived &m)
Definition: Determinant.h:28
std::conditional_t< Evaluate, PlainObject, typename ref_selector< T >::type > type
Definition: XprHelper.h:549
Definition: ForwardDeclarations.h:21