Peano
Loading...
Searching...
No Matches
DynamicMatrix.h
Go to the documentation of this file.
1// This file is part of the Peano project. For conditions of distribution and
2// use, please see the copyright notice at www.peano-framework.org
3#pragma once
4
5#include <string>
6
7#include "ScalarOperations.h"
8#include "tarch/Assertions.h"
9
10namespace tarch {
11 namespace la {
12 class DynamicMatrix;
13 } // namespace la
14} // namespace tarch
15
21private:
22 int _cols;
23 int _rows;
24 double* _m;
25
26 static inline int serialise(
27 int row,
28 int col,
29 [[maybe_unused]] int Rows,
30 int Cols
31 ) {
32 return col + row * Cols;
33 }
34
35 int inline serialise(int row, int col) const {
36 return serialise(row, col, _rows, _cols);
37 }
38
39public:
43 DynamicMatrix(int rows, int cols);
44
51
53 int rows,
54 int cols,
55 std::initializer_list<std::initializer_list<double>> values
56 );
57
60
67 const DynamicMatrix& lhs,
68 const DynamicMatrix& rhs,
69 bool innerProduct
70 );
71
72 DynamicMatrix(int rows, int cols, double* data);
73
75
95 std::initializer_list<std::initializer_list<double>> values
96 );
97 /*
98 template <int Cols>
99 DynamicMatrix& operator=( double values[][Cols] ) {
100 assertionEquals(Cols,_cols);
101 for (int col=0; col<_cols; col++)
102 for (int row=0; row<_rows; row++) {
103 _m[ serialise(row,col) ] = values[row][col];
104 }
105 return *this;
106 }
107 */
108
109 template <int Cols> bool operator==(double values[][Cols]) const {
110 bool result = Cols == _cols;
111 for (int col = 0; col < _cols; col++)
112 for (int row = 0; row < _rows; row++) {
113 result &= tarch::la::equals(_m[serialise(row, col)], values[row][col]);
114 }
115 return result;
116 }
117
118 bool operator==(const DynamicMatrix& matrix) const;
119
120 std::string toString(bool addLineBreaks = false) const;
124 static std::string vectorToString(
125 double* values,
126 int entries,
127 bool addLineBreaks = false
128 );
129
130 double* data() { return _m; }
131 const double* data() const { return _m; }
132
133 double& operator()(int row, int col);
134 double operator()(int row, int col) const;
135
136 void multiply(double* result, double* x);
137 void multiplyBySmallMatrix(double* result, const DynamicMatrix& matrix) const;
138
159 double* __restrict__ result,
160 const double* __restrict__ x,
161 int batchCount,
162 int resultSize,
163 int firstRow
164 );
165
170 double* __restrict__ result,
171 const double* __restrict__ x,
172 int batchCount
173 );
174
186 void replicateRows(
187 int blockSize,
188 int numberOfReplications,
189 int shiftAfterEveryReplication,
190 bool extendColumnsToAccommodateShifts
191 );
192 void replicateCols(
193 int blockSize,
194 int numberOfReplications,
195 int shiftAfterEveryReplication,
196 bool extendColumnsToAccommodateShifts
197 );
198
210 void insertEmptyColumns(int number, int where, int repeatEveryKColumns = 0);
211 void insertEmptyRows(int number, int where, int repeatEveryKColumns = 0);
212
216 void shiftRowsDown(int shift, bool wrap = false);
217 void shiftColumnsRight(int shift, bool wrap = false);
218
219 void removeColumn(int number);
220
224 void scale(double value);
225
229 static tarch::la::DynamicMatrix id(int rows);
230
231 int rows() const;
232 int cols() const;
233};
234
239 const tarch::la::DynamicMatrix& lhs,
240 const tarch::la::DynamicMatrix& rhs
241);
242
246);
247
248// void multiply( double* result, double* x );
tarch::la::DynamicMatrix operator*(const tarch::la::DynamicMatrix &A, const tarch::la::DynamicMatrix &B)
tarch::la::DynamicMatrix kroneckerProduct(const tarch::la::DynamicMatrix &lhs, const tarch::la::DynamicMatrix &rhs)
Wrapper around static routine, so I don't have to use full-qualified name.
My standard matrix is a matrix where the size is fixed at compile time.
double & operator()(int row, int col)
~DynamicMatrix()
Free the array on the heap.
void multiply(double *result, double *x)
void batchedMultiplyAoS(double *__restrict__ result, const double *__restrict__ x, int batchCount, int resultSize, int firstRow)
This operation assumes that x holds a whole batch of vectors in AoS format.
void insertEmptyRows(int number, int where, int repeatEveryKColumns=0)
static std::string vectorToString(double *values, int entries, bool addLineBreaks=false)
I often need this in combination with the toString() operation above.
void removeColumn(int number)
const double * data() const
void multiplyBySmallMatrix(double *result, const DynamicMatrix &matrix) const
void shiftColumnsRight(int shift, bool wrap=false)
DynamicMatrix & operator=(const DynamicMatrix &)=delete
bool operator==(double values[][Cols]) const
void shiftRowsDown(int shift, bool wrap=false)
Shift the rows to the right.
void insertEmptyColumns(int number, int where, int repeatEveryKColumns=0)
Insert zero columns.
void replicateRows(int blockSize, int numberOfReplications, int shiftAfterEveryReplication, bool extendColumnsToAccommodateShifts)
Split the matrix into blocks of rows of size blockSize.
DynamicMatrix(int rows, int cols)
Create empty matrix.
void replicateCols(int blockSize, int numberOfReplications, int shiftAfterEveryReplication, bool extendColumnsToAccommodateShifts)
int serialise(int row, int col) const
std::string toString(bool addLineBreaks=false) const
static tarch::la::DynamicMatrix id(int rows)
Create (square) identify matrix with rows rows and column.
static int serialise(int row, int col, int Rows, int Cols)
void scale(double value)
Scale all entries.
Vector< Rows, Scalar > col(const Matrix< Rows, Cols, Scalar > &matrix, int whichColumn)
Extract row from matrix.
bool equals(const Matrix< Rows, Cols, Scalar > &lhs, const Matrix< Rows, Cols, Scalar > &rhs, const Scalar &tolerance=NUMERICAL_ZERO_DIFFERENCE)
Compares to matrices on equality by means of a numerical accuracy.
Vector< Cols, Scalar > row(const Matrix< Rows, Cols, Scalar > &matrix, int whichRow)
Extract row from matrix.
Have to include this header, as I need access to the SYCL_EXTERNAL keyword.
Definition accelerator.h:19