MoCSI API Reference
Loading...
Searching...
No Matches
DenseMatrix.h
Go to the documentation of this file.
1#ifndef DENSE_MATRIX_H
2#define DENSE_MATRIX_H
3
4#include <iostream>
5#include <string>
6#include <valarray>
7#include <vector>
8
9#include "GenericMatrix.h"
10
15template <typename T>
16class DenseMatrix : public GenericMatrix<T>
17{
18 public:
20 DenseMatrix(int rows, int cols);
21 DenseMatrix(int rows, int cols, const std::valarray<T>& values);
22
23 T& operator()(int rows, int cols) override;
24 T operator()(int rows, int cols) const override;
25
26 DenseMatrix(const DenseMatrix&) = default;
27 DenseMatrix& operator=(const DenseMatrix&) = default;
28
31
32 std::valarray<T> scalarAdd(T value) const override;
33 std::valarray<T> matrixAddElementWise(const DenseMatrix& dense_matrix) const;
34 std::valarray<T> scalarSubtract(T value) const override;
35 std::valarray<T> matrixSubtractElementWise(const DenseMatrix& dense_matrix) const;
36 std::valarray<T> scalarMultiply(T value) const override;
37 std::valarray<T> matrixMultiplyElementWise(const DenseMatrix& dense_matrix) const;
39 std::valarray<T> scalarDivide(T value) const override;
40 std::valarray<T> matrixDivideElementWise(const DenseMatrix& dense_matrix) const;
41
42 void buildAtRuntime(int rows, int cols) override;
43 void updateNonZeroElements() override;
44 const std::vector<std::pair<int, int>>& getNonZeroElements() override
45 {
46 return this->m_non_zero_elements;
47 }
48
49 ~DenseMatrix() override = default;
50};
51
55template <typename T>
59
66template <typename T>
70
80template <typename T>
81DenseMatrix<T>::DenseMatrix(int rows, int cols, const std::valarray<T>& values)
82 : GenericMatrix<T>{rows, cols, values, false}
83{
84 if (values.size() != rows * cols)
85 {
86 throw std::invalid_argument("Size mismatch between dimensions and provided values.");
87 }
88}
89
96template <typename T>
97T& DenseMatrix<T>::operator()(const int rows, const int cols)
98{
99 if (rows >= this->m_rows || cols >= this->m_cols)
100 {
101 throw MatrixOutOfBoundsError(rows, cols, this->m_rows, this->m_cols);
102 }
103 return this->m_matrix[rows * this->m_cols + cols];
104}
105
113template <typename T>
114T DenseMatrix<T>::operator()(const int rows, const int cols) const
115{
116 if (rows >= this->m_rows || cols >= this->m_cols)
117 {
118 throw MatrixOutOfBoundsError(rows, cols, this->m_rows, this->m_cols);
119 }
120 return this->m_matrix[rows * this->m_cols + cols];
121}
122
128template <typename T>
129std::valarray<T> DenseMatrix<T>::scalarAdd(const T value) const
130{
131 return this->m_matrix + value;
132}
133
139template <typename T>
141{
142 return this->m_matrix + dense_matrix.m_matrix;
143}
144
150template <typename T>
151std::valarray<T> DenseMatrix<T>::scalarSubtract(const T value) const
152{
153 return this->m_matrix - value;
154}
155
161template <typename T>
163{
164 return this->m_matrix - dense_matrix.m_matrix;
165}
166
172template <typename T>
173std::valarray<T> DenseMatrix<T>::scalarMultiply(const T value) const
174{
175 return this->m_matrix * value;
176}
177
183template <typename T>
185{
186 // Multiply matrices A^(n x m) * B^(m x k) = C^(n x k)
187 // Not very efficient algorithm, please revise if used more intensily in the future!
188 std::valarray<T> result(this->m_rows * dense_matrix.m_cols);
189 for (int i = 0; i < this->m_rows; i++)
190 {
191 for (int j = 0; j < dense_matrix.m_cols; j++)
192 {
193 T sum = {0};
194 for (int k = 0; k < this->m_cols; k++)
195 {
196 sum += this->m_matrix[i * this->m_cols + k]
197 * dense_matrix.m_matrix[k * dense_matrix.m_cols + j];
198 }
199 result[i * this->m_cols + j] = sum;
200 }
201 }
202 return {this->m_rows, dense_matrix.m_cols, result};
203}
204
210template <typename T>
212{
213 return this->m_matrix * dense_matrix.m_matrix;
214}
215
221template <typename T>
222std::valarray<T> DenseMatrix<T>::scalarDivide(const T value) const
223{
224 return this->m_matrix / value;
225}
226
232template <typename T>
234{
235 return this->m_matrix / dense_matrix.m_matrix;
236}
237
244template <typename T>
246{
247 std::pair<int, int> size = dm_1.size();
248 return {size.first, size.second, dm_1.matrix_add_element_wise(dm_2)};
249}
250
257template <typename T>
259{
260 std::pair<int, int> size = dense_matrix.size();
261 return {size.first, size.second, dense_matrix.scalarAdd(value)};
262}
263
271template <typename T>
273{
274 std::pair<int, int> size = dense_matrix.size();
275 return {size.first, size.second, dense_matrix.scalarAdd(value)};
276}
277
284template <typename T>
286{
287 std::pair<int, int> size = dm_1.size();
288 return {size.first, size.second, dm_1.matrix_subtract_element_wise(dm_2)};
289}
290
297template <typename T>
299{
300 std::pair<int, int> size = dense_matrix.size();
301 return {size.first, size.second, dense_matrix.scalarSubtract(value)};
302}
303
311template <typename T>
313{
314 std::pair<int, int> size = dense_matrix.size();
315 return {size.first, size.second, -dense_matrix.scalarSubtract(value)};
316}
317
328template <typename T>
330{
331 std::pair<int, int> size = dm_1.size();
332 return {size.first, size.second, dm_1.matrixMultiplyElementWise(dm_2)};
333}
334
341template <typename T>
343{
344 std::pair<int, int> size = dense_matrix.size();
345 return {size.first, size.second, dense_matrix.scalarMultiply(value)};
346}
347
358template <typename T>
360{
361 std::pair<int, int> size = dense_matrix.size();
362 return {size.first, size.second, dense_matrix.scalarMultiply(value)};
363}
364
371template <typename T>
373{
374 std::pair<int, int> size = dm_1.size();
375 return {size.first, size.second, dm_1.matrix_divide_element_wise(dm_2)};
376}
377
384template <typename T>
386{
387 std::pair<int, int> size = dense_matrix.size();
388 return {size.first, size.second, dense_matrix.scalarDivide(value)};
389}
390
397template <typename T>
399{
400 std::pair<int, int> size = dense_matrix.size();
401 return {size.first, size.second, 1 / dense_matrix.scalarDivide(value)};
402}
403
410template <typename T>
411std::ostream& operator<<(std::ostream& out, const DenseMatrix<T>& dense_matrix)
412{
413 std::pair<int, int> size = dense_matrix.size();
414 for (int i = 0; i < size.first; i++)
415 {
416 for (int j = 0; j < size.second; j++)
417 {
418 std::cout << dense_matrix(i, j) << " ";
419 }
420 std::cout << "\n";
421 }
422 return out;
423}
424
425template <typename T>
426void DenseMatrix<T>::buildAtRuntime(const int rows, const int cols)
427{
428 this->m_rows = rows;
429 this->m_cols = cols;
430 this->m_matrix.resize(rows * cols);
431}
432
433template <typename T>
435{
436 for (int i{0}; i < this->m_rows; i++)
437 {
438 for (int j{0}; j < this->m_cols; j++)
439 {
440 if (this->m_matrix[i * this->m_cols + j] != 0)
441 {
442 this->m_non_zero_elements.emplace_back(i, j);
443 }
444 }
445 }
446}
447#endif
DenseMatrix< T > operator+(const DenseMatrix< T > &dm_1, const DenseMatrix< T > &dm_2)
Overload of the binary + operator.
Definition DenseMatrix.h:245
DenseMatrix< T > operator/(const DenseMatrix< T > &dm_1, const DenseMatrix< T > &dm_2)
Overload of the binary / operator.
Definition DenseMatrix.h:372
DenseMatrix< T > operator*(const DenseMatrix< T > &dm_1, const DenseMatrix< T > &dm_2)
Overload of the binary * operator. Does matrix multiplication/matrix product.
Definition DenseMatrix.h:329
DenseMatrix< T > operator-(const DenseMatrix< T > &dm_1, const DenseMatrix< T > &dm_2)
Overload of the binary - operator.
Definition DenseMatrix.h:285
std::ostream & operator<<(std::ostream &out, const DenseMatrix< T > &dense_matrix)
Overload of the << operator.
Definition DenseMatrix.h:411
Concrete implemenatation of a matrix class representing a dense matrix. Offers basic access and eleme...
Definition DenseMatrix.h:17
DenseMatrix< T > matrixMultiply(const DenseMatrix &dense_matrix) const
Mathematical matrix multiplication of two DenseMatrix objects.
Definition DenseMatrix.h:184
std::valarray< T > matrixAddElementWise(const DenseMatrix &dense_matrix) const
Addition of two DenseMatrix objects element-wise.
Definition DenseMatrix.h:140
DenseMatrix(DenseMatrix &&)=default
DenseMatrix & operator=(DenseMatrix &&)=default
DenseMatrix & operator=(const DenseMatrix &)=default
const std::vector< std::pair< int, int > > & getNonZeroElements() override
Definition DenseMatrix.h:44
DenseMatrix(const DenseMatrix &)=default
std::valarray< T > scalarSubtract(T value) const override
Subtraction of a scalar from the matrix values element-wise.
Definition DenseMatrix.h:151
std::valarray< T > matrixMultiplyElementWise(const DenseMatrix &dense_matrix) const
Multiplication of two DenseMatrix objects element-wise.
Definition DenseMatrix.h:211
std::valarray< T > matrixDivideElementWise(const DenseMatrix &dense_matrix) const
Division of two DenseMatrix objects element-wise.
Definition DenseMatrix.h:233
std::valarray< T > matrixSubtractElementWise(const DenseMatrix &dense_matrix) const
Subtraction of two DenseMatrix objects element-wise.
Definition DenseMatrix.h:162
std::valarray< T > scalarMultiply(T value) const override
Multiplication of a scalar to the matrix values element-wise.
Definition DenseMatrix.h:173
~DenseMatrix() override=default
std::valarray< T > scalarAdd(T value) const override
Addition of a scalar to the matrix values element-wise.
Definition DenseMatrix.h:129
void buildAtRuntime(int rows, int cols) override
Definition DenseMatrix.h:426
T & operator()(int rows, int cols) override
Parentheses operator to allow access to the matrix values as Matrix(row,column).
Definition DenseMatrix.h:97
std::valarray< T > scalarDivide(T value) const override
Division of a scalar from the matrix values element-wise.
Definition DenseMatrix.h:222
void updateNonZeroElements() override
Definition DenseMatrix.h:434
DenseMatrix()
Constructor used for buildAtRuntime.
Definition DenseMatrix.h:56
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
std::vector< std::pair< int, int > > m_non_zero_elements
Definition GenericMatrix.h:23
Error class to throw when the matrix is accessed out of bounds. Inherits from std::exception.
Definition GenericMatrix.h:122