16 : std::runtime_error(
"Accessing zero element at (" + std::
to_string(
row) +
","
33 std::valarray<int> m_column_indices;
34 std::valarray<int> m_row_pointers;
35 int m_number_non_zeros{};
36 bool m_dynamic_creation{};
37 std::vector<std::tuple<T, int, int>> m_temporary_dynamic_create_storage{};
39 int findIndex(
int rows,
int cols)
const;
110 if (m_dynamic_creation)
112 m_temporary_dynamic_create_storage.emplace_back(
value,
rows,
cols);
129 if (m_dynamic_creation)
132 m_temporary_dynamic_create_storage.begin(), m_temporary_dynamic_create_storage.end(),
133 [](std::tuple<T, int, int>
lhs, std::tuple<T, int, int>
rhs)
134 { return std::get<1>(lhs) < std::get<1>(rhs); });
136 m_temporary_dynamic_create_storage.begin(), m_temporary_dynamic_create_storage.end(),
137 [](std::tuple<T, int, int>
lhs, std::tuple<T, int, int>
rhs)
138 { return std::get<2>(lhs) < std::get<2>(rhs); });
144 std::sort(m_temporary_dynamic_create_storage.begin(),
145 m_temporary_dynamic_create_storage.end(),
146 [](std::tuple<T, int, int>
lhs, std::tuple<T, int, int>
rhs)
148 if (std::get<1>(lhs) == std::get<1>(rhs))
150 return std::get<2>(lhs) < std::get<2>(rhs);
152 return std::get<1>(
lhs) < std::get<1>(
rhs);
155 this->m_matrix.resize(m_temporary_dynamic_create_storage.size());
156 m_column_indices.resize(m_temporary_dynamic_create_storage.size());
159 m_row_pointers.resize(this->m_rows + 1);
166 for (
auto it{m_temporary_dynamic_create_storage.begin()};;
it++)
168 is_last = (
it == m_temporary_dynamic_create_storage.end());
192 for (
int i{0};
i < m_temporary_dynamic_create_storage.size();
i++)
194 this->m_matrix[
i] = std::get<0>(m_temporary_dynamic_create_storage[
i]);
195 m_column_indices[
i] = std::get<2>(m_temporary_dynamic_create_storage[
i]);
215 if (m_dynamic_creation)
222 std::sort(m_temporary_dynamic_create_storage.begin(),
223 m_temporary_dynamic_create_storage.end(),
224 [](std::tuple<T, int, int>
lhs, std::tuple<T, int, int>
rhs)
226 if (std::get<1>(lhs) == std::get<1>(rhs))
228 return std::get<2>(lhs) < std::get<2>(rhs);
230 return std::get<1>(
lhs) < std::get<1>(
rhs);
233 this->m_matrix.resize(m_temporary_dynamic_create_storage.size());
234 m_column_indices.resize(m_temporary_dynamic_create_storage.size());
237 m_row_pointers.resize(
rows + 1);
244 for (
auto it{m_temporary_dynamic_create_storage.begin()};;
it++)
246 is_last = (
it == m_temporary_dynamic_create_storage.end());
270 for (
int i{0};
i < m_temporary_dynamic_create_storage.size();
i++)
272 this->m_matrix[
i] = std::get<0>(m_temporary_dynamic_create_storage[
i]);
273 m_column_indices[
i] = std::get<2>(m_temporary_dynamic_create_storage[
i]);
328 if (
rows >= this->m_rows ||
cols >= this->m_cols)
370 throw static_cast<T>(0.0);
382 return this->m_matrix +
value;
393 return this->m_matrix -
value;
404 return this->m_matrix *
value;
415 return this->m_matrix /
value;
424 if (this->m_matrix.size() > 0)
426 for (
int i{0};
i < m_row_pointers.size() - 1;
i++)
429 m_row_pointers[
i], m_row_pointers[
i + 1] - m_row_pointers[
i], 1)];
431 for (
int j{0};
j < this->m_cols;
j++)
441 std::cout <<
"0" <<
' ';
449 std::cout <<
"Matrix is empty\n";
456 for (
const auto&
position_tuple : m_temporary_dynamic_create_storage)
458 this->m_non_zero_elements.emplace_back(std::get<1>(
position_tuple),
Concrete implementation of a matrix class representing a Compressed Sparse Rows (CSR) matrix....
Definition CsrMatrix.h:31
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:326
void printMatrixInDenseFormat()
Prints the entire matrix with zero values.
Definition CsrMatrix.h:422
std::valarray< T > scalarSubtract(T value) const override
Subtraction of a scalar from the matrix values element-wise.
Definition CsrMatrix.h:391
void registerElement(T value, int rows, int cols)
Registers the element and it's initial value within a COO style vector for dynamic creation.
Definition CsrMatrix.h:108
std::valarray< T > scalarDivide(T value) const override
Division of a scalar from the matrix values element-wise.
Definition CsrMatrix.h:413
const std::vector< std::pair< int, int > > & getNonZeroElements() override
Definition CsrMatrix.h:60
void updateNonZeroElements() override
Definition CsrMatrix.h:454
std::valarray< T > scalarMultiply(T value) const override
Multiplication of a scalar to the matrix values element-wise.
Definition CsrMatrix.h:402
CsrSparseMatrix()
Constructor for an empty CsrSparseMatrix object. Leaves all storage arrays empty but sets the flag to...
Definition CsrMatrix.h:74
~CsrSparseMatrix() override=default
std::valarray< T > scalarAdd(T value) const override
Addition of a scalar to the matrix values element-wise.
Definition CsrMatrix.h:380
void buildAtRuntime()
Builds the CSR matrix from the registered elements. It sorts all elements of the COO format vector by...
Definition CsrMatrix.h:127
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:17
std::vector< std::pair< int, int > > m_non_zero_elements
Definition GenericMatrix.h:23
Error class to throw when the matrix is accessed out of bounds. Inherits from std::exception.
Definition GenericMatrix.h:122
Definition CsrMatrix.h:13
StaticSparseMatrixAccessError(const int row, const int col)
Definition CsrMatrix.h:15