Eigen::SparseInverse< Scalar > Class Template Reference

calculate sparse subset of inverse of sparse matrix More...

Public Types

typedef SparseMatrix< Scalar, ColMajorMatrixType
 
typedef SparseMatrix< Scalar, RowMajorRowMatrixType
 

Public Member Functions

SparseInversecompute (const SparseMatrix< Scalar > &A)
 Calculate the sparse inverse from a given sparse input. More...
 
const MatrixTypeinverse () const
 return the already-calculated sparse inverse, or a 0x0 matrix if it could not be computed More...
 
 SparseInverse ()
 
 SparseInverse (const SparseLU< MatrixType > &slu)
 This Constructor is for if you already have a factored SparseLU and would like to use it to calculate a sparse inverse. More...
 

Static Public Member Functions

static MatrixType computeInverse (const RowMatrixType &Upper, const Matrix< Scalar, Dynamic, 1 > &inverseDiagonal, const MatrixType &Lower)
 Internal function to calculate the inverse from strictly upper, diagonal and strictly lower components. More...
 
static MatrixType computeInverse (const SparseLU< MatrixType > &slu)
 Internal function to calculate the sparse inverse in a functional way. More...
 

Private Attributes

MatrixType _result
 

Detailed Description

template<typename Scalar>
class Eigen::SparseInverse< Scalar >

calculate sparse subset of inverse of sparse matrix

This class returns a sparse subset of the inverse of the input matrix. The nonzeros correspond to the nonzeros of the input, plus any additional elements required due to fill-in of the internal LU factorization. This is is minimized via a applying a fill-reducing permutation as part of the LU factorization.

If there are specific entries of the input matrix which you need inverse values for, which are zero for the input, you need to insert entries into the input sparse matrix for them to be calculated.

Due to the sensitive nature of matrix inversion, particularly on large matrices which are made possible via sparsity, high accuracy dot products based on Kahan summation are used to reduce numerical error. If you still encounter numerical errors you may with to equilibrate your matrix before calculating the inverse, as well as making sure it is actually full rank.

Definition at line 127 of file SparseInverse.h.

Member Typedef Documentation

◆ MatrixType

template<typename Scalar >
typedef SparseMatrix<Scalar, ColMajor> Eigen::SparseInverse< Scalar >::MatrixType

Definition at line 129 of file SparseInverse.h.

◆ RowMatrixType

template<typename Scalar >
typedef SparseMatrix<Scalar, RowMajor> Eigen::SparseInverse< Scalar >::RowMatrixType

Definition at line 130 of file SparseInverse.h.

Constructor & Destructor Documentation

◆ SparseInverse() [1/2]

template<typename Scalar >
Eigen::SparseInverse< Scalar >::SparseInverse ( )
inline

Definition at line 132 of file SparseInverse.h.

132 {}

◆ SparseInverse() [2/2]

template<typename Scalar >
Eigen::SparseInverse< Scalar >::SparseInverse ( const SparseLU< MatrixType > &  slu)
inline

This Constructor is for if you already have a factored SparseLU and would like to use it to calculate a sparse inverse.

Just call this constructor with your already factored SparseLU class and you can directly call the .inverse() method to get the result.

Definition at line 141 of file SparseInverse.h.

141 { _result = computeInverse(slu); }
static MatrixType computeInverse(const SparseLU< MatrixType > &slu)
Internal function to calculate the sparse inverse in a functional way.

Member Function Documentation

◆ compute()

template<typename Scalar >
SparseInverse& Eigen::SparseInverse< Scalar >::compute ( const SparseMatrix< Scalar > &  A)
inline

Calculate the sparse inverse from a given sparse input.

Definition at line 146 of file SparseInverse.h.

146  {
147  SparseLU<MatrixType> slu;
148  slu.compute(A);
149  _result = computeInverse(slu);
150  return *this;
151  }

◆ computeInverse() [1/2]

template<typename Scalar >
static MatrixType Eigen::SparseInverse< Scalar >::computeInverse ( const RowMatrixType Upper,
const Matrix< Scalar, Dynamic, 1 > &  inverseDiagonal,
const MatrixType Lower 
)
inlinestatic

Internal function to calculate the inverse from strictly upper, diagonal and strictly lower components.

Definition at line 184 of file SparseInverse.h.

185  {
186  // Calculate the 'minimal set', which is the nonzeros of (L+U).transpose()
187  // It could be zeroed, but we will overwrite all non-zeros anyways.
188  MatrixType colInv = Lower.transpose().template triangularView<UnitUpper>();
189  colInv += Upper.transpose();
190 
191  // We also need rowmajor representation in order to do efficient row-wise dot products
192  RowMatrixType rowInv = Upper.transpose().template triangularView<UnitLower>();
193  rowInv += Lower.transpose();
194 
195  // Use the Takahashi algorithm to build the supporting elements of the inverse
196  // upwards and to the left, from the bottom right element, 1 col/row at a time
197  for (Index recurseLevel = Upper.cols() - 1; recurseLevel >= 0; recurseLevel--) {
198  const auto& col = Lower.col(recurseLevel);
199  const auto& row = Upper.row(recurseLevel);
200 
201  // Calculate the inverse values for the nonzeros in this column
202  typename MatrixType::ReverseInnerIterator colIter(colInv, recurseLevel);
203  for (; recurseLevel < colIter.index(); --colIter) {
204  const Scalar element = -accurateDot(col, rowInv.row(colIter.index()));
205  colIter.valueRef() = element;
206  rowInv.coeffRef(colIter.index(), recurseLevel) = element;
207  }
208 
209  // Calculate the inverse values for the nonzeros in this row
210  typename RowMatrixType::ReverseInnerIterator rowIter(rowInv, recurseLevel);
211  for (; recurseLevel < rowIter.index(); --rowIter) {
212  const Scalar element = -accurateDot(row, colInv.col(rowIter.index()));
213  rowIter.valueRef() = element;
214  colInv.coeffRef(recurseLevel, rowIter.index()) = element;
215  }
216 
217  // And finally the diagonal, which corresponds to both row and col iterator now
218  const Scalar diag = inverseDiagonal(recurseLevel) - accurateDot(row, colInv.col(recurseLevel));
219  rowIter.valueRef() = diag;
220  colIter.valueRef() = diag;
221  }
222 
223  return colInv;
224  }
RowXpr row(Index i) const
ColXpr col(Index i) const
Matrix< float, 1, Dynamic > MatrixType
SparseMatrix< Scalar, RowMajor > RowMatrixType
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
Derived::Scalar accurateDot(const SparseMatrixBase< Derived > &A, const SparseMatrixBase< OtherDerived > &other)
computes an accurate dot product on two sparse vectors
Definition: SparseInverse.h:80

◆ computeInverse() [2/2]

template<typename Scalar >
static MatrixType Eigen::SparseInverse< Scalar >::computeInverse ( const SparseLU< MatrixType > &  slu)
inlinestatic

Internal function to calculate the sparse inverse in a functional way.

Returns
A sparse inverse representation, or, if the decomposition didn't complete, a 0x0 matrix.

Definition at line 162 of file SparseInverse.h.

162  {
163  if (slu.info() != Success) {
164  return MatrixType(0, 0);
165  }
166 
167  // Extract from SparseLU and decompose into L, inverse D and U terms
170  {
171  RowMatrixType DU = slu.matrixU().toSparse();
172  invD = DU.diagonal().cwiseInverse();
173  Upper = (invD.asDiagonal() * DU).template triangularView<StrictlyUpper>();
174  }
175  MatrixType Lower = slu.matrixL().toSparse().template triangularView<StrictlyLower>();
176 
177  // Compute the inverse and reapply the permutation matrix from the LU decomposition
178  return slu.colsPermutation().transpose() * computeInverse(Upper, invD, Lower) * slu.rowsPermutation();
179  }
SparseMatrix< Scalar, ColMajor > MatrixType

◆ inverse()

template<typename Scalar >
const MatrixType& Eigen::SparseInverse< Scalar >::inverse ( ) const
inline

return the already-calculated sparse inverse, or a 0x0 matrix if it could not be computed

Definition at line 156 of file SparseInverse.h.

156 { return _result; }

Member Data Documentation

◆ _result

template<typename Scalar >
MatrixType Eigen::SparseInverse< Scalar >::_result
private

Definition at line 227 of file SparseInverse.h.


The documentation for this class was generated from the following file: