Peano
Loading...
Searching...
No Matches
MatrixMatrixOperations.cpph
Go to the documentation of this file.
1#ifdef UseOpenblas
2#include <cblas.h>
3#endif
4
5template <int Rows, int Cols, int X, typename Scalar>
7 const Matrix<Rows, X, Scalar>& lhs,
8 const Matrix<X, Cols, Scalar>& rhs
9) {
10 Matrix<Rows, Cols, Scalar> result(0);
11
12 for (int i = 0; i < Rows; i++) {
13 for (int j = 0; j < Cols; j++) {
14 for (int k = 0; k < X; k++) {
15 result(i, j) += lhs(i, k) * rhs(k, j);
16 }
17 }
18 }
19
20 return result;
21}
22
23
24template <int Rows, int Cols, int X, typename Scalar>
26 const Matrix<Rows, X, Scalar>& lhs,
27 const Matrix<X, Cols, Scalar>& rhs
28) {
29 Matrix<Rows, Cols, Scalar> result;
30
31 for (int i = 0; i < Rows; i++) {
32 for (int j = 0; j < Cols; j++) {
33 result(i, j) = lhs(i, j) * rhs(i, j);
34 }
35 }
36
37 return result;
38}
39
40template <int Rows, int Cols, int X, typename Scalar>
42 const Matrix<Rows, X, Scalar>& lhs,
43 const Matrix<X, Cols, Scalar>& rhs
44) {
45 return multiply(lhs, rhs);
46}
47
48#ifdef UseOpenblas
57template <int Rows, int Cols, int X>
59 const Matrix<Rows, X, double>& lhs,
60 const Matrix<X, Cols, double>& rhs
61) {
62 Matrix<Rows, Cols, double> output;
63 cblas_dgemm(
64 CblasRowMajor,
65 CblasNoTrans,
66 CblasNoTrans,
67 Rows,
68 Cols,
69 X,
70 1.0,
71 lhs.data(),
72 X,
73 rhs.data(),
74 Cols,
75 0.0,
76 output.data(),
77 Cols
78 );
79 return output;
80}
81#endif
82
83template <int Rows, int Cols, typename Scalar>
87) {
88 for (int i = 0; i < Rows; i++) {
89 for (int j = 0; j < Cols; j++) {
90 if (lhs(i, j) != rhs(i, j)) {
91 return false;
92 }
93 }
94 }
95 return true;
96}
97
98
99template <int Rows, int Cols, typename Scalar>
103 const Scalar& tolerance
104) {
105 for (int i = 0; i < Rows; i++) {
106 for (int j = 0; j < Cols; j++) {
107 if (!equals(lhs(i, j), rhs(i, j), tolerance)) {
108 return false;
109 }
110 }
111 }
112 return true;
113}
114
115
116template <int Rows, int Cols, typename Scalar>
118 const Matrix<Rows, Cols, Scalar>& lhs,
119 const Matrix<Rows, Cols, Scalar>& rhs
120) {
122#ifdef CompilerICC
123#pragma ivdep
124#endif
125 for (int i = 0; i < Rows; i++) {
126 for (int j = 0; j < Cols; j++) {
127 result(i, j) = lhs(i, j) + rhs(i, j);
128 }
129 }
130 return result;
131}
132
133
134template <int Rows, int Cols, typename Scalar>
136 const Matrix<Rows, Cols, Scalar>& lhs,
137 const Matrix<Rows, Cols, Scalar>& rhs
138) {
140#ifdef CompilerICC
141#pragma ivdep
142#endif
143 for (int i = 0; i < Rows; i++) {
144 for (int j = 0; j < Cols; j++) {
145 result(i, j) = lhs(i, j) - rhs(i, j);
146 }
147 }
148 return result;
149}
150
151
152template <int Rows, int Cols, typename Scalar>
156 const Scalar& tolerance
157) {
158 for (int i = 0; i < Rows; i++) {
159 for (int j = 0; j < Cols; j++) {
160 if (std::abs(lhs(i, j) - rhs(i, j)) > tolerance)
161 return std::pair<int, int>(i, j);
162 }
163 }
164 return std::pair<int, int>(-1, -1);
165}
Static (i.e.
Definition Matrix.h:31
j
Definition euler.py:99
CF abs(const CF &cf)
Matrix< Rows, Cols, Scalar > multiply(const Matrix< Rows, X, Scalar > &lhs, const Matrix< X, Cols, Scalar > &rhs)
Performs a matrix x matrix multiplication.
bool operator==(const Matrix< Rows, Cols, Scalar > &lhs, const Matrix< Rows, Cols, Scalar > &rhs)
Bitwise comparison of the components of two matrices on equality.
Matrix< Rows, Cols, Scalar > operator-(const Matrix< Rows, Cols, Scalar > &lhs, const Matrix< Rows, Cols, Scalar > &rhs)
Matrix< Rows, Cols, Scalar > operator+(const Matrix< Rows, Cols, Scalar > &lhs, const Matrix< Rows, Cols, Scalar > &rhs)
Matrix< Rows, Cols, Scalar > operator*(const Matrix< Rows, X, Scalar > &lhs, const Matrix< X, Cols, Scalar > &rhs)
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.
std::pair< int, int > equalsReturnIndex(const Matrix< Rows, Cols, Scalar > &lhs, const Matrix< Rows, Cols, Scalar > &rhs, const Scalar &tolerance=NUMERICAL_ZERO_DIFFERENCE)
Return Index of element which is not equals.
Matrix< Rows, Cols, Scalar > multiplyComponents(const Matrix< Rows, X, Scalar > &lhs, const Matrix< X, Cols, Scalar > &rhs)