11 #ifndef EIGEN_INVERSE_IMPL_H
12 #define EIGEN_INVERSE_IMPL_H
24 template<
typename MatrixType,
typename ResultType,
int Size = MatrixType::RowsAtCompileTime>
25 struct compute_inverse
28 static inline void run(
const MatrixType& matrix, ResultType& result)
30 result = matrix.partialPivLu().inverse();
34 template<
typename MatrixType,
typename ResultType,
int Size = MatrixType::RowsAtCompileTime>
35 struct compute_inverse_and_det_with_check { };
41 template<
typename MatrixType,
typename ResultType>
42 struct compute_inverse<
MatrixType, ResultType, 1>
45 static inline void run(
const MatrixType& matrix, ResultType& result)
48 internal::evaluator<MatrixType> matrixEval(matrix);
49 result.coeffRef(0,0) = Scalar(1) / matrixEval.coeff(0,0);
53 template<
typename MatrixType,
typename ResultType>
54 struct compute_inverse_and_det_with_check<
MatrixType, ResultType, 1>
57 static inline void run(
61 typename ResultType::Scalar& determinant,
66 determinant = matrix.coeff(0,0);
67 invertible =
abs(determinant) > absDeterminantThreshold;
68 if(invertible) result.coeffRef(0,0) =
typename ResultType::Scalar(1) / determinant;
76 template<
typename MatrixType,
typename ResultType>
79 const MatrixType& matrix,
const typename ResultType::Scalar& invdet,
82 typename ResultType::Scalar temp = matrix.coeff(0,0);
83 result.coeffRef(0,0) = matrix.coeff(1,1) * invdet;
84 result.coeffRef(1,0) = -matrix.coeff(1,0) * invdet;
85 result.coeffRef(0,1) = -matrix.coeff(0,1) * invdet;
86 result.coeffRef(1,1) = temp * invdet;
89 template<
typename MatrixType,
typename ResultType>
90 struct compute_inverse<
MatrixType, ResultType, 2>
93 static inline void run(
const MatrixType& matrix, ResultType& result)
95 typedef typename ResultType::Scalar Scalar;
101 template<
typename MatrixType,
typename ResultType>
102 struct compute_inverse_and_det_with_check<
MatrixType, ResultType, 2>
105 static inline void run(
109 typename ResultType::Scalar& determinant,
114 typedef typename ResultType::Scalar Scalar;
115 determinant = matrix.determinant();
116 invertible =
abs(determinant) > absDeterminantThreshold;
117 if(!invertible)
return;
118 const Scalar invdet = Scalar(1) / determinant;
127 template<
typename MatrixType,
int i,
int j>
137 return m.coeff(i1, j1) *
m.coeff(i2, j2)
138 -
m.coeff(i1, j2) *
m.coeff(i2, j1);
141 template<
typename MatrixType,
typename ResultType>
145 const typename ResultType::Scalar& invdet,
150 typedef typename ResultType::Scalar Scalar;
151 const Scalar c01 = cofactor_3x3<MatrixType,0,1>(matrix) * invdet;
152 const Scalar c11 = cofactor_3x3<MatrixType,1,1>(matrix) * invdet;
153 const Scalar c02 = cofactor_3x3<MatrixType,0,2>(matrix) * invdet;
154 result.coeffRef(1,2) = cofactor_3x3<MatrixType,2,1>(matrix) * invdet;
155 result.coeffRef(2,1) = cofactor_3x3<MatrixType,1,2>(matrix) * invdet;
156 result.coeffRef(2,2) = cofactor_3x3<MatrixType,2,2>(matrix) * invdet;
157 result.coeffRef(1,0) = c01;
158 result.coeffRef(1,1) = c11;
159 result.coeffRef(2,0) = c02;
160 result.row(0) = cofactors_col0 * invdet;
163 template<
typename MatrixType,
typename ResultType>
164 struct compute_inverse<
MatrixType, ResultType, 3>
167 static inline void run(
const MatrixType& matrix, ResultType& result)
169 typedef typename ResultType::Scalar Scalar;
171 cofactors_col0.
coeffRef(0) = cofactor_3x3<MatrixType,0,0>(matrix);
172 cofactors_col0.
coeffRef(1) = cofactor_3x3<MatrixType,1,0>(matrix);
173 cofactors_col0.
coeffRef(2) = cofactor_3x3<MatrixType,2,0>(matrix);
174 const Scalar det = (cofactors_col0.
cwiseProduct(matrix.col(0))).sum();
175 const Scalar invdet = Scalar(1) / det;
180 template<
typename MatrixType,
typename ResultType>
181 struct compute_inverse_and_det_with_check<
MatrixType, ResultType, 3>
184 static inline void run(
188 typename ResultType::Scalar& determinant,
192 typedef typename ResultType::Scalar Scalar;
193 Matrix<Scalar,3,1> cofactors_col0;
194 cofactors_col0.
coeffRef(0) = cofactor_3x3<MatrixType,0,0>(matrix);
195 cofactors_col0.coeffRef(1) = cofactor_3x3<MatrixType,1,0>(matrix);
196 cofactors_col0.coeffRef(2) = cofactor_3x3<MatrixType,2,0>(matrix);
197 determinant = (cofactors_col0.cwiseProduct(matrix.col(0))).sum();
199 if(!invertible)
return;
200 const Scalar invdet = Scalar(1) / determinant;
209 template<
typename Derived>
214 return matrix.
coeff(i1,j1)
218 template<
typename MatrixType,
int i,
int j>
235 template<
int Arch,
typename Scalar,
typename MatrixType,
typename ResultType>
236 struct compute_inverse_size4
239 static void run(
const MatrixType& matrix, ResultType& result)
241 result.coeffRef(0,0) = cofactor_4x4<MatrixType,0,0>(matrix);
242 result.coeffRef(1,0) = -cofactor_4x4<MatrixType,0,1>(matrix);
243 result.coeffRef(2,0) = cofactor_4x4<MatrixType,0,2>(matrix);
244 result.coeffRef(3,0) = -cofactor_4x4<MatrixType,0,3>(matrix);
245 result.coeffRef(0,2) = cofactor_4x4<MatrixType,2,0>(matrix);
246 result.coeffRef(1,2) = -cofactor_4x4<MatrixType,2,1>(matrix);
247 result.coeffRef(2,2) = cofactor_4x4<MatrixType,2,2>(matrix);
248 result.coeffRef(3,2) = -cofactor_4x4<MatrixType,2,3>(matrix);
249 result.coeffRef(0,1) = -cofactor_4x4<MatrixType,1,0>(matrix);
250 result.coeffRef(1,1) = cofactor_4x4<MatrixType,1,1>(matrix);
251 result.coeffRef(2,1) = -cofactor_4x4<MatrixType,1,2>(matrix);
252 result.coeffRef(3,1) = cofactor_4x4<MatrixType,1,3>(matrix);
253 result.coeffRef(0,3) = -cofactor_4x4<MatrixType,3,0>(matrix);
254 result.coeffRef(1,3) = cofactor_4x4<MatrixType,3,1>(matrix);
255 result.coeffRef(2,3) = -cofactor_4x4<MatrixType,3,2>(matrix);
256 result.coeffRef(3,3) = cofactor_4x4<MatrixType,3,3>(matrix);
257 result /= (matrix.col(0).cwiseProduct(result.row(0).transpose())).sum();
261 template<
typename MatrixType,
typename ResultType>
262 struct compute_inverse<
MatrixType, ResultType, 4>
263 : compute_inverse_size4<Architecture::Target, typename MatrixType::Scalar,
264 MatrixType, ResultType>
268 template<
typename MatrixType,
typename ResultType>
269 struct compute_inverse_and_det_with_check<
MatrixType, ResultType, 4>
272 static inline void run(
276 typename ResultType::Scalar& determinant,
281 determinant = matrix.determinant();
282 invertible =
abs(determinant) > absDeterminantThreshold;
284 compute_inverse<MatrixType, ResultType>::run(matrix,
inverse);
286 else if(invertible) {
288 compute_inverse<MatrixType, ResultType>::run(matrix_t,
inverse);
302 template<
typename DstXprType,
typename XprType>
303 struct Assignment<DstXprType, Inverse<XprType>,
internal::assign_op<typename DstXprType::Scalar,typename XprType::Scalar>, Dense2Dense>
305 typedef Inverse<XprType> SrcXprType;
307 static void run(DstXprType &dst,
const SrcXprType &src,
const internal::assign_op<typename DstXprType::Scalar,typename XprType::Scalar> &)
309 Index dstRows = src.rows();
310 Index dstCols = src.cols();
311 if((dst.rows()!=dstRows) || (dst.cols()!=dstCols))
312 dst.resize(dstRows, dstCols);
314 const int Size =
plain_enum_min(XprType::ColsAtCompileTime, DstXprType::ColsAtCompileTime);
317 &&
"Aliasing problem detected in inverse(), you need to do inverse().eval() here.");
319 typedef typename internal::nested_eval<XprType,XprType::ColsAtCompileTime>::type ActualXprType;
320 typedef internal::remove_all_t<ActualXprType> ActualXprTypeCleanded;
322 ActualXprType actual_xpr(src.nestedExpression());
324 compute_inverse<ActualXprTypeCleanded, DstXprType>::run(actual_xpr, dst);
348 template<
typename Derived>
377 template<
typename Derived>
378 template<
typename ResultType>
381 typename ResultType::Scalar& determinant,
390 typedef std::conditional_t<
391 RowsAtCompileTime == 2,
395 internal::compute_inverse_and_det_with_check<MatrixType, ResultType>::run
396 (derived(), absDeterminantThreshold,
inverse, determinant, invertible);
418 template<
typename Derived>
419 template<
typename ResultType>
429 computeInverseAndDetWithCheck(
inverse,determinant,invertible,absDeterminantThreshold);
const AbsReturnType abs() const
#define EIGEN_DEVICE_FUNC
#define EIGEN_ONLY_USED_FOR_DEBUG(x)
#define EIGEN_STATIC_ASSERT(X, MSG)
Matrix< float, 1, Dynamic > MatrixType
NumTraits< Scalar >::Real RealScalar
std::conditional_t< internal::is_same< typename internal::traits< Derived >::XprKind, MatrixXpr >::value, PlainMatrix, PlainArray > PlainObject
The plain matrix or array type corresponding to this expression.
internal::traits< Derived >::Scalar Scalar
CoeffReturnType coeff(Index row, Index col) const
Expression of the inverse of another expression.
Base class for all dense matrices, vectors, and expressions.
void computeInverseWithCheck(ResultType &inverse, bool &invertible, const RealScalar &absDeterminantThreshold=NumTraits< Scalar >::dummy_precision()) const
const SparseMatrixBase< OtherDerived >::template CwiseProductDenseReturnType< Derived >::Type cwiseProduct(const SparseMatrixBase< OtherDerived > &other) const
const Inverse< Derived > inverse() const
void computeInverseAndDetWithCheck(ResultType &inverse, typename ResultType::Scalar &determinant, bool &invertible, const RealScalar &absDeterminantThreshold=NumTraits< Scalar >::dummy_precision()) const
The matrix class, also used for vectors and row-vectors.
constexpr Scalar & coeffRef(Index rowId, Index colId)
void compute_inverse_size3_helper(const MatrixType &matrix, const typename ResultType::Scalar &invdet, const Matrix< typename ResultType::Scalar, 3, 1 > &cofactors_col0, ResultType &result)
constexpr int plain_enum_min(A a, B b)
MatrixType::Scalar cofactor_3x3(const MatrixType &m)
MatrixType::Scalar cofactor_4x4(const MatrixType &matrix)
typename remove_all< T >::type remove_all_t
void compute_inverse_size2_helper(const MatrixType &matrix, const typename ResultType::Scalar &invdet, ResultType &result)
EIGEN_ALWAYS_INLINE const T::Scalar * extract_data(const T &m)
const Derived::Scalar general_det3_helper(const MatrixBase< Derived > &matrix, int i1, int i2, int i3, int j1, int j2, int j3)
EIGEN_ALWAYS_INLINE std::enable_if_t< NumTraits< T >::IsSigned||NumTraits< T >::IsComplex, typename NumTraits< T >::Real > abs(const T &x)
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_abs_op< typename Derived::Scalar >, const Derived > abs(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_inverse_op< typename Derived::Scalar >, const Derived > inverse(const Eigen::ArrayBase< Derived > &x)
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.