18#ifndef EIGEN_SPARSE_MATRIX_HPP
19#define EIGEN_SPARSE_MATRIX_HPP
25#include <external/Eigen/Sparse>
26#include <external/Eigen/SparseLU>
27#include <external/Eigen/IterativeLinearSolvers>
28#include <external/Eigen/SVD>
29#include <external/EigenUnsupported/Eigen/IterativeSolvers>
50template <
typename T,
int StorageOrder = Eigen::ColMajor>
54 using MatrixType = Eigen::SparseMatrix<T, StorageOrder>;
71 triplets_.reserve(std::accumulate(
nnz.begin(),
nnz.end(), 0));
99 matrix_->reserve(
nnz);
113 triplets_.shrink_to_fit();
115 matrix_->setFromTriplets(
118 [] (
const T&,
const T &
b) { return b; }
121 matrix_->setFromTriplets(triplets_.begin(), triplets_.end());
136 return matrix_->coeff(
row,
col);
151 triplets_.push_back(Eigen::Triplet<T> (
row,
col,
a));
152 insert_mode_on_ =
true;
166 matrix_->coeffRef(
row,
col) =
a;
167 matrix_->makeCompressed();
184 triplets_.push_back(Eigen::Triplet<T> (
row,
col,
a));
185 insert_mode_on_ =
false;
199 matrix_->coeffRef(
row,
col) +=
a;
210 for (
Index kk = 0;
kk < matrix_->outerSize(); ++
kk)
211 for (
typename MatrixType::InnerIterator
it((*matrix_),
kk);
it; ++
it)
212 it.valueRef() =
a *
it.value();
213 matrix_->makeCompressed();
223 solver_.compute((*matrix_));
224 if (solver_.info() != Eigen::Success)
225 throw std::runtime_error(
"Matrix decomposition failed.");
246 xd.raw_matrix() = solver_.solve(
bd.raw_matrix());
248 if (solver_.info() != Eigen::Success)
249 throw std::runtime_error(
"Matrix solve failed.");
275 for (
typename MatrixType::InnerIterator
it (
xd.raw_matrix(),
kk);
it; ++
it)
278 matrix_->insertFromTriplets(
281 [] (
const T&,
const T &
b) {
return b; }
309 for (
typename MatrixType::InnerIterator
it (
xd.raw_matrix(),
kk);
it; ++
it)
345 virtual const T*
data()
const override
346 {
return matrix_->valuePtr(); };
354 {
return matrix_->valuePtr(); };
359 std::vector<Eigen::Triplet<T>> triplets_;
360 bool insert_mode_on_ =
true;
361 bool assembled_ =
false;
363 Eigen::SparseLU<MatrixType> solver_;
364 bool factorized_ =
false;
Base class for Eigen-based matrices.
MatrixType & raw_matrix()
Returns a writable reference to the underlying raw matrix - use with caution.
Eigen-based sparse matrix wrapper.
void set_block(const MatrixBase< T > &x, Index row_start, Index col_start, const T &a=T(1)) override
Sets a block of values in this matrix to the values of a given matrix, starting at a given position.
void set_value(Index row, Index col, const T &a) override
Sets the matrix value at the specified row and column.
void factorize()
Computes and stores the LU factors using sparse factorization. The original matrix is not modified.
void mat_solve(MatrixBase< T > &x, const MatrixBase< T > &b) override
Solves for matrix with a direct solver, where is this matrix, and is a given right-hand side matr...
virtual T * data() override
Returns a writable pointer to the underlying raw data - use with care.
void add_block(const MatrixBase< T > &x, Index row_start, Index col_start, const T &a=T(1)) override
Adds a block of values to this matrix from the values of a given matrix, starting at a given position...
void preallocate_directly(const std::vector< Index > &nnz)
Preallocates memory for a given number of non-zero values per row.
void add_value_directly(Index row, Index col, const T &a)
Adds to the matrix value at the specified row and column directly, without using a cache.
void assemble() override
Assembles cached data into the matrix.
T value(Index row, Index col) const override
Returns the matrix value at the specified row and column.
void preallocate(const std::vector< Index > &nnz) override
Preallocates memory for a given number of non-zero values per row.
void preallocate(Index num_entries) override
Preallocates memory for a given total number of non-zero values.
virtual const T * data() const override
Returns a read-only pointer to the underlying raw data.
void add_value(Index row, Index col, const T &a) override
Adds to the matrix value at the specified row and column.
void get_block(MatrixBase< T > &x, Index row_start, Index col_start, Index b_rows, Index b_cols) const override
Retrieves a block of values from this matrix.
void scale(const T &a) override
Scales all matrix entries by a given value.
void set_value_directly(Index row, Index col, const T &a)
Sets the matrix value at the specified row and column directly, without using a cache.
Eigen::Matrix< T, N, 1 > EigColVecN
Fixed-size column vector of size N containing type T.
std::size_t Index
Unsigned integer type for indices and container sizes.
Primary namespace for the OpenBEM library.