11#ifndef EIGEN_INVERSE_IMPL_H
12#define EIGEN_INVERSE_IMPL_H
22template<
typename MatrixType,
typename ResultType,
int Size = MatrixType::RowsAtCompileTime>
26 static inline void run(
const MatrixType& matrix, ResultType& result)
28 result = matrix.partialPivLu().inverse();
32template<
typename MatrixType,
typename ResultType,
int Size = MatrixType::RowsAtCompileTime>
33struct compute_inverse_and_det_with_check { };
39template<
typename MatrixType,
typename ResultType>
40struct compute_inverse<MatrixType, ResultType, 1>
43 static inline void run(
const MatrixType& matrix, ResultType& result)
45 typedef typename MatrixType::Scalar Scalar;
46 internal::evaluator<MatrixType> matrixEval(matrix);
47 result.coeffRef(0,0) = Scalar(1) / matrixEval.coeff(0,0);
51template<
typename MatrixType,
typename ResultType>
52struct compute_inverse_and_det_with_check<MatrixType, ResultType, 1>
55 static inline void run(
56 const MatrixType& matrix,
57 const typename MatrixType::RealScalar& absDeterminantThreshold,
59 typename ResultType::Scalar& determinant,
64 determinant = matrix.coeff(0,0);
65 invertible = abs(determinant) > absDeterminantThreshold;
66 if(invertible) result.coeffRef(0,0) =
typename ResultType::Scalar(1) / determinant;
74template<
typename MatrixType,
typename ResultType>
76inline void compute_inverse_size2_helper(
77 const MatrixType& matrix,
const typename ResultType::Scalar& invdet,
80 result.coeffRef(0,0) = matrix.coeff(1,1) * invdet;
81 result.coeffRef(1,0) = -matrix.coeff(1,0) * invdet;
82 result.coeffRef(0,1) = -matrix.coeff(0,1) * invdet;
83 result.coeffRef(1,1) = matrix.coeff(0,0) * invdet;
86template<
typename MatrixType,
typename ResultType>
87struct compute_inverse<MatrixType, ResultType, 2>
90 static inline void run(
const MatrixType& matrix, ResultType& result)
92 typedef typename ResultType::Scalar Scalar;
93 const Scalar invdet =
typename MatrixType::Scalar(1) / matrix.determinant();
94 compute_inverse_size2_helper(matrix, invdet, result);
98template<
typename MatrixType,
typename ResultType>
99struct compute_inverse_and_det_with_check<MatrixType, ResultType, 2>
102 static inline void run(
103 const MatrixType& matrix,
104 const typename MatrixType::RealScalar& absDeterminantThreshold,
106 typename ResultType::Scalar& determinant,
111 typedef typename ResultType::Scalar Scalar;
112 determinant = matrix.determinant();
113 invertible = abs(determinant) > absDeterminantThreshold;
114 if(!invertible)
return;
115 const Scalar invdet = Scalar(1) / determinant;
116 compute_inverse_size2_helper(matrix, invdet, inverse);
124template<
typename MatrixType,
int i,
int j>
126inline typename MatrixType::Scalar cofactor_3x3(
const MatrixType& m)
134 return m.coeff(i1, j1) * m.coeff(i2, j2)
135 - m.coeff(i1, j2) * m.coeff(i2, j1);
138template<
typename MatrixType,
typename ResultType>
140inline void compute_inverse_size3_helper(
141 const MatrixType& matrix,
142 const typename ResultType::Scalar& invdet,
143 const Matrix<typename ResultType::Scalar,3,1>& cofactors_col0,
146 result.row(0) = cofactors_col0 * invdet;
147 result.coeffRef(1,0) = cofactor_3x3<MatrixType,0,1>(matrix) * invdet;
148 result.coeffRef(1,1) = cofactor_3x3<MatrixType,1,1>(matrix) * invdet;
149 result.coeffRef(1,2) = cofactor_3x3<MatrixType,2,1>(matrix) * invdet;
150 result.coeffRef(2,0) = cofactor_3x3<MatrixType,0,2>(matrix) * invdet;
151 result.coeffRef(2,1) = cofactor_3x3<MatrixType,1,2>(matrix) * invdet;
152 result.coeffRef(2,2) = cofactor_3x3<MatrixType,2,2>(matrix) * invdet;
155template<
typename MatrixType,
typename ResultType>
156struct compute_inverse<MatrixType, ResultType, 3>
159 static inline void run(
const MatrixType& matrix, ResultType& result)
161 typedef typename ResultType::Scalar Scalar;
162 Matrix<typename MatrixType::Scalar,3,1> cofactors_col0;
163 cofactors_col0.coeffRef(0) = cofactor_3x3<MatrixType,0,0>(matrix);
164 cofactors_col0.coeffRef(1) = cofactor_3x3<MatrixType,1,0>(matrix);
165 cofactors_col0.coeffRef(2) = cofactor_3x3<MatrixType,2,0>(matrix);
166 const Scalar det = (cofactors_col0.cwiseProduct(matrix.col(0))).sum();
167 const Scalar invdet = Scalar(1) / det;
168 compute_inverse_size3_helper(matrix, invdet, cofactors_col0, result);
172template<
typename MatrixType,
typename ResultType>
173struct compute_inverse_and_det_with_check<MatrixType, ResultType, 3>
176 static inline void run(
177 const MatrixType& matrix,
178 const typename MatrixType::RealScalar& absDeterminantThreshold,
180 typename ResultType::Scalar& determinant,
185 typedef typename ResultType::Scalar Scalar;
186 Matrix<Scalar,3,1> cofactors_col0;
187 cofactors_col0.coeffRef(0) = cofactor_3x3<MatrixType,0,0>(matrix);
188 cofactors_col0.coeffRef(1) = cofactor_3x3<MatrixType,1,0>(matrix);
189 cofactors_col0.coeffRef(2) = cofactor_3x3<MatrixType,2,0>(matrix);
190 determinant = (cofactors_col0.cwiseProduct(matrix.col(0))).sum();
191 invertible = abs(determinant) > absDeterminantThreshold;
192 if(!invertible)
return;
193 const Scalar invdet = Scalar(1) / determinant;
194 compute_inverse_size3_helper(matrix, invdet, cofactors_col0, inverse);
202template<
typename Derived>
204inline const typename Derived::Scalar general_det3_helper
205(
const MatrixBase<Derived>& matrix,
int i1,
int i2,
int i3,
int j1,
int j2,
int j3)
207 return matrix.coeff(i1,j1)
208 * (matrix.coeff(i2,j2) * matrix.coeff(i3,j3) - matrix.coeff(i2,j3) * matrix.coeff(i3,j2));
211template<
typename MatrixType,
int i,
int j>
213inline typename MatrixType::Scalar cofactor_4x4(
const MatrixType& matrix)
223 return general_det3_helper(matrix, i1, i2, i3, j1, j2, j3)
224 + general_det3_helper(matrix, i2, i3, i1, j1, j2, j3)
225 + general_det3_helper(matrix, i3, i1, i2, j1, j2, j3);
228template<
int Arch,
typename Scalar,
typename MatrixType,
typename ResultType>
229struct compute_inverse_size4
232 static void run(
const MatrixType& matrix, ResultType& result)
234 result.coeffRef(0,0) = cofactor_4x4<MatrixType,0,0>(matrix);
235 result.coeffRef(1,0) = -cofactor_4x4<MatrixType,0,1>(matrix);
236 result.coeffRef(2,0) = cofactor_4x4<MatrixType,0,2>(matrix);
237 result.coeffRef(3,0) = -cofactor_4x4<MatrixType,0,3>(matrix);
238 result.coeffRef(0,2) = cofactor_4x4<MatrixType,2,0>(matrix);
239 result.coeffRef(1,2) = -cofactor_4x4<MatrixType,2,1>(matrix);
240 result.coeffRef(2,2) = cofactor_4x4<MatrixType,2,2>(matrix);
241 result.coeffRef(3,2) = -cofactor_4x4<MatrixType,2,3>(matrix);
242 result.coeffRef(0,1) = -cofactor_4x4<MatrixType,1,0>(matrix);
243 result.coeffRef(1,1) = cofactor_4x4<MatrixType,1,1>(matrix);
244 result.coeffRef(2,1) = -cofactor_4x4<MatrixType,1,2>(matrix);
245 result.coeffRef(3,1) = cofactor_4x4<MatrixType,1,3>(matrix);
246 result.coeffRef(0,3) = -cofactor_4x4<MatrixType,3,0>(matrix);
247 result.coeffRef(1,3) = cofactor_4x4<MatrixType,3,1>(matrix);
248 result.coeffRef(2,3) = -cofactor_4x4<MatrixType,3,2>(matrix);
249 result.coeffRef(3,3) = cofactor_4x4<MatrixType,3,3>(matrix);
250 result /= (matrix.col(0).cwiseProduct(result.row(0).transpose())).sum();
254template<
typename MatrixType,
typename ResultType>
255struct compute_inverse<MatrixType, ResultType, 4>
256 : compute_inverse_size4<Architecture::Target, typename MatrixType::Scalar,
257 MatrixType, ResultType>
261template<
typename MatrixType,
typename ResultType>
262struct compute_inverse_and_det_with_check<MatrixType, ResultType, 4>
265 static inline void run(
266 const MatrixType& matrix,
267 const typename MatrixType::RealScalar& absDeterminantThreshold,
269 typename ResultType::Scalar& determinant,
274 determinant = matrix.determinant();
275 invertible = abs(determinant) > absDeterminantThreshold;
276 if(invertible) compute_inverse<MatrixType, ResultType>::run(matrix, inverse);
289template<
typename DstXprType,
typename XprType>
290struct Assignment<DstXprType, Inverse<XprType>, internal::assign_op<typename DstXprType::Scalar,typename XprType::Scalar>, Dense2Dense>
292 typedef Inverse<XprType> SrcXprType;
293 static void run(DstXprType &dst,
const SrcXprType &src,
const internal::assign_op<typename DstXprType::Scalar,typename XprType::Scalar> &)
295 Index dstRows = src.rows();
296 Index dstCols = src.cols();
297 if((dst.rows()!=dstRows) || (dst.cols()!=dstCols))
298 dst.resize(dstRows, dstCols);
300 const int Size = EIGEN_PLAIN_ENUM_MIN(XprType::ColsAtCompileTime,DstXprType::ColsAtCompileTime);
301 EIGEN_ONLY_USED_FOR_DEBUG(Size);
302 eigen_assert(( (Size<=1) || (Size>4) || (extract_data(src.nestedExpression())!=extract_data(dst)))
303 &&
"Aliasing problem detected in inverse(), you need to do inverse().eval() here.");
305 typedef typename internal::nested_eval<XprType,XprType::ColsAtCompileTime>::type ActualXprType;
306 typedef typename internal::remove_all<ActualXprType>::type ActualXprTypeCleanded;
308 ActualXprType actual_xpr(src.nestedExpression());
310 compute_inverse<ActualXprTypeCleanded, DstXprType>::run(actual_xpr, dst);
334template<
typename Derived>
338 eigen_assert(rows() == cols());
339 return
Inverse<Derived>(derived());
360template<typename Derived>
361template<typename ResultType>
362inline
void MatrixBase<Derived>::computeInverseAndDetWithCheck(
364 typename ResultType::
Scalar& determinant,
366 const RealScalar& absDeterminantThreshold
370 eigen_assert(rows() == cols());
373 typedef typename internal::conditional<
374 RowsAtCompileTime == 2,
375 typename internal::remove_all<typename internal::nested_eval<Derived, 2>::type>::type,
378 internal::compute_inverse_and_det_with_check<MatrixType, ResultType>::run
379 (derived(), absDeterminantThreshold,
inverse, determinant, invertible);
399template<
typename Derived>
400template<
typename ResultType>
404 const RealScalar& absDeterminantThreshold
407 RealScalar determinant;
409 eigen_assert(rows() == cols());
410 computeInverseAndDetWithCheck(
inverse,determinant,invertible,absDeterminantThreshold);
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:66
Expression of the inverse of another expression.
Definition: Inverse.h:44
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:50
Namespace containing all symbols from the Eigen library.
Definition: Core:287
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_inverse_op< typename Derived::Scalar >, const Derived > inverse(const Eigen::ArrayBase< Derived > &x)
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:151