This repository has been archived by the owner on Aug 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatrixNM.h
1997 lines (1646 loc) · 101 KB
/
MatrixNM.h
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
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* fixed size rectangular (i.e. ROW x COL) numerical matrix (although general, designed for small matrix).
*
* Dan Israel Malta
**/
#pragma once
#include "FloatingPointTraits.h"
#include "Constants.h"
#include "static_for.h"
#include "Common.h"
#include "VectorN.h"
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cstring>
#include <assert.h>
#include <algorithm>
#include <array>
#include <vector>
#include <type_traits>
#include <utility>
#include <limits>
namespace Numeric {
/*****************/
/* Matrix Layout */
/*****************/
// access matrix as row-major
template<std::size_t ROW, std::size_t COL> struct RowMajor {
static const bool m_RowMajor{ true };
constexpr static std::size_t Index(const std::size_t xi_row, const std::size_t xi_col) {
return (xi_row * COL + xi_col);
}
};
// access matrix as column-major
template<std::size_t ROW, std::size_t COL> struct ColumnMajor {
static const bool m_RowMajor{ false }; // column major...
constexpr static std::size_t Index(const std::size_t xi_row, const std::size_t xi_col) {
return (xi_col * COL + xi_row);
}
};
// type trait to see if the property 'm_RowMajor' is included in a struct
template<typename T, typename = void> struct has_RowMajor : std::false_type { };
template<typename T> struct has_RowMajor<T, decltype(std::declval<T>().m_RowMajor, void())> : std::true_type { };
// type trait to see if the method 'Index' is included in a struct
template<typename T, typename = void> struct has_Index : std::false_type { };
template<typename T> struct has_Index<T, decltype(std::declval<T>().Index, void())> : std::true_type { };
/*********************************/
/* Matrix Accessors/Constructors */
/*********************************/
// 1D matrix accessors
struct Column { std::size_t m_Value; Column(const std::size_t& xi_value) : m_Value(xi_value) {}; }; // get a specific column from the matrix
struct Row { std::size_t m_Value; Row(const std::size_t& xi_value) : m_Value(xi_value) {}; }; // get a specific row from the matrix
enum aDiagonal { Diagonal }; // get matrix diagonal
// 2D matrix accessor (syntactic sugar...)
enum aLowerTriangular { LowerTriangular }; // get matrix lower triangular portion
enum aUpperTriangular { UpperTriangular }; // get matrix upper triangular portion
// matrix constructor type
enum cIdentity { Eye }; // construct an identity matrix
enum cOuterProduct { Outerproduct }; // construct a matrix as outer product
enum cRowWise { RowWise }; // construct matrix row-wise
enum cColumnWise { ColumnWise }; // construct matrix column-wise
enum cDiagonalWise { DiagonalWise }; // construct matrix diagonal-wise
enum cVanDerMonde { VanDerMonde }; // construct a Van-Der-Monde matrix
enum cToeplitz { Toeplitz }; // construct a Toeplitz matrix
enum cAxisAngle { AxisAngle }; // construct a rotation matrix (3x3) from an axis (normalized unit vector) and rotation angle (given by its sine & cosine components)
/**
* \brief fixed size numerical matrix
*
* @param {T, in} underlying type
* @param {ROW, in} number of rows
* @param {COL, in} number of columns
* @param {LAYOUT, in} matrix memory layout (row major or column major; by default it is row major)
**/
template<typename T, std::size_t ROW, std::size_t COL = ROW, class LAYOUT = RowMajor<ROW, COL>> class MatrixNM {
static_assert(std::is_arithmetic<T>::value, "MatrixNM<T,ROW, COL> - T must be of numerical type.");
static_assert(ROW != 0, "MatrixNM<T,ROW, COL> - ROW/COL parameters must be positive.");
static_assert(COL != 0, "MatrixNM<T,ROW, COL> - ROW/COL parameters must be positive.");
static_assert(std::is_class<LAYOUT>::value, "MatrixNM<T,ROW, COL, LAYOUT> - LAYOUT must be a struct of type 'RowMajor<ROW, COL>' or 'ColMajor<ROW, COL>'.");
static_assert(has_RowMajor<LAYOUT>::value, "MatrixNM<T,ROW, COL, LAYOUT> - LAYOUT must include the property 'm_RowMajor'.");
static_assert(has_Index<LAYOUT>::value, "MatrixNM<T,ROW, COL, LAYOUT> - LAYOUT must include the method 'Index'.");
// properties
private:
const enum : bool { m_RowMajor = (LAYOUT::m_RowMajor) ? true : false }; // layout type
const enum : std::size_t { SIZE = ROW * COL }; // number of elements in matrix
VectorN<T, SIZE> m_data; // matrix data
// constructors (from various collections/values)
public:
// default constructor
explicit constexpr MatrixNM() : m_data() {}
// construct using a single value
explicit constexpr MatrixNM(const T xi_value) : m_data(xi_value) {}
// construct from VectorN
explicit constexpr MatrixNM(const VectorN<T, SIZE>& xi_vec) : m_data(xi_vec) {}
// construct from a moveable array (usage: MatrixNM<float, 3, 2> v{{ 1.0f, 2.0f, 3.0f,
// 1.0f, 2.0f, 3.0f }}; )
explicit constexpr MatrixNM(T(&&xi_array)[SIZE]) : m_data(std::make_move_iterator(std::begin(xi_array)), std::make_move_iterator(std::end(xi_array))) {
// adjust matrix for ColumnMajor style
if constexpr (!m_RowMajor) {
static_for<0, ROW>([&](std::size_t i) {
static_for<0, COL>([&](std::size_t j) {
std::swap(m_data[LAYOUT::Index(i, j)], m_data[LAYOUT::Index(j, i)]);
});
});
}
}
// construct using list initializer
// throws "array iterator + offset out of range" if more then SIZE elements are entered
explicit constexpr MatrixNM(const std::initializer_list<T>&& xi_list) : m_data(std::move(xi_list)) {
// adjust matrix for ColumnMajor style
if constexpr (!m_RowMajor) {
static_for<0, ROW>([&](std::size_t i) {
static_for<0, COL>([&](std::size_t j) {
std::swap(m_data[LAYOUT::Index(i, j)], m_data[LAYOUT::Index(j, i)]);
});
});
}
}
// construct using std::array
explicit constexpr MatrixNM(const std::array<T, SIZE>& xi_arr) : m_data(xi_arr) {
// adjust matrix for ColumnMajor style
if constexpr (!m_RowMajor) {
static_for<0, ROW>([&](std::size_t i) {
static_for<0, COL>([&](std::size_t j) {
std::swap(m_data[LAYOUT::Index(i, j)], m_data[LAYOUT::Index(j, i)]);
});
});
}
}
explicit constexpr MatrixNM(std::array<T, SIZE>&& xi_arr) : m_data(std::move(xi_arr)) {
xi_arr = nullptr;
// adjust matrix for ColumnMajor style
if constexpr (!m_RowMajor) {
static_for<0, ROW>([&](std::size_t i) {
static_for<0, COL>([&](std::size_t j) {
std::swap(m_data[LAYOUT::Index(i, j)], m_data[LAYOUT::Index(j, i)]);
});
});
}
}
// construct using std::vector
explicit constexpr MatrixNM(const std::vector<T>& xi_vec) : m_data(xi_vec) {
assert(SIZE == xi_vec.size());
// adjust matrix for ColumnMajor style
if constexpr (!m_RowMajor) {
static_for<0, ROW>([&](std::size_t i) {
static_for<0, COL>([&](std::size_t j) {
std::swap(m_data[LAYOUT::Index(i, j)], m_data[LAYOUT::Index(j, i)]);
});
});
}
}
explicit constexpr MatrixNM(std::vector<T>&& xi_vec) : m_data(std::move(xi_vec)) {
assert(SIZE == xi_vec.size());
xi_vec = nullptr;
// adjust matrix for ColumnMajor style
if constexpr (!m_RowMajor) {
static_for<0, ROW>([&](std::size_t i) {
static_for<0, COL>([&](std::size_t j) {
std::swap(m_data[LAYOUT::Index(i, j)], m_data[LAYOUT::Index(j, i)]);
});
});
}
}
// construct an I matrix
explicit constexpr MatrixNM(cIdentity) : m_data(T{}) {
static_assert(ROW == COL, "MatrixNM must be cubic.");
static_for<0, ROW>([&](std::size_t i) {
m_data[LAYOUT::Index(i, i)] = static_cast<T>(1);
});
}
// construct the outer product from two vectors (usage: MatrixNM(Outerproduct, vec1, vec2))
explicit constexpr MatrixNM(cOuterProduct, const VectorN<T, ROW>& xi_x, const VectorN<T, ROW>& xi_y) {
static_assert(ROW == COL, "MatrixNM must be cubic.");
static_for<0, ROW>([&](std::size_t i) {
static_for<0, COL>([&](std::size_t j) {
m_data[LAYOUT::Index(i, j)] = xi_x[i] * xi_y[j];
});
});
}
explicit constexpr MatrixNM(cOuterProduct, VectorN<T, ROW>&& xi_x, VectorN<T, ROW>&& xi_y) {
static_assert(ROW == COL, "MatrixNM must be cubic.");
static_for<0, ROW>([&](std::size_t i) {
static_for<0, COL>([&](std::size_t j) {
m_data[LAYOUT::Index(i, j)] = std::move(xi_x[i]) * std::move(xi_y[j]);
});
});
}
// construct a matrix such that all its rows are the same vector
explicit constexpr MatrixNM(cRowWise, const VectorN<T, COL>& xi_vec) {
static_for<0, ROW>([&](std::size_t i) {
static_for<0, COL>([&](std::size_t j) {
m_data[LAYOUT::Index(i, j)] = xi_vec[j];
});
});
}
explicit constexpr MatrixNM(cRowWise, VectorN<T, COL>&& xi_vec) {
static_for<0, ROW>([&](std::size_t i) {
static_for<0, COL>([&](std::size_t j) {
m_data[LAYOUT::Index(i, j)] = std::move(xi_vec[j]);
});
});
}
// construct a matrix such that all its columns are the same vector
explicit constexpr MatrixNM(cColumnWise, const VectorN<T, ROW>& xi_vec) {
static_for<0, COL>([&](std::size_t i) {
static_for<0, ROW>([&](std::size_t j) {
m_data[LAYOUT::Index(j, i)] = xi_vec[j];
});
});
}
explicit constexpr MatrixNM(cColumnWise, VectorN<T, ROW>&& xi_vec) {
static_for<0, COL>([&](std::size_t i) {
static_for<0, ROW>([&](std::size_t j) {
m_data[LAYOUT::Index(j, i)] = std::move(xi_vec[j]);
});
});
}
// construct a matrix such that its diagonal is same vector
explicit constexpr MatrixNM(cDiagonalWise, const VectorN<T, ROW>& xi_vec) : m_data(T{}) {
static_assert(ROW == COL, "MatrixNM must be cubic.");
static_for<0, ROW>([&](std::size_t i) {
m_data[LAYOUT::Index(i, i)] = xi_vec[i];
});
}
explicit constexpr MatrixNM(cDiagonalWise, VectorN<T, ROW>&& xi_vec) : m_data(T{}) {
static_assert(ROW == COL, "MatrixNM must be cubic.");
static_for<0, ROW>([&](std::size_t i) {
m_data[LAYOUT::Index(i, i)] = std::move(xi_vec[i]);
});
}
// construct a Van-Der-Monde matrix
explicit constexpr MatrixNM(cVanDerMonde, const VectorN<T, ROW>& xi_vec) {
static_assert(ROW == COL, "MatrixNM must be cubic.");
static_for<0, ROW>([&](std::size_t i) {
for (std::size_t j{}; j < ROW; ++j) {
const T power{ static_cast<T>(ROW - j - 1) };
m_data[LAYOUT::Index(i, j)] = static_cast<T>(std::pow(xi_vec[i], power));
}
});
}
explicit constexpr MatrixNM(cVanDerMonde, VectorN<T, ROW>&& xi_vec) {
static_assert(ROW == COL, "MatrixNM must be cubic.");
static_for<0, ROW>([&](std::size_t i) {
for (std::size_t j{}; j < ROW; ++j) {
const T power{ static_cast<T>(ROW - j - 1) };
m_data[LAYOUT::Index(i, j)] = static_cast<T>(std::pow(std::move(xi_vec[i]), power));
}
});
}
// construct a Toeplitz matrix
explicit constexpr MatrixNM(cToeplitz, const VectorN<T, ROW>& xi_vec) {
static_assert(ROW == COL, "MatrixNM must be cubic.");
static_for<0, ROW>([&](std::size_t i) {
static_for<0, ROW>([&](std::size_t j) {
m_data[LAYOUT::Index(i, j)] = (i >= j) ? xi_vec[i - j] : xi_vec[j - i];
});
});
}
explicit constexpr MatrixNM(cToeplitz, VectorN<T, ROW>&& xi_vec) {
static_assert(ROW == COL, "MatrixNM must be cubic.");
static_for<0, ROW>([&](std::size_t i) {
static_for<0, ROW>([&](std::size_t j) {
m_data[LAYOUT::Index(i, j)] = (i >= j) ? std::move(xi_vec[i - j]) : std::move(xi_vec[j - i]);
});
});
}
// construct a rotation matrix from an axis (normalized) and an angle (given by its sine & cosine components)
explicit constexpr MatrixNM(cAxisAngle, const VectorN<T, 3>& xi_axis, const T xi_sine, const T xi_cosine) {
static_assert(ROW == COL, "Rotation matrix must be cubic.");
static_assert(ROW == 3, "Rotation matrix must be 3x3.");
// locals
const T oneMinusCosine{ T(1) - xi_cosine },
xx{ xi_axis.X() * xi_axis.X() },
xy{ xi_axis.X() * xi_axis.Y() },
xz{ xi_axis.X() * xi_axis.Z() },
yy{ xi_axis.Y() * xi_axis.Y() },
yz{ xi_axis.Y() * xi_axis.Z() },
zz{ xi_axis.Z() * xi_axis.Z() };
if constexpr (m_RowMajor) {
m_data = { xi_cosine + xx * oneMinusCosine, xy * oneMinusCosine + xi_axis.Z() * xi_sine, xz * oneMinusCosine - xi_axis.Y() * xi_sine,
xy * oneMinusCosine - xi_axis.Z() * xi_sine, xi_cosine + yy * oneMinusCosine, yz * oneMinusCosine + xi_axis.X() * xi_sine,
xz * oneMinusCosine + xi_axis.Y() * xi_sine, yz * oneMinusCosine - xi_axis.X() * xi_sine, xi_cosine + zz * oneMinusCosine };
}
else {
m_data = { xi_cosine + xx * oneMinusCosine, xy * oneMinusCosine - xi_axis.Z() * xi_sine, xz * oneMinusCosine + xi_axis.Y() * xi_sine,
xy * oneMinusCosine + xi_axis.Z() * xi_sine, xi_cosine + yy * oneMinusCosine, yz * oneMinusCosine - xi_axis.X() * xi_sine,
xz * oneMinusCosine - xi_axis.Y() * xi_sine, yz * oneMinusCosine + xi_axis.X() * xi_sine, xi_cosine + zz * oneMinusCosine };
}
}
// copy semantics
MatrixNM(const MatrixNM&) = default;
MatrixNM& operator=(const MatrixNM&) = default;
// move semantics
MatrixNM(MatrixNM&&) noexcept = default;
MatrixNM& operator=(MatrixNM&&) noexcept = default;
// assignment operator
public:
// assign an element
constexpr MatrixNM& operator=(const T xi_value) noexcept {
m_data = xi_value;
return *this;
}
// assign a VectorN
constexpr MatrixNM& operator=(const VectorN<T, SIZE>& xi_vec) noexcept {
m_data = xi_vec;
return *this;
}
constexpr MatrixNM& operator=(VectorN<T, SIZE>&& xi_vec) noexcept {
m_data = std::move(xi_vec);
return *this;
}
// assign from a list
constexpr MatrixNM& operator=(const std::initializer_list<T>& xi_list) {
// throws "array iterator + offset out of range" if more then N elements are entered
m_data = xi_list;
return *this;
}
constexpr MatrixNM& operator=(std::initializer_list<T>&& xi_list) {
// throws "array iterator + offset out of range" if more then N elements are entered
m_data = std::move(xi_list);
return *this;
}
// assign from array
constexpr MatrixNM& operator=(const T(&&xi_array)[SIZE]) {
std::move(&xi_array[0], &xi_array[SIZE], m_data);
return *this;
};
// assign from std::array
constexpr MatrixNM& operator=(const std::array<T, SIZE>& xi_arr) {
m_data = xi_arr;
return *this;
}
constexpr MatrixNM& operator=(std::array<T, SIZE>&& xi_arr) {
m_data = std::move(xi_arr);
return *this;
}
// assign from std::vector
constexpr MatrixNM& operator=(const std::vector<T>& xi_vec) {
assert(SIZE == xi_vec.size());
m_data = xi_vec;
return *this;
}
constexpr MatrixNM& operator=(std::vector<T>&& xi_vec) {
assert(SIZE == xi_vec.size());
m_data = std::move(xi_vec);
return *this;
}
// set/get operations
public:
// '[]' per-index element access (should not be used outside this header!)
constexpr T operator[](const std::size_t i) const { return m_data[i]; }
constexpr T& operator[](const std::size_t i) { return m_data[i]; }
// '(row, col)' element access
constexpr T operator()(const std::size_t row, const std::size_t col) const { return m_data[LAYOUT::Index(row, col)]; }
constexpr T& operator()(const std::size_t row, const std::size_t col) { return m_data[LAYOUT::Index(row, col)]; }
// get a specific row
VectorN<T, COL> constexpr operator()(const Row& xi_row) const {
VectorN<T, COL> xo_row;
const std::size_t row{ xi_row.m_Value };
static_for<0, COL>([&](std::size_t i) {
xo_row[i] = m_data[LAYOUT::Index(row, i)];
});
return xo_row;
}
// get a specific column
VectorN<T, ROW> constexpr operator()(const Column& xi_col) const {
VectorN<T, ROW> xo_col;
const std::size_t col{ xi_col.m_Value };
static_for<0, ROW>([&](std::size_t i) {
xo_col[i] = m_data[LAYOUT::Index(i, col)];
});
return xo_col;
}
// get diagonal
VectorN<T, ROW> constexpr operator()(aDiagonal) const {
static_assert(ROW == COL, "matrix must be cubic.");
VectorN<T, ROW> xo_diag;
static_for<0, ROW>([&](std::size_t i) {
xo_diag[i] = m_data[LAYOUT::Index(i, i)];
});
return xo_diag;
}
// get lower triangular
MatrixNM<T, ROW, COL> constexpr operator()(aLowerTriangular) const {
MatrixNM<T, ROW, COL> xo_lower(0.0f);
static_for<0, ROW>([&](std::size_t i) {
for (std::size_t j{}; j <= i; ++j) {
xo_lower(i, j) = m_data[LAYOUT::Index(i, j)];
}
});
return xo_lower;
}
MatrixNM<T, ROW, COL> constexpr operator()(aLowerTriangular) {
MatrixNM<T, ROW, COL> xo_lower(0.0f);
static_for<0, ROW>([&](std::size_t i) {
for (std::size_t j{}; j <= i; ++j) {
xo_lower(i, j) = m_data[LAYOUT::Index(i, j)];
}
});
return xo_lower;
}
// get upper triangular
MatrixNM<T, ROW, COL> constexpr operator()(aUpperTriangular) const {
MatrixNM<T, ROW, COL> xo_upper(0.0f);
static_for<0, ROW>([&](std::size_t i) {
for (std::size_t j{ i }; j < COL; ++j) {
xo_upper(i, j) = m_data[LAYOUT::Index(i, j)];
}
});
return xo_upper;
}
// get a block of matrix {row start, row end, column start, column end} (the block size is known at compile time)
template<std::size_t ROW_MIN, std::size_t ROW_MAX, std::size_t COL_MIN, std::size_t COL_MAX>
constexpr MatrixNM<T, ROW_MAX - ROW_MIN, COL_MAX - COL_MIN> GetRegion() const noexcept {
static_assert(ROW_MIN < ROW_MAX, "GetRegion(): ROW_MIN < ROW_MAX.");
static_assert(COL_MIN < COL_MAX, "GetRegion(): COL_MIN < COL_MAX.");
static_assert(ROW_MAX <= ROW, "GetRegion(): ROW_MAX < ROW.");
static_assert(COL_MAX <= COL, "GetRegion(): COL_MAX < COL.");
MatrixNM<T, ROW_MAX - ROW_MIN, COL_MAX - COL_MIN> xo_block(T{});
static_for<0, ROW_MAX - ROW_MIN>([&](std::size_t i) {
static_for<0, COL_MAX - COL_MIN>([&](std::size_t j) {
xo_block(i, j) = m_data[LAYOUT::Index(i + ROW_MIN, j + COL_MIN)];
});
});
return xo_block;
}
// --- special setters/getters for 2x2/3x3/4x4 elements matrix ---
// 2x2
template<typename = typename std::enable_if<(ROW == COL) && (ROW == 2)>::type> constexpr VectorN<T, 2> X() const noexcept { return VectorN<T, 2>{ m_data[LAYOUT::Index(0, 0)],
m_data[LAYOUT::Index(0, 1)] }; }
template<typename = typename std::enable_if<(ROW == COL) && (ROW == 2)>::type> constexpr VectorN<T, 2> Y() const noexcept { return VectorN<T, 2>{ m_data[LAYOUT::Index(1, 0)],
m_data[LAYOUT::Index(1, 1)] }; }
// 3x3 (extract Euler angles from rotation matrix)
template<typename = typename std::enable_if<(ROW == COL) && (ROW == 3)>::type>
constexpr VectorN<T, 3> Euler() const noexcept {
return VectorN<T, 3>{ std::atan2(-this->operator()(1,2), this->operator()(2,2)) ,
std::atan2(this->operator()(0,2), std::hypot(this->operator()(1,2), this->operator()(2,2))),
std::atan2(this->operator()(0,1), this->operator()(0,0)) };
}
// 3x3, 4x4
template<typename = typename std::enable_if<(ROW == COL) && ((ROW == 3) || (ROW == 4))>::type> constexpr VectorN<T, 3> X() const noexcept { return VectorN<T, 3>{ m_data[LAYOUT::Index(0, 0)],
m_data[LAYOUT::Index(0, 1)],
m_data[LAYOUT::Index(0, 2)] }; }
template<typename = typename std::enable_if<(ROW == COL) && ((ROW == 3) || (ROW == 4))>::type> constexpr VectorN<T, 3> Y() const noexcept { return VectorN<T, 3>{ m_data[LAYOUT::Index(1, 0)],
m_data[LAYOUT::Index(1, 1)],
m_data[LAYOUT::Index(1, 2)] };
}
template<typename = typename std::enable_if<(ROW == COL) && ((ROW == 3) || (ROW == 4))>::type> constexpr VectorN<T, 3> Z() const noexcept { return VectorN<T, 3>{ m_data[LAYOUT::Index(2, 0)],
m_data[LAYOUT::Index(2, 1)],
m_data[LAYOUT::Index(2, 2)] };
}
// 4X4 (affine matrix translation vector (either last row or last column))
template<typename = typename std::enable_if<(ROW == COL) && (ROW == 4)>::type> constexpr VectorN<T, 3> Translation() const noexcept { return VectorN<T, 3>{ m_data[LAYOUT::Index(3, 0)],
m_data[LAYOUT::Index(3, 1)],
m_data[LAYOUT::Index(3, 2)] };
}
// 4x4 (affine matrix scaling vector (its the diagonal of the 3x3 block))
template<typename = typename std::enable_if<(ROW == COL) && (ROW == 4)>::type> constexpr VectorN<T, 3> Scale() const noexcept { return VectorN<T, 3>{ this->operator()(0, 0),
this->operator()(1, 1),
this->operator()(2, 2) };
}
// 4x4 (affine matrix transformation matrix)
template<typename = typename std::enable_if<(ROW == COL) && (ROW == 4)>::type> constexpr MatrixNM<T, 3, 3> DCM() const noexcept { return this->GetRegion<0, 3, 0, 3>(); }
// numerical assignment operator overloading
public:
// operations with scalars
#define M_OPERATOR(OP) \
constexpr MatrixNM& operator OP (const T xi_value) { \
m_data OP xi_value; \
return *this; \
}
M_OPERATOR(+=);
M_OPERATOR(-=);
M_OPERATOR(*=);
M_OPERATOR(/=);
M_OPERATOR(&=);
M_OPERATOR(|=);
M_OPERATOR(^=);
M_OPERATOR(>>=);
M_OPERATOR(<<=);
#undef M_OPERATOR
// operations with equally size matrix
#define M_OPERATOR(OP, AOP) \
constexpr MatrixNM& operator OP (const MatrixNM<T, ROW, COL>& xi_mat) { \
static_for<0, SIZE>([&](std::size_t i) { \
m_data[i] AOP xi_mat[i]; \
}); \
return *this; \
} \
constexpr MatrixNM& operator OP (MatrixNM<T, ROW, COL>&& xi_mat) { \
static_for<0, SIZE>([&](std::size_t i) { \
m_data[i] AOP std::move(xi_mat[i]); \
}); \
return *this; \
}
M_OPERATOR(+, +=);
M_OPERATOR(-, -=);
M_OPERATOR(&, &=);
M_OPERATOR(| , |=);
M_OPERATOR(^, ^=);
M_OPERATOR(>> , >>=);
M_OPERATOR(<< , <<=);
#undef M_OPERATOR
// cubic matrix multiplication
constexpr MatrixNM& operator *= (const MatrixNM<T, ROW, COL>& xi_mat) {
static_assert(ROW == COL, "MatrixNM *= MatrixNM operations is only allowed when both matrix are cubic.");
static_assert(m_RowMajor == xi_mat.IsRowMajor(), "MatrixNM *= MatrixNM operations is only allowed when both matrix layout is identical.");
MatrixNM<T, ROW, COL>&& xo_mul;
static_for<0, ROW>([&](std::size_t i) {
static_for<0, COL>([&](std::size_t j) {
T sum{};
static_for<0, ROW>([&](std::size_t ii) {
sum += m_data[LAYOUT::Index(i, ii)] * xi_mat(i, j);
});
xo_mul[LAYOUT::Index(i, j)] = sum;
});
});
*this = std::move(xo_mul);
return *this;
}
constexpr MatrixNM& operator *= (MatrixNM<T, ROW, COL>&& xi_mat) {
static_assert(ROW == COL, "MatrixNM *= MatrixNM operations is only allowed when both matrix are cubic.");
static_assert(m_RowMajor == xi_mat.IsRowMajor(), "MatrixNM *= MatrixNM operations is only allowed when both matrix layout is identical.");
MatrixNM<T, ROW, COL>&& xo_mul;
static_for<0, ROW>([&](std::size_t i) {
static_for<0, COL>([&](std::size_t j) {
T sum{};
static_for<0, ROW>([&](std::size_t ii) {
sum += m_data[LAYOUT::Index(i, ii)] * std::move(xi_mat(i, j));
});
xo_mul[LAYOUT::Index(i, j)] = sum;
});
});
*this = std::move(xo_mul);
return *this;
}
// stream operator overloading
public:
// output matrix elements to a stream
friend std::ostream& operator<<(std::ostream& xio_stream, const MatrixNM& xi_mat) {
xio_stream << "\n{\n";
static_for<0, ROW>([&](std::size_t i) {
xio_stream << "{";
static_for<0, COL - 1>([&](std::size_t j) {
xio_stream << xi_mat(i, j) << ", ";
});
xio_stream << xi_mat(i, COL - 1) << "}\n";
});
return xio_stream << "\n}\n";
}
// element wise iterators
public:
T* begin() { return &m_data[0]; }
const T* begin() const { return &m_data[0]; }
T* end() { return begin() + SIZE; }
const T* end() const { return begin() + SIZE; }
// modifiers
public:
// swap rows
void SwapRows(const std::size_t a, const std::size_t b) {
static_for<0, COL>([&](std::size_t i) {
std::swap(m_data[LAYOUT::Index(a, i)], m_data[LAYOUT::Index(b, i)]);
});
}
// swap columns
void SwapColumns(const std::size_t a, const std::size_t b) {
static_for<0, ROW>([&](std::size_t i) {
std::swap(m_data[LAYOUT::Index(i, a)], m_data[LAYOUT::Index(i, b)]);
});
}
// set row
void SetRow(const std::size_t xi_inedx, const T xi_value) {
static_for<0, COL>([&](std::size_t i) {
m_data[LAYOUT::Index(xi_inedx, i)] = xi_value;
});
}
void SetRow(const std::size_t xi_inedx, const VectorN<T, COL> xi_vec) {
static_for<0, COL>([&](std::size_t i) {
m_data[LAYOUT::Index(xi_inedx, i)] = xi_vec[i];
});
}
// set column
void SetColumn(const std::size_t xi_inedx, const T xi_value) {
static_for<0, ROW>([&](std::size_t i) {
m_data[LAYOUT::Index(i, xi_inedx)] = xi_value;
});
}
void SetColumn(const std::size_t xi_inedx, const VectorN<T, ROW> xi_vec) {
static_for<0, ROW>([&](std::size_t i) {
m_data[LAYOUT::Index(i, xi_inedx)] = xi_vec[i];
});
}
// return a transposed matrix
constexpr MatrixNM<T, ROW, COL> Transpose() noexcept {
MatrixNM<T, ROW, COL> xo_transposed;
static_for<0, ROW>([&](std::size_t i) {
static_for<0, COL>([&](std::size_t j) {
xo_transposed(j, i) = m_data[LAYOUT::Index(i, j)];
});
});
return xo_transposed;
}
// return the (i,j) minor (matrix, not determinant value) of a given matrix
template<std::size_t I, std::size_t J> constexpr MatrixNM<T, ROW - 1, COL - 1> Minor() noexcept {
static_assert(I < ROW, "MatrixNM<T, COL, ROW>::Minor() index are out of bound");
static_assert(J < COL, "MatrixNM<T, COL, ROW>::Minor() index are out of bound");
MatrixNM<T, ROW - 1, COL - 1> xo_minor;
// top left region
static_for<0, I>([&](std::size_t i) {
static_for<0, J>([&](std::size_t j) {
xo_minor(i, j) = m_data[LAYOUT::Index(i, j)];
});
});
// top right region
static_for<0, I>([&](std::size_t i) {
static_for<J + 1, COL>([&](std::size_t j) {
xo_minor(i, j - 1) = m_data[LAYOUT::Index(i, j)];
});
});
// bottom left region
static_for<I + 1, ROW>([&](std::size_t i) {
static_for<0, J>([&](std::size_t j) {
xo_minor(i - 1, j) = m_data[LAYOUT::Index(i, j)];
});
});
// bottom right region
static_for<I + 1, ROW>([&](std::size_t i) {
static_for<J + 1, COL>([&](std::size_t j) {
xo_minor(i - 1, j - 1) = m_data[LAYOUT::Index(i, j)];
});
});
return xo_minor;
}
// queries
public:
// minimal element in the entire matrix
constexpr T MinElement() const noexcept {
return *std::min_element(&m_data[0], &m_data[SIZE]);
}
// maximal element in the entire matrix
constexpr T MaxElement() const noexcept {
return *std::max_element(&m_data[0], &m_data[SIZE]);
}
// check if a unary predicate returns true for all elements in matrix
bool AllOf(std::function<bool(const T)> xi_predicate) const noexcept {
return std::all_of(&m_data[0], &m_data[SIZE], xi_predicate);
}
// check if a unary predicate returns true for at least one elements in matrix
bool AnyOf(std::function<bool(const T)> xi_predicate) const noexcept {
return std::any_of(&m_data[0], &m_data[SIZE], xi_predicate);
}
// check if a unary predicate returns true for no elements in matrix
bool NoneOf(std::function<bool(const T)> xi_predicate) const noexcept {
return std::none_of(&m_data[0], &m_data[SIZE], xi_predicate);
}
// return occurrence of a given value in a matrix
constexpr std::size_t Count(const T& xi_value) const noexcept {
return std::count(&m_data[0], &m_data[SIZE], xi_value);
}
// return MATRIX norm (L2; its 'length')
constexpr T Norm() const noexcept {
T xo_dot{};
static_for<0, SIZE()>([&](std::size_t i) {
xo_dot += m_data[i] * m_data[i];
});
return std::sqrt(xo_dot);
}
/**
* \brief tests whether MATRIX is normalized.
*
* @param {double, in} tolerance for normalization test (default is 2 * epsilon)
* @param {bool, out} true if squared length indicate a normalized algebric structure.
**/
constexpr bool IsNormalized(const T& xi_tol = static_cast<T>(2) * FloatingPointTrait<T>::epsilon()) const noexcept {
return (std::abs(Norm() - static_cast<T>(1)) < static_cast<T>(2) * FloatingPointTrait<T>::epsilon());
}
// return matrix size
constexpr std::size_t Size() const noexcept { return SIZE; }
// test if matrix is row major
constexpr bool IsRowMajor() const noexcept { return m_RowMajor; }
// test if matrix is square
constexpr bool IsSqure() const noexcept { return (COL == ROW); }
/**
* \brief check if a CUBIC matrix is symmetric (around its diagonal; A = A^T)
*
* @param {bool, out} true if matrix is symmetric (around its diagonal)
**/
constexpr bool IsSymmetric() const noexcept {
static_assert(ROW == COL, "MatrixNM must be cubic");
bool xo_symmetric{ true };
for (std::size_t j{}; (j < ROW) && xo_symmetric; ++j) {
for (std::size_t i{}; (i < ROW) && xo_symmetric; ++i) {
xo_symmetric = FloatingPointTrait<T>::Equals(m_data[LAYOUT::Index(i, j)], m_data[LAYOUT::Index(j, i)]);
}
}
return xo_symmetric;
}
/**
* \brief check if a CUBIC matrix is skew-symmetric (around its diagonal; -A = A^T)
*
* @param {bool, out} true if matrix is skew-symmetric (around its diagonal)
**/
constexpr bool IsSkewSymmetric() const noexcept {
static_assert(ROW == COL, "MatrixNM must be cubic");
bool xo_symmetric{ true };
for (std::size_t j{}; (j < ROW) && xo_symmetric; ++j) {
for (std::size_t i{}; (i < ROW) && xo_symmetric; ++i) {
xo_symmetric = FloatingPointTrait<T>::Equals(m_data[LAYOUT::Index(i, j)], -m_data[LAYOUT::Index(j, i)]);
}
}
return xo_symmetric;
}
/**
* \brief check if a CUBIC matrix is upper triangular
*
* @param {bool, out} true if matrix is upper triangular
**/
constexpr bool IsUpperTriangular() const noexcept {
static_assert(ROW == COL, "MatrixNM must be cubic");
bool xo_triangular{ true };
for (std::size_t i{}; (i < ROW) && xo_triangular; ++i) {
for (std::size_t j{}; (j < i) && xo_triangular; ++j) {
xo_triangular = IsZero(m_data[LAYOUT::Index(i, j)]);
}
}
return xo_triangular;
}
/**
* \brief check if a CUBIC matrix is lower triangular
*
* @param {bool, out} true if matrix is lower triangular
**/
constexpr bool IsLowerTriangular() const noexcept {
static_assert(ROW == COL, "MatrixNM must be cubic");
bool xo_triangular{ true };
for (std::size_t i{}; (i < ROW) && xo_triangular; ++i) {
for (std::size_t j{ i + 1 }; (j < ROW) && xo_triangular; ++j) {
xo_triangular = IsZero(m_data[LAYOUT::Index(i, j)]);
}
}
return xo_triangular;
}
/**
* \brief check if a CUBIC matrix is diagonal
*
* @param {bool, out} true if matrix is diagonal
**/
constexpr bool IsDiagonal() const noexcept {
static_assert(ROW == COL, "MatrixNM must be cubic");
// diagonal
bool xo_diagonal{ true };
for (std::size_t i{}; (i < ROW) && xo_diagonal; ++i) {
xo_diagonal = !IsZero(m_data[LAYOUT::Index(i, i)]);
}
if (!xo_diagonal) {
return false;
}
// rest of matrix
for (std::size_t i{}; (i < ROW) && xo_diagonal; ++i) {
for (std::size_t j{}; (j < COL) && xo_diagonal; ++j) {
if (i != j) {
xo_diagonal = IsZero(m_data[LAYOUT::Index(i, j)]);
}
}
}
return xo_diagonal;
}
/**
* \brief check if a CUBIC matrix is permutation matrix (there is only one '1' in every column and row)
*
* @param {bool, out} true if matrix is permutation matrix
**/
constexpr bool IsPermutation() const noexcept {
static_assert(ROW == COL, "MatrixNM must be cubic");
const T rowTol{ static_cast<T>(1) + static_cast<T>(ROW) * FloatingPointTrait<T>::epsilon() },
colTol{ static_cast<T>(1) + static_cast<T>(COL) * FloatingPointTrait<T>::epsilon() };
bool xo_permutation{ true };
for (std::size_t i{}; (i < ROW) && xo_permutation; ++i) {
VectorN<T, COL>&& tempRow{ this->operator()(Row(i)) };
xo_permutation = !(Sum(tempRow) > rowTol);
VectorN<T, ROW>&& tempCol{ this->operator()(Column(i)) };
xo_permutation &= !(Sum(tempCol) > colTol);
}
return xo_permutation;
}
/**
* \brief tests whether the matrix is per-column-normalized.
*
* @param {T, in} tolerance for normalization test (default is 2 * epsilon)
* @param {bool, out} true if each of matrix columns is normalized, false otherwise
**/
constexpr bool IsNormalizedPerColumn(const T& xi_tol = T(2) * FloatingPointTrait<T>::epsilon()) const noexcept {
bool xo_normalized{ true };
for (std::size_t i{}; (i < COL) && xo_normalized; ++i) {
VectorN<T, ROW>&& column{ this->operator()(Column(i)) };
xo_normalized = std::move(column).IsNormalized(xi_tol);
}
return xo_normalized;
}
/**
* \brief tests whether the matrix is per-row-normalized.
*
* @param {T, in} tolerance for normalization test (default is 2 * epsilon)
* @param {bool, out} true if each of matrix rows is normalized, false otherwise
**/
constexpr bool IsNormalizedPerRow(const T& xi_tol = T(2) * FloatingPointTrait<T>::epsilon()) const noexcept {
bool xo_normalized{ true };
for (std::size_t i{}; (i < ROW) && xo_normalized; ++i) {
VectorN<T, COL>&& row{ this->operator()(Row(i)) };
xo_normalized = std::move(row).IsNormalized(xi_tol);
}
return xo_normalized;
}
// element wise numerical operations
public: