39 std::valarray<int> m_column_indices;
40 std::valarray<int> m_row_pointers;
41 int m_number_non_zeros{};
42 bool m_dynamic_creation{};
43 std::vector<std::tuple<T, int, int>> m_temporary_dynamic_create_storage{};
45 int findIndex(
int rows,
int cols)
const;
130 if (m_dynamic_creation)
132 m_temporary_dynamic_create_storage.emplace_back(
value,
rows,
cols);
149 if (m_dynamic_creation)
152 m_temporary_dynamic_create_storage.begin(), m_temporary_dynamic_create_storage.end(),
153 [](std::tuple<T, int, int>
lhs, std::tuple<T, int, int>
rhs)
154 { return std::get<1>(lhs) < std::get<1>(rhs); });
156 m_temporary_dynamic_create_storage.begin(), m_temporary_dynamic_create_storage.end(),
157 [](std::tuple<T, int, int>
lhs, std::tuple<T, int, int>
rhs)
158 { return std::get<2>(lhs) < std::get<2>(rhs); });
164 std::sort(m_temporary_dynamic_create_storage.begin(),
165 m_temporary_dynamic_create_storage.end(),
166 [](std::tuple<T, int, int>
lhs, std::tuple<T, int, int>
rhs)
168 if (std::get<1>(lhs) == std::get<1>(rhs))
170 return std::get<2>(lhs) < std::get<2>(rhs);
172 return std::get<1>(
lhs) < std::get<1>(
rhs);
176 for (
auto it = m_temporary_dynamic_create_storage.begin();;)
178 if (std::get<1>(*
it) == std::get<1>(*(
it + 1))
179 && std::get<2>(*
it) == std::get<2>(*(
it + 1)))
181 it = m_temporary_dynamic_create_storage.erase(
it);
187 if (
it + 1 == m_temporary_dynamic_create_storage.end())
193 this->
m_matrix.resize(m_temporary_dynamic_create_storage.size());
194 m_column_indices.resize(m_temporary_dynamic_create_storage.size());
197 m_row_pointers.resize(this->
m_rows + 1);
203 for (
auto it{m_temporary_dynamic_create_storage.begin()};;
it++)
205 is_last = (
it == m_temporary_dynamic_create_storage.end());
221 m_row_pointers[
i] = -1;
232 for (
int i{
static_cast<int>(m_row_pointers.size()) - 1};
i > 0;
i--)
234 if (m_row_pointers[
i - 1] == -1)
236 m_row_pointers[
i - 1] = m_row_pointers[
i];
248 for (
int i{0};
i < m_temporary_dynamic_create_storage.size();
i++)
250 this->
m_matrix[
i] = std::get<0>(m_temporary_dynamic_create_storage[
i]);
251 m_column_indices[
i] = std::get<2>(m_temporary_dynamic_create_storage[
i]);
271 if (m_dynamic_creation)
278 std::sort(m_temporary_dynamic_create_storage.begin(),
279 m_temporary_dynamic_create_storage.end(),
280 [](std::tuple<T, int, int>
lhs, std::tuple<T, int, int>
rhs)
282 if (std::get<1>(lhs) == std::get<1>(rhs))
284 return std::get<2>(lhs) < std::get<2>(rhs);
286 return std::get<1>(
lhs) < std::get<1>(
rhs);
290 for (
auto it = m_temporary_dynamic_create_storage.begin();;)
292 if (std::get<1>(*
it) == std::get<1>(*(
it + 1))
293 && std::get<2>(*
it) == std::get<2>(*(
it + 1)))
295 it = m_temporary_dynamic_create_storage.erase(
it);
301 if (
it + 1 == m_temporary_dynamic_create_storage.end())
307 this->
m_matrix.resize(m_temporary_dynamic_create_storage.size());
308 m_column_indices.resize(m_temporary_dynamic_create_storage.size());
311 m_row_pointers.resize(
rows + 1);
318 for (
auto it{m_temporary_dynamic_create_storage.begin()};;
it++)
320 is_last = (
it == m_temporary_dynamic_create_storage.end());
336 m_row_pointers[
i] = -1;
357 for (
int i{
static_cast<int>(m_row_pointers.size()) - 1};
i > 0;
i--)
359 if (m_row_pointers[
i - 1] == -1)
361 m_row_pointers[
i - 1] = m_row_pointers[
i];
373 for (
int i{0};
i < m_temporary_dynamic_create_storage.size();
i++)
375 this->
m_matrix[
i] = std::get<0>(m_temporary_dynamic_create_storage[
i]);
376 m_column_indices[
i] = std::get<2>(m_temporary_dynamic_create_storage[
i]);
433 std::cerr <<
"[CsrSparseMatrix]: Trying to access a matrix out of bounds!\n";
435 static_cast<std::string
>(
"[CsrSparseMatrix]:"));
468 std::cerr <<
"[CsrSparseMatrix]: Trying to access a matrix out of bounds!\n";
470 static_cast<std::string
>(
"[CsrSparseMatrix]:"));
478 throw static_cast<T>(0.0);
602 for (
int i{0};
i < m_row_pointers.size() - 1;
i++)
605 m_row_pointers[
i], m_row_pointers[
i + 1] - m_row_pointers[
i], 1)];
617 std::cout <<
"0" <<
' ';
625 std::cout <<
"Matrix is empty\n";
637 for (
const auto&
position_tuple : m_temporary_dynamic_create_storage)
MatrixType
Definition CoreEnums.h:36
Concrete implementation of a matrix class representing a Compressed Sparse Rows (CSR) matrix....
Definition CsrMatrix.h:37
void registerElement(T value, int rows, int cols) override
Registers the element and it's initial value within a COO style vector for dynamic creation.
Definition CsrMatrix.h:128
T & operator()(int rows, int cols) override
Parentheses operator to allow access to the non-zero matrix values as Matrix(row,column)....
Definition CsrMatrix.h:429
void printMatrixInDenseFormat()
Prints the entire matrix with zero values.
Definition CsrMatrix.h:598
std::valarray< T > scalarSubtract(T value) const override
Subtraction of a scalar from the matrix values element-wise.
Definition CsrMatrix.h:499
std::valarray< T > scalarDivide(T value) const override
Division of a scalar from the matrix values element-wise.
Definition CsrMatrix.h:521
const std::vector< std::pair< int, int > > & getNonZeroElements() override
Definition CsrMatrix.h:75
void updateNonZeroElements() override
Sets the m_non_zero_elememts parameter of the parent class.
Definition CsrMatrix.h:635
std::valarray< T > vectorElemMultiply(const std::valarray< T > &value) const override
Multiplication of a vector to the matrix values element-wise.
Definition CsrMatrix.h:553
std::valarray< T > scalarMultiply(T value) const override
Multiplication of a scalar to the matrix values element-wise.
Definition CsrMatrix.h:510
const std::valarray< int > & getColumnIndices()
Definition CsrMatrix.h:81
int getNonZeroElementNumber() const
Definition CsrMatrix.h:80
CsrSparseMatrix()
Constructor for an empty CsrSparseMatrix object. Leaves all storage arrays empty but sets the flag to...
Definition CsrMatrix.h:94
~CsrSparseMatrix() override=default
const std::valarray< int > & getRowPointers()
Definition CsrMatrix.h:82
std::valarray< T > scalarAdd(T value) const override
Addition of a scalar to the matrix values element-wise.
Definition CsrMatrix.h:488
std::valarray< T > vectorElemSubtract(const std::valarray< T > &value) const override
Subtraction of a vector from the matrix values element-wise.
Definition CsrMatrix.h:542
std::valarray< T > matrixVectorMultiplication(const std::valarray< T > &value) const override
Matrix-vector-multiplication for sparse matrices.
Definition CsrMatrix.h:577
std::valarray< T > vectorElemAdd(const std::valarray< T > &value) const override
Addition of a vector to the matrix values element-wise.
Definition CsrMatrix.h:531
const MatrixType getMatrixType() override
Definition CsrMatrix.h:71
std::valarray< T > vectorElemDivide(const std::valarray< T > &value) const override
Division of a vector from the matrix values element-wise.
Definition CsrMatrix.h:564
void buildAtRuntime()
Builds the CSR matrix from the registered elements. It sorts all elements of the COO format vector by...
Definition CsrMatrix.h:147
const std::valarray< T > & getMatrixElements()
Definition CsrMatrix.h:83
Template generalized Matrix object that should be the base for all further implementations of a matri...
Definition GenericMatrix.h:19
std::valarray< T > m_matrix
Definition GenericMatrix.h:24
std::vector< std::pair< int, int > > m_non_zero_elements
Definition GenericMatrix.h:25
std::pair< int, int > size() const
Function that returns the size of the matrix as a pair in (rows, columns) format.
Definition GenericMatrix.h:120
int m_rows
Definition GenericMatrix.h:21
int m_cols
Definition GenericMatrix.h:22
Error class to throw when the matrix is accessed out of bounds. Inherits from std::exception.
Definition GenericMatrix.h:152
Error class to be thrown when a zero element is tried to be accessed as a reference.
Definition CsrMatrix.h:17
StaticSparseMatrixAccessError(const int row, const int col)
Definition CsrMatrix.h:19