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());
226 for (
int i{0};
i < m_temporary_dynamic_create_storage.size();
i++)
228 this->
m_matrix[
i] = std::get<0>(m_temporary_dynamic_create_storage[
i]);
229 m_column_indices[
i] = std::get<2>(m_temporary_dynamic_create_storage[
i]);
249 if (m_dynamic_creation)
256 std::sort(m_temporary_dynamic_create_storage.begin(),
257 m_temporary_dynamic_create_storage.end(),
258 [](std::tuple<T, int, int>
lhs, std::tuple<T, int, int>
rhs)
260 if (std::get<1>(lhs) == std::get<1>(rhs))
262 return std::get<2>(lhs) < std::get<2>(rhs);
264 return std::get<1>(
lhs) < std::get<1>(
rhs);
268 for (
auto it = m_temporary_dynamic_create_storage.begin();;)
270 if (std::get<1>(*
it) == std::get<1>(*(
it + 1))
271 && std::get<2>(*
it) == std::get<2>(*(
it + 1)))
273 it = m_temporary_dynamic_create_storage.erase(
it);
279 if (
it + 1 == m_temporary_dynamic_create_storage.end())
285 this->
m_matrix.resize(m_temporary_dynamic_create_storage.size());
286 m_column_indices.resize(m_temporary_dynamic_create_storage.size());
289 m_row_pointers.resize(
rows + 1);
295 for (
auto it{m_temporary_dynamic_create_storage.begin()};;
it++)
297 is_last = (
it == m_temporary_dynamic_create_storage.end());
320 for (
int i{0};
i < m_temporary_dynamic_create_storage.size();
i++)
322 this->
m_matrix[
i] = std::get<0>(m_temporary_dynamic_create_storage[
i]);
323 m_column_indices[
i] = std::get<2>(m_temporary_dynamic_create_storage[
i]);
420 throw static_cast<T>(0.0);
544 for (
int i{0};
i < m_row_pointers.size() - 1;
i++)
547 m_row_pointers[
i], m_row_pointers[
i + 1] - m_row_pointers[
i], 1)];
559 std::cout <<
"0" <<
' ';
567 std::cout <<
"Matrix is empty\n";
579 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:376
void printMatrixInDenseFormat()
Prints the entire matrix with zero values.
Definition CsrMatrix.h:540
std::valarray< T > scalarSubtract(T value) const override
Subtraction of a scalar from the matrix values element-wise.
Definition CsrMatrix.h:441
std::valarray< T > scalarDivide(T value) const override
Division of a scalar from the matrix values element-wise.
Definition CsrMatrix.h:463
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:577
std::valarray< T > vectorElemMultiply(const std::valarray< T > &value) const override
Multiplication of a vector to the matrix values element-wise.
Definition CsrMatrix.h:495
std::valarray< T > scalarMultiply(T value) const override
Multiplication of a scalar to the matrix values element-wise.
Definition CsrMatrix.h:452
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:430
std::valarray< T > vectorElemSubtract(const std::valarray< T > &value) const override
Subtraction of a vector from the matrix values element-wise.
Definition CsrMatrix.h:484
std::valarray< T > matrixVectorMultiplication(const std::valarray< T > &value) const override
Matrix-vector-multiplication for sparse matrices.
Definition CsrMatrix.h:519
std::valarray< T > vectorElemAdd(const std::valarray< T > &value) const override
Addition of a vector to the matrix values element-wise.
Definition CsrMatrix.h:473
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:506
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