Inverse.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-2010 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_INVERSE_H
11 #define EIGEN_INVERSE_H
12 
13 namespace Eigen {
14 
15 namespace internal {
16 
17 /**********************************
18 *** General case implementation ***
19 **********************************/
20 
21 template<typename MatrixType, typename ResultType, int Size = MatrixType::RowsAtCompileTime>
23 {
24  static inline void run(const MatrixType& matrix, ResultType& result)
25  {
26  result = matrix.partialPivLu().inverse();
27  }
28 };
29 
30 template<typename MatrixType, typename ResultType, int Size = MatrixType::RowsAtCompileTime>
31 struct compute_inverse_and_det_with_check { /* nothing! general case not supported. */ };
32 
33 /****************************
34 *** Size 1 implementation ***
35 ****************************/
36 
37 template<typename MatrixType, typename ResultType>
38 struct compute_inverse<MatrixType, ResultType, 1>
39 {
40  static inline void run(const MatrixType& matrix, ResultType& result)
41  {
42  typedef typename MatrixType::Scalar Scalar;
43  result.coeffRef(0,0) = Scalar(1) / matrix.coeff(0,0);
44  }
45 };
46 
47 template<typename MatrixType, typename ResultType>
48 struct compute_inverse_and_det_with_check<MatrixType, ResultType, 1>
49 {
50  static inline void run(
51  const MatrixType& matrix,
52  const typename MatrixType::RealScalar& absDeterminantThreshold,
53  ResultType& result,
54  typename ResultType::Scalar& determinant,
55  bool& invertible
56  )
57  {
58  using std::abs;
59  determinant = matrix.coeff(0,0);
60  invertible = abs(determinant) > absDeterminantThreshold;
61  if(invertible) result.coeffRef(0,0) = typename ResultType::Scalar(1) / determinant;
62  }
63 };
64 
65 /****************************
66 *** Size 2 implementation ***
67 ****************************/
68 
69 template<typename MatrixType, typename ResultType>
71  const MatrixType& matrix, const typename ResultType::Scalar& invdet,
72  ResultType& result)
73 {
74  result.coeffRef(0,0) = matrix.coeff(1,1) * invdet;
75  result.coeffRef(1,0) = -matrix.coeff(1,0) * invdet;
76  result.coeffRef(0,1) = -matrix.coeff(0,1) * invdet;
77  result.coeffRef(1,1) = matrix.coeff(0,0) * invdet;
78 }
79 
80 template<typename MatrixType, typename ResultType>
81 struct compute_inverse<MatrixType, ResultType, 2>
82 {
83  static inline void run(const MatrixType& matrix, ResultType& result)
84  {
85  typedef typename ResultType::Scalar Scalar;
86  const Scalar invdet = typename MatrixType::Scalar(1) / matrix.determinant();
87  compute_inverse_size2_helper(matrix, invdet, result);
88  }
89 };
90 
91 template<typename MatrixType, typename ResultType>
92 struct compute_inverse_and_det_with_check<MatrixType, ResultType, 2>
93 {
94  static inline void run(
95  const MatrixType& matrix,
96  const typename MatrixType::RealScalar& absDeterminantThreshold,
97  ResultType& inverse,
98  typename ResultType::Scalar& determinant,
99  bool& invertible
100  )
101  {
102  using std::abs;
103  typedef typename ResultType::Scalar Scalar;
104  determinant = matrix.determinant();
105  invertible = abs(determinant) > absDeterminantThreshold;
106  if(!invertible) return;
107  const Scalar invdet = Scalar(1) / determinant;
108  compute_inverse_size2_helper(matrix, invdet, inverse);
109  }
110 };
111 
112 /****************************
113 *** Size 3 implementation ***
114 ****************************/
115 
116 template<typename MatrixType, int i, int j>
117 inline typename MatrixType::Scalar cofactor_3x3(const MatrixType& m)
118 {
119  enum {
120  i1 = (i+1) % 3,
121  i2 = (i+2) % 3,
122  j1 = (j+1) % 3,
123  j2 = (j+2) % 3
124  };
125  return m.coeff(i1, j1) * m.coeff(i2, j2)
126  - m.coeff(i1, j2) * m.coeff(i2, j1);
127 }
128 
129 template<typename MatrixType, typename ResultType>
131  const MatrixType& matrix,
132  const typename ResultType::Scalar& invdet,
133  const Matrix<typename ResultType::Scalar,3,1>& cofactors_col0,
134  ResultType& result)
135 {
136  result.row(0) = cofactors_col0 * invdet;
137  result.coeffRef(1,0) = cofactor_3x3<MatrixType,0,1>(matrix) * invdet;
138  result.coeffRef(1,1) = cofactor_3x3<MatrixType,1,1>(matrix) * invdet;
139  result.coeffRef(1,2) = cofactor_3x3<MatrixType,2,1>(matrix) * invdet;
140  result.coeffRef(2,0) = cofactor_3x3<MatrixType,0,2>(matrix) * invdet;
141  result.coeffRef(2,1) = cofactor_3x3<MatrixType,1,2>(matrix) * invdet;
142  result.coeffRef(2,2) = cofactor_3x3<MatrixType,2,2>(matrix) * invdet;
143 }
144 
145 template<typename MatrixType, typename ResultType>
146 struct compute_inverse<MatrixType, ResultType, 3>
147 {
148  static inline void run(const MatrixType& matrix, ResultType& result)
149  {
150  typedef typename ResultType::Scalar Scalar;
152  cofactors_col0.coeffRef(0) = cofactor_3x3<MatrixType,0,0>(matrix);
153  cofactors_col0.coeffRef(1) = cofactor_3x3<MatrixType,1,0>(matrix);
154  cofactors_col0.coeffRef(2) = cofactor_3x3<MatrixType,2,0>(matrix);
155  const Scalar det = (cofactors_col0.cwiseProduct(matrix.col(0))).sum();
156  const Scalar invdet = Scalar(1) / det;
157  compute_inverse_size3_helper(matrix, invdet, cofactors_col0, result);
158  }
159 };
160 
161 template<typename MatrixType, typename ResultType>
162 struct compute_inverse_and_det_with_check<MatrixType, ResultType, 3>
163 {
164  static inline void run(
165  const MatrixType& matrix,
166  const typename MatrixType::RealScalar& absDeterminantThreshold,
167  ResultType& inverse,
168  typename ResultType::Scalar& determinant,
169  bool& invertible
170  )
171  {
172  using std::abs;
173  typedef typename ResultType::Scalar Scalar;
174  Matrix<Scalar,3,1> cofactors_col0;
175  cofactors_col0.coeffRef(0) = cofactor_3x3<MatrixType,0,0>(matrix);
176  cofactors_col0.coeffRef(1) = cofactor_3x3<MatrixType,1,0>(matrix);
177  cofactors_col0.coeffRef(2) = cofactor_3x3<MatrixType,2,0>(matrix);
178  determinant = (cofactors_col0.cwiseProduct(matrix.col(0))).sum();
179  invertible = abs(determinant) > absDeterminantThreshold;
180  if(!invertible) return;
181  const Scalar invdet = Scalar(1) / determinant;
182  compute_inverse_size3_helper(matrix, invdet, cofactors_col0, inverse);
183  }
184 };
185 
186 /****************************
187 *** Size 4 implementation ***
188 ****************************/
189 
190 template<typename Derived>
191 inline const typename Derived::Scalar general_det3_helper
192 (const MatrixBase<Derived>& matrix, int i1, int i2, int i3, int j1, int j2, int j3)
193 {
194  return matrix.coeff(i1,j1)
195  * (matrix.coeff(i2,j2) * matrix.coeff(i3,j3) - matrix.coeff(i2,j3) * matrix.coeff(i3,j2));
196 }
197 
198 template<typename MatrixType, int i, int j>
199 inline typename MatrixType::Scalar cofactor_4x4(const MatrixType& matrix)
200 {
201  enum {
202  i1 = (i+1) % 4,
203  i2 = (i+2) % 4,
204  i3 = (i+3) % 4,
205  j1 = (j+1) % 4,
206  j2 = (j+2) % 4,
207  j3 = (j+3) % 4
208  };
209  return general_det3_helper(matrix, i1, i2, i3, j1, j2, j3)
210  + general_det3_helper(matrix, i2, i3, i1, j1, j2, j3)
211  + general_det3_helper(matrix, i3, i1, i2, j1, j2, j3);
212 }
213 
214 template<int Arch, typename Scalar, typename MatrixType, typename ResultType>
216 {
217  static void run(const MatrixType& matrix, ResultType& result)
218  {
219  result.coeffRef(0,0) = cofactor_4x4<MatrixType,0,0>(matrix);
220  result.coeffRef(1,0) = -cofactor_4x4<MatrixType,0,1>(matrix);
221  result.coeffRef(2,0) = cofactor_4x4<MatrixType,0,2>(matrix);
222  result.coeffRef(3,0) = -cofactor_4x4<MatrixType,0,3>(matrix);
223  result.coeffRef(0,2) = cofactor_4x4<MatrixType,2,0>(matrix);
224  result.coeffRef(1,2) = -cofactor_4x4<MatrixType,2,1>(matrix);
225  result.coeffRef(2,2) = cofactor_4x4<MatrixType,2,2>(matrix);
226  result.coeffRef(3,2) = -cofactor_4x4<MatrixType,2,3>(matrix);
227  result.coeffRef(0,1) = -cofactor_4x4<MatrixType,1,0>(matrix);
228  result.coeffRef(1,1) = cofactor_4x4<MatrixType,1,1>(matrix);
229  result.coeffRef(2,1) = -cofactor_4x4<MatrixType,1,2>(matrix);
230  result.coeffRef(3,1) = cofactor_4x4<MatrixType,1,3>(matrix);
231  result.coeffRef(0,3) = -cofactor_4x4<MatrixType,3,0>(matrix);
232  result.coeffRef(1,3) = cofactor_4x4<MatrixType,3,1>(matrix);
233  result.coeffRef(2,3) = -cofactor_4x4<MatrixType,3,2>(matrix);
234  result.coeffRef(3,3) = cofactor_4x4<MatrixType,3,3>(matrix);
235  result /= (matrix.col(0).cwiseProduct(result.row(0).transpose())).sum();
236  }
237 };
238 
239 template<typename MatrixType, typename ResultType>
240 struct compute_inverse<MatrixType, ResultType, 4>
241  : compute_inverse_size4<Architecture::Target, typename MatrixType::Scalar,
242  MatrixType, ResultType>
243 {
244 };
245 
246 template<typename MatrixType, typename ResultType>
247 struct compute_inverse_and_det_with_check<MatrixType, ResultType, 4>
248 {
249  static inline void run(
250  const MatrixType& matrix,
251  const typename MatrixType::RealScalar& absDeterminantThreshold,
252  ResultType& inverse,
253  typename ResultType::Scalar& determinant,
254  bool& invertible
255  )
256  {
257  using std::abs;
258  determinant = matrix.determinant();
259  invertible = abs(determinant) > absDeterminantThreshold;
260  if(invertible) compute_inverse<MatrixType, ResultType>::run(matrix, inverse);
261  }
262 };
263 
264 /*************************
265 *** MatrixBase methods ***
266 *************************/
267 
268 template<typename MatrixType>
269 struct traits<inverse_impl<MatrixType> >
270 {
271  typedef typename MatrixType::PlainObject ReturnType;
272 };
273 
274 template<typename MatrixType>
275 struct inverse_impl : public ReturnByValue<inverse_impl<MatrixType> >
276 {
277  typedef typename MatrixType::Index Index;
280  MatrixTypeNested m_matrix;
281 
282  inverse_impl(const MatrixType& matrix)
283  : m_matrix(matrix)
284  {}
285 
286  inline Index rows() const { return m_matrix.rows(); }
287  inline Index cols() const { return m_matrix.cols(); }
288 
289  template<typename Dest> inline void evalTo(Dest& dst) const
290  {
291  const int Size = EIGEN_PLAIN_ENUM_MIN(MatrixType::ColsAtCompileTime,Dest::ColsAtCompileTime);
293  eigen_assert(( (Size<=1) || (Size>4) || (extract_data(m_matrix)!=extract_data(dst)))
294  && "Aliasing problem detected in inverse(), you need to do inverse().eval() here.");
295 
297  }
298 };
299 
300 } // end namespace internal
301 
319 template<typename Derived>
321 {
322  EIGEN_STATIC_ASSERT(!NumTraits<Scalar>::IsInteger,THIS_FUNCTION_IS_NOT_FOR_INTEGER_NUMERIC_TYPES)
323  eigen_assert(rows() == cols());
324  return internal::inverse_impl<Derived>(derived());
325 }
326 
345 template<typename Derived>
346 template<typename ResultType>
348  ResultType& inverse,
349  typename ResultType::Scalar& determinant,
350  bool& invertible,
351  const RealScalar& absDeterminantThreshold
352  ) const
353 {
354  // i'd love to put some static assertions there, but SFINAE means that they have no effect...
355  eigen_assert(rows() == cols());
356  // for 2x2, it's worth giving a chance to avoid evaluating.
357  // for larger sizes, evaluating has negligible cost and limits code size.
358  typedef typename internal::conditional<
359  RowsAtCompileTime == 2,
362  >::type MatrixType;
364  (derived(), absDeterminantThreshold, inverse, determinant, invertible);
365 }
366 
384 template<typename Derived>
385 template<typename ResultType>
387  ResultType& inverse,
388  bool& invertible,
389  const RealScalar& absDeterminantThreshold
390  ) const
391 {
392  RealScalar determinant;
393  // i'd love to put some static assertions there, but SFINAE means that they have no effect...
394  eigen_assert(rows() == cols());
395  computeInverseAndDetWithCheck(inverse,determinant,invertible,absDeterminantThreshold);
396 }
397 
398 } // end namespace Eigen
399 
400 #endif // EIGEN_INVERSE_H
MatrixType::Scalar cofactor_4x4(const MatrixType &matrix)
Definition: Inverse.h:199
void compute_inverse_size3_helper(const MatrixType &matrix, const typename ResultType::Scalar &invdet, const Matrix< typename ResultType::Scalar, 3, 1 > &cofactors_col0, ResultType &result)
Definition: Inverse.h:130
#define EIGEN_ONLY_USED_FOR_DEBUG(x)
void evalTo(Dest &dst) const
Definition: Inverse.h:289
inverse_impl(const MatrixType &matrix)
Definition: Inverse.h:282
void compute_inverse_size2_helper(const MatrixType &matrix, const typename ResultType::Scalar &invdet, ResultType &result)
Definition: Inverse.h:70
static void run(const MatrixType &matrix, const typename MatrixType::RealScalar &absDeterminantThreshold, ResultType &result, typename ResultType::Scalar &determinant, bool &invertible)
Definition: Inverse.h:50
static void run(const MatrixType &matrix, ResultType &result)
Definition: Inverse.h:40
internal::eval< MatrixType >::type MatrixTypeNested
Definition: Inverse.h:278
Definition: LDLT.h:16
static void run(const MatrixType &matrix, const typename MatrixType::RealScalar &absDeterminantThreshold, ResultType &inverse, typename ResultType::Scalar &determinant, bool &invertible)
Definition: Inverse.h:94
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:88
MatrixTypeNested m_matrix
Definition: Inverse.h:280
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
Definition: StaticAssert.h:111
#define EIGEN_PLAIN_ENUM_MIN(a, b)
remove_all< MatrixTypeNested >::type MatrixTypeNestedCleaned
Definition: Inverse.h:279
static void run(const MatrixType &matrix, ResultType &result)
Definition: Inverse.h:24
EIGEN_STRONG_INLINE const CwiseUnaryOp< internal::scalar_abs_op< Scalar >, const Derived > abs() const
static void run(const MatrixType &matrix, const typename MatrixType::RealScalar &absDeterminantThreshold, ResultType &inverse, typename ResultType::Scalar &determinant, bool &invertible)
Definition: Inverse.h:164
const CwiseUnaryOp< internal::scalar_inverse_op< Scalar >, const Derived > inverse() const
void computeInverseAndDetWithCheck(ResultType &inverse, typename ResultType::Scalar &determinant, bool &invertible, const RealScalar &absDeterminantThreshold=NumTraits< Scalar >::dummy_precision()) const
Definition: Inverse.h:347
NumTraits< Scalar >::Real RealScalar
Definition: DenseBase.h:65
void computeInverseWithCheck(ResultType &inverse, bool &invertible, const RealScalar &absDeterminantThreshold=NumTraits< Scalar >::dummy_precision()) const
Definition: Inverse.h:386
static void run(const MatrixType &matrix, const typename MatrixType::RealScalar &absDeterminantThreshold, ResultType &inverse, typename ResultType::Scalar &determinant, bool &invertible)
Definition: Inverse.h:249
static void run(const MatrixType &matrix, ResultType &result)
Definition: Inverse.h:217
static void run(const MatrixType &matrix, ResultType &result)
Definition: Inverse.h:83
const Derived::Scalar general_det3_helper(const MatrixBase< Derived > &matrix, int i1, int i2, int i3, int j1, int j2, int j3)
Definition: Inverse.h:192
MatrixType::Index Index
Definition: Inverse.h:277
const T::Scalar * extract_data(const T &m)
Definition: BlasUtil.h:255
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:127
#define eigen_assert(x)
const internal::inverse_impl< Derived > inverse() const
Definition: Inverse.h:320
MatrixType::Scalar cofactor_3x3(const MatrixType &m)
Definition: Inverse.h:117
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
static void run(const MatrixType &matrix, ResultType &result)
Definition: Inverse.h:148


tuw_aruco
Author(s): Lukas Pfeifhofer
autogenerated on Mon Jun 10 2019 15:40:51