mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
* added an in-place version of inverseProduct which
might be twice faster fot small fixed size matrix * added a sparse triangular solver (sparse version of inverseProduct) * various other improvements in the Sparse module
This commit is contained in:
@@ -25,6 +25,53 @@
|
||||
#ifndef EIGEN_INVERSEPRODUCT_H
|
||||
#define EIGEN_INVERSEPRODUCT_H
|
||||
|
||||
|
||||
/** "in-place" version of MatrixBase::inverseProduct() where the result is written in \a other
|
||||
*
|
||||
* \sa inverseProduct()
|
||||
*/
|
||||
template<typename Derived>
|
||||
template<typename OtherDerived>
|
||||
void MatrixBase<Derived>::inverseProductInPlace(MatrixBase<OtherDerived>& other) const
|
||||
{
|
||||
ei_assert(cols() == other.rows());
|
||||
ei_assert(!(Flags & ZeroDiagBit));
|
||||
ei_assert(Flags & (UpperTriangularBit|LowerTriangularBit));
|
||||
|
||||
for(int c=0 ; c<other.cols() ; ++c)
|
||||
{
|
||||
if(Flags & LowerTriangularBit)
|
||||
{
|
||||
// forward substitution
|
||||
if(!(Flags & UnitDiagBit))
|
||||
other.coeffRef(0,c) = other.coeff(0,c)/coeff(0, 0);
|
||||
for(int i=1; i<rows(); ++i)
|
||||
{
|
||||
Scalar tmp = other.coeff(i,c) - ((this->row(i).start(i)) * other.col(c).start(i)).coeff(0,0);
|
||||
if (Flags & UnitDiagBit)
|
||||
other.coeffRef(i,c) = tmp;
|
||||
else
|
||||
other.coeffRef(i,c) = tmp/coeff(i,i);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// backward substitution
|
||||
if(!(Flags & UnitDiagBit))
|
||||
other.coeffRef(cols()-1,c) = other.coeff(cols()-1, c)/coeff(rows()-1, cols()-1);
|
||||
for(int i=rows()-2 ; i>=0 ; --i)
|
||||
{
|
||||
Scalar tmp = other.coeff(i,c)
|
||||
- ((this->row(i).end(cols()-i-1)) * other.col(c).end(cols()-i-1)).coeff(0,0);
|
||||
if (Flags & UnitDiagBit)
|
||||
other.coeffRef(i,c) = tmp;
|
||||
else
|
||||
other.coeffRef(i,c) = tmp/coeff(i,i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** \returns the product of the inverse of \c *this with \a other.
|
||||
*
|
||||
* This function computes the inverse-matrix matrix product inverse(\c*this) * \a other
|
||||
@@ -43,48 +90,8 @@ template<typename Derived>
|
||||
template<typename OtherDerived>
|
||||
typename OtherDerived::Eval MatrixBase<Derived>::inverseProduct(const MatrixBase<OtherDerived>& other) const
|
||||
{
|
||||
assert(cols() == other.rows());
|
||||
assert(!(Flags & ZeroDiagBit));
|
||||
assert(Flags & (UpperTriangularBit|LowerTriangularBit));
|
||||
|
||||
typename OtherDerived::Eval res(other.rows(), other.cols());
|
||||
|
||||
for(int c=0 ; c<other.cols() ; ++c)
|
||||
{
|
||||
if(Flags & LowerTriangularBit)
|
||||
{
|
||||
// forward substitution
|
||||
if(Flags & UnitDiagBit)
|
||||
res.coeffRef(0,c) = other.coeff(0,c);
|
||||
else
|
||||
res.coeffRef(0,c) = other.coeff(0,c)/coeff(0, 0);
|
||||
for(int i=1; i<rows(); ++i)
|
||||
{
|
||||
Scalar tmp = other.coeff(i,c) - ((this->row(i).start(i)) * res.col(c).start(i)).coeff(0,0);
|
||||
if (Flags & UnitDiagBit)
|
||||
res.coeffRef(i,c) = tmp;
|
||||
else
|
||||
res.coeffRef(i,c) = tmp/coeff(i,i);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// backward substitution
|
||||
if(Flags & UnitDiagBit)
|
||||
res.coeffRef(cols()-1,c) = other.coeff(cols()-1,c);
|
||||
else
|
||||
res.coeffRef(cols()-1,c) = other.coeff(cols()-1, c)/coeff(rows()-1, cols()-1);
|
||||
for(int i=rows()-2 ; i>=0 ; --i)
|
||||
{
|
||||
Scalar tmp = other.coeff(i,c)
|
||||
- ((this->row(i).end(cols()-i-1)) * res.col(c).end(cols()-i-1)).coeff(0,0);
|
||||
if (Flags & UnitDiagBit)
|
||||
res.coeffRef(i,c) = tmp;
|
||||
else
|
||||
res.coeffRef(i,c) = tmp/coeff(i,i);
|
||||
}
|
||||
}
|
||||
}
|
||||
typename OtherDerived::Eval res(other);
|
||||
inverseProductInPlace(res);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
@@ -296,6 +296,9 @@ template<typename Derived> class MatrixBase
|
||||
template<typename OtherDerived>
|
||||
typename OtherDerived::Eval inverseProduct(const MatrixBase<OtherDerived>& other) const;
|
||||
|
||||
template<typename OtherDerived>
|
||||
void inverseProductInPlace(MatrixBase<OtherDerived>& other) const;
|
||||
|
||||
|
||||
template<typename OtherDerived>
|
||||
Scalar dot(const MatrixBase<OtherDerived>& other) const;
|
||||
|
||||
@@ -191,6 +191,11 @@ enum {
|
||||
Sparse = SparseBit
|
||||
};
|
||||
|
||||
enum {
|
||||
ColMajor = 0,
|
||||
RowMajor = RowMajorBit
|
||||
};
|
||||
|
||||
const int FullyCoherentAccessPattern = 0x1;
|
||||
const int InnerCoherentAccessPattern = 0x2 | FullyCoherentAccessPattern;
|
||||
const int OuterCoherentAccessPattern = 0x4 | InnerCoherentAccessPattern;
|
||||
|
||||
Reference in New Issue
Block a user