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";
108 if (values.size() != rows * cols && !allow_mismatched_size)
109 {
110 throw std::invalid_argument("Size mismatch between dimensions and provided values.");
111 }
112}
113
117template <typename T>
118std::pair<int, int> GenericMatrix<T>::size() const
119{
120 return std::pair<int, int>{m_rows, m_cols};
121}
122
126template <typename T>
128{
129 m_matrix *= 0;
130}
131
139template <typename T>
141{
142 m_matrix = value;
143}
144
149class MatrixOutOfBoundsError : public std::exception
150{
151 private:
152 std::string m_error_string;
153
154 public:
165 {
166 std::ostringstream oss;
167 oss << "Tried accessing matrix at index (" << access_row << ", " << access_col << ").\n"
168 << "Nominal bounds of this matrix are: (" << rows << ", " << cols << ").\n";
169 m_error_string = oss.str();
170 }
171
175 [[nodiscard]] const char* what() const noexcept override { return m_error_string.c_str(); }
176
177 ~MatrixOutOfBoundsError() override = default;
178};
179
180#endif
MatrixType
Definition CoreEnums.h:33
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:19
virtual const MatrixType getMatrixType()=0
void zeroMatrix()
Function that zeros out the matrix.
Definition GenericMatrix.h:127
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:140
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:118
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:150
const char * what() const noexcept override
Supplies the error message as a c-style string/char array.
Definition GenericMatrix.h:175
~MatrixOutOfBoundsError() override=default
MatrixOutOfBoundsError(int access_row, int access_col, int rows, int cols)
Constructor for the MatrixErrorOutOfBounds class. Builds the error message and saves it to the member...
Definition GenericMatrix.h:164