-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixUT.tpp
987 lines (882 loc) · 28.4 KB
/
MatrixUT.tpp
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
#ifndef __ML_MATRIX_TPP
#define __ML_MATRIX_TPP
/***************************************************************************************************
Constructors
*/
// Create and fill a matrix (with 0s if not specified)
template<typename T>
MLMatrix<T>::MLMatrix(unsigned _rows, unsigned _cols, const T initial) {
rows = _rows;
cols = _cols;
mat.resize(rows);
for (unsigned i = 0; i < rows; ++i) {
mat[i].resize(cols, initial);
}
}
// random matrix Constructor
template<typename T> MLMatrix<T>::MLMatrix(unsigned _rows, unsigned _cols, const T min, const T max)
{
rows = _rows;
cols = _cols;
mat.resize(rows);
for (size_t i = 0; i < rows; ++i) {
mat[i].resize(cols);
for (size_t j = 0; j < cols; ++j) {
if (std::is_floating_point<T>::value) { // float
float x = esp_random();
x /= UINT32_MAX;
mat[i][j] = min + x * (max - min);
} else {
mat[i][j] = random(min, max);
}
}
}
}
// Create a new matrix of the same size as another matrix, filled with constant (default 0)
template<typename T> template<typename U> MLMatrix<T>::MLMatrix(MLMatrix<U> const &rhs, const T initial)
{
rows = rhs.get_rows();
cols = rhs.get_cols();
mat.resize(rows);
for (size_t i = 0; i < rows; ++i) {
mat[i].resize(cols, initial);
}
}
// Copy Constructor (from a vector)
template<typename T> template<typename U> MLMatrix<T>::MLMatrix(const std::vector<U>& rhs) {
rows = rhs.size();
cols = 1;
mat.resize(rows);
for (size_t i = 0; i < rows; ++i) mat[i].resize(cols, static_cast<T>(rhs[i]));
}
// Copy Constructor (from a vector of vectors, each vector is a new row)
template<typename T> template<typename U> MLMatrix<T>::MLMatrix(const std::vector<std::vector<U> >& rhs) {
rows = rhs.size();
cols = rhs[0].size();
mat.resize(rows);
for (size_t i = 0; i < rows; ++i) {
mat[i].resize(cols);
for (size_t j = 0; j < cols; ++j) mat[i][j] = static_cast<T>(rhs[i][j]);
}
}
// Copy Constructor (from an array)
template<typename T> template<typename U> MLMatrix<T>::MLMatrix(const U rhs[], const unsigned dim) {
rows = dim;
cols = 1;
mat.resize(rows);
for (size_t i = 0; i < rows; ++i) mat[i].resize(cols, static_cast<T>(rhs[i]));
}
// (Virtual) Destructor
template<typename T>
MLMatrix<T>::~MLMatrix() {}
/***************************************************************************************************
Accessors
*/
// Access the individual elements
template<typename T>
typename std::vector<T>::reference MLMatrix<T>::operator()(const unsigned& row, const unsigned& col) {
return mat[row][col];
}
// Access the individual elements (const)
template<typename T>
T const MLMatrix<T>::operator()(const unsigned& row, const unsigned& col) const {
return mat[row][col];
}
/***************************************************************************************************
Initialization methods
*/
// Assignment Operator
template<typename T> template<typename U> MLMatrix<T>& MLMatrix<T>::operator=(const MLMatrix<U>& rhs) {
rows = rhs.get_rows();
cols = rhs.get_cols();
mat.resize(rows);
for (size_t i = 0; i < rows; ++i) {
mat[i].resize(cols);
for (size_t j = 0; j < cols; ++j) {
mat[i][j] = static_cast<T>(rhs(i, j));
}
}
return *this;
}
// Assignment from vector
template<typename T> template<typename U> MLMatrix<T>& MLMatrix<T>::operator=(const std::vector<U>& rhs) {
rows = rhs.size();
cols = 1;
mat.resize(rows);
for (size_t i = 0; i < rows; ++i) {
mat[i].resize(cols);
mat[i][0] = static_cast<T>(rhs[i]);
}
return *this;
}
// Assignment from vector of vectors
template<typename T> template<typename U> MLMatrix<T>& MLMatrix<T>::operator=(const std::vector<std::vector<U> >& rhs) {
rows = rhs.size();
cols = rhs[0].size();
mat.resize(rows);
for (size_t i = 0; i < rows; ++i) {
mat[i].resize(cols);
for (size_t j = 0; j < cols; ++j) mat[i][j] = static_cast<T>(rhs[i][j]);
}
return *this;
}
/* Assignment from array. Usage:
float R[5] = {1,2,3,4,5};
MLMatrix<uint8_t> S;
S.fromArray(R, 5);
*/
template<typename T> template<typename U> MLMatrix<T>& MLMatrix<T>::fromArray(const U rhs[], const unsigned dim) {
rows = dim;
cols = 1;
mat.resize(rows);
for (size_t i = 0; i < rows; ++i) mat[i].resize(cols, static_cast<T>(rhs[i]));
return *this;
}
/***************************************************************************************************
Overloaded operators
*/
// Cumulative addition of this matrix and another
template<typename T> template<typename U> MLMatrix<T>& MLMatrix<T>::operator+=(const MLMatrix<U>& rhs) {
if ( rows != rhs.get_rows() || cols != rhs.get_cols() ) { // matrices of different sizes
Serial.printf("Addition error: dimensions do not match (%d, %d)+(%d, %d)", rows, cols, rhs.get_rows()), rhs.get_cols();
while(1);
}
MLMatrix result = (*this) + rhs;
(*this) = result;
return *this;
}
// Cumulative substraction of this matrix and another
template<typename T> template<typename U> MLMatrix<T>& MLMatrix<T>::operator-=(const MLMatrix<U>& rhs) {
if ( rows != rhs.get_rows() || cols != rhs.get_cols() ) { // matrices of different sizes
Serial.printf("Addition error: dimensions do not match (%d, %d)-(%d, %d)", rows, cols, rhs.get_rows()), rhs.get_cols();
while(1);
}
MLMatrix result = (*this) - rhs;
(*this) = result;
return *this;
}
// Cumulative left multiplication of this matrix and another
template<typename T> template<typename U> MLMatrix<T>& MLMatrix<T>::operator*=(const MLMatrix<U>& rhs) {
if (cols != rhs.get_rows()) {
Serial.printf("Multiplication error: dimensions do not match (%d, %d).(%d, %d)", rows, cols, rhs.get_rows(), rhs.get_cols());
while(1);
}
MLMatrix result = (*this) * rhs;
(*this) = result;
return *this;
}
// Matrix/scalar addition
template<typename T> template<typename U> MLMatrix<T>& MLMatrix<T>::operator+=(const U& rhs) {
MLMatrix result = (*this) + rhs;
(*this) = result;
return *this;
}
// Matrix/scalar substraction
template<typename T> template<typename U> MLMatrix<T>& MLMatrix<T>::operator-=(const U& rhs) {
MLMatrix result = (*this) - rhs;
(*this) = result;
return *this;
}
// Matrix/scalar multiplication
template<typename T> template<typename U> MLMatrix<T>& MLMatrix<T>::operator*=(const U& rhs) {
MLMatrix result = (*this) * rhs;
(*this) = result;
return *this;
}
// Matrix/scalar division
template<typename T> template<typename U> MLMatrix<T>& MLMatrix<T>::operator/=(const U& rhs) {
MLMatrix result = (*this) / rhs;
(*this) = result;
return *this;
}
/***************************************************************************************************
Comparison operators
*/
// Determine if two matrices are equal and return true, otherwise return false.
template <typename T> template<typename U>
const bool MLMatrix<T>::operator==(const MLMatrix<U> &rhs) const
{
if ( rows != rhs.get_rows() || cols != rhs.get_cols() ) return false; // matrices of different sizes
for (size_t i = 0; i < rows; ++i)
for (size_t j = 0; j < cols; ++j)
if (mat[i][i] != rhs(i,j)) return false;
return true; // matrices are equal
}
template <typename T> template<typename U> const bool MLMatrix<T>::operator!=(const MLMatrix<U> &rhs) const
{
return !(*this == rhs);
}
// Compare 2 matrices element wise
template <typename T> template<typename U> MLMatrix<bool> MLMatrix<T>::operator<(const MLMatrix<U> &rhs)
{
if (rows != rhs.get_rows() || cols != rhs.get_cols()) { // matrices of different sizes
Serial.printf("Comparison error: dimensions do not match (%d, %d)<(%d, %d)", rows, cols, rhs.get_rows()), rhs.get_cols();
while(1);
}
MLMatrix<bool> result(rows, cols, 0);
for (size_t i = 0; i < rows; ++i)
for (size_t j = 0; j < cols; ++j)
result(i,j) = (mat[i][j] < rhs(i,j)) ? true : false;
return result;
}
template <typename T> template<typename U> MLMatrix<bool> MLMatrix<T>::operator>=(const MLMatrix<U> &rhs)
{
if ( rows != rhs.get_rows() || cols != rhs.get_cols() ) { // matrices of different sizes
Serial.printf("Comparison error: dimensions do not match (%d, %d)>=(%d, %d)", rows, cols, rhs.get_rows(), rhs.get_cols());
while(1);
}
MLMatrix<bool> result(rows, cols, 0);
for (size_t i = 0; i < rows; ++i)
for (size_t j = 0; j < cols; ++j)
result(i,j) = (mat[i][j] >= rhs(i,j)) ? true : false;
return result;
}
/***************************************************************************************************
Operations on matrices
*/
// Calculate a transpose of this matrix
template<typename T>
MLMatrix<T> MLMatrix<T>::transpose() {
MLMatrix result(cols, rows, 0);
for (size_t i = 0; i < cols; ++i) {
for (size_t j = 0; j < rows; ++j) {
result(i,j) = mat[j][i];
}
}
return result;
}
// Return a matrix with elemnts squared: M = A.square()
template<typename T>
MLMatrix<T> MLMatrix<T>::square ()
{
MLMatrix result(rows, cols, 0);
for (size_t i=0; i<rows; ++i) {
for (size_t j=0; j<cols; ++j) {
result(i,j) = pow(mat[i][j], 2);
}
}
return result;
}
/*
Hadamard (element-wise) product
Usage:
MLMatrix<int> a(10, 50, 0, 10); // define the first matrix
MLMatrix<int> b(10, 50, 0, 10); // define the second matrix, same dimensions
a = a.Hadamard(b);
*/
template<typename T> template<typename U> MLMatrix<T> MLMatrix<T>::Hadamard(const MLMatrix<U>& rhs, bool clip)
{
if ( rows != rhs.get_rows() || cols != rhs.get_cols() ) { // matrices of different sizes
Serial.printf("Hadamard product error: dimensions do not match (%d, %d).(%d, %d)", rows, cols, rhs.get_rows(), rhs.get_cols());
while(1);
}
MLMatrix<T> result(rows, cols, 0);
if (!clip) {
for ( size_t i = 0; i < rows; ++i )
for ( size_t j = 0; j < cols; ++j )
result(i,j) = mat[i][j] * static_cast<T>(rhs(i,j));
} else {
for ( size_t i = 0; i < rows; ++i )
for ( size_t j = 0; j < cols; ++j ) {
float R = (float)mat[i][j] * (float)rhs(i,j);
if (R < std::numeric_limits<T>::min()) R = std::numeric_limits<T>::min();
if (R > std::numeric_limits<T>::max()) R = std::numeric_limits<T>::max();
result(i,j) = T(R);
}
}
return result;
}
/***************************************************************************************************
Matrix vector operations
*/
// Obtain a vector of the diagonal elements
// Usage : std::vector<int> Diag = mat.diag_vec();
template<typename T>
std::vector<T> MLMatrix<T>::diag_vec() {
std::vector<T> result(rows, 0);
for (unsigned i=0; i<rows; ++i) result[i] = mat[i][i];
return result;
}
// Cumulative multiplication of a matrix by a vector
// Cumulative left multiplication of this matrix and another
template<typename T> template<typename U> std::vector<T>& MLMatrix<T>::operator*=(const std::vector<U>& rhs) {
std::vector<T> result = (*this) * rhs;
(*this) = result;
return *this;
}
/***************************************************************************************************
Vector operations
*/
// Vector dot product, using matrices
/*
Example usage:
std::vector<int> v1 = {1, 2, 3};
std::vector<int> v2 = {4, 5, 6};
MLMatrix<int> mv1(3, 1, 0);
mv1 = v1;
MLMatrix<int> mv2(3, 1, 0);
mv2 = v2;
int P = mv1.MdotProd(mv2, true);
*/
template<typename T> template<typename U> auto MLMatrix<T>::MdotProd(const MLMatrix<U>& rhs, bool clip) ->
decltype(std::declval<U>()*std::declval<T>())
{
using ret_t = decltype(std::declval<U>()*std::declval<T>());
if (rhs.get_cols() !=1 || cols != 1) {
Serial.printf("Dot product error: please use matrices with 1 column\n");
while(1);
}
if (rhs.get_rows() != rows) {
Serial.printf("Dot product error: dimensions do not match (%d, %d)\n",rhs.get_rows(), rows);
while(1);
}
double sum = 0.0f;
for (unsigned i = 0; i < rows; ++i) sum += mat[i][0] * rhs(i, 0);
if (clip) { // Clip the result at min and max value of type T
// float sum = std::inner_product(std::begin(a.mat), std::end(a.mat), std::begin(mat), 0.0);;
if (sum < std::numeric_limits<ret_t>::min()) sum = std::numeric_limits<ret_t>::min();
if (sum > std::numeric_limits<ret_t>::max()) sum = std::numeric_limits<ret_t>::max();
}
return static_cast<ret_t>(sum);
}
/***************************************************************************************************
Norms
*/
/* Various normS of a matrix
Usage:
MLMatrix<float> a(10, 50, 0.0f, 10.0f);
float normL0 = a.L0Norm();
float normL1 = a.L1Norm();
float normL2 = a.L2Norm();
*/
template<typename T>
T MLMatrix<T>::L2Norm()
{
T L2 = T(0);
for (unsigned i=0; i<rows; ++i) {
for (unsigned j=0; j<cols; ++j) {
L2 += pow(this->mat[i][j], 2);
}
}
return sqrt(L2);
}
template<typename T>
T MLMatrix<T>::L1Norm()
{
T L1 = T(0);
for (unsigned i=0; i<rows; ++i) {
for (unsigned j=0; j<cols; ++j) {
float L = abs(this->mat[i][j]);
L1 = (L > L1)? L: L1;
}
}
return L1;
}
template<typename T>
int MLMatrix<T>::L0Norm() // number of non zero elements
{
int L0 = 0;
for (unsigned i=0; i<rows; ++i) {
for (unsigned j=0; j<cols; ++j) {
if (this->mat[i][j] != 0) ++L0;
}
}
return L0;
}
// Max, min, mean, std deviation
template<typename T>
T MLMatrix<T>::max() const
{
T max = std::numeric_limits<T>::min();
for (unsigned i=0; i<rows; ++i) {
for (unsigned j=0; j<cols; ++j) {
if (this->mat[i][j] > max) max = this->mat[i][j];
}
}
return max;
}
template<typename T>
T MLMatrix<T>::min() const
{
T min = std::numeric_limits<T>::max();
for (unsigned i=0; i<rows; ++i) {
for (unsigned j=0; j<cols; ++j) {
if (this->mat[i][j] < min) min = this->mat[i][j];
}
}
return min;
}
template<typename T>
float MLMatrix<T>::mean() const
{
float mean = 0.0f;
for (unsigned i=0; i<rows; ++i) {
for (unsigned j=0; j<cols; ++j) {
mean += this->mat[i][j];
}
}
mean /= float(rows * cols);
return mean;
}
// Compute the mean absolute value of a row
template<typename T>
float MLMatrix<T>::meanRow(int rowNumber)
{
float mean = 0.0f;
for (unsigned j=0; j<cols; ++j) mean += abs(this->mat[rowNumber][j]);
mean /= float(cols);
return mean;
}
template<typename T>
float MLMatrix<T>::stdev(const float mean) const
{
float stdev = 0.0f;
for (unsigned i=0; i<rows; ++i) {
for (unsigned j=0; j<cols; ++j) {
stdev += pow(this->mat[i][j] - mean, 2);
}
}
stdev /= float(rows * cols);
stdev = sqrt(stdev);
return stdev;
}
// Find the place of the minimum value
template <typename T>
void MLMatrix<T>::indexMin(int &indexRow, int &indexCol)
{
T minVal = this->mat[0][0];
for (int i = 0; i < rows; ++i)
for (int j = 0; j < cols; ++j)
if (this->mat[i][j] < minVal) {
minVal = this->mat[i][j];
indexRow = i;
indexCol = j;
}
}
// Find the place of the maximum value
template <typename T>
void MLMatrix<T>::indexMax(int &indexRow, int &indexCol)
{
indexRow = 0;
indexCol = 0;
T maxVal = this->mat[0][0];
for (int i = 0; i < rows; ++i)
for (int j = 0; j < cols; ++j)
if (this->mat[i][j] > maxVal) {
maxVal = this->mat[i][j];
indexRow = i;
indexCol = j;
}
}
/***************************************************************************************************
Misc methods
*/
// Get the number of rows of the matrix
template<typename T>
unsigned MLMatrix<T>::get_rows() const { return this->rows; }
// Get the number of columns of the matrix
template<typename T>
unsigned MLMatrix<T>::get_cols() const { return this->cols; }
template <typename T>
void MLMatrix<T>::setSize(const int _rows, const int _cols, const T val)
{
rows = _rows;
cols = _cols;
mat.resize(_rows);
for (size_t i = 0; i < mat.size(); ++i) {
mat[i].resize(_cols, val);
}
}
// Display the matrix
// usage: mat.print();
template <typename T>
void MLMatrix<T>::print()
{
Serial.printf("%d rows, %d cols\n",rows, cols);
for (size_t i = 0; i < rows; ++i) {
if (std::is_floating_point<T>::value) { // float
for (size_t j = 0; j < cols - 1; ++j) Serial.printf("%8.3f, ", this->mat[i][j]);
Serial.printf("%8.3f\n", this->mat[i][cols - 1]);
} else { // integer
for (size_t j = 0; j < cols - 1; ++j) Serial.printf("%5d, ", this->mat[i][j]);
Serial.printf("%5d\n", this->mat[i][cols - 1]);
}
}
}
// Specific function for boolean matrices
template <typename T>
void MLMatrix<T>::printBool()
{
Serial.printf("%d rows, %d cols\n",rows, cols);
for (size_t i = 0; i < rows; ++i) {
for (size_t j = 0; j < cols - 1; ++j)
if (mat[i][j]) Serial.print("true, ");
else Serial.print("false, ");
if (mat[i][cols - 1]) Serial.println("true");
else Serial.println("false");
}
}
// Display the matrix size
// usage: mat.printSize();
template <typename T>
void MLMatrix<T>::printSize()
{
Serial.printf("(%d, %d)",rows, cols);
}
/*
Apply a given function to the elements of a matrix (element-wise)
The function must be written as:
T function(T x) { ... }
For example :
int plusOne (int x) { return x+1; }
Usage:
MLMatrix<int> a(10, 50, 0, 10); // define the first matrix
a.applySelf( &function ); // changes the matrix
MLMatrix<int> b = a.apply( &function ); // does not change the matrix
*/
template<typename T>
MLMatrix<T> MLMatrix<T>::applySelf(T (*function)(T))
{
for (unsigned i=0; i<rows; ++i) {
for (unsigned j=0; j<cols; ++j) {
mat[i][j] = function(mat[i][j]);
}
}
return *this;
}
template<typename T>
MLMatrix<T> MLMatrix<T>::apply(T (*function)(T))
{
MLMatrix result(rows, cols, 0);
for (unsigned i=0; i<rows; ++i) {
for (unsigned j=0; j<cols; ++j) {
result(i,j) = function(this->mat[i][j]);
}
}
return result;
}
// Apply a random change to all elements of a matrix
// Example : randomChange(0.1) applies random multiplication by a factor in [0.9, 1.1]
template<typename T>
MLMatrix<T> MLMatrix<T>::randomChange(const float amplitude)
{
for (unsigned i=0; i<rows; ++i) {
for (unsigned j=0; j<cols; ++j) {
// random number between -1 and +1
float rand = float(random(10000)) / 10000.0f * 2.0f - 1.0f; // random in [-1, +1]
mat[i][j] = mat[i][j] * (1.0f + rand * amplitude);
}
}
return *this;
}
/* Generate a random matrix with normal distribution
using the polar form of Box Muller algorithm
usage: MLMatrix<float> u(30, 30, 0.0f);
u.randomNormal(0.0f,1.0f);
*/
template<typename T>
MLMatrix<T> MLMatrix<T>::randomNormal(const float mean, float std_dev)
{
std_dev = abs(std_dev);
int dim = rows * cols;
float eps = 0.001f;
MLMatrix<T> N(rows, cols, 0);
MLMatrix<T> C(dim, 1, 0.0f);
for (unsigned i=0; i<rows; ++i)
for (unsigned j=0; j<cols; ++j) {
float r = 1.0f;
float u;
do {
u = 2 * float(random(100000)) / 100000.0f - 1;
float v = 2 * float(random(100000)) / 100000.0f - 1;
r = u * u + v * v;
} while (r > 1.0f && r != 0.0f);
mat[i][j] = u * sqrt(-2.0f * log(r) / r);
mat[i][j] = mat[i][j] * std_dev - mean;
}
return *this;
}
// Scale the norm to a given value
// usage: bool zeroNorm = X.normScale2(val);
// Returns true if L2 norm is zero, else false
template <typename T>
bool MLMatrix<T>::normScale2 (float value)
{
bool zeroNorm = false;
value = abs(value);
float L2 = this->L2Norm();
if (L2 == 0.0f) zeroNorm = true; // don't scale if L2 norm is zero
else {
float coef = value / L2;
for (unsigned i=0; i<rows; ++i)
for (unsigned j=0; j<cols; ++j)
mat[i][j] *= coef;
}
return zeroNorm;
}
// Clip all values less than threshold to zero
// Leads to: |abs(value)| > threshold or zero
// Returns: number of clipped values
template <typename T>
int MLMatrix<T>::clipToZero (float threshold)
{
int nbClip = 0;
threshold = abs(threshold);
for (unsigned i=0; i<rows; ++i) {
for (unsigned j=0; j<cols; ++j) {
if (abs(mat[i][j]) <= threshold) {
mat[i][j] = 0.0f;
++nbClip;
}
}
}
return nbClip;
}
// Set all values less than threshold to threshold
template <typename T>
int MLMatrix<T>::clipMin (float threshold)
{
int nbClip = 0;
threshold = abs(threshold);
for (unsigned i=0; i<rows; ++i) {
for (unsigned j=0; j<cols; ++j) {
if (abs(mat[i][j]) < threshold && mat[i][j] >= 0) {
mat[i][j] = threshold;
++nbClip;
}
if (abs(mat[i][j]) < threshold && mat[i][j] < 0) {
mat[i][j] = -threshold;
++nbClip;
}
}
}
return nbClip;
}
// Set all values greater than threshold to threshold
// Leads to: -threshold < value < threshold
template <typename T>
int MLMatrix<T>::clipMax (float threshold)
{
int nbClip = 0;
threshold = abs(threshold);
for (unsigned i=0; i<rows; ++i) {
for (unsigned j=0; j<cols; ++j) {
if (mat[i][j] > threshold) {
mat[i][j] = threshold;
++nbClip;
}
if (mat[i][j] < -threshold) {
mat[i][j] = -threshold;
++nbClip;
}
}
}
return nbClip;
}
// Create a matrix with the sign (+1 or -1) of each element of an input matrix
template<typename T>
MLMatrix<T> MLMatrix<T>::sgn()
{
MLMatrix<T> S(rows, cols, 0);
for (unsigned i=0; i<rows; ++i) {
for (unsigned j=0; j<cols; ++j) {
S(i,j) = (0 > mat[i][j]) ? T(-1) : T(1);
}
}
return S;
}
// Set an entire row or column to zero
template <typename T>
void MLMatrix<T>::setZeroRow(const int rowNumber)
{
for (int j = 0; j < cols; ++j) this->mat[rowNumber][j] = T(0);
}
template <typename T>
void MLMatrix<T>::setZeroCol(const int colNumber)
{
for (int i = 0; i < rows; ++i) this->mat[i][colNumber] = T(0);
}
// Set an entire row or column to a given value
template <typename T>
void MLMatrix<T>::setRow(const int rowNumber, const T value)
{
for (int j = 0; j < cols; ++j) this->mat[rowNumber][j] = value;
}
template <typename T>
void MLMatrix<T>::setCol(const int colNumber, const T value)
{
for (int i = 0; i < rows; ++i) this->mat[i][colNumber] = value;
}
// Replace an entire row or column with values from a 1D matrix
template <typename T>
void MLMatrix<T>::setRowMat(const int rowNumber, const MLMatrix<T> values)
{
for (int j = 0; j < cols; ++j) this->mat[rowNumber][j] = values(0, j);
}
template <typename T>
void MLMatrix<T>::setColMat(const int colNumber, MLMatrix<T> values)
{
for (int i = 0; i < rows; ++i) this->mat[i][colNumber] = values(i, 0);
}
// Apply dropout to a matrix: elements are set to zero with a given probability
// Returns the dropout mask: a matrix of the same size with
// 1 if the element didn't change and 0 if was set to 0
template <typename T>
MLMatrix<uint8_t> MLMatrix<T>::dropout(const float proba)
{
MLMatrix<uint8_t> mask(rows, cols, 1);
for (unsigned i=0; i<rows; ++i)
for (unsigned j=0; j<cols; ++j)
if (float(random(10000)) / 10000.0f < proba) {
mat[i][j] = T(0);
mask(i,j) = 0;
}
return mask;
}
///////////////////////////////////////////////////////////////
// Pruning functions
///////////////////////////////////////////////////////////////
// Put the values of the matrix in a vector and sort the vector in descending order
// usage: std::vector<float> vec = M.sortValues();
// argument true (default) if sort on absolute values, false if not
template<typename T>
std::vector<T> MLMatrix<T>::sortValues(bool absVal)
{
std::vector<T> vec;
for (unsigned i=0; i<rows; ++i)
for (unsigned j=0; j<cols; ++j) {
if (absVal) vec.push_back(abs(mat[i][j]));
else vec.push_back(mat[i][j]);
}
sort(vec.begin(), vec.end(), std::greater<T>());
return vec;
}
// Verify if a row or column is full of 0
template<typename T>
bool MLMatrix<T>::zeroRow(int rowNumber)
{
bool isZero = false;
MLMatrix<T> R(1, cols, T(0));
for (unsigned j=0; j<cols; ++j) R(0,j) = mat[rowNumber][j];
T valMax = R.max();
T valMin = R.min();
isZero = (valMax == T(0)) && (valMin == T(0)) ? true : false;
return isZero;
}
template<typename T>
bool MLMatrix<T>::zeroCol(int colNumber)
{
bool isZero = false;
MLMatrix<T> C(1, rows, T(0));
for (unsigned i=0; i<rows; ++i) C(0,i) = mat[i][colNumber];
T valMax = C.max();
T valMin = C.min();
isZero = (valMax == T(0)) && (valMin == T(0)) ? true : false;
return isZero;
}
template<typename T>
uint16_t MLMatrix<T>::countZeroRow(int rowNumber)
{
uint16_t zero = 0;
for (unsigned j=0; j<cols; ++j) if(mat[rowNumber][j] == T(0)) ++ zero;
return zero;
}
template<typename T>
uint16_t MLMatrix<T>::countZeroCol(int colNumber)
{
uint16_t zero = 0;
for (unsigned i=0; i<rows; ++i) if(mat[i][colNumber] == T(0)) ++ zero;
return zero;
}
// Extract a row or a column from a matrix
template<typename T>
MLMatrix<T> MLMatrix<T>::row(const uint16_t rowNumber)
{
MLMatrix<T> result(1, cols, 0);
if (rowNumber > rows) {
Serial.printf("Row extraction error: row %d greater than %d\n", rowNumber, rows);
while(1);
}
for (unsigned j=0; j<cols; ++j) result(0, j) = mat[rowNumber][j];
return result;
}
template<typename T>
MLMatrix<T> MLMatrix<T>::col(const uint16_t colNumber)
{
MLMatrix<T> result(rows, 1, 0);
if (colNumber > cols) {
Serial.printf("Column extraction error: col %d greater than %d\n", colNumber, cols);
while(1);
}
for (unsigned i=0; i<rows; ++i) result(i, 0) = mat[i][colNumber];
return result;
}
/* Extract a submatrix
row0 <= row < row0 + nrows
col0 <= col < col0 + ncols
*/
template<typename T>
MLMatrix<T> MLMatrix<T>::subMatrix(const uint16_t row0, const uint16_t nrows, const uint16_t col0, const uint16_t ncols)
{
if (row0 + nrows > rows || col0 + ncols > cols) {
Serial.printf ("Submatrix extraction error (rows from %d to %d, cols from %d to %d)", row0, row0+nrows, col0, col0+ncols);
while(1);
}
MLMatrix<T> result(nrows, ncols, 0);
for (unsigned i=0; i<nrows; ++i)
for (unsigned j=0; j<ncols; ++j)
result(i, j) = mat[row0 + i][col0 + j];
return result;
}
// Remove a row from the matrix
// usage A.removeRow(n);
template<typename T>
MLMatrix<T> MLMatrix<T>::removeRow(const uint16_t index)
{
if (index > rows) {
Serial.printf ("Remove error: cannot remove row %d, size is %d\n", index, rows);
while(1);
}
for (unsigned i=index; i<rows-1; ++i)
for (unsigned j=0; j<cols; ++j)
mat[i][j] = mat[i+1][j];
--rows;
return *this;
}
// Remove a column from the matrix
// usage A.removeCol(n);
template<typename T>
MLMatrix<T> MLMatrix<T>::removeCol(const uint16_t index)
{
if (index > cols) {
Serial.printf ("Remove error: cannot remove col %d, size is %d\n", index, cols);
while(1);
}
for (unsigned i=0; i<rows; ++i)
for (unsigned j=index; j<cols-1; ++j)
mat[i][j] = mat[i][j+1];
--cols;
return *this;
}
///////////////////////////////////////////////////////////////
/*
Specific methods for the DeepShift algorithm
https://arxiv.org/abs/1905.13298#
*/
///////////////////////////////////////////////////////////////
// Returns the integral values nearest to the elements of a matrix, with halfway cases rounded away from zero.
// Adds 'shift' (default is 10) to the elements of the matrix
template<typename T>
MLMatrix<uint8_t> MLMatrix<T>::matRound(uint8_t shift)
{
MLMatrix<uint8_t> result(rows, cols, 0);
for (unsigned i = 0; i < rows; ++i) {
for (unsigned j = 0; j < cols; ++j) {
if (mat[i][j] < T(-shift)) mat[i][j] = T(-shift);
if (mat[i][j] > T( shift)) mat[i][j] = T( shift);
result(i,j) = round(this->mat[i][j] + shift);
}
}
return result;
}
#endif