10 #ifndef EIGEN_INVERSE_H 11 #define EIGEN_INVERSE_H 21 template<
typename MatrixType,
typename ResultType,
int Size = MatrixType::RowsAtCompileTime>
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>
37 template<
typename MatrixType,
typename ResultType>
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>
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>
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>
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();
91 template<
typename MatrixType,
typename ResultType>
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;
116 template<
typename MatrixType,
int i,
int j>
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>
131 const MatrixType& matrix,
132 const typename ResultType::Scalar& invdet,
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>
148 static inline void run(
const MatrixType& matrix, ResultType& result)
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;
161 template<
typename MatrixType,
typename ResultType>
165 const MatrixType& matrix,
166 const typename MatrixType::RealScalar& absDeterminantThreshold,
168 typename ResultType::Scalar& determinant,
173 typedef typename ResultType::Scalar Scalar;
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;
190 template<
typename Derived>
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)
214 template<
int Arch,
typename Scalar,
typename MatrixType,
typename ResultType>
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>
242 MatrixType, ResultType>
246 template<
typename MatrixType,
typename ResultType>
250 const MatrixType& matrix,
251 const typename MatrixType::RealScalar& absDeterminantThreshold,
253 typename ResultType::Scalar& determinant,
258 determinant = matrix.determinant();
259 invertible =
abs(determinant) > absDeterminantThreshold;
268 template<
typename MatrixType>
274 template<
typename MatrixType>
277 typedef typename MatrixType::Index
Index;
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 294 &&
"Aliasing problem detected in inverse(), you need to do inverse().eval() here.");
319 template<
typename Derived>
345 template<
typename Derived>
346 template<
typename ResultType>
349 typename ResultType::Scalar& determinant,
359 RowsAtCompileTime == 2,
364 (derived(), absDeterminantThreshold, inverse, determinant, invertible);
384 template<
typename Derived>
385 template<
typename ResultType>
395 computeInverseAndDetWithCheck(inverse,determinant,invertible,absDeterminantThreshold);
400 #endif // EIGEN_INVERSE_H MatrixType::Scalar cofactor_4x4(const MatrixType &matrix)
void compute_inverse_size3_helper(const MatrixType &matrix, const typename ResultType::Scalar &invdet, const Matrix< typename ResultType::Scalar, 3, 1 > &cofactors_col0, ResultType &result)
#define EIGEN_ONLY_USED_FOR_DEBUG(x)
void evalTo(Dest &dst) const
inverse_impl(const MatrixType &matrix)
MatrixType::PlainObject ReturnType
void compute_inverse_size2_helper(const MatrixType &matrix, const typename ResultType::Scalar &invdet, ResultType &result)
static void run(const MatrixType &matrix, const typename MatrixType::RealScalar &absDeterminantThreshold, ResultType &result, typename ResultType::Scalar &determinant, bool &invertible)
static void run(const MatrixType &matrix, ResultType &result)
internal::eval< MatrixType >::type MatrixTypeNested
static void run(const MatrixType &matrix, const typename MatrixType::RealScalar &absDeterminantThreshold, ResultType &inverse, typename ResultType::Scalar &determinant, bool &invertible)
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
MatrixTypeNested m_matrix
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
#define EIGEN_PLAIN_ENUM_MIN(a, b)
remove_all< MatrixTypeNested >::type MatrixTypeNestedCleaned
static void run(const MatrixType &matrix, ResultType &result)
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)
const CwiseUnaryOp< internal::scalar_inverse_op< Scalar >, const Derived > inverse() const
EIGEN_STRONG_INLINE Scalar & coeffRef(Index rowId, Index colId)
void computeInverseAndDetWithCheck(ResultType &inverse, typename ResultType::Scalar &determinant, bool &invertible, const RealScalar &absDeterminantThreshold=NumTraits< Scalar >::dummy_precision()) const
NumTraits< Scalar >::Real RealScalar
void computeInverseWithCheck(ResultType &inverse, bool &invertible, const RealScalar &absDeterminantThreshold=NumTraits< Scalar >::dummy_precision()) const
static void run(const MatrixType &matrix, const typename MatrixType::RealScalar &absDeterminantThreshold, ResultType &inverse, typename ResultType::Scalar &determinant, bool &invertible)
static void run(const MatrixType &matrix, ResultType &result)
static void run(const MatrixType &matrix, ResultType &result)
const Derived::Scalar general_det3_helper(const MatrixBase< Derived > &matrix, int i1, int i2, int i3, int j1, int j2, int j3)
const T::Scalar * extract_data(const T &m)
The matrix class, also used for vectors and row-vectors.
const internal::inverse_impl< Derived > inverse() const
MatrixType::Scalar cofactor_3x3(const MatrixType &m)
Base class for all dense matrices, vectors, and expressions.
static void run(const MatrixType &matrix, ResultType &result)