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;
128 if (m_dynamic_creation)
130 m_temporary_dynamic_create_storage.emplace_back(
value,
rows,
cols);
147 if (m_dynamic_creation)
150 m_temporary_dynamic_create_storage.begin(), m_temporary_dynamic_create_storage.end(),
151 [](std::tuple<T, int, int>
lhs, std::tuple<T, int, int>
rhs)
152 { return std::get<1>(lhs) < std::get<1>(rhs); });
154 m_temporary_dynamic_create_storage.begin(), m_temporary_dynamic_create_storage.end(),
155 [](std::tuple<T, int, int>
lhs, std::tuple<T, int, int>
rhs)
156 { return std::get<2>(lhs) < std::get<2>(rhs); });
162 std::sort(m_temporary_dynamic_create_storage.begin(),
163 m_temporary_dynamic_create_storage.end(),
164 [](std::tuple<T, int, int>
lhs, std::tuple<T, int, int>
rhs)
166 if (std::get<1>(lhs) == std::get<1>(rhs))
168 return std::get<2>(lhs) < std::get<2>(rhs);
170 return std::get<1>(
lhs) < std::get<1>(
rhs);
174 for (
auto it = m_temporary_dynamic_create_storage.begin();;)
176 if (std::get<1>(*
it) == std::get<1>(*(
it + 1))
177 && std::get<2>(*
it) == std::get<2>(*(
it + 1)))
179 it = m_temporary_dynamic_create_storage.erase(
it);
185 if (
it + 1 == m_temporary_dynamic_create_storage.end())
191 this->
m_matrix.resize(m_temporary_dynamic_create_storage.size());
192 m_column_indices.resize(m_temporary_dynamic_create_storage.size());
195 m_row_pointers.resize(this->
m_rows + 1);
201 for (
auto it{m_temporary_dynamic_create_storage.begin()};;
it++)
203 is_last = (
it == m_temporary_dynamic_create_storage.end());
219 m_row_pointers[
i] = -1;
230 for (
int i{
static_cast<int>(m_row_pointers.size()) - 1};
i > 0;
i--)
232 if (m_row_pointers[
i - 1] == -1)
234 m_row_pointers[
i - 1] = m_row_pointers[
i];
246 for (
int i{0};
i < m_temporary_dynamic_create_storage.size();
i++)
248 this->
m_matrix[
i] = std::get<0>(m_temporary_dynamic_create_storage[
i]);
249 m_column_indices[
i] = std::get<2>(m_temporary_dynamic_create_storage[
i]);
269 if (m_dynamic_creation)
276 std::sort(m_temporary_dynamic_create_storage.begin(),
277 m_temporary_dynamic_create_storage.end(),
278 [](std::tuple<T, int, int>
lhs, std::tuple<T, int, int>
rhs)
280 if (std::get<1>(lhs) == std::get<1>(rhs))
282 return std::get<2>(lhs) < std::get<2>(rhs);
284 return std::get<1>(
lhs) < std::get<1>(
rhs);
288 for (
auto it = m_temporary_dynamic_create_storage.begin();;)
290 if (std::get<1>(*
it) == std::get<1>(*(
it + 1))
291 && std::get<2>(*
it) == std::get<2>(*(
it + 1)))
293 it = m_temporary_dynamic_create_storage.erase(
it);
299 if (
it + 1 == m_temporary_dynamic_create_storage.end())
305 this->
m_matrix.resize(m_temporary_dynamic_create_storage.size());
306 m_column_indices.resize(m_temporary_dynamic_create_storage.size());
309 m_row_pointers.resize(
rows + 1);
316 for (
auto it{m_temporary_dynamic_create_storage.begin()};;
it++)
318 is_last = (
it == m_temporary_dynamic_create_storage.end());
334 m_row_pointers[
i] = -1;
355 for (
int i{
static_cast<int>(m_row_pointers.size()) - 1};
i > 0;
i--)
357 if (m_row_pointers[
i - 1] == -1)
359 m_row_pointers[
i - 1] = m_row_pointers[
i];
371 for (
int i{0};
i < m_temporary_dynamic_create_storage.size();
i++)
373 this->
m_matrix[
i] = std::get<0>(m_temporary_dynamic_create_storage[
i]);
374 m_column_indices[
i] = std::get<2>(m_temporary_dynamic_create_storage[
i]);
471 throw static_cast<T>(0.0);
595 for (
int i{0};
i < m_row_pointers.size() - 1;
i++)
598 m_row_pointers[
i], m_row_pointers[
i + 1] - m_row_pointers[
i], 1)];
610 std::cout <<
"0" <<
' ';
618 std::cout <<
"Matrix is empty\n";
630 for (
const auto&
position_tuple : m_temporary_dynamic_create_storage)
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:126
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:427
void printMatrixInDenseFormat()
Prints the entire matrix with zero values.
Definition CsrMatrix.h:591
std::valarray< T > scalarSubtract(T value) const override
Subtraction of a scalar from the matrix values element-wise.
Definition CsrMatrix.h:492
std::valarray< T > scalarDivide(T value) const override
Division of a scalar from the matrix values element-wise.
Definition CsrMatrix.h:514
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:628
std::valarray< T > vectorElemMultiply(const std::valarray< T > &value) const override
Multiplication of a vector to the matrix values element-wise.
Definition CsrMatrix.h:546
std::valarray< T > scalarMultiply(T value) const override
Multiplication of a scalar to the matrix values element-wise.
Definition CsrMatrix.h:503
const std::valarray< int > & getColumnIndices()
Definition CsrMatrix.h:79
int getNonZeroElementNumber() const
Definition CsrMatrix.h:78
CsrSparseMatrix()
Constructor for an empty CsrSparseMatrix object. Leaves all storage arrays empty but sets the flag to...
Definition CsrMatrix.h:92
~CsrSparseMatrix() override=default
const std::valarray< int > & getRowPointers()
Definition CsrMatrix.h:80
std::valarray< T > scalarAdd(T value) const override
Addition of a scalar to the matrix values element-wise.
Definition CsrMatrix.h:481
std::valarray< T > vectorElemSubtract(const std::valarray< T > &value) const override
Subtraction of a vector from the matrix values element-wise.
Definition CsrMatrix.h:535
std::valarray< T > matrixVectorMultiplication(const std::valarray< T > &value) const override
Matrix-vector-multiplication for sparse matrices.
Definition CsrMatrix.h:570
std::valarray< T > vectorElemAdd(const std::valarray< T > &value) const override
Addition of a vector to the matrix values element-wise.
Definition CsrMatrix.h:524
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:557
void buildAtRuntime()
Builds the CSR matrix from the registered elements. It sorts all elements of the COO format vector by...
Definition CsrMatrix.h:145
const std::valarray< T > & getMatrixElements()
Definition CsrMatrix.h:81
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:118
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: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