-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixUT.hpp
292 lines (253 loc) · 11.1 KB
/
MatrixUT.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
Matrix linear algebra library for ML applications (ML: Machine Learning)
Some errors were corrected, many methods and constructors added
(c) Lesept june 2022 [email protected]
*/
#ifndef __ML_MATRIX_HPP
#define __ML_MATRIX_HPP
#include <Arduino.h>
template <typename T> class MLMatrix {
private:
std::vector<std::vector<T> > mat;
size_t rows;
size_t cols;
public:
MLMatrix() = default;
template<typename U> explicit MLMatrix(MLMatrix<U> const &, const T = 0);
template<typename U> explicit MLMatrix(const std::vector<U>&);
template<typename U> explicit MLMatrix(const std::vector<std::vector<U> >&);
template<typename U> explicit MLMatrix(const U rhs[], const unsigned);
explicit MLMatrix(unsigned, unsigned, const T, const T);
explicit MLMatrix(unsigned, unsigned, const T = 0);
virtual ~MLMatrix();
// Access the individual elements: mat(i,j)
typename std::vector<T>::reference operator()(const unsigned& row, const unsigned& col);
T const operator()(const unsigned& row, const unsigned& col) const;
// Operator overloading, for "standard" mathematical matrix operations
template<typename U> MLMatrix<T>& operator=(const MLMatrix<U>&); // copy a matrix
template<typename U> MLMatrix<T>& operator=(const std::vector<U>&); // copy a vector
template<typename U> MLMatrix<T>& operator=(const std::vector<std::vector<U> >&); // copy a vector of vectors
template<typename U> MLMatrix<T>& fromArray(const U rhs[], const unsigned); // copy an array
template<typename U> MLMatrix<T>& operator+=(const MLMatrix<U>&);
template<typename U> MLMatrix<T>& operator-=(const MLMatrix<U>&);
template<typename U> MLMatrix<T>& operator*=(const MLMatrix<U>&);
// Matrix comparison
template<typename U> const bool operator==(const MLMatrix<U> &) const;
template<typename U> const bool operator!=(const MLMatrix<U> &) const;
template<typename U> MLMatrix<bool> operator< (const MLMatrix<U> &);
template<typename U> MLMatrix<bool> operator>=(const MLMatrix<U> &);
// Operations on matrices
MLMatrix<T> transpose();
MLMatrix<T> square ();
template<typename U> MLMatrix<T> Hadamard (const MLMatrix<U>& rhs, bool clip=false);
// Matrix/scalar operations
template<typename U> MLMatrix<T>& operator+=(const U& rhs);
template<typename U> MLMatrix<T>& operator-=(const U& rhs);
template<typename U> MLMatrix<T>& operator*=(const U& rhs);
template<typename U> MLMatrix<T>& operator/=(const U& rhs);
// Matrix/vector operations
template<typename U> std::vector<T>& operator*=(const std::vector<U>& rhs);
std::vector<T> diag_vec();
// Vector operations
template<typename U> auto MdotProd(const MLMatrix<U>& rhs, bool clip=false) -> decltype(std::declval<U>()*std::declval<T>());
// MLMatrix<T> times(const MLMatrix<T>& rhs, bool clip=false);
// Extract row or col
MLMatrix<T> row(const uint16_t);
MLMatrix<T> col(const uint16_t);
MLMatrix<T> subMatrix(const uint16_t, const uint16_t, const uint16_t, const uint16_t);
// Pruning functions
std::vector<T> sortValues(bool = true);
bool zeroRow(int);
bool zeroCol(int);
uint16_t countZeroRow(int);
uint16_t countZeroCol(int);
// Remove element
MLMatrix<T> removeRow(const uint16_t);
MLMatrix<T> removeCol(const uint16_t);
// Norms
int L0Norm();
T L1Norm();
T L2Norm();
T max() const;
T min() const;
float mean() const;
float stdev(const float) const;
float meanRow(int);
// Find min and max values index
void indexMin(int &, int &);
void indexMax(int &, int &);
// Display the matrix
void print();
void printBool();
void printSize(); // Only display the size (rows, cols)
// Access the row and column sizes
unsigned get_rows() const;
unsigned get_cols() const;
void setSize(const int _rows, const int _cols, const T = T(0));
// Misc
MLMatrix<T> applySelf(T (*function)(T));
MLMatrix<T> apply(T (*function)(T)) ;
MLMatrix<T> randomChange(const float);
MLMatrix<T> randomNormal(const float, const float);
MLMatrix<T> normScale (float, bool &);
bool normScale2 (float);
int clipToZero (float);
int clipMin (float);
int clipMax (float);
MLMatrix<T> sgn();
void setZeroCol(const int);
void setZeroRow(const int);
void setCol(const int, const T);
void setRow(const int, const T);
void setColMat(const int, const MLMatrix<T>);
void setRowMat(const int, const MLMatrix<T>);
MLMatrix<uint8_t> dropout(const float);
// DeepShift methods
MLMatrix<uint8_t> matRound(uint8_t = 10);
};
template<typename U,typename T> auto operator+(
MLMatrix<U> const &lhs,
MLMatrix<T> const &rhs) ->
MLMatrix<decltype(std::declval<U>()+std::declval<T>())>
{
if ( lhs.get_rows() != rhs.get_rows() || lhs.get_cols() != rhs.get_cols() ) { // matrices of different sizes
Serial.printf("Addition error: dimensions do not match (%d, %d)+(%d, %d)",
lhs.get_rows(), lhs.get_cols(), rhs.get_rows(), rhs.get_cols());
while(1);
}
MLMatrix<decltype(std::declval<U>()+std::declval<T>())> Res(lhs.get_rows(), lhs.get_cols());
for (size_t i = 0; i < lhs.get_rows(); ++i)
for (size_t j = 0; j < lhs.get_cols(); ++j)
Res(i,j) = lhs(i,j) + rhs(i,j);
return Res;
}
template<typename U,typename T> auto operator-(
MLMatrix<U> const &lhs,
MLMatrix<T> const &rhs) ->
MLMatrix<decltype(std::declval<U>()-std::declval<T>())>
{
if ( lhs.get_rows() != rhs.get_rows() || lhs.get_cols() != rhs.get_cols() ) { // matrices of different sizes
Serial.printf("Substraction error: dimensions do not match (%d, %d)-(%d, %d)",
lhs.get_rows(), lhs.get_cols(), rhs.get_rows(), rhs.get_cols());
while(1);
}
MLMatrix<decltype(std::declval<U>()-std::declval<T>())> Res(lhs.get_rows(), lhs.get_cols());
for (size_t i = 0; i < lhs.get_rows(); ++i)
for (size_t j = 0; j < lhs.get_cols(); ++j)
Res(i,j) = lhs(i,j) - rhs(i,j);
return Res;
}
template<typename U,typename T> auto operator*(
MLMatrix<U> const &lhs,
MLMatrix<T> const &rhs) ->
MLMatrix<decltype(std::declval<U>()*std::declval<T>())>
{
if ( lhs.get_cols() != rhs.get_rows() ) { // matrices of different sizes
Serial.printf("Multiplication error: dimensions do not match (%d, %d)*(%d, %d)",
lhs.get_rows(), lhs.get_cols(), rhs.get_rows(), rhs.get_cols());
while(1);
}
MLMatrix<decltype(std::declval<U>()*std::declval<T>())> Res(lhs.get_rows(), rhs.get_cols(), 0);
for (size_t i = 0; i < lhs.get_rows(); ++i)
for (size_t j = 0; j < rhs.get_cols(); ++j)
for (size_t k = 0; k < rhs.get_rows(); ++k)
Res(i,j) += lhs(i,k) * rhs(k,j);
return Res;
}
// Matrix/scalar operations
template<typename U,typename T> auto operator+(
MLMatrix<U> const &lhs,
T const &rhs) ->
MLMatrix<decltype(std::declval<U>()+std::declval<T>())>
{
MLMatrix<decltype(std::declval<U>()+std::declval<T>())> Res(lhs.get_rows(), lhs.get_cols());
for (size_t i = 0; i < lhs.get_rows(); ++i)
for (size_t j = 0; j < lhs.get_cols(); ++j)
Res(i,j) = lhs(i,j) + rhs;
return Res;
}
template<typename U,typename T> auto operator-(
MLMatrix<U> const &lhs,
T const &rhs) ->
MLMatrix<decltype(std::declval<U>()-std::declval<T>())>
{
MLMatrix<decltype(std::declval<U>()-std::declval<T>())> Res(lhs.get_rows(), lhs.get_cols());
for (size_t i = 0; i < lhs.get_rows(); ++i)
for (size_t j = 0; j < lhs.get_cols(); ++j)
Res(i,j) = lhs(i,j) - rhs;
return Res;
}
template<typename U,typename T> auto operator*(
MLMatrix<U> const &lhs,
T const &rhs) ->
MLMatrix<decltype(std::declval<U>()*std::declval<T>())>
{
MLMatrix<decltype(std::declval<U>()*std::declval<T>())> Res(lhs.get_rows(), lhs.get_cols());
for (size_t i = 0; i < lhs.get_rows(); ++i)
for (size_t j = 0; j < lhs.get_cols(); ++j)
Res(i,j) = lhs(i,j) * rhs;
return Res;
}
template<typename U,typename T> auto operator/(
MLMatrix<U> const &lhs,
T const &rhs) ->
MLMatrix<decltype(std::declval<U>()/std::declval<T>())>
{
if (rhs == 0) {
Serial.println("Error: division by 0");
return lhs;
}
MLMatrix<decltype(std::declval<U>()+std::declval<T>())> Res(lhs.get_rows(), lhs.get_cols());
for (size_t i = 0; i < lhs.get_rows(); ++i)
for (size_t j = 0; j < lhs.get_cols(); ++j)
Res(i,j) = lhs(i,j) / rhs;
return Res;
}
template<typename U,typename T> auto operator+(
T const &rhs, MLMatrix<U> const &lhs) ->
MLMatrix<decltype(std::declval<U>()+std::declval<T>())>
{
MLMatrix<decltype(std::declval<U>()+std::declval<T>())> Res(lhs.get_rows(), lhs.get_cols());
for (size_t i = 0; i < lhs.get_rows(); ++i)
for (size_t j = 0; j < lhs.get_cols(); ++j)
Res(i,j) = lhs(i,j) + rhs;
return Res;
}
template<typename U,typename T> auto operator-(
T const &rhs, MLMatrix<U> const &lhs) ->
MLMatrix<decltype(std::declval<U>()-std::declval<T>())>
{
MLMatrix<decltype(std::declval<U>()-std::declval<T>())> Res(lhs.get_rows(), lhs.get_cols());
for (size_t i = 0; i < lhs.get_rows(); ++i)
for (size_t j = 0; j < lhs.get_cols(); ++j)
Res(i,j) = lhs(i,j) - rhs;
return Res;
}
template<typename U,typename T> auto operator*(
T const &rhs, MLMatrix<U> const &lhs) ->
MLMatrix<decltype(std::declval<U>()*std::declval<T>())>
{
MLMatrix<decltype(std::declval<U>()*std::declval<T>())> Res(lhs.get_rows(), lhs.get_cols());
for (size_t i = 0; i < lhs.get_rows(); ++i)
for (size_t j = 0; j < lhs.get_cols(); ++j)
Res(i,j) = lhs(i,j) * rhs;
return Res;
}
// Matrix/vector operations
template<typename U,typename T> auto operator*(
MLMatrix<T> const &lhs, std::vector<U> const &rhs) ->
std::vector<decltype(std::declval<T>()*std::declval<U>())>
{
if (lhs.get_cols() != rhs.size()) {
Serial.printf("Multiplication error: dimensions do not match (%d, %d)*(%d)",
lhs.get_rows(), lhs.get_cols(), rhs.size());
while(1);
}
std::vector<decltype(std::declval<T>()*std::declval<U>())> Res(lhs.get_rows(), 0);
for (size_t i = 0; i < lhs.get_rows(); ++i)
for (size_t j = 0; j < lhs.get_cols(); ++j)
Res[i] += lhs(i,j) * rhs[j];
return Res;
}
#include "MatrixUT.tpp"
#endif