10 #ifndef EIGEN_INVERSE_H
11 #define EIGEN_INVERSE_H
21 template<
typename MatrixType,
typename ResultType,
int Size = MatrixType::RowsAtCompileTime>
22 struct compute_inverse
24 static inline void run(
const MatrixType& matrix, ResultType& result)
26 result = matrix.partialPivLu().inverse();
30 template<
typename MatrixType,
typename ResultType,
int Size = MatrixType::RowsAtCompileTime>
31 struct compute_inverse_and_det_with_check { };
37 template<
typename MatrixType,
typename ResultType>
38 struct compute_inverse<MatrixType, ResultType, 1>
40 static inline void run(
const MatrixType& matrix, ResultType& result)
42 typedef typename MatrixType::Scalar Scalar;
43 result.coeffRef(0,0) = Scalar(1) / matrix.coeff(0,0);
47 template<
typename MatrixType,
typename ResultType>
48 struct compute_inverse_and_det_with_check<MatrixType, ResultType, 1>
50 static inline void run(
51 const MatrixType& matrix,
52 const typename MatrixType::RealScalar& absDeterminantThreshold,
54 typename ResultType::Scalar& determinant,
59 determinant = matrix.coeff(0,0);
60 invertible = abs(determinant) > absDeterminantThreshold;
61 if(invertible) result.coeffRef(0,0) =
typename ResultType::Scalar(1) / determinant;
69 template<
typename MatrixType,
typename ResultType>
70 inline void compute_inverse_size2_helper(
71 const MatrixType& matrix,
const typename ResultType::Scalar& invdet,
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;
80 template<
typename MatrixType,
typename ResultType>
81 struct compute_inverse<MatrixType, ResultType, 2>
83 static inline void run(
const MatrixType& matrix, ResultType& result)
85 typedef typename ResultType::Scalar Scalar;
86 const Scalar invdet =
typename MatrixType::Scalar(1) / matrix.determinant();
87 compute_inverse_size2_helper(matrix, invdet, result);
91 template<
typename MatrixType,
typename ResultType>
92 struct compute_inverse_and_det_with_check<MatrixType, ResultType, 2>
94 static inline void run(
95 const MatrixType& matrix,
96 const typename MatrixType::RealScalar& absDeterminantThreshold,
98 typename ResultType::Scalar& determinant,
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);
116 template<
typename MatrixType,
int i,
int j>
117 inline typename MatrixType::Scalar cofactor_3x3(
const MatrixType& m)
125 return m.coeff(i1, j1) * m.coeff(i2, j2)
126 - m.coeff(i1, j2) * m.coeff(i2, j1);
129 template<
typename MatrixType,
typename ResultType>
130 inline void compute_inverse_size3_helper(
131 const MatrixType& matrix,
132 const typename ResultType::Scalar& invdet,
133 const Matrix<typename ResultType::Scalar,3,1>& cofactors_col0,
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;
145 template<
typename MatrixType,
typename ResultType>
146 struct compute_inverse<MatrixType, ResultType, 3>
148 static inline void run(
const MatrixType& matrix, ResultType& result)
150 typedef typename ResultType::Scalar Scalar;
151 Matrix<typename MatrixType::Scalar,3,1> cofactors_col0;
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);
161 template<
typename MatrixType,
typename ResultType>
162 struct compute_inverse_and_det_with_check<MatrixType, ResultType, 3>
164 static inline void run(
165 const MatrixType& matrix,
166 const typename MatrixType::RealScalar& absDeterminantThreshold,
168 typename ResultType::Scalar& determinant,
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);
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)
194 return matrix.coeff(i1,j1)
195 * (matrix.coeff(i2,j2) * matrix.coeff(i3,j3) - matrix.coeff(i2,j3) * matrix.coeff(i3,j2));
198 template<
typename MatrixType,
int i,
int j>
199 inline typename MatrixType::Scalar cofactor_4x4(
const MatrixType& matrix)
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);
214 template<
int Arch,
typename Scalar,
typename MatrixType,
typename ResultType>
215 struct compute_inverse_size4
217 static void run(
const MatrixType& matrix, ResultType& result)
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();
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>
246 template<
typename MatrixType,
typename ResultType>
247 struct compute_inverse_and_det_with_check<MatrixType, ResultType, 4>
249 static inline void run(
250 const MatrixType& matrix,
251 const typename MatrixType::RealScalar& absDeterminantThreshold,
253 typename ResultType::Scalar& determinant,
258 determinant = matrix.determinant();
259 invertible = abs(determinant) > absDeterminantThreshold;
260 if(invertible) compute_inverse<MatrixType, ResultType>::run(matrix, inverse);
268 template<
typename MatrixType>
269 struct traits<inverse_impl<MatrixType> >
271 typedef typename MatrixType::PlainObject ReturnType;
274 template<
typename MatrixType>
275 struct inverse_impl :
public ReturnByValue<inverse_impl<MatrixType> >
277 typedef typename MatrixType::Index Index;
278 typedef typename internal::eval<MatrixType>::type MatrixTypeNested;
279 typedef typename remove_all<MatrixTypeNested>::type MatrixTypeNestedCleaned;
280 MatrixTypeNested m_matrix;
282 inverse_impl(
const MatrixType& matrix)
286 inline Index rows()
const {
return m_matrix.rows(); }
287 inline Index cols()
const {
return m_matrix.cols(); }
289 template<
typename Dest>
inline void evalTo(Dest& dst)
const
291 const int Size = EIGEN_PLAIN_ENUM_MIN(MatrixType::ColsAtCompileTime,Dest::ColsAtCompileTime);
292 EIGEN_ONLY_USED_FOR_DEBUG(Size);
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.");
296 compute_inverse<MatrixTypeNestedCleaned, Dest>::run(m_matrix, dst);
319 template<
typename Derived>
323 eigen_assert(rows() == cols());
324 return internal::inverse_impl<Derived>(derived());
345 template<
typename Derived>
346 template<
typename ResultType>
349 typename ResultType::Scalar& determinant,
351 const RealScalar& absDeterminantThreshold
355 eigen_assert(rows() == cols());
358 typedef typename internal::conditional<
359 RowsAtCompileTime == 2,
360 typename internal::remove_all<typename internal::nested<Derived, 2>::type>::type,
363 internal::compute_inverse_and_det_with_check<MatrixType, ResultType>::run
364 (derived(), absDeterminantThreshold, inverse, determinant, invertible);
384 template<
typename Derived>
385 template<
typename ResultType>
389 const RealScalar& absDeterminantThreshold
392 RealScalar determinant;
394 eigen_assert(rows() == cols());
395 computeInverseAndDetWithCheck(inverse,determinant,invertible,absDeterminantThreshold);
400 #endif // EIGEN_INVERSE_H
void computeInverseAndDetWithCheck(ResultType &inverse, typename ResultType::Scalar &determinant, bool &invertible, const RealScalar &absDeterminantThreshold=NumTraits< Scalar >::dummy_precision()) const
Definition: Inverse.h:347
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:88
void computeInverseWithCheck(ResultType &inverse, bool &invertible, const RealScalar &absDeterminantThreshold=NumTraits< Scalar >::dummy_precision()) const
Definition: Inverse.h:386
const internal::inverse_impl< Derived > inverse() const
Definition: Inverse.h:320
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:127