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());
218 m_row_pointers[
i] = -1;
229 for (
int i{
static_cast<int>(m_row_pointers.size()) - 1};
i > 0;
i--)
231 if (m_row_pointers[
i - 1] == -1)
233 m_row_pointers[
i - 1] = m_row_pointers[
i];
245 for (
int i{0};
i < m_temporary_dynamic_create_storage.size();
i++)
247 this->
m_matrix[
i] = std::get<0>(m_temporary_dynamic_create_storage[
i]);
248 m_column_indices[
i] = std::get<2>(m_temporary_dynamic_create_storage[
i]);
268 if (m_dynamic_creation)
275 std::sort(m_temporary_dynamic_create_storage.begin(),
276 m_temporary_dynamic_create_storage.end(),
277 [](std::tuple<T, int, int>
lhs, std::tuple<T, int, int>
rhs)
279 if (std::get<1>(lhs) == std::get<1>(rhs))
281 return std::get<2>(lhs) < std::get<2>(rhs);
283 return std::get<1>(
lhs) < std::get<1>(
rhs);
287 for (
auto it = m_temporary_dynamic_create_storage.begin();;)
289 if (std::get<1>(*
it) == std::get<1>(*(
it + 1))
290 && std::get<2>(*
it) == std::get<2>(*(
it + 1)))
292 it = m_temporary_dynamic_create_storage.erase(
it);
298 if (
it + 1 == m_temporary_dynamic_create_storage.end())
304 this->
m_matrix.resize(m_temporary_dynamic_create_storage.size());
305 m_column_indices.resize(m_temporary_dynamic_create_storage.size());
308 m_row_pointers.resize(
rows + 1);
314 for (
auto it{m_temporary_dynamic_create_storage.begin()};;
it++)
316 is_last = (
it == m_temporary_dynamic_create_storage.end());
331 m_row_pointers[
i] = -1;
342 for (
int i{
static_cast<int>(m_row_pointers.size()) - 1};
i > 0;
i--)
344 if (m_row_pointers[
i - 1] == -1)
346 m_row_pointers[
i - 1] = m_row_pointers[
i];
358 for (
int i{0};
i < m_temporary_dynamic_create_storage.size();
i++)
360 this->
m_matrix[
i] = std::get<0>(m_temporary_dynamic_create_storage[
i]);
361 m_column_indices[
i] = std::get<2>(m_temporary_dynamic_create_storage[
i]);
458 throw static_cast<T>(0.0);
582 for (
int i{0};
i < m_row_pointers.size() - 1;
i++)
585 m_row_pointers[
i], m_row_pointers[
i + 1] - m_row_pointers[
i], 1)];
597 std::cout <<
"0" <<
' ';
605 std::cout <<
"Matrix is empty\n";
617 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:414
void printMatrixInDenseFormat()
Prints the entire matrix with zero values.
Definition CsrMatrix.h:578
std::valarray< T > scalarSubtract(T value) const override
Subtraction of a scalar from the matrix values element-wise.
Definition CsrMatrix.h:479
std::valarray< T > scalarDivide(T value) const override
Division of a scalar from the matrix values element-wise.
Definition CsrMatrix.h:501
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:615
std::valarray< T > vectorElemMultiply(const std::valarray< T > &value) const override
Multiplication of a vector to the matrix values element-wise.
Definition CsrMatrix.h:533
std::valarray< T > scalarMultiply(T value) const override
Multiplication of a scalar to the matrix values element-wise.
Definition CsrMatrix.h:490
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:468
std::valarray< T > vectorElemSubtract(const std::valarray< T > &value) const override
Subtraction of a vector from the matrix values element-wise.
Definition CsrMatrix.h:522
std::valarray< T > matrixVectorMultiplication(const std::valarray< T > &value) const override
Matrix-vector-multiplication for sparse matrices.
Definition CsrMatrix.h:557
std::valarray< T > vectorElemAdd(const std::valarray< T > &value) const override
Addition of a vector to the matrix values element-wise.
Definition CsrMatrix.h:511
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:544
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