11#ifndef EIGEN_SPARSEINVERSE_H
12#define EIGEN_SPARSEINVERSE_H
15#include "./InternalHeaderCheck.h"
17#include "../../../../Eigen/Sparse"
18#include "../../../../Eigen/SparseLU"
35template <
typename Scalar>
42 Scalar value()
const {
return _sum; }
44 void operator+=(Scalar increment) {
45 const Scalar correctedIncrement = increment + _correction;
46 const Scalar previousSum = _sum;
47 _sum += correctedIncrement;
48 _correction = correctedIncrement - (_sum - previousSum);
51template <
typename Scalar, Index W
idth = 16>
63 Scalar value()
const {
return _block.topRows(_blockUsed).sum() + _totalSum.value(); }
65 void operator+=(Scalar increment) {
66 _block(_blockUsed++, 0) = increment;
67 if (_blockUsed == Width) {
68 _totalSum += _block.sum();
81template <
typename Derived,
typename OtherDerived>
83 typedef typename Derived::Scalar Scalar;
84 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
85 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
86 EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(Derived, OtherDerived)
87 static_assert(std::is_same<Scalar, typename OtherDerived::Scalar>::value,
"mismatched types");
89 internal::evaluator<Derived> thisEval(A.
derived());
90 typename Derived::ReverseInnerIterator i(thisEval, 0);
92 internal::evaluator<OtherDerived> otherEval(other.
derived());
93 typename OtherDerived::ReverseInnerIterator j(otherEval, 0);
97 if (i.index() == j.index()) {
98 res += numext::conj(i.value()) * j.value();
101 }
else if (i.index() > j.index())
128template <
typename Scalar>
158 const MatrixType&
inverse()
const {
return _result; }
166 return MatrixType(0, 0);
173 RowMatrixType DU = slu.
matrixU().toSparse();
174 invD = DU.
diagonal().cwiseInverse();
175 Upper = invD.asDiagonal() * DU.template triangularView<StrictlyUpper>();
177 MatrixType
Lower = slu.
matrixL().toSparse().template triangularView<StrictlyLower>();
187 const MatrixType&
Lower) {
190 MatrixType colInv =
Lower.transpose().template triangularView<UnitUpper>();
191 colInv +=
Upper.transpose();
194 RowMatrixType rowInv =
Upper.transpose().template triangularView<UnitLower>();
195 rowInv +=
Lower.transpose();
199 for (Index recurseLevel =
Upper.cols() - 1; recurseLevel >= 0; recurseLevel--) {
200 const auto& col =
Lower.col(recurseLevel);
201 const auto& row =
Upper.row(recurseLevel);
204 typename MatrixType::ReverseInnerIterator colIter(colInv, recurseLevel);
205 for (; recurseLevel < colIter.index(); --colIter) {
206 const Scalar element = -
accurateDot(col, rowInv.
row(colIter.index()));
207 colIter.valueRef() = element;
208 rowInv.
coeffRef(colIter.index(), recurseLevel) = element;
212 typename RowMatrixType::ReverseInnerIterator rowIter(rowInv, recurseLevel);
213 for (; recurseLevel < rowIter.index(); --rowIter) {
214 const Scalar element = -
accurateDot(row, colInv.
col(rowIter.index()));
215 rowIter.valueRef() = element;
216 colInv.
coeffRef(recurseLevel, rowIter.index()) = element;
220 const Scalar diag = inverseDiagonal(recurseLevel) -
accurateDot(row, colInv.
col(recurseLevel));
221 rowIter.valueRef() = diag;
222 colIter.valueRef() = diag;
Kahan algorithm based accumulator.
Definition SparseInverse.h:36
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 component...
Definition SparseInverse.h:186
SparseInverse(const SparseLU< MatrixType > &slu)
This Constructor is for if you already have a factored SparseLU and would like to use it to calculate...
Definition SparseInverse.h:143
static MatrixType computeInverse(const SparseLU< MatrixType > &slu)
Internal function to calculate the sparse inverse in a functional way.
Definition SparseInverse.h:164
const MatrixType & inverse() const
return the already-calculated sparse inverse, or a 0x0 matrix if it could not be computed
Definition SparseInverse.h:158
SparseInverse & compute(const SparseMatrix< Scalar > &A)
Calculate the sparse inverse from a given sparse input.
Definition SparseInverse.h:148
SparseLUMatrixUReturnType< SCMatrix, Map< SparseMatrix< Scalar, ColMajor, StorageIndex > > > matrixU() const
SparseLUMatrixLReturnType< SCMatrix > matrixL() const
void compute(const MatrixType &matrix)
ComputationInfo info() const
const PermutationType & colsPermutation() const
const PermutationType & rowsPermutation() const
constexpr RowXpr row(Index i)
constexpr ColXpr col(Index i)
Scalar & coeffRef(Index row, Index col)
DiagonalReturnType diagonal()
Namespace containing all symbols from the Eigen library.
Derived::Scalar accurateDot(const SparseMatrixBase< Derived > &A, const SparseMatrixBase< OtherDerived > &other)
computes an accurate dot product on two sparse vectors
Definition SparseInverse.h:82
constexpr Derived & derived()