32 #include "algebraCore.h"
69 void insert(
const int i,
const int j,
const double val)
71 assert(i >= 0 && i < rows.size());
72 assert(j >= 0 && j < rows.size());
77 std::vector<SparseVector> rows;
111 for (
size_t i = 0; i < source.rows.size(); ++i)
115 row.
indices.reserve(source_row.size());
116 row.
values.reserve(source_row.size());
117 for (
auto it = source_row.begin(); it != source_row.end(); ++it)
119 row.
indices.push_back(it->first);
120 row.
values.push_back(it->second);
128 for (
size_t i = 0; i < shape.size(); ++i)
130 const std::set<int>& row_shape = shape[i];
132 row.
indices.reserve(row_shape.size());
133 for (
auto it = row_shape.begin(); it != row_shape.end(); ++it)
135 row.
values.resize(row_shape.size());
143 for (
double& value: row.values)
151 void add(
int i,
int j,
double val)
153 assert(i >= 0 && i <
rows.size());
154 assert(j >= 0 && j <
rows.size());
156 auto it = std::lower_bound(row.
indices.begin(), row.
indices.end(), j);
157 assert(it != row.
indices.end() && *it == j);
158 int k = it - row.
indices.begin();
159 std::mutex& mutex = row.
mutex;
160 double& value = row.
values[k];
167 void print(std::ostream & flux = std::cout)
const
173 for (
size_t k = 0; k < row.indices.size(); ++k)
175 flux << row.indices[k] <<
": " << row.values[k];
176 flux << (k < row.indices.size()-1 ?
", " :
"\n");
186 assert(i >= 0 && i <
rows.size());
187 assert(j >= 0 && j <
rows.size());
189 auto it = std::lower_bound(row.
indices.begin(), row.
indices.end(), j);
190 if (it == row.
indices.end() || *it != j)
192 int k = it - row.
indices.begin();
197 template <
typename T>
198 void mult(std::vector<T>
const& X, std::vector<T> &Y)
200 assert(X.size() ==
rows.size());
201 assert(Y.size() ==
rows.size());
202 std::transform(std::execution::par,
rows.begin(),
rows.end(), Y.begin(),
206 for (size_t k = 0; k < row.indices.size(); ++k)
207 val += row.values[k] * X[row.indices[k]];
213 template <
typename T>
216 assert(
D.size() ==
rows.size());
217 for (
size_t i = 0; i <
rows.size(); ++i)
219 const double c = (*this)(i,i);
230 std::vector<SparseVector>
rows;
Read-mode square sparse matrix.
Definition: sparseMat.h:87
r_sparseMat(const MatrixShape &shape)
Definition: sparseMat.h:126
void mult(std::vector< T > const &X, std::vector< T > &Y)
Definition: sparseMat.h:198
std::vector< SparseVector > rows
Definition: sparseMat.h:230
void build_diag_precond(std::vector< T > &D) const
Definition: sparseMat.h:214
void clear()
Definition: sparseMat.h:140
r_sparseMat(const w_sparseMat &source)
Definition: sparseMat.h:109
void print(std::ostream &flux=std::cout) const
Definition: sparseMat.h:167
double operator()(const int i, const int j) const
Definition: sparseMat.h:184
void add(int i, int j, double val)
Definition: sparseMat.h:151
Write-mode sparse matrix.
Definition: sparseMat.h:51
w_sparseMat(int N)
Definition: sparseMat.h:61
void insert(const int i, const int j, const double val)
Definition: sparseMat.h:69
std::map< int, double > SparseVector
Definition: sparseMat.h:57
const int N
Definition: facette.h:17
constexpr double D
Definition: tetra.h:32
std::vector< std::set< int > > MatrixShape
Definition: sparseMat.h:41
Definition: sparseMat.h:101
std::vector< double > values
Definition: sparseMat.h:104
std::vector< int > indices
Definition: sparseMat.h:103
std::mutex mutex
Definition: sparseMat.h:102