Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
InverseImpl.h
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// Copyright (C) 2014 Gael Guennebaud <gael.guennebaud@inria.fr>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10// SPDX-License-Identifier: MPL-2.0
11
12#ifndef EIGEN_INVERSE_IMPL_H
13#define EIGEN_INVERSE_IMPL_H
14
15// IWYU pragma: private
16#include "./InternalHeaderCheck.h"
17
18namespace Eigen {
19
20namespace internal {
21
22/**********************************
23*** General case implementation ***
24**********************************/
25
26template <typename MatrixType, typename ResultType, int Size = MatrixType::RowsAtCompileTime>
27struct compute_inverse {
28 EIGEN_DEVICE_FUNC static inline void run(const MatrixType& matrix, ResultType& result) {
29 result = matrix.partialPivLu().inverse();
30 }
31};
32
33template <typename MatrixType, typename ResultType, int Size = MatrixType::RowsAtCompileTime>
34struct compute_inverse_and_det_with_check { /* nothing! general case not supported. */
35};
36
37/****************************
38*** Size 1 implementation ***
39****************************/
40
41template <typename MatrixType, typename ResultType>
42struct compute_inverse<MatrixType, ResultType, 1> {
43 EIGEN_DEVICE_FUNC static inline void run(const MatrixType& matrix, ResultType& result) {
44 using Scalar = typename MatrixType::Scalar;
45 internal::evaluator<MatrixType> matrixEval(matrix);
46 result.coeffRef(0, 0) = Scalar(1) / matrixEval.coeff(0, 0);
47 }
48};
49
50template <typename MatrixType, typename ResultType>
51struct compute_inverse_and_det_with_check<MatrixType, ResultType, 1> {
52 EIGEN_DEVICE_FUNC static inline void run(const MatrixType& matrix,
53 const typename MatrixType::RealScalar& absDeterminantThreshold,
54 ResultType& result, typename ResultType::Scalar& determinant,
55 bool& invertible) {
56 determinant = matrix.coeff(0, 0);
57 invertible = numext::abs(determinant) > absDeterminantThreshold;
58 if (invertible) result.coeffRef(0, 0) = typename ResultType::Scalar(1) / determinant;
59 }
60};
61
62/****************************
63*** Size 2 implementation ***
64****************************/
65
66template <typename MatrixType, typename ResultType>
67EIGEN_DEVICE_FUNC inline void compute_inverse_size2_helper(const MatrixType& matrix,
68 const typename ResultType::Scalar& invdet,
69 ResultType& result) {
70 typename ResultType::Scalar temp = matrix.coeff(0, 0);
71 result.coeffRef(0, 0) = matrix.coeff(1, 1) * invdet;
72 result.coeffRef(1, 0) = -matrix.coeff(1, 0) * invdet;
73 result.coeffRef(0, 1) = -matrix.coeff(0, 1) * invdet;
74 result.coeffRef(1, 1) = temp * invdet;
75}
76
77template <typename MatrixType, typename ResultType>
78struct compute_inverse<MatrixType, ResultType, 2> {
79 EIGEN_DEVICE_FUNC static inline void run(const MatrixType& matrix, ResultType& result) {
80 using Scalar = typename ResultType::Scalar;
81 const Scalar invdet = typename MatrixType::Scalar(1) / matrix.determinant();
82 compute_inverse_size2_helper(matrix, invdet, result);
83 }
84};
85
86template <typename MatrixType, typename ResultType>
87struct compute_inverse_and_det_with_check<MatrixType, ResultType, 2> {
88 EIGEN_DEVICE_FUNC static inline void run(const MatrixType& matrix,
89 const typename MatrixType::RealScalar& absDeterminantThreshold,
90 ResultType& inverse, typename ResultType::Scalar& determinant,
91 bool& invertible) {
92 using Scalar = typename ResultType::Scalar;
93 determinant = matrix.determinant();
94 invertible = numext::abs(determinant) > absDeterminantThreshold;
95 if (!invertible) return;
96 const Scalar invdet = Scalar(1) / determinant;
97 compute_inverse_size2_helper(matrix, invdet, inverse);
98 }
99};
100
101/****************************
102*** Size 3 implementation ***
103****************************/
104
105template <typename MatrixType, int i, int j>
106EIGEN_DEVICE_FUNC inline typename MatrixType::Scalar cofactor_3x3(const MatrixType& m) {
107 enum { i1 = (i + 1) % 3, i2 = (i + 2) % 3, j1 = (j + 1) % 3, j2 = (j + 2) % 3 };
108 return m.coeff(i1, j1) * m.coeff(i2, j2) - m.coeff(i1, j2) * m.coeff(i2, j1);
109}
110
111template <typename MatrixType, typename ResultType>
112EIGEN_DEVICE_FUNC inline void compute_inverse_size3_helper(
113 const MatrixType& matrix, const typename ResultType::Scalar& invdet,
114 const Matrix<typename ResultType::Scalar, 3, 1>& cofactors_col0, ResultType& result) {
115 // Compute cofactors in a way that avoids aliasing issues.
116 using Scalar = typename ResultType::Scalar;
117 const Scalar c01 = cofactor_3x3<MatrixType, 0, 1>(matrix) * invdet;
118 const Scalar c11 = cofactor_3x3<MatrixType, 1, 1>(matrix) * invdet;
119 const Scalar c02 = cofactor_3x3<MatrixType, 0, 2>(matrix) * invdet;
120 result.coeffRef(1, 2) = cofactor_3x3<MatrixType, 2, 1>(matrix) * invdet;
121 result.coeffRef(2, 1) = cofactor_3x3<MatrixType, 1, 2>(matrix) * invdet;
122 result.coeffRef(2, 2) = cofactor_3x3<MatrixType, 2, 2>(matrix) * invdet;
123 result.coeffRef(1, 0) = c01;
124 result.coeffRef(1, 1) = c11;
125 result.coeffRef(2, 0) = c02;
126 result.row(0) = cofactors_col0 * invdet;
127}
128
129template <typename MatrixType, typename ResultType>
130struct compute_inverse<MatrixType, ResultType, 3> {
131 EIGEN_DEVICE_FUNC static inline void run(const MatrixType& matrix, ResultType& result) {
132 using Scalar = typename ResultType::Scalar;
133 Matrix<typename MatrixType::Scalar, 3, 1> cofactors_col0;
134 cofactors_col0.coeffRef(0) = cofactor_3x3<MatrixType, 0, 0>(matrix);
135 cofactors_col0.coeffRef(1) = cofactor_3x3<MatrixType, 1, 0>(matrix);
136 cofactors_col0.coeffRef(2) = cofactor_3x3<MatrixType, 2, 0>(matrix);
137 const Scalar det = (cofactors_col0.cwiseProduct(matrix.col(0))).sum();
138 const Scalar invdet = Scalar(1) / det;
139 compute_inverse_size3_helper(matrix, invdet, cofactors_col0, result);
140 }
141};
142
143template <typename MatrixType, typename ResultType>
144struct compute_inverse_and_det_with_check<MatrixType, ResultType, 3> {
145 EIGEN_DEVICE_FUNC static inline void run(const MatrixType& matrix,
146 const typename MatrixType::RealScalar& absDeterminantThreshold,
147 ResultType& inverse, typename ResultType::Scalar& determinant,
148 bool& invertible) {
149 using Scalar = typename ResultType::Scalar;
150 Matrix<Scalar, 3, 1> cofactors_col0;
151 cofactors_col0.coeffRef(0) = cofactor_3x3<MatrixType, 0, 0>(matrix);
152 cofactors_col0.coeffRef(1) = cofactor_3x3<MatrixType, 1, 0>(matrix);
153 cofactors_col0.coeffRef(2) = cofactor_3x3<MatrixType, 2, 0>(matrix);
154 determinant = (cofactors_col0.cwiseProduct(matrix.col(0))).sum();
155 invertible = Eigen::numext::abs(determinant) > absDeterminantThreshold;
156 if (!invertible) return;
157 const Scalar invdet = Scalar(1) / determinant;
158 compute_inverse_size3_helper(matrix, invdet, cofactors_col0, inverse);
159 }
160};
161
162/****************************
163*** Size 4 implementation ***
164****************************/
165
166template <typename Derived>
167EIGEN_DEVICE_FUNC inline const typename Derived::Scalar general_det3_helper(const MatrixBase<Derived>& matrix, int i1,
168 int i2, int i3, int j1, int j2, int j3) {
169 return matrix.coeff(i1, j1) *
170 (matrix.coeff(i2, j2) * matrix.coeff(i3, j3) - matrix.coeff(i2, j3) * matrix.coeff(i3, j2));
171}
172
173template <typename MatrixType, int i, int j>
174EIGEN_DEVICE_FUNC inline typename MatrixType::Scalar cofactor_4x4(const MatrixType& matrix) {
175 enum { i1 = (i + 1) % 4, i2 = (i + 2) % 4, i3 = (i + 3) % 4, j1 = (j + 1) % 4, j2 = (j + 2) % 4, j3 = (j + 3) % 4 };
176 return general_det3_helper(matrix, i1, i2, i3, j1, j2, j3) + general_det3_helper(matrix, i2, i3, i1, j1, j2, j3) +
177 general_det3_helper(matrix, i3, i1, i2, j1, j2, j3);
178}
179
180template <int Arch, typename Scalar, typename MatrixType, typename ResultType>
181struct compute_inverse_size4 {
182 EIGEN_DEVICE_FUNC static void run(const MatrixType& matrix, ResultType& result) {
183 result.coeffRef(0, 0) = cofactor_4x4<MatrixType, 0, 0>(matrix);
184 result.coeffRef(1, 0) = -cofactor_4x4<MatrixType, 0, 1>(matrix);
185 result.coeffRef(2, 0) = cofactor_4x4<MatrixType, 0, 2>(matrix);
186 result.coeffRef(3, 0) = -cofactor_4x4<MatrixType, 0, 3>(matrix);
187 result.coeffRef(0, 2) = cofactor_4x4<MatrixType, 2, 0>(matrix);
188 result.coeffRef(1, 2) = -cofactor_4x4<MatrixType, 2, 1>(matrix);
189 result.coeffRef(2, 2) = cofactor_4x4<MatrixType, 2, 2>(matrix);
190 result.coeffRef(3, 2) = -cofactor_4x4<MatrixType, 2, 3>(matrix);
191 result.coeffRef(0, 1) = -cofactor_4x4<MatrixType, 1, 0>(matrix);
192 result.coeffRef(1, 1) = cofactor_4x4<MatrixType, 1, 1>(matrix);
193 result.coeffRef(2, 1) = -cofactor_4x4<MatrixType, 1, 2>(matrix);
194 result.coeffRef(3, 1) = cofactor_4x4<MatrixType, 1, 3>(matrix);
195 result.coeffRef(0, 3) = -cofactor_4x4<MatrixType, 3, 0>(matrix);
196 result.coeffRef(1, 3) = cofactor_4x4<MatrixType, 3, 1>(matrix);
197 result.coeffRef(2, 3) = -cofactor_4x4<MatrixType, 3, 2>(matrix);
198 result.coeffRef(3, 3) = cofactor_4x4<MatrixType, 3, 3>(matrix);
199 result /= (matrix.col(0).cwiseProduct(result.row(0).transpose())).sum();
200 }
201};
202
203template <typename MatrixType, typename ResultType>
204struct compute_inverse<MatrixType, ResultType, 4>
205 : compute_inverse_size4<Architecture::Target, typename MatrixType::Scalar, MatrixType, ResultType> {};
206
207template <typename MatrixType, typename ResultType>
208struct compute_inverse_and_det_with_check<MatrixType, ResultType, 4> {
209 EIGEN_DEVICE_FUNC static inline void run(const MatrixType& matrix,
210 const typename MatrixType::RealScalar& absDeterminantThreshold,
211 ResultType& inverse, typename ResultType::Scalar& determinant,
212 bool& invertible) {
213 determinant = matrix.determinant();
214 invertible = numext::abs(determinant) > absDeterminantThreshold;
215 if (invertible && extract_data(matrix) != extract_data(inverse)) {
216 compute_inverse<MatrixType, ResultType>::run(matrix, inverse);
217 } else if (invertible) {
218 MatrixType matrix_t = matrix;
219 compute_inverse<MatrixType, ResultType>::run(matrix_t, inverse);
220 }
221 }
222};
223
224/*************************
225*** MatrixBase methods ***
226*************************/
227
228} // end namespace internal
229
230namespace internal {
231
232// Specialization for "dense = dense_xpr.inverse()"
233template <typename DstXprType, typename XprType>
234struct Assignment<DstXprType, Inverse<XprType>,
235 internal::assign_op<typename DstXprType::Scalar, typename XprType::Scalar>, Dense2Dense> {
236 using SrcXprType = Inverse<XprType>;
237 EIGEN_DEVICE_FUNC static void run(DstXprType& dst, const SrcXprType& src,
238 const internal::assign_op<typename DstXprType::Scalar, typename XprType::Scalar>&) {
239 Index dstRows = src.rows();
240 Index dstCols = src.cols();
241 if ((dst.rows() != dstRows) || (dst.cols() != dstCols)) dst.resize(dstRows, dstCols);
242
243 const int Size = plain_enum_min(XprType::ColsAtCompileTime, DstXprType::ColsAtCompileTime);
244 EIGEN_ONLY_USED_FOR_DEBUG(Size);
245 eigen_assert(((Size <= 1) || (Size > 4) || (extract_data(src.nestedExpression()) != extract_data(dst))) &&
246 "Aliasing problem detected in inverse(), you need to do inverse().eval() here.");
247
248 using ActualXprType = typename internal::nested_eval<XprType, XprType::ColsAtCompileTime>::type;
249 using ActualXprTypeCleanded = internal::remove_all_t<ActualXprType>;
250
251 ActualXprType actual_xpr(src.nestedExpression());
252
253 compute_inverse<ActualXprTypeCleanded, DstXprType>::run(actual_xpr, dst);
254 }
255};
256
257} // end namespace internal
258
276template <typename Derived>
277EIGEN_DEVICE_FUNC inline Inverse<Derived> MatrixBase<Derived>::inverse() const {
278 EIGEN_STATIC_ASSERT(!NumTraits<Scalar>::IsInteger, THIS_FUNCTION_IS_NOT_FOR_INTEGER_NUMERIC_TYPES)
279 EIGEN_INTERNAL_ASSERT_SQUARE(*this);
280 return Inverse<Derived>(derived());
281}
282
303template <typename Derived>
304template <typename ResultType>
306 typename ResultType::Scalar& determinant,
307 bool& invertible,
308 const RealScalar& absDeterminantThreshold) const {
309 EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(Derived, ResultType)
310 EIGEN_INTERNAL_ASSERT_SQUARE(*this);
311 inverse.resize(rows(), cols());
312 // for 2x2, it's worth giving a chance to avoid evaluating.
313 // for larger sizes, evaluating has negligible cost and limits code size.
314 using MatrixType =
315 std::conditional_t<RowsAtCompileTime == 2,
316 internal::remove_all_t<typename internal::nested_eval<Derived, 2>::type>, PlainObject>;
317 internal::compute_inverse_and_det_with_check<MatrixType, ResultType>::run(derived(), absDeterminantThreshold, inverse,
318 determinant, invertible);
319}
320
340template <typename Derived>
341template <typename ResultType>
342inline void MatrixBase<Derived>::computeInverseWithCheck(ResultType& inverse, bool& invertible,
343 const RealScalar& absDeterminantThreshold) const {
345 computeInverseAndDetWithCheck(inverse, determinant, invertible, absDeterminantThreshold);
346}
347
348} // end namespace Eigen
349
350#endif // EIGEN_INVERSE_IMPL_H
@ RowsAtCompileTime
Definition DenseBase.h:97
typename internal::traits< Derived >::Scalar Scalar
Definition DenseBase.h:63
Expression of the inverse of another expression.
Definition Inverse.h:44
void computeInverseWithCheck(ResultType &inverse, bool &invertible, const RealScalar &absDeterminantThreshold=NumTraits< Scalar >::dummy_precision()) const
Definition InverseImpl.h:342
Inverse< Derived > inverse() const
Definition InverseImpl.h:277
Scalar determinant() const
Definition Determinant.h:88
void computeInverseAndDetWithCheck(ResultType &inverse, typename ResultType::Scalar &determinant, bool &invertible, const RealScalar &absDeterminantThreshold=NumTraits< Scalar >::dummy_precision()) const
Definition InverseImpl.h:305