MoCSI API Reference
Loading...
Searching...
No Matches
GenericMatrix.h
Go to the documentation of this file.
1#ifndef GENERIC_MATRIX_H
2#define GENERIC_MATRIX_H
3#include <exception>
4#include <iostream>
5#include <sstream>
6#include <string>
7#include <valarray>
8#include <vector>
9
10#include "CoreEnums.h"
11
17template <typename T>
19{
20 protected:
21 int m_rows{0};
22 int m_cols{0};
23
24 std::valarray<T> m_matrix{};
25 std::vector<std::pair<int, int>> m_non_zero_elements;
26
27 public:
29 GenericMatrix(int rows, int cols);
30 GenericMatrix(int rows, int cols, const std::valarray<T>& values,
32
33 GenericMatrix(const GenericMatrix&) = default;
35
38
39 std::pair<int, int> size() const;
40
41 virtual T& operator()(int rows, int cols) = 0;
42 virtual T operator()(int rows, int cols) const = 0;
43
44 virtual std::valarray<T> scalarAdd(T value) const = 0;
45 virtual std::valarray<T> scalarSubtract(T value) const = 0;
46 virtual std::valarray<T> scalarMultiply(T value) const = 0;
47 virtual std::valarray<T> scalarDivide(T value) const = 0;
48
49 virtual std::valarray<T> vectorElemAdd(const std::valarray<T>& value) const = 0;
50 virtual std::valarray<T> vectorElemSubtract(const std::valarray<T>& value) const = 0;
51 virtual std::valarray<T> vectorElemMultiply(const std::valarray<T>& value) const = 0;
52 virtual std::valarray<T> vectorElemDivide(const std::valarray<T>& value) const = 0;
53
54 virtual std::valarray<T> matrixVectorMultiplication(
55 const std::valarray<T>& value) const = 0;
56
57 virtual void buildAtRuntime(int rows, int cols) = 0;
58 virtual void registerElement(T value, int rows, int cols) {};
59
60 virtual void updateNonZeroElements() = 0;
61 virtual const std::vector<std::pair<int, int>>& getNonZeroElements() = 0;
62
63 virtual const MatrixType getMatrixType() = 0;
64
65 void zeroMatrix();
66
67 void overwriteMatrixValues(const std::valarray<T>& value);
68
69 virtual ~GenericMatrix() = default;
70};
71
76template <typename T>
80
87template <typename T>
89{
90 m_matrix.resize(rows * cols);
91}
92
102template <typename T>
103GenericMatrix<T>::GenericMatrix(int rows, int cols, const std::valarray<T>& values,
105 : m_rows{rows}, m_cols{cols}, m_matrix{values}
106{
107 std::cout << "\n";
109 {
110 std::cerr << "Size mismatch between dimensions and provided values.\n";
111 throw std::invalid_argument(
112 static_cast<std::string>("Size mismatch between dimensions and provided values."));
113 }
114}
115
119template <typename T>
120std::pair<int, int> GenericMatrix<T>::size() const
121{
122 return std::pair<int, int>{m_rows, m_cols};
123}
124
128template <typename T>
130{
131 m_matrix *= 0;
132}
133
141template <typename T>
143{
144 m_matrix = value;
145}
146
151class MatrixOutOfBoundsError : public std::exception
152{
153 private:
154 std::string m_error_string;
155
156 public:
167 const std::string& tag)
168 {
169 std::ostringstream oss;
170 oss << tag << " Tried accessing matrix at index (" << access_row << ", " << access_col
171 << ").\n"
172 << "Nominal bounds of this matrix are: (" << rows << ", " << cols << ").\n";
173 m_error_string = oss.str();
174 }
175
179 [[nodiscard]] const char* what() const noexcept override { return m_error_string.c_str(); }
180
181 ~MatrixOutOfBoundsError() override = default;
182};
183
184#endif
MatrixType
Definition CoreEnums.h:36
Concrete implementation of a matrix class representing a Compressed Sparse Rows (CSR) matrix....
Definition CsrMatrix.h:37
CsrSparseMatrix()
Constructor for an empty CsrSparseMatrix object. Leaves all storage arrays empty but sets the flag to...
Definition CsrMatrix.h:94
Template generalized Matrix object that should be the base for all further implementations of a matri...
Definition GenericMatrix.h:19
virtual const MatrixType getMatrixType()=0
void zeroMatrix()
Function that zeros out the matrix.
Definition GenericMatrix.h:129
GenericMatrix(GenericMatrix &&)=default
virtual void buildAtRuntime(int rows, int cols)=0
virtual std::valarray< T > scalarSubtract(T value) const =0
void overwriteMatrixValues(const std::valarray< T > &value)
Overwrites the matrix values with a new valarray. This is used for optimized overwrite when arithmeti...
Definition GenericMatrix.h:142
virtual std::valarray< T > vectorElemMultiply(const std::valarray< T > &value) const =0
std::valarray< T > m_matrix
Definition GenericMatrix.h:24
GenericMatrix(const GenericMatrix &)=default
std::vector< std::pair< int, int > > m_non_zero_elements
Definition GenericMatrix.h:25
GenericMatrix & operator=(const GenericMatrix &)=default
std::pair< int, int > size() const
Function that returns the size of the matrix as a pair in (rows, columns) format.
Definition GenericMatrix.h:120
virtual std::valarray< T > scalarAdd(T value) const =0
virtual void registerElement(T value, int rows, int cols)
Definition GenericMatrix.h:58
virtual T & operator()(int rows, int cols)=0
GenericMatrix & operator=(GenericMatrix &&)=default
int m_rows
Definition GenericMatrix.h:21
virtual void updateNonZeroElements()=0
virtual std::valarray< T > vectorElemAdd(const std::valarray< T > &value) const =0
virtual ~GenericMatrix()=default
GenericMatrix()
Constructor for an empty GenericMatrix object. This should only be used for dynamically created matri...
Definition GenericMatrix.h:77
virtual std::valarray< T > vectorElemDivide(const std::valarray< T > &value) const =0
virtual std::valarray< T > scalarMultiply(T value) const =0
virtual const std::vector< std::pair< int, int > > & getNonZeroElements()=0
virtual T operator()(int rows, int cols) const =0
virtual std::valarray< T > scalarDivide(T value) const =0
virtual std::valarray< T > vectorElemSubtract(const std::valarray< T > &value) const =0
virtual std::valarray< T > matrixVectorMultiplication(const std::valarray< T > &value) const =0
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:152
MatrixOutOfBoundsError(int access_row, int access_col, int rows, int cols, const std::string &tag)
Constructor for the MatrixErrorOutOfBounds class. Builds the error message and saves it to the member...
Definition GenericMatrix.h:166
const char * what() const noexcept override
Supplies the error message as a c-style string/char array.
Definition GenericMatrix.h:179
~MatrixOutOfBoundsError() override=default