G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Adjugate.h
Go to the documentation of this file.
1 
14 namespace Eigen {
15 
16 namespace internal {
17 
18 // *** General case adjugate implementation
19 
20 template<typename MatrixType, typename ResultType, int Size = MatrixType::RowsAtCompileTime>
21 struct compute_adjugate
22 {
23  static inline void run(const MatrixType& matrix, ResultType& result)
24  {
25  eigen_assert( matrix.rows() == matrix.cols() && "Matrix is not square.");
26 
27  switch( matrix.rows() )
28  {
29  case 1:
30  compute_adjugate<MatrixType, ResultType, 1>::run(matrix,result);
31  break;
32  case 2:
33  compute_adjugate<MatrixType, ResultType, 2>::run(matrix,result);
34  break;
35  case 3:
36  compute_adjugate<MatrixType, ResultType, 3>::run(matrix,result);
37  break;
38  case 4:
39  compute_adjugate<MatrixType, ResultType, 4>::run(matrix,result);
40  break;
41  default:
42  eigen_assert( false && "Not implemented.");
43  break;
44  };
45  }
46 
47 };
48 
49 // *** Size 1 implementation
50 
51 template<typename MatrixType, typename ResultType>
52 struct compute_adjugate<MatrixType, ResultType, 1>
53 {
54  static inline void run(const MatrixType& , ResultType& result)
55  {
56  result.coeffRef(0,0) = typename ResultType::Scalar(1);
57  }
58 };
59 
60 // *** Size 2 implementation
61 
62 template<typename MatrixType, typename ResultType>
63 struct compute_adjugate<MatrixType, ResultType, 2>
64 {
65  static inline void run(const MatrixType& matrix, ResultType& result)
66  {
67  result.coeffRef(0,0) = matrix.coeff(1,1);
68  result.coeffRef(1,0) = -matrix.coeff(1,0);
69  result.coeffRef(0,1) = -matrix.coeff(0,1);
70  result.coeffRef(1,1) = matrix.coeff(0,0);
71  }
72 };
73 
74 // *** Size 3 implementation
75 
76 template<typename MatrixType, typename ResultType>
77 struct compute_adjugate<MatrixType, ResultType, 3>
78 {
79  static inline void run(const MatrixType& matrix, ResultType& result)
80  {
81  result.coeffRef(0,0) = cofactor_3x3<MatrixType,0,0>(matrix);
82  result.coeffRef(0,1) = cofactor_3x3<MatrixType,1,0>(matrix);
83  result.coeffRef(0,2) = cofactor_3x3<MatrixType,2,0>(matrix);
84  result.coeffRef(1,0) = cofactor_3x3<MatrixType,0,1>(matrix);
85  result.coeffRef(1,1) = cofactor_3x3<MatrixType,1,1>(matrix);
86  result.coeffRef(1,2) = cofactor_3x3<MatrixType,2,1>(matrix);
87  result.coeffRef(2,0) = cofactor_3x3<MatrixType,0,2>(matrix);
88  result.coeffRef(2,1) = cofactor_3x3<MatrixType,1,2>(matrix);
89  result.coeffRef(2,2) = cofactor_3x3<MatrixType,2,2>(matrix);
90  }
91 };
92 
93 // *** Size 4 implementation
94 
95 template<typename MatrixType, typename ResultType>
96 struct compute_adjugate<MatrixType, ResultType, 4>
97 {
98  static inline void run(const MatrixType& matrix, ResultType& result)
99  {
100  result.coeffRef(0,0) = cofactor_4x4<MatrixType,0,0>(matrix);
101  result.coeffRef(1,0) = -cofactor_4x4<MatrixType,0,1>(matrix);
102  result.coeffRef(2,0) = cofactor_4x4<MatrixType,0,2>(matrix);
103  result.coeffRef(3,0) = -cofactor_4x4<MatrixType,0,3>(matrix);
104  result.coeffRef(0,2) = cofactor_4x4<MatrixType,2,0>(matrix);
105  result.coeffRef(1,2) = -cofactor_4x4<MatrixType,2,1>(matrix);
106  result.coeffRef(2,2) = cofactor_4x4<MatrixType,2,2>(matrix);
107  result.coeffRef(3,2) = -cofactor_4x4<MatrixType,2,3>(matrix);
108  result.coeffRef(0,1) = -cofactor_4x4<MatrixType,1,0>(matrix);
109  result.coeffRef(1,1) = cofactor_4x4<MatrixType,1,1>(matrix);
110  result.coeffRef(2,1) = -cofactor_4x4<MatrixType,1,2>(matrix);
111  result.coeffRef(3,1) = cofactor_4x4<MatrixType,1,3>(matrix);
112  result.coeffRef(0,3) = -cofactor_4x4<MatrixType,3,0>(matrix);
113  result.coeffRef(1,3) = cofactor_4x4<MatrixType,3,1>(matrix);
114  result.coeffRef(2,3) = -cofactor_4x4<MatrixType,3,2>(matrix);
115  result.coeffRef(3,3) = cofactor_4x4<MatrixType,3,3>(matrix);
116  }
117 };
118 
119 // *** Adjugate expression
120 
121 template<typename MatrixType>
122 struct adjugate_impl : public ReturnByValue<adjugate_impl<MatrixType> >
123 {
124  typedef typename MatrixType::Index Index;
125  typedef typename internal::eval<MatrixType>::type MatrixTypeNested;
126  typedef typename remove_all<MatrixTypeNested>::type MatrixTypeNestedCleaned;
127  MatrixTypeNested m_matrix;
128 
129  adjugate_impl(const MatrixType& matrix)
130  : m_matrix(matrix)
131  {}
132 
133  inline Index rows() const { return m_matrix.rows(); }
134  inline Index cols() const { return m_matrix.cols(); }
135 
136  template<typename Dest> inline void evalTo(Dest& dst) const
137  {
138  static const int Size = EIGEN_PLAIN_ENUM_MIN(MatrixType::ColsAtCompileTime,Dest::ColsAtCompileTime);
139  EIGEN_ONLY_USED_FOR_DEBUG(Size);
140  eigen_assert(( (Size<=1) || (Size>4) || (extract_data(m_matrix)!=extract_data(dst)))
141  && "Aliasing problem detected in adjugate(), you need to do adjugate().eval() here.");
142 
143  compute_adjugate<MatrixTypeNestedCleaned, Dest>::run(m_matrix, dst);
144  }
145 };
146 
147 template<typename MatrixType>
148 struct traits<adjugate_impl<MatrixType> >
149 {
150  typedef typename MatrixType::PlainObject ReturnType;
151 };
152 
153 } // namespace internal
154 
155 
156 // *** Implementation of MatrixBase::adjugate()
157 
159 template<typename Derived>
160 inline const internal::adjugate_impl<Derived>
161 MatrixBase<Derived>::adjugate() const
162 {
163  eigen_assert(rows() == cols());
164  return internal::adjugate_impl<Derived>(derived());
165 }
166 
168 template<typename Derived>
169 inline void MatrixBase<Derived>::adjugateInPlace()
170 {
171  derived() = adjugate().eval();
172 }
173 
174 }