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();
76template <
bool Conjugate,
typename Derived,
typename OtherDerived>
77typename Derived::Scalar accurate_sparse_dot(
const SparseMatrixBase<Derived>& A,
78 const SparseMatrixBase<OtherDerived>& other) {
79 using Scalar =
typename Derived::Scalar;
80 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
81 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
82 EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(Derived, OtherDerived)
83 static_assert(std::is_same<Scalar, typename OtherDerived::Scalar>::value,
"mismatched types");
85 internal::evaluator<Derived> thisEval(A.derived());
86 typename Derived::ReverseInnerIterator i(thisEval, 0);
88 internal::evaluator<OtherDerived> otherEval(other.derived());
89 typename OtherDerived::ReverseInnerIterator j(otherEval, 0);
93 if (i.index() == j.index()) {
94 res += (Conjugate ? numext::conj(i.value()) : i.value()) * j.value();
97 }
else if (i.index() > j.index())
114template <
typename Derived,
typename OtherDerived>
116 return internal::accurate_sparse_dot<true>(A, other);
138template <
typename Scalar>
168 const MatrixType&
inverse()
const {
return _result; }
176 return MatrixType(0, 0);
183 RowMatrixType DU = slu.
matrixU().toSparse();
184 invD = DU.
diagonal().cwiseInverse();
185 Upper = invD.asDiagonal() * DU.template triangularView<StrictlyUpper>();
187 MatrixType
Lower = slu.
matrixL().toSparse().template triangularView<StrictlyLower>();
197 const MatrixType&
Lower) {
200 MatrixType colInv =
Lower.transpose().template triangularView<UnitUpper>();
201 colInv +=
Upper.transpose();
204 RowMatrixType rowInv =
Upper.transpose().template triangularView<UnitLower>();
205 rowInv +=
Lower.transpose();
209 for (Index recurseLevel =
Upper.cols() - 1; recurseLevel >= 0; recurseLevel--) {
210 const auto& col =
Lower.col(recurseLevel);
211 const auto& row =
Upper.row(recurseLevel);
214 typename MatrixType::ReverseInnerIterator colIter(colInv, recurseLevel);
215 for (; recurseLevel < colIter.index(); --colIter) {
216 const Scalar element = -internal::accurate_sparse_dot<false>(col, rowInv.
row(colIter.index()));
217 colIter.valueRef() = element;
218 rowInv.
coeffRef(colIter.index(), recurseLevel) = element;
222 typename RowMatrixType::ReverseInnerIterator rowIter(rowInv, recurseLevel);
223 for (; recurseLevel < rowIter.index(); --rowIter) {
224 const Scalar element = -internal::accurate_sparse_dot<false>(row, colInv.
col(rowIter.index()));
225 rowIter.valueRef() = element;
226 colInv.
coeffRef(recurseLevel, rowIter.index()) = element;
231 inverseDiagonal(recurseLevel) - internal::accurate_sparse_dot<false>(row, colInv.
col(recurseLevel));
232 rowIter.valueRef() = diag;
233 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:196
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:153
static MatrixType computeInverse(const SparseLU< MatrixType > &slu)
Internal function to calculate the sparse inverse in a functional way.
Definition SparseInverse.h:174
const MatrixType & inverse() const
return the already-calculated sparse inverse, or a 0x0 matrix if it could not be computed
Definition SparseInverse.h:168
SparseInverse & compute(const SparseMatrix< Scalar > &A)
Calculate the sparse inverse from a given sparse input.
Definition SparseInverse.h:158
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:115