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
15template <typename T>
17{
18 protected:
19 int m_rows{0};
20 int m_cols{0};
21
22 std::valarray<T> m_matrix{};
23 std::vector<std::pair<int, int>> m_non_zero_elements;
24
25 public:
27 GenericMatrix(int rows, int cols);
28 GenericMatrix(int rows, int cols, const std::valarray<T>& values,
30
31 GenericMatrix(const GenericMatrix&) = default;
33
36
37 std::pair<int, int> size() const;
38
39 virtual T& operator()(int rows, int cols) = 0;
40 virtual T operator()(int rows, int cols) const = 0;
41
42 virtual std::valarray<T> scalarAdd(T value) const = 0;
43 virtual std::valarray<T> scalarSubtract(T value) const = 0;
44 virtual std::valarray<T> scalarMultiply(T value) const = 0;
45 virtual std::valarray<T> scalarDivide(T value) const = 0;
46
47 virtual void buildAtRuntime(int rows, int cols) = 0;
48
49 virtual void updateNonZeroElements() = 0;
50 virtual const std::vector<std::pair<int, int>>& getNonZeroElements() = 0;
51
52 void zeroMatrix();
53
54 virtual ~GenericMatrix() = default;
55};
56
61template <typename T>
65
72template <typename T>
74{
75 m_matrix.resize(rows * cols);
76}
77
87template <typename T>
88GenericMatrix<T>::GenericMatrix(int rows, int cols, const std::valarray<T>& values,
90 : m_rows{rows}, m_cols{cols}, m_matrix{values}
91{
92 std::cout << "\n";
93 if (values.size() != rows * cols && !allow_mismatched_size)
94 {
95 throw std::invalid_argument("Size mismatch between dimensions and provided values.");
96 }
97}
98
102template <typename T>
103std::pair<int, int> GenericMatrix<T>::size() const
104{
105 return std::pair<int, int>{m_rows, m_cols};
106}
107
111template <typename T>
113{
114 m_matrix *= 0;
115}
116
121class MatrixOutOfBoundsError : public std::exception
122{
123 private:
124 std::string m_error_string;
125
126 public:
137 {
138 std::ostringstream oss;
139 oss << "Tried accessing matrix at index (" << access_row << ", " << access_col << ").\n"
140 << "Nominal bounds of this matrix are: (" << rows << ", " << cols << ").\n";
141 m_error_string = oss.str();
142 }
143
147 [[nodiscard]] const char* what() const noexcept override { return m_error_string.c_str(); }
148
149 ~MatrixOutOfBoundsError() override = default;
150};
151
152#endif
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:17
void zeroMatrix()
Function that zeros out the matrix.
Definition GenericMatrix.h:112
GenericMatrix(GenericMatrix &&)=default
virtual void buildAtRuntime(int rows, int cols)=0
virtual std::valarray< T > scalarSubtract(T value) const =0
std::valarray< T > m_matrix
Definition GenericMatrix.h:22
GenericMatrix(const GenericMatrix &)=default
std::vector< std::pair< int, int > > m_non_zero_elements
Definition GenericMatrix.h:23
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:103
virtual std::valarray< T > scalarAdd(T value) const =0
virtual T & operator()(int rows, int cols)=0
GenericMatrix & operator=(GenericMatrix &&)=default
int m_rows
Definition GenericMatrix.h:19
virtual void updateNonZeroElements()=0
virtual ~GenericMatrix()=default
GenericMatrix()
Constructor for an empty GenericMatrix object. This should only be used for dynamically created matri...
Definition GenericMatrix.h:62
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
int m_cols
Definition GenericMatrix.h:20
Error class to throw when the matrix is accessed out of bounds. Inherits from std::exception.
Definition GenericMatrix.h:122
const char * what() const noexcept override
Supplies the error message as a c-style string/char array.
Definition GenericMatrix.h:147
~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:136