17#include "assembly_types.h"
19#include <Eigen/Sparse>
51template <
typename SCALAR>
90 triplets_.push_back(Eigen::Triplet<SCALAR>(i, j, increment));
108 template <
typename PREDICATE>
110 auto new_last = std::remove_if(
112 [pred](
Triplet &trp) { return (pred(trp.row(), trp.col())); });
140 template <
typename VECTOR>
141 [[nodiscard]] Eigen::Matrix<SCALAR, Eigen::Dynamic, 1>
MatVecMult(
142 SCALAR alpha,
const VECTOR &vec)
const;
162 template <
typename VECTOR,
typename RESULTVECTOR>
163 void MatVecMult(SCALAR alpha,
const VECTOR &vec, RESULTVECTOR &resvec)
const;
172 [[nodiscard]] Eigen::SparseMatrix<Scalar>
makeSparse()
const {
173 Eigen::SparseMatrix<Scalar> result;
176 "matrix has zero rows or columns, this is probably an error.");
187 [[nodiscard]] Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>
189 Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> mat(
rows_,
cols_);
194 "Illegal triplet index (" << trp.col() <<
',' << trp.row() <<
")");
195 mat(trp.row(), trp.col()) += trp.value();
206 o <<
rows_ <<
" x " <<
cols_ <<
" COO matrix" << std::endl;
208 o <<
"(" << trp.row() <<
',' << trp.col() <<
") -> " << trp.value()
218 template <
typename SCALARTYPE>
228template <
typename SCALARTYPE>
234template <
typename SCALAR>
235template <
typename VECTOR>
237 SCALAR alpha,
const VECTOR &vec)
const {
238 LF_ASSERT_MSG(vec.size() >= cols_,
239 "size mismatch: " << cols_ <<
" <-> " << vec.size());
240 Eigen::VectorXd result(rows_);
242 for (
const Triplet &trp : triplets_) {
243 result[trp.row()] += trp.value() * (alpha * vec[trp.col()]);
248template <
typename SCALAR>
249template <
typename VECTOR,
typename RESULTVECTOR>
251 RESULTVECTOR &resvec)
const {
252 LF_ASSERT_MSG(vec.size() >= cols_,
253 "Vector vec size mismatch: " << cols_ <<
" <-> " << vec.size());
255 resvec.size() >= rows_,
256 "Vector result size mismatch: " << cols_ <<
" <-> " << resvec.size());
257 for (
const Triplet &trp : triplets_) {
258 resvec[trp.row()] += trp.value() * (alpha * vec[trp.col()]);
A temporary data structure for matrix in COO format.
void PrintInfo(std::ostream &o) const
Ouput of triplet list describing COO matrix.
void setZero()
Erase all entries of the matrix.
Index cols() const
return number of column
void setZero(PREDICATE &&pred)
Erase specific entries of the COO matrix, that is, set them to zero.
Eigen::Matrix< SCALAR, Eigen::Dynamic, 1 > MatVecMult(SCALAR alpha, const VECTOR &vec) const
Computes the product of a (scaled) vector with the matrix in COO format.
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > makeDense() const
Create an Eigen::MatrixX from the COO format.
COOMatrix(COOMatrix &&) noexcept=default
const TripletVec & triplets() const
std::vector< Triplet > TripletVec
void MatVecMult(SCALAR alpha, const VECTOR &vec, RESULTVECTOR &resvec) const
In-situ computes the product of a vector with the matrix in COO format.
Eigen::SparseMatrix< Scalar > makeSparse() const
Create an Eigen::SparseMatrix out of the COO format.
Eigen::Triplet< SCALAR > Triplet
COOMatrix(const COOMatrix &)=default
COOMatrix(size_type num_rows, size_type num_cols)
TripletVec & triplets()
Gives access to the vector of triplets.
Index rows() const
return number of rows
void AddToEntry(gdof_idx_t i, gdof_idx_t j, SCALAR increment)
Add a value to the specified entry.
friend std::ostream & operator<<(std::ostream &o, const COOMatrix< SCALARTYPE > &mat)
Output operator for COO matrix.
D.o.f. index mapping and assembly facilities.
lf::base::size_type size_type
std::ostream & operator<<(std::ostream &o, const COOMatrix< SCALARTYPE > &mat)