20 : std::runtime_error(
"Accessing zero element at (" + std::
to_string(
row) +
","
37 std::valarray<int> m_column_indices;
38 std::valarray<int> m_row_pointers;
39 int m_number_non_zeros{};
40 bool m_dynamic_creation{};
41 std::vector<std::tuple<T, int, int>> m_temporary_dynamic_create_storage{};
43 int findIndex(
int rows,
int cols)
const;
123 if (m_dynamic_creation)
125 m_temporary_dynamic_create_storage.emplace_back(
value,
rows,
cols);
142 if (m_dynamic_creation)
145 m_temporary_dynamic_create_storage.begin(), m_temporary_dynamic_create_storage.end(),
146 [](std::tuple<T, int, int>
lhs, std::tuple<T, int, int>
rhs)
147 { return std::get<1>(lhs) < std::get<1>(rhs); });
149 m_temporary_dynamic_create_storage.begin(), m_temporary_dynamic_create_storage.end(),
150 [](std::tuple<T, int, int>
lhs, std::tuple<T, int, int>
rhs)
151 { return std::get<2>(lhs) < std::get<2>(rhs); });
157 std::sort(m_temporary_dynamic_create_storage.begin(),
158 m_temporary_dynamic_create_storage.end(),
159 [](std::tuple<T, int, int>
lhs, std::tuple<T, int, int>
rhs)
161 if (std::get<1>(lhs) == std::get<1>(rhs))
163 return std::get<2>(lhs) < std::get<2>(rhs);
165 return std::get<1>(
lhs) < std::get<1>(
rhs);
169 for (
auto it = m_temporary_dynamic_create_storage.begin();;)
171 if (std::get<1>(*
it) == std::get<1>(*(
it + 1))
172 && std::get<2>(*
it) == std::get<2>(*(
it + 1)))
174 it = m_temporary_dynamic_create_storage.erase(
it);
180 if (
it + 1 == m_temporary_dynamic_create_storage.end())
186 this->m_matrix.resize(m_temporary_dynamic_create_storage.size());
187 m_column_indices.resize(m_temporary_dynamic_create_storage.size());
190 m_row_pointers.resize(this->m_rows + 1);
196 for (
auto it{m_temporary_dynamic_create_storage.begin()};;
it++)
198 is_last = (
it == m_temporary_dynamic_create_storage.end());
216 m_row_pointers[this->m_rows] = this->m_matrix.size();
221 for (
int i{0};
i < m_temporary_dynamic_create_storage.size();
i++)
223 this->m_matrix[
i] = std::get<0>(m_temporary_dynamic_create_storage[
i]);
224 m_column_indices[
i] = std::get<2>(m_temporary_dynamic_create_storage[
i]);
244 if (m_dynamic_creation)
251 std::sort(m_temporary_dynamic_create_storage.begin(),
252 m_temporary_dynamic_create_storage.end(),
253 [](std::tuple<T, int, int>
lhs, std::tuple<T, int, int>
rhs)
255 if (std::get<1>(lhs) == std::get<1>(rhs))
257 return std::get<2>(lhs) < std::get<2>(rhs);
259 return std::get<1>(
lhs) < std::get<1>(
rhs);
263 for (
auto it = m_temporary_dynamic_create_storage.begin();;)
265 if (std::get<1>(*
it) == std::get<1>(*(
it + 1))
266 && std::get<2>(*
it) == std::get<2>(*(
it + 1)))
268 it = m_temporary_dynamic_create_storage.erase(
it);
274 if (
it + 1 == m_temporary_dynamic_create_storage.end())
280 this->m_matrix.resize(m_temporary_dynamic_create_storage.size());
281 m_column_indices.resize(m_temporary_dynamic_create_storage.size());
284 m_row_pointers.resize(
rows + 1);
290 for (
auto it{m_temporary_dynamic_create_storage.begin()};;
it++)
292 is_last = (
it == m_temporary_dynamic_create_storage.end());
310 m_row_pointers[
rows] = this->m_matrix.size();
315 for (
int i{0};
i < m_temporary_dynamic_create_storage.size();
i++)
317 this->m_matrix[
i] = std::get<0>(m_temporary_dynamic_create_storage[
i]);
318 m_column_indices[
i] = std::get<2>(m_temporary_dynamic_create_storage[
i]);
373 if (
rows >= this->m_rows ||
cols >= this->m_cols)
415 throw static_cast<T>(0.0);
427 return this->m_matrix +
value;
438 return this->m_matrix -
value;
449 return this->m_matrix *
value;
460 return this->m_matrix /
value;
470 return this->m_matrix +
value;
481 return this->m_matrix -
value;
492 return this->m_matrix *
value;
503 return this->m_matrix /
value;
516 std::valarray<T>
result(this->m_rows);
519 for (
int i{0};
i < this->m_rows;
i++)
537 if (this->m_matrix.size() > 0)
539 for (
int i{0};
i < m_row_pointers.size() - 1;
i++)
542 m_row_pointers[
i], m_row_pointers[
i + 1] - m_row_pointers[
i], 1)];
544 for (
int j{0};
j < this->m_cols;
j++)
554 std::cout <<
"0" <<
' ';
562 std::cout <<
"Matrix is empty\n";
574 for (
const auto&
position_tuple : m_temporary_dynamic_create_storage)
576 this->m_non_zero_elements.emplace_back(std::get<1>(
position_tuple),
MatrixType
Definition CoreEnums.h:33
Concrete implementation of a matrix class representing a Compressed Sparse Rows (CSR) matrix....
Definition CsrMatrix.h:35
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:121
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:371
void printMatrixInDenseFormat()
Prints the entire matrix with zero values.
Definition CsrMatrix.h:535
std::valarray< T > scalarSubtract(T value) const override
Subtraction of a scalar from the matrix values element-wise.
Definition CsrMatrix.h:436
std::valarray< T > scalarDivide(T value) const override
Division of a scalar from the matrix values element-wise.
Definition CsrMatrix.h:458
const std::vector< std::pair< int, int > > & getNonZeroElements() override
Definition CsrMatrix.h:73
void updateNonZeroElements() override
Sets the m_non_zero_elememts parameter of the parent class.
Definition CsrMatrix.h:572
std::valarray< T > vectorElemMultiply(const std::valarray< T > &value) const override
Multiplication of a vector to the matrix values element-wise.
Definition CsrMatrix.h:490
std::valarray< T > scalarMultiply(T value) const override
Multiplication of a scalar to the matrix values element-wise.
Definition CsrMatrix.h:447
CsrSparseMatrix()
Constructor for an empty CsrSparseMatrix object. Leaves all storage arrays empty but sets the flag to...
Definition CsrMatrix.h:87
~CsrSparseMatrix() override=default
std::valarray< T > scalarAdd(T value) const override
Addition of a scalar to the matrix values element-wise.
Definition CsrMatrix.h:425
std::valarray< T > vectorElemSubtract(const std::valarray< T > &value) const override
Subtraction of a vector from the matrix values element-wise.
Definition CsrMatrix.h:479
std::valarray< T > matrixVectorMultiplication(const std::valarray< T > &value) const override
Matrix-vector-multiplication for sparse matrices.
Definition CsrMatrix.h:514
std::valarray< T > vectorElemAdd(const std::valarray< T > &value) const override
Addition of a vector to the matrix values element-wise.
Definition CsrMatrix.h:468
const MatrixType getMatrixType() override
Definition CsrMatrix.h:69
std::valarray< T > vectorElemDivide(const std::valarray< T > &value) const override
Division of a vector from the matrix values element-wise.
Definition CsrMatrix.h:501
void buildAtRuntime()
Builds the CSR matrix from the registered elements. It sorts all elements of the COO format vector by...
Definition CsrMatrix.h:140
Error class to throw when a module chain has circular or missing dependencies. Inherits from std::exc...
Definition ChainManager.h:28
Template generalized Matrix object that should be the base for all further implementations of a matri...
Definition GenericMatrix.h:19
std::vector< std::pair< int, int > > m_non_zero_elements
Definition GenericMatrix.h:25
Error class to throw when the matrix is accessed out of bounds. Inherits from std::exception.
Definition GenericMatrix.h:150
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