2008-04-14 08:20:24 +00:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
2009-05-22 20:25:33 +02:00
|
|
|
// for linear algebra.
|
2008-04-14 08:20:24 +00:00
|
|
|
//
|
2010-04-28 18:51:38 -04:00
|
|
|
// Copyright (C) 2008-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
|
2008-04-14 08:20:24 +00:00
|
|
|
//
|
2012-07-13 14:42:47 -04:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2008-04-14 08:20:24 +00:00
|
|
|
|
|
|
|
|
#ifndef EIGEN_INVERSE_H
|
|
|
|
|
#define EIGEN_INVERSE_H
|
|
|
|
|
|
2012-04-15 11:06:28 +01:00
|
|
|
namespace Eigen {
|
|
|
|
|
|
2010-10-25 10:15:22 -04:00
|
|
|
namespace internal {
|
|
|
|
|
|
2009-10-26 12:30:29 -04:00
|
|
|
/**********************************
|
|
|
|
|
*** General case implementation ***
|
|
|
|
|
**********************************/
|
|
|
|
|
|
|
|
|
|
template<typename MatrixType, typename ResultType, int Size = MatrixType::RowsAtCompileTime>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct compute_inverse
|
2009-10-26 12:30:29 -04:00
|
|
|
{
|
2013-02-07 19:06:14 +01:00
|
|
|
EIGEN_DEVICE_FUNC
|
2009-10-26 12:30:29 -04:00
|
|
|
static inline void run(const MatrixType& matrix, ResultType& result)
|
|
|
|
|
{
|
2009-10-28 18:19:29 -04:00
|
|
|
result = matrix.partialPivLu().inverse();
|
2009-10-26 12:30:29 -04:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename MatrixType, typename ResultType, int Size = MatrixType::RowsAtCompileTime>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct compute_inverse_and_det_with_check { /* nothing! general case not supported. */ };
|
2009-10-26 12:30:29 -04:00
|
|
|
|
|
|
|
|
/****************************
|
|
|
|
|
*** Size 1 implementation ***
|
|
|
|
|
****************************/
|
|
|
|
|
|
|
|
|
|
template<typename MatrixType, typename ResultType>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct compute_inverse<MatrixType, ResultType, 1>
|
2009-10-26 12:30:29 -04:00
|
|
|
{
|
2013-02-07 19:06:14 +01:00
|
|
|
EIGEN_DEVICE_FUNC
|
2009-10-26 12:30:29 -04:00
|
|
|
static inline void run(const MatrixType& matrix, ResultType& result)
|
|
|
|
|
{
|
|
|
|
|
typedef typename MatrixType::Scalar Scalar;
|
2014-02-20 14:18:24 +01:00
|
|
|
#ifdef EIGEN_TEST_EVALUATORS
|
|
|
|
|
typename internal::evaluator<MatrixType>::type matrixEval(matrix);
|
|
|
|
|
result.coeffRef(0,0) = Scalar(1) / matrixEval.coeff(0,0);
|
|
|
|
|
#else
|
2009-10-26 12:30:29 -04:00
|
|
|
result.coeffRef(0,0) = Scalar(1) / matrix.coeff(0,0);
|
2014-02-20 14:18:24 +01:00
|
|
|
#endif
|
2009-10-26 12:30:29 -04:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename MatrixType, typename ResultType>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct compute_inverse_and_det_with_check<MatrixType, ResultType, 1>
|
2009-10-26 12:30:29 -04:00
|
|
|
{
|
2013-02-07 19:06:14 +01:00
|
|
|
EIGEN_DEVICE_FUNC
|
2009-10-26 12:30:29 -04:00
|
|
|
static inline void run(
|
|
|
|
|
const MatrixType& matrix,
|
|
|
|
|
const typename MatrixType::RealScalar& absDeterminantThreshold,
|
|
|
|
|
ResultType& result,
|
|
|
|
|
typename ResultType::Scalar& determinant,
|
|
|
|
|
bool& invertible
|
|
|
|
|
)
|
|
|
|
|
{
|
2012-11-06 15:25:50 +01:00
|
|
|
using std::abs;
|
2009-10-26 12:30:29 -04:00
|
|
|
determinant = matrix.coeff(0,0);
|
2010-10-25 10:15:22 -04:00
|
|
|
invertible = abs(determinant) > absDeterminantThreshold;
|
2009-10-26 12:30:29 -04:00
|
|
|
if(invertible) result.coeffRef(0,0) = typename ResultType::Scalar(1) / determinant;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/****************************
|
|
|
|
|
*** Size 2 implementation ***
|
|
|
|
|
****************************/
|
2008-07-15 23:56:17 +00:00
|
|
|
|
2009-10-26 11:18:23 -04:00
|
|
|
template<typename MatrixType, typename ResultType>
|
2013-02-07 19:06:14 +01:00
|
|
|
EIGEN_DEVICE_FUNC
|
2010-10-25 10:15:22 -04:00
|
|
|
inline void compute_inverse_size2_helper(
|
2009-10-26 11:18:23 -04:00
|
|
|
const MatrixType& matrix, const typename ResultType::Scalar& invdet,
|
|
|
|
|
ResultType& result)
|
2009-06-29 20:47:37 +02:00
|
|
|
{
|
2014-02-20 14:18:24 +01:00
|
|
|
result.coeffRef(0,0) = matrix.coeff(1,1) * invdet;
|
2009-10-26 11:18:23 -04:00
|
|
|
result.coeffRef(1,0) = -matrix.coeff(1,0) * invdet;
|
|
|
|
|
result.coeffRef(0,1) = -matrix.coeff(0,1) * invdet;
|
2014-02-20 14:18:24 +01:00
|
|
|
result.coeffRef(1,1) = matrix.coeff(0,0) * invdet;
|
2009-06-29 20:47:37 +02:00
|
|
|
}
|
|
|
|
|
|
2009-10-26 11:18:23 -04:00
|
|
|
template<typename MatrixType, typename ResultType>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct compute_inverse<MatrixType, ResultType, 2>
|
2008-07-15 23:56:17 +00:00
|
|
|
{
|
2013-02-07 19:06:14 +01:00
|
|
|
EIGEN_DEVICE_FUNC
|
2009-10-26 12:30:29 -04:00
|
|
|
static inline void run(const MatrixType& matrix, ResultType& result)
|
|
|
|
|
{
|
|
|
|
|
typedef typename ResultType::Scalar Scalar;
|
|
|
|
|
const Scalar invdet = typename MatrixType::Scalar(1) / matrix.determinant();
|
2010-10-25 10:15:22 -04:00
|
|
|
compute_inverse_size2_helper(matrix, invdet, result);
|
2009-10-26 12:30:29 -04:00
|
|
|
}
|
|
|
|
|
};
|
2008-04-14 08:20:24 +00:00
|
|
|
|
2009-10-26 11:18:23 -04:00
|
|
|
template<typename MatrixType, typename ResultType>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct compute_inverse_and_det_with_check<MatrixType, ResultType, 2>
|
2008-04-14 17:07:12 +00:00
|
|
|
{
|
2013-02-07 19:06:14 +01:00
|
|
|
EIGEN_DEVICE_FUNC
|
2009-10-26 12:30:29 -04:00
|
|
|
static inline void run(
|
|
|
|
|
const MatrixType& matrix,
|
|
|
|
|
const typename MatrixType::RealScalar& absDeterminantThreshold,
|
|
|
|
|
ResultType& inverse,
|
|
|
|
|
typename ResultType::Scalar& determinant,
|
|
|
|
|
bool& invertible
|
|
|
|
|
)
|
|
|
|
|
{
|
2012-11-06 15:25:50 +01:00
|
|
|
using std::abs;
|
2009-10-26 12:30:29 -04:00
|
|
|
typedef typename ResultType::Scalar Scalar;
|
|
|
|
|
determinant = matrix.determinant();
|
2010-10-25 10:15:22 -04:00
|
|
|
invertible = abs(determinant) > absDeterminantThreshold;
|
2009-10-26 12:30:29 -04:00
|
|
|
if(!invertible) return;
|
|
|
|
|
const Scalar invdet = Scalar(1) / determinant;
|
2010-10-25 10:15:22 -04:00
|
|
|
compute_inverse_size2_helper(matrix, invdet, inverse);
|
2009-10-26 12:30:29 -04:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/****************************
|
|
|
|
|
*** Size 3 implementation ***
|
|
|
|
|
****************************/
|
2008-04-14 17:07:12 +00:00
|
|
|
|
2010-04-22 20:40:31 -04:00
|
|
|
template<typename MatrixType, int i, int j>
|
2013-02-07 19:06:14 +01:00
|
|
|
EIGEN_DEVICE_FUNC
|
2010-10-25 10:15:22 -04:00
|
|
|
inline typename MatrixType::Scalar cofactor_3x3(const MatrixType& m)
|
2010-04-22 20:40:31 -04:00
|
|
|
{
|
|
|
|
|
enum {
|
|
|
|
|
i1 = (i+1) % 3,
|
|
|
|
|
i2 = (i+2) % 3,
|
|
|
|
|
j1 = (j+1) % 3,
|
|
|
|
|
j2 = (j+2) % 3
|
|
|
|
|
};
|
|
|
|
|
return m.coeff(i1, j1) * m.coeff(i2, j2)
|
|
|
|
|
- m.coeff(i1, j2) * m.coeff(i2, j1);
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-26 11:18:23 -04:00
|
|
|
template<typename MatrixType, typename ResultType>
|
2013-02-07 19:06:14 +01:00
|
|
|
EIGEN_DEVICE_FUNC
|
2010-10-25 10:15:22 -04:00
|
|
|
inline void compute_inverse_size3_helper(
|
2009-10-26 11:18:23 -04:00
|
|
|
const MatrixType& matrix,
|
|
|
|
|
const typename ResultType::Scalar& invdet,
|
|
|
|
|
const Matrix<typename ResultType::Scalar,3,1>& cofactors_col0,
|
|
|
|
|
ResultType& result)
|
2008-04-14 08:20:24 +00:00
|
|
|
{
|
2009-10-26 11:18:23 -04:00
|
|
|
result.row(0) = cofactors_col0 * invdet;
|
2010-10-25 10:15:22 -04:00
|
|
|
result.coeffRef(1,0) = cofactor_3x3<MatrixType,0,1>(matrix) * invdet;
|
|
|
|
|
result.coeffRef(1,1) = cofactor_3x3<MatrixType,1,1>(matrix) * invdet;
|
|
|
|
|
result.coeffRef(1,2) = cofactor_3x3<MatrixType,2,1>(matrix) * invdet;
|
|
|
|
|
result.coeffRef(2,0) = cofactor_3x3<MatrixType,0,2>(matrix) * invdet;
|
|
|
|
|
result.coeffRef(2,1) = cofactor_3x3<MatrixType,1,2>(matrix) * invdet;
|
|
|
|
|
result.coeffRef(2,2) = cofactor_3x3<MatrixType,2,2>(matrix) * invdet;
|
2008-04-15 20:15:36 +00:00
|
|
|
}
|
2008-04-14 08:20:24 +00:00
|
|
|
|
2009-10-26 11:18:23 -04:00
|
|
|
template<typename MatrixType, typename ResultType>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct compute_inverse<MatrixType, ResultType, 3>
|
2009-06-29 20:47:37 +02:00
|
|
|
{
|
2013-02-07 19:06:14 +01:00
|
|
|
EIGEN_DEVICE_FUNC
|
2009-10-26 12:30:29 -04:00
|
|
|
static inline void run(const MatrixType& matrix, ResultType& result)
|
|
|
|
|
{
|
|
|
|
|
typedef typename ResultType::Scalar Scalar;
|
2010-10-26 16:47:01 +02:00
|
|
|
Matrix<typename MatrixType::Scalar,3,1> cofactors_col0;
|
2010-10-25 10:15:22 -04:00
|
|
|
cofactors_col0.coeffRef(0) = cofactor_3x3<MatrixType,0,0>(matrix);
|
|
|
|
|
cofactors_col0.coeffRef(1) = cofactor_3x3<MatrixType,1,0>(matrix);
|
|
|
|
|
cofactors_col0.coeffRef(2) = cofactor_3x3<MatrixType,2,0>(matrix);
|
2009-11-18 18:15:19 +01:00
|
|
|
const Scalar det = (cofactors_col0.cwiseProduct(matrix.col(0))).sum();
|
2009-10-26 12:30:29 -04:00
|
|
|
const Scalar invdet = Scalar(1) / det;
|
2010-10-25 10:15:22 -04:00
|
|
|
compute_inverse_size3_helper(matrix, invdet, cofactors_col0, result);
|
2009-10-26 12:30:29 -04:00
|
|
|
}
|
|
|
|
|
};
|
2009-06-29 20:47:37 +02:00
|
|
|
|
2009-10-06 09:26:28 -04:00
|
|
|
template<typename MatrixType, typename ResultType>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct compute_inverse_and_det_with_check<MatrixType, ResultType, 3>
|
2009-10-26 11:18:23 -04:00
|
|
|
{
|
2013-02-07 19:06:14 +01:00
|
|
|
EIGEN_DEVICE_FUNC
|
2009-10-26 12:30:29 -04:00
|
|
|
static inline void run(
|
|
|
|
|
const MatrixType& matrix,
|
|
|
|
|
const typename MatrixType::RealScalar& absDeterminantThreshold,
|
|
|
|
|
ResultType& inverse,
|
|
|
|
|
typename ResultType::Scalar& determinant,
|
|
|
|
|
bool& invertible
|
|
|
|
|
)
|
|
|
|
|
{
|
2012-11-06 15:25:50 +01:00
|
|
|
using std::abs;
|
2009-10-26 12:30:29 -04:00
|
|
|
typedef typename ResultType::Scalar Scalar;
|
|
|
|
|
Matrix<Scalar,3,1> cofactors_col0;
|
2010-10-25 10:15:22 -04:00
|
|
|
cofactors_col0.coeffRef(0) = cofactor_3x3<MatrixType,0,0>(matrix);
|
|
|
|
|
cofactors_col0.coeffRef(1) = cofactor_3x3<MatrixType,1,0>(matrix);
|
|
|
|
|
cofactors_col0.coeffRef(2) = cofactor_3x3<MatrixType,2,0>(matrix);
|
2009-11-18 18:15:19 +01:00
|
|
|
determinant = (cofactors_col0.cwiseProduct(matrix.col(0))).sum();
|
2010-10-25 10:15:22 -04:00
|
|
|
invertible = abs(determinant) > absDeterminantThreshold;
|
2009-10-26 12:30:29 -04:00
|
|
|
if(!invertible) return;
|
|
|
|
|
const Scalar invdet = Scalar(1) / determinant;
|
2010-10-25 10:15:22 -04:00
|
|
|
compute_inverse_size3_helper(matrix, invdet, cofactors_col0, inverse);
|
2009-10-26 12:30:29 -04:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/****************************
|
|
|
|
|
*** Size 4 implementation ***
|
|
|
|
|
****************************/
|
2009-10-26 11:18:23 -04:00
|
|
|
|
2010-04-22 20:40:31 -04:00
|
|
|
template<typename Derived>
|
2013-02-07 19:06:14 +01:00
|
|
|
EIGEN_DEVICE_FUNC
|
2010-10-25 10:15:22 -04:00
|
|
|
inline const typename Derived::Scalar general_det3_helper
|
2010-04-22 20:40:31 -04:00
|
|
|
(const MatrixBase<Derived>& matrix, int i1, int i2, int i3, int j1, int j2, int j3)
|
|
|
|
|
{
|
|
|
|
|
return matrix.coeff(i1,j1)
|
|
|
|
|
* (matrix.coeff(i2,j2) * matrix.coeff(i3,j3) - matrix.coeff(i2,j3) * matrix.coeff(i3,j2));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename MatrixType, int i, int j>
|
2013-02-07 19:06:14 +01:00
|
|
|
EIGEN_DEVICE_FUNC
|
2010-10-25 10:15:22 -04:00
|
|
|
inline typename MatrixType::Scalar cofactor_4x4(const MatrixType& matrix)
|
2010-04-22 20:40:31 -04:00
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
};
|
2010-10-25 10:15:22 -04:00
|
|
|
return general_det3_helper(matrix, i1, i2, i3, j1, j2, j3)
|
|
|
|
|
+ general_det3_helper(matrix, i2, i3, i1, j1, j2, j3)
|
|
|
|
|
+ general_det3_helper(matrix, i3, i1, i2, j1, j2, j3);
|
2010-04-22 20:40:31 -04:00
|
|
|
}
|
|
|
|
|
|
2009-12-14 22:47:14 -05:00
|
|
|
template<int Arch, typename Scalar, typename MatrixType, typename ResultType>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct compute_inverse_size4
|
2008-04-14 08:20:24 +00:00
|
|
|
{
|
2013-02-07 19:06:14 +01:00
|
|
|
EIGEN_DEVICE_FUNC
|
2009-12-14 22:47:14 -05:00
|
|
|
static void run(const MatrixType& matrix, ResultType& result)
|
2009-10-26 12:30:29 -04:00
|
|
|
{
|
2010-10-25 10:15:22 -04:00
|
|
|
result.coeffRef(0,0) = cofactor_4x4<MatrixType,0,0>(matrix);
|
|
|
|
|
result.coeffRef(1,0) = -cofactor_4x4<MatrixType,0,1>(matrix);
|
|
|
|
|
result.coeffRef(2,0) = cofactor_4x4<MatrixType,0,2>(matrix);
|
|
|
|
|
result.coeffRef(3,0) = -cofactor_4x4<MatrixType,0,3>(matrix);
|
|
|
|
|
result.coeffRef(0,2) = cofactor_4x4<MatrixType,2,0>(matrix);
|
|
|
|
|
result.coeffRef(1,2) = -cofactor_4x4<MatrixType,2,1>(matrix);
|
|
|
|
|
result.coeffRef(2,2) = cofactor_4x4<MatrixType,2,2>(matrix);
|
|
|
|
|
result.coeffRef(3,2) = -cofactor_4x4<MatrixType,2,3>(matrix);
|
|
|
|
|
result.coeffRef(0,1) = -cofactor_4x4<MatrixType,1,0>(matrix);
|
|
|
|
|
result.coeffRef(1,1) = cofactor_4x4<MatrixType,1,1>(matrix);
|
|
|
|
|
result.coeffRef(2,1) = -cofactor_4x4<MatrixType,1,2>(matrix);
|
|
|
|
|
result.coeffRef(3,1) = cofactor_4x4<MatrixType,1,3>(matrix);
|
|
|
|
|
result.coeffRef(0,3) = -cofactor_4x4<MatrixType,3,0>(matrix);
|
|
|
|
|
result.coeffRef(1,3) = cofactor_4x4<MatrixType,3,1>(matrix);
|
|
|
|
|
result.coeffRef(2,3) = -cofactor_4x4<MatrixType,3,2>(matrix);
|
|
|
|
|
result.coeffRef(3,3) = cofactor_4x4<MatrixType,3,3>(matrix);
|
2009-12-23 09:07:01 +01:00
|
|
|
result /= (matrix.col(0).cwiseProduct(result.row(0).transpose())).sum();
|
2008-04-14 08:20:24 +00:00
|
|
|
}
|
2008-07-15 23:56:17 +00:00
|
|
|
};
|
|
|
|
|
|
2009-10-26 11:18:23 -04:00
|
|
|
template<typename MatrixType, typename ResultType>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct compute_inverse<MatrixType, ResultType, 4>
|
|
|
|
|
: compute_inverse_size4<Architecture::Target, typename MatrixType::Scalar,
|
2009-12-14 22:47:14 -05:00
|
|
|
MatrixType, ResultType>
|
2009-06-29 20:47:37 +02:00
|
|
|
{
|
2008-07-15 23:56:17 +00:00
|
|
|
};
|
2008-04-14 08:20:24 +00:00
|
|
|
|
2009-10-06 09:26:28 -04:00
|
|
|
template<typename MatrixType, typename ResultType>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct compute_inverse_and_det_with_check<MatrixType, ResultType, 4>
|
2008-07-15 23:56:17 +00:00
|
|
|
{
|
2013-02-07 19:06:14 +01:00
|
|
|
EIGEN_DEVICE_FUNC
|
2009-10-26 12:30:29 -04:00
|
|
|
static inline void run(
|
|
|
|
|
const MatrixType& matrix,
|
|
|
|
|
const typename MatrixType::RealScalar& absDeterminantThreshold,
|
|
|
|
|
ResultType& inverse,
|
|
|
|
|
typename ResultType::Scalar& determinant,
|
|
|
|
|
bool& invertible
|
|
|
|
|
)
|
2008-04-14 08:20:24 +00:00
|
|
|
{
|
2012-11-06 15:25:50 +01:00
|
|
|
using std::abs;
|
2009-10-26 12:30:29 -04:00
|
|
|
determinant = matrix.determinant();
|
2010-10-25 10:15:22 -04:00
|
|
|
invertible = abs(determinant) > absDeterminantThreshold;
|
|
|
|
|
if(invertible) compute_inverse<MatrixType, ResultType>::run(matrix, inverse);
|
2008-04-14 08:20:24 +00:00
|
|
|
}
|
2008-07-15 23:56:17 +00:00
|
|
|
};
|
|
|
|
|
|
2009-10-26 12:30:29 -04:00
|
|
|
/*************************
|
|
|
|
|
*** MatrixBase methods ***
|
|
|
|
|
*************************/
|
2008-04-14 08:20:24 +00:00
|
|
|
|
2014-02-20 14:18:24 +01:00
|
|
|
#ifndef EIGEN_TEST_EVALUATORS
|
2009-10-26 14:16:50 -04:00
|
|
|
template<typename MatrixType>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct traits<inverse_impl<MatrixType> >
|
2009-10-26 14:16:50 -04:00
|
|
|
{
|
2010-02-20 15:53:57 +01:00
|
|
|
typedef typename MatrixType::PlainObject ReturnType;
|
2009-10-26 14:16:50 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename MatrixType>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct inverse_impl : public ReturnByValue<inverse_impl<MatrixType> >
|
2009-10-26 14:16:50 -04:00
|
|
|
{
|
2010-05-30 16:00:58 -04:00
|
|
|
typedef typename MatrixType::Index Index;
|
2010-10-26 16:47:01 +02:00
|
|
|
typedef typename internal::eval<MatrixType>::type MatrixTypeNested;
|
|
|
|
|
typedef typename remove_all<MatrixTypeNested>::type MatrixTypeNestedCleaned;
|
2012-02-03 23:18:26 +01:00
|
|
|
MatrixTypeNested m_matrix;
|
2009-10-26 14:16:50 -04:00
|
|
|
|
2013-02-07 19:06:14 +01:00
|
|
|
EIGEN_DEVICE_FUNC
|
2010-10-25 10:15:22 -04:00
|
|
|
inverse_impl(const MatrixType& matrix)
|
2009-10-26 14:16:50 -04:00
|
|
|
: m_matrix(matrix)
|
|
|
|
|
{}
|
|
|
|
|
|
2013-02-07 19:06:14 +01:00
|
|
|
EIGEN_DEVICE_FUNC inline Index rows() const { return m_matrix.rows(); }
|
|
|
|
|
EIGEN_DEVICE_FUNC inline Index cols() const { return m_matrix.cols(); }
|
2009-10-26 14:16:50 -04:00
|
|
|
|
2013-02-07 19:06:14 +01:00
|
|
|
template<typename Dest>
|
|
|
|
|
EIGEN_DEVICE_FUNC
|
|
|
|
|
inline void evalTo(Dest& dst) const
|
2009-10-26 14:16:50 -04:00
|
|
|
{
|
2010-06-12 13:24:02 +02:00
|
|
|
const int Size = EIGEN_PLAIN_ENUM_MIN(MatrixType::ColsAtCompileTime,Dest::ColsAtCompileTime);
|
2010-07-01 04:27:45 +02:00
|
|
|
EIGEN_ONLY_USED_FOR_DEBUG(Size);
|
2010-10-25 10:15:22 -04:00
|
|
|
eigen_assert(( (Size<=1) || (Size>4) || (extract_data(m_matrix)!=extract_data(dst)))
|
2010-06-02 10:12:13 +02:00
|
|
|
&& "Aliasing problem detected in inverse(), you need to do inverse().eval() here.");
|
|
|
|
|
|
2010-10-25 10:15:22 -04:00
|
|
|
compute_inverse<MatrixTypeNestedCleaned, Dest>::run(m_matrix, dst);
|
2009-10-26 14:16:50 -04:00
|
|
|
}
|
|
|
|
|
};
|
2014-02-20 14:18:24 +01:00
|
|
|
#endif
|
|
|
|
|
} // end namespace internal
|
|
|
|
|
|
|
|
|
|
#ifdef EIGEN_TEST_EVALUATORS
|
|
|
|
|
|
|
|
|
|
// TODO move the general declaration in Core, and rename this file DenseInverseImpl.h, or something like this...
|
|
|
|
|
|
|
|
|
|
template<typename XprType,typename StorageKind> class InverseImpl;
|
|
|
|
|
|
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
|
|
template<typename XprType>
|
|
|
|
|
struct traits<Inverse<XprType> >
|
|
|
|
|
: traits<typename XprType::PlainObject>
|
|
|
|
|
{
|
|
|
|
|
typedef typename XprType::PlainObject PlainObject;
|
|
|
|
|
typedef traits<PlainObject> BaseTraits;
|
|
|
|
|
enum {
|
|
|
|
|
Flags = BaseTraits::Flags & RowMajorBit,
|
|
|
|
|
CoeffReadCost = Dynamic
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // end namespace internal
|
|
|
|
|
|
|
|
|
|
/** \class Inverse
|
|
|
|
|
* \ingroup LU_Module
|
|
|
|
|
*
|
|
|
|
|
* \brief Expression of the inverse of another expression
|
|
|
|
|
*
|
|
|
|
|
* \tparam XprType the type of the expression we are taking the inverse
|
|
|
|
|
*
|
|
|
|
|
* This class represents an expression of A.inverse()
|
|
|
|
|
* and most of the time this is the only way it is used.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
template<typename XprType>
|
|
|
|
|
class Inverse : public InverseImpl<XprType,typename internal::traits<XprType>::StorageKind>
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
typedef typename XprType::Index Index;
|
|
|
|
|
typedef typename XprType::PlainObject PlainObject;
|
|
|
|
|
typedef typename internal::nested<XprType>::type XprTypeNested;
|
|
|
|
|
typedef typename internal::remove_all<XprTypeNested>::type XprTypeNestedCleaned;
|
|
|
|
|
|
|
|
|
|
Inverse(const XprType &xpr)
|
|
|
|
|
: m_xpr(xpr)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
EIGEN_DEVICE_FUNC Index rows() const { return m_xpr.rows(); }
|
|
|
|
|
EIGEN_DEVICE_FUNC Index cols() const { return m_xpr.cols(); }
|
|
|
|
|
|
|
|
|
|
EIGEN_DEVICE_FUNC const XprTypeNestedCleaned& nestedExpression() const { return m_xpr; }
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
XprTypeNested &m_xpr;
|
|
|
|
|
};
|
2009-10-26 14:16:50 -04:00
|
|
|
|
2014-02-20 14:18:24 +01:00
|
|
|
// Specialization of the Inverse expression for dense expressions
|
|
|
|
|
template<typename XprType>
|
|
|
|
|
class InverseImpl<XprType,Dense>
|
|
|
|
|
: public MatrixBase<Inverse<XprType> >
|
|
|
|
|
{
|
|
|
|
|
typedef Inverse<XprType> Derived;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
typedef MatrixBase<Derived> Base;
|
|
|
|
|
EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
Scalar coeff(Index row, Index col) const;
|
|
|
|
|
Scalar coeff(Index i) const;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
|
|
// Evaluator of Inverse -> eval into a temporary
|
|
|
|
|
template<typename XprType>
|
|
|
|
|
struct evaluator<Inverse<XprType> >
|
|
|
|
|
: public evaluator<typename Inverse<XprType>::PlainObject>::type
|
|
|
|
|
{
|
|
|
|
|
typedef Inverse<XprType> InverseType;
|
|
|
|
|
typedef typename InverseType::PlainObject PlainObject;
|
|
|
|
|
typedef typename evaluator<PlainObject>::type Base;
|
|
|
|
|
|
|
|
|
|
typedef evaluator type;
|
|
|
|
|
typedef evaluator nestedType;
|
|
|
|
|
|
|
|
|
|
evaluator(const InverseType& inv_xpr)
|
|
|
|
|
: m_result(inv_xpr.rows(), inv_xpr.cols())
|
|
|
|
|
{
|
|
|
|
|
::new (static_cast<Base*>(this)) Base(m_result);
|
|
|
|
|
|
|
|
|
|
typedef typename internal::nested_eval<XprType,XprType::ColsAtCompileTime>::type ActualXprType;
|
|
|
|
|
typedef typename internal::remove_all<ActualXprType>::type ActualXprTypeCleanded;
|
|
|
|
|
|
|
|
|
|
ActualXprType actual_xpr(inv_xpr.nestedExpression());
|
|
|
|
|
|
|
|
|
|
compute_inverse<ActualXprTypeCleanded, PlainObject>::run(actual_xpr, m_result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
PlainObject m_result;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Specialization for "dst = xpr.inverse()"
|
|
|
|
|
// NOTE we need to specialize it for Dense2Dense to avoid ambiguous specialization error and a Sparse2Sparse specialization must exist somewhere
|
|
|
|
|
template<typename DstXprType, typename XprType, typename Scalar>
|
|
|
|
|
struct Assignment<DstXprType, Inverse<XprType>, internal::assign_op<Scalar>, Dense2Dense, Scalar>
|
|
|
|
|
{
|
|
|
|
|
typedef Inverse<XprType> SrcXprType;
|
|
|
|
|
static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op<Scalar> &)
|
|
|
|
|
{
|
|
|
|
|
// FIXME shall we resize dst here?
|
|
|
|
|
const int Size = EIGEN_PLAIN_ENUM_MIN(XprType::ColsAtCompileTime,DstXprType::ColsAtCompileTime);
|
|
|
|
|
EIGEN_ONLY_USED_FOR_DEBUG(Size);
|
|
|
|
|
eigen_assert(( (Size<=1) || (Size>4) || (extract_data(src.nestedExpression())!=extract_data(dst)))
|
|
|
|
|
&& "Aliasing problem detected in inverse(), you need to do inverse().eval() here.");
|
|
|
|
|
|
|
|
|
|
typedef typename internal::nested_eval<XprType,XprType::ColsAtCompileTime>::type ActualXprType;
|
|
|
|
|
typedef typename internal::remove_all<ActualXprType>::type ActualXprTypeCleanded;
|
|
|
|
|
|
|
|
|
|
ActualXprType actual_xpr(src.nestedExpression());
|
|
|
|
|
|
|
|
|
|
compute_inverse<ActualXprTypeCleanded, DstXprType>::run(actual_xpr, dst);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2010-10-25 10:15:22 -04:00
|
|
|
} // end namespace internal
|
|
|
|
|
|
2014-02-20 14:18:24 +01:00
|
|
|
#endif
|
|
|
|
|
|
2008-06-02 20:08:37 +00:00
|
|
|
/** \lu_module
|
|
|
|
|
*
|
2008-07-15 23:56:17 +00:00
|
|
|
* \returns the matrix inverse of this matrix.
|
|
|
|
|
*
|
2009-12-14 22:47:14 -05:00
|
|
|
* For small fixed sizes up to 4x4, this method uses cofactors.
|
2009-10-28 18:19:29 -04:00
|
|
|
* In the general case, this method uses class PartialPivLU.
|
2008-07-15 23:56:17 +00:00
|
|
|
*
|
2009-10-26 11:18:23 -04:00
|
|
|
* \note This matrix must be invertible, otherwise the result is undefined. If you need an
|
|
|
|
|
* invertibility check, do the following:
|
|
|
|
|
* \li for fixed sizes up to 4x4, use computeInverseAndDetWithCheck().
|
2009-10-28 18:19:29 -04:00
|
|
|
* \li for the general case, use class FullPivLU.
|
2008-04-14 08:20:24 +00:00
|
|
|
*
|
2008-07-15 23:56:17 +00:00
|
|
|
* Example: \include MatrixBase_inverse.cpp
|
|
|
|
|
* Output: \verbinclude MatrixBase_inverse.out
|
2008-04-14 08:20:24 +00:00
|
|
|
*
|
2009-10-26 11:18:23 -04:00
|
|
|
* \sa computeInverseAndDetWithCheck()
|
2008-04-14 08:20:24 +00:00
|
|
|
*/
|
2014-02-20 14:18:24 +01:00
|
|
|
#ifdef EIGEN_TEST_EVALUATORS
|
|
|
|
|
template<typename Derived>
|
|
|
|
|
inline const Inverse<Derived> MatrixBase<Derived>::inverse() const
|
|
|
|
|
{
|
|
|
|
|
EIGEN_STATIC_ASSERT(!NumTraits<Scalar>::IsInteger,THIS_FUNCTION_IS_NOT_FOR_INTEGER_NUMERIC_TYPES)
|
|
|
|
|
eigen_assert(rows() == cols());
|
|
|
|
|
return Inverse<Derived>(derived());
|
|
|
|
|
}
|
|
|
|
|
#else
|
2008-04-14 08:20:24 +00:00
|
|
|
template<typename Derived>
|
2010-10-25 10:15:22 -04:00
|
|
|
inline const internal::inverse_impl<Derived> MatrixBase<Derived>::inverse() const
|
2008-04-14 08:20:24 +00:00
|
|
|
{
|
2010-04-28 18:51:38 -04:00
|
|
|
EIGEN_STATIC_ASSERT(!NumTraits<Scalar>::IsInteger,THIS_FUNCTION_IS_NOT_FOR_INTEGER_NUMERIC_TYPES)
|
2010-10-25 10:15:22 -04:00
|
|
|
eigen_assert(rows() == cols());
|
|
|
|
|
return internal::inverse_impl<Derived>(derived());
|
2008-04-14 08:20:24 +00:00
|
|
|
}
|
2014-02-20 14:18:24 +01:00
|
|
|
#endif
|
2008-04-14 08:20:24 +00:00
|
|
|
|
2009-06-29 20:47:37 +02:00
|
|
|
/** \lu_module
|
|
|
|
|
*
|
2009-10-26 11:18:23 -04:00
|
|
|
* Computation of matrix inverse and determinant, with invertibility check.
|
2009-06-29 20:47:37 +02:00
|
|
|
*
|
2009-10-26 11:18:23 -04:00
|
|
|
* This is only for fixed-size square matrices of size up to 4x4.
|
2009-06-29 20:47:37 +02:00
|
|
|
*
|
2009-10-26 11:18:23 -04:00
|
|
|
* \param inverse Reference to the matrix in which to store the inverse.
|
2013-06-17 14:28:42 +00:00
|
|
|
* \param determinant Reference to the variable in which to store the determinant.
|
2009-10-26 11:18:23 -04:00
|
|
|
* \param invertible Reference to the bool variable in which to store whether the matrix is invertible.
|
|
|
|
|
* \param absDeterminantThreshold Optional parameter controlling the invertibility check.
|
|
|
|
|
* The matrix will be declared invertible if the absolute value of its
|
|
|
|
|
* determinant is greater than this threshold.
|
2009-06-29 20:47:37 +02:00
|
|
|
*
|
2009-10-26 14:37:43 -04:00
|
|
|
* Example: \include MatrixBase_computeInverseAndDetWithCheck.cpp
|
|
|
|
|
* Output: \verbinclude MatrixBase_computeInverseAndDetWithCheck.out
|
|
|
|
|
*
|
2009-10-26 14:16:50 -04:00
|
|
|
* \sa inverse(), computeInverseWithCheck()
|
2009-06-29 20:47:37 +02:00
|
|
|
*/
|
|
|
|
|
template<typename Derived>
|
2009-10-06 09:26:28 -04:00
|
|
|
template<typename ResultType>
|
2009-10-26 11:18:23 -04:00
|
|
|
inline void MatrixBase<Derived>::computeInverseAndDetWithCheck(
|
|
|
|
|
ResultType& inverse,
|
|
|
|
|
typename ResultType::Scalar& determinant,
|
|
|
|
|
bool& invertible,
|
|
|
|
|
const RealScalar& absDeterminantThreshold
|
|
|
|
|
) const
|
2009-06-29 20:47:37 +02:00
|
|
|
{
|
2009-10-26 11:18:23 -04:00
|
|
|
// i'd love to put some static assertions there, but SFINAE means that they have no effect...
|
2010-10-25 10:15:22 -04:00
|
|
|
eigen_assert(rows() == cols());
|
2010-06-01 09:01:39 -04:00
|
|
|
// for 2x2, it's worth giving a chance to avoid evaluating.
|
|
|
|
|
// for larger sizes, evaluating has negligible cost and limits code size.
|
2010-10-25 22:13:49 +02:00
|
|
|
typedef typename internal::conditional<
|
2010-06-01 09:01:39 -04:00
|
|
|
RowsAtCompileTime == 2,
|
2010-10-26 16:47:01 +02:00
|
|
|
typename internal::remove_all<typename internal::nested<Derived, 2>::type>::type,
|
2010-06-01 09:01:39 -04:00
|
|
|
PlainObject
|
2010-10-25 22:13:49 +02:00
|
|
|
>::type MatrixType;
|
2010-10-25 10:15:22 -04:00
|
|
|
internal::compute_inverse_and_det_with_check<MatrixType, ResultType>::run
|
2009-10-26 11:18:23 -04:00
|
|
|
(derived(), absDeterminantThreshold, inverse, determinant, invertible);
|
2009-06-29 20:47:37 +02:00
|
|
|
}
|
|
|
|
|
|
2009-10-26 14:16:50 -04:00
|
|
|
/** \lu_module
|
|
|
|
|
*
|
|
|
|
|
* Computation of matrix inverse, with invertibility check.
|
|
|
|
|
*
|
|
|
|
|
* This is only for fixed-size square matrices of size up to 4x4.
|
|
|
|
|
*
|
|
|
|
|
* \param inverse Reference to the matrix in which to store the inverse.
|
|
|
|
|
* \param invertible Reference to the bool variable in which to store whether the matrix is invertible.
|
|
|
|
|
* \param absDeterminantThreshold Optional parameter controlling the invertibility check.
|
|
|
|
|
* The matrix will be declared invertible if the absolute value of its
|
|
|
|
|
* determinant is greater than this threshold.
|
|
|
|
|
*
|
2009-10-26 14:37:43 -04:00
|
|
|
* Example: \include MatrixBase_computeInverseWithCheck.cpp
|
|
|
|
|
* Output: \verbinclude MatrixBase_computeInverseWithCheck.out
|
|
|
|
|
*
|
2009-10-26 14:16:50 -04:00
|
|
|
* \sa inverse(), computeInverseAndDetWithCheck()
|
|
|
|
|
*/
|
|
|
|
|
template<typename Derived>
|
|
|
|
|
template<typename ResultType>
|
|
|
|
|
inline void MatrixBase<Derived>::computeInverseWithCheck(
|
|
|
|
|
ResultType& inverse,
|
|
|
|
|
bool& invertible,
|
|
|
|
|
const RealScalar& absDeterminantThreshold
|
|
|
|
|
) const
|
|
|
|
|
{
|
|
|
|
|
RealScalar determinant;
|
|
|
|
|
// i'd love to put some static assertions there, but SFINAE means that they have no effect...
|
2010-10-25 10:15:22 -04:00
|
|
|
eigen_assert(rows() == cols());
|
2009-10-26 14:16:50 -04:00
|
|
|
computeInverseAndDetWithCheck(inverse,determinant,invertible,absDeterminantThreshold);
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-15 11:06:28 +01:00
|
|
|
} // end namespace Eigen
|
|
|
|
|
|
2008-04-14 08:20:24 +00:00
|
|
|
#endif // EIGEN_INVERSE_H
|