-
Notifications
You must be signed in to change notification settings - Fork 4
/
MatrixOperations.h
1821 lines (1550 loc) · 82.4 KB
/
MatrixOperations.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
/**
* GLSL++
*
* Dan Israel Malta
**/
#pragma once
#include "common.h"
#include "VectorBase.h"
#include "MatrixBase.h"
#include <algorithm>
#include <functional>
#include <bitset>
namespace GLSLCPP {
// ----------------------
// --- "constructors" ---
// ----------------------
/**
* \brief given a cubicmatrix - convert it to I.
*
* @param {MatrixBase, in|out} cubic matrix to be transformed to an 'I'
**/
template<typename T, REQUIRE(is_Cubic<T>::value)>
constexpr void MakeIdentity(T& xio_mat) noexcept {
using _T = underlying_type_t<T>;
xio_mat = _T{};
for (std::size_t i{}; i < Rows_v<T>; ++i) {
xio_mat(i, i) = static_cast<_T>(1);
}
}
/**
* \brief given two equally sized vectors, create a matrix from their outer product
*
* @param {VectorBase, in} vector #1
* @param {VectorBase, in} vector #2
* @param {MatrixBase, out} outer product of input vectors
**/
template<typename T, typename U, REQUIRE(Are_TwoVectorsSimilar_v<T, U> && (Length_v<T> == 2))>
constexpr MatrixBase<typename std::common_type<underlying_type_t<T>, underlying_type_t<U>>::type, Length_v<T>, Length_v<T>> OuterProduct(const T& xi_x, const U& xi_y) noexcept {
using _T = underlying_type_t<T>;
using _U = underlying_type_t<U>;
using TYPE = typename std::common_type<_T, _U>::type;
constexpr std::size_t N{ Length_v<T> };
return MatrixBase<TYPE, N, N>(xi_x[0] * xi_y[0], xi_x[0] * xi_y[1],
xi_x[1] * xi_y[0], xi_x[1] * xi_y[1]);
}
template<typename T, typename U, REQUIRE(Are_TwoVectorsSimilar_v<T, U> && (Length_v<T> == 3))>
constexpr MatrixBase<typename std::common_type<underlying_type_t<T>, underlying_type_t<U>>::type, Length_v<T>, Length_v<T>> OuterProduct(const T& xi_x, const U& xi_y) noexcept {
using _T = underlying_type_t<T>;
using _U = underlying_type_t<U>;
using TYPE = typename std::common_type<_T, _U>::type;
constexpr std::size_t N{ Length_v<T> };
return MatrixBase<TYPE, N, N>(xi_x[0] * xi_y[0], xi_x[0] * xi_y[1], xi_x[0] * xi_y[2],
xi_x[1] * xi_y[0], xi_x[1] * xi_y[1], xi_x[1] * xi_y[2],
xi_x[2] * xi_y[0], xi_x[2] * xi_y[1], xi_x[2] * xi_y[2]);
}
template<typename T, typename U, REQUIRE(Are_TwoVectorsSimilar_v<T, U> && (Length_v<T> == 4))>
constexpr MatrixBase<typename std::common_type<underlying_type_t<T>, underlying_type_t<U>>::type, Length_v<T>, Length_v<T>> OuterProduct(const T& xi_x, const U& xi_y) noexcept {
using _T = underlying_type_t<T>;
using _U = underlying_type_t<U>;
using TYPE = typename std::common_type<_T, _U>::type;
constexpr std::size_t N{ Length_v<T> };
return MatrixBase<TYPE, N, N>(xi_x[0] * xi_y[0], xi_x[0] * xi_y[1], xi_x[0] * xi_y[2], xi_x[0] * xi_y[3],
xi_x[1] * xi_y[0], xi_x[1] * xi_y[1], xi_x[1] * xi_y[2], xi_x[1] * xi_y[3],
xi_x[2] * xi_y[0], xi_x[2] * xi_y[1], xi_x[2] * xi_y[2], xi_x[2] * xi_y[3],
xi_x[3] * xi_y[0], xi_x[3] * xi_y[1], xi_x[3] * xi_y[2], xi_x[3] * xi_y[3]);
}
template<typename T, typename U, REQUIRE(Are_TwoVectorsSimilar_v<T, U> && (Length_v<T> > 4))>
constexpr MatrixBase<typename std::common_type<underlying_type_t<T>, underlying_type_t<U>>::type, Length_v<T>, Length_v<T>> OuterProduct(const T& xi_x, const U& xi_y) noexcept {
using _T = underlying_type_t<T>;
using _U = underlying_type_t<U>;
using TYPE = typename std::common_type<_T, _U>::type;
constexpr std::size_t N{ Length_v<T> };
MatrixBase<TYPE, N, N> xo_mat;
for (std::size_t i{}; i < N; ++i) {
for (std::size_t j{}; j < N; ++j) {
xo_mat(i, j) = xi_x[i] * xi_y[j];
}
}
return xo_mat;
}
/**
* \brief construct a givens rotation matrix (rotation in the plane spanned by two coordinates axes)
*
* @param {T, in} cosine of rotation angle
* @param {T, in} sine of rotation angle
* @param {T, in} column to start rotation
* @param {MatrixBase, out} givens rotation matrix
**/
template<typename T, std::size_t N, REQUIRE(std::is_arithmetic_v<T> && (N > 1))>
constexpr inline MatrixBase<T, N, N> GivensRotation(const T xi_cosine, const T xi_sine, const std::size_t xi_col) noexcept {
assert(xi_col + 1 < N && " GivensRotation(..., column) - 'column' argument is out of bound.");
MatrixBase<T, N, N> xo_givens;
xo_givens(xi_col, xi_col ) = xi_cosine;
xo_givens(xi_col, xi_col + 1) = -xi_sine;
xo_givens(xi_col + 1, xi_col + 1) = xi_cosine;
xo_givens(xi_col + 1, xi_col ) = xi_sine;
return xo_givens;
}
/**
* \brief construct a householder matrix from vector 'xi_vec'
*
* @param {VectorBase, in} vector
* @param {MatrixBase, out} householder ,atrix
**/
template<typename T, REQUIRE(is_Vector_v<T> && (Length_v<T> == 2))>
constexpr inline MatrixBase<underlying_type_t<T>, 2, 2> Householder(const T& xi_vec) noexcept {
using _T = underlying_type_t<T>;
constexpr _T one{ static_cast<_T>(1) },
two{ static_cast<_T>(-2) };
return MatrixBase<_T, 2, 2>(one + two * xi_vec[0] * xi_vec[0], two * xi_vec[0] * xi_vec[1],
two * xi_vec[1] * xi_vec[0], one + two * xi_vec[1] * xi_vec[1]);
}
template<typename T, REQUIRE(is_Vector_v<T> && (Length_v<T> == 3))>
constexpr inline MatrixBase<underlying_type_t<T>, 3, 3> Householder(const T & xi_vec) noexcept {
using _T = underlying_type_t<T>;
constexpr _T one{ static_cast<_T>(1) },
two{ static_cast<_T>(-2) };
return MatrixBase<_T, 3, 3>(one + two * xi_vec[0] * xi_vec[0], two * xi_vec[0] * xi_vec[1], two * xi_vec[0] * xi_vec[2],
two * xi_vec[1] * xi_vec[0], one + two * xi_vec[1] * xi_vec[1], two * xi_vec[1] * xi_vec[2],
two * xi_vec[2] * xi_vec[0], two * xi_vec[2] * xi_vec[1], one + two * xi_vec[2] * xi_vec[2]);
}
template<typename T, REQUIRE(is_Vector_v<T> && (Length_v<T> == 4))>
constexpr inline MatrixBase<underlying_type_t<T>, 3, 3> Householder(const T & xi_vec) noexcept {
using _T = underlying_type_t<T>;
constexpr _T one{ static_cast<_T>(1) },
two{ static_cast<_T>(-2) };
return MatrixBase<_T, 3, 3>(one + two * xi_vec[0] * xi_vec[0], two * xi_vec[0] * xi_vec[1], two * xi_vec[0] * xi_vec[2], two * xi_vec[0] * xi_vec[3],
two * xi_vec[1] * xi_vec[0], one + two * xi_vec[1] * xi_vec[1], two * xi_vec[1] * xi_vec[2], two * xi_vec[1] * xi_vec[3],
two * xi_vec[2] * xi_vec[0], two * xi_vec[2] * xi_vec[1], one + two * xi_vec[2] * xi_vec[2], two * xi_vec[2] * xi_vec[3],
two * xi_vec[3] * xi_vec[0], two * xi_vec[3] * xi_vec[1], two * xi_vec[3] * xi_vec[2], one + two * xi_vec[3] * xi_vec[3]);
}
template<typename T, REQUIRE(is_Vector_v<T> && (Length_v<T> > 4))>
constexpr inline MatrixBase<underlying_type_t<T>, Length_v<T>, Length_v<T>> Householder(const T& xi_vec) noexcept {
using _T = underlying_type_t<T>;
constexpr std::size_t N{ Length_v<T> };
MatrixBase<_T, N, N> reflect(OuterProduct(xi_vec, xi_vec)),
xo_mat;
reflect *= static_cast<_T>(2);
xo_mat -= reflect;
return xo_mat;
}
/**
* \brief construct a van-der-monde matrix
*
* @param {VectorBase, in} vector
* @param {MatrixBase, out} van-der-monde matrix
**/
template<typename T, REQUIRE(is_Vector_v<T>)>
constexpr MatrixBase<underlying_type_t<T>, Length_v<T>, Length_v<T>> VanDerMonde(const T& xi_vec) noexcept {
using _T = underlying_type_t<T>;
constexpr std::size_t N{ Length_v<T> };
MatrixBase<_T, N, N> xo_mat;
for (std::size_t i{}; i < N; ++i) {
for (std::size_t j{}; j < N; ++j) {
const _T power{ static_cast<_T>(N - j - 1) };
xo_mat(i, j) = static_cast<_T>(std::pow(xi_vec[i], power));
}
}
return xo_mat;
}
/**
* \brief construct a Toeplitz matrix
*
* @param {VectorBase, in} vector
* @param {MatrixBase, out} Toeplitz matrix
**/
template<typename T, REQUIRE(is_Vector_v<T>)>
constexpr MatrixBase<underlying_type_t<T>, Length_v<T>, Length_v<T>> Toeplitz(const T& xi_vec) noexcept {
using _T = underlying_type_t<T>;
constexpr std::size_t N{ Length_v<T> };
MatrixBase<_T, N, N> xo_mat;
for (std::size_t i{}; i < N; ++i) {
for (std::size_t j{}; j < N; ++j) {
xo_mat(j, i) = (j >= i) ? xi_vec[j - i] : xi_vec[i - j];
}
}
return xo_mat;
}
/**
* \brief construct a rotation matrix (3x3) from a normalized axis and an angle (given by its trigonometric components
*
* @param {VectorBase, in} normalized axis
* @param {T, in} rotation angle sine
* @param {T, in} rotation angle cosine
* @param {MatrixBase, out} rotation matrix matrix (3x3)
**/
template<typename T, REQUIRE(Is_VectorOfLength_v<T, 3>)>
constexpr MatrixBase<underlying_type_t<T>, 3, 3> FromAxisAngle(const T& xi_axis, const underlying_type_t<T> xi_sine, const underlying_type_t<T> xi_cosine) noexcept {
using _T = underlying_type_t<T>;
assert(IsNormalized(xi_axis) && " FromAxisAngle(axis, ...) - axis is not normalized.");
assert(std::abs(xi_sine) <= static_cast<_T>(1) && " FromAxisAngle(..., sine, ...) - rotation angle sine must be in the region [-1, 1].");
assert(std::abs(xi_cosine) <= static_cast<_T>(1) && " FromAxisAngle(..., ..., cosine) - rotation angle cosine must be in the region [-1, 1].");
// locals
const _T oneMinusCosine{ static_cast<_T>(1) - xi_cosine },
xx{ xi_axis[0] * xi_axis[0] },
xy{ xi_axis[0] * xi_axis[1] },
xz{ xi_axis[0] * xi_axis[2] },
yy{ xi_axis[1] * xi_axis[1] },
yz{ xi_axis[1] * xi_axis[2] },
zz{ xi_axis[2] * xi_axis[2] },
xyOne{ xy * oneMinusCosine },
xzOne{ xz * oneMinusCosine },
yzOne{ yz * oneMinusCosine },
axisSine0{ xi_axis[0] * xi_sine },
axisSine1{ xi_axis[1] * xi_sine },
axisSine2{ xi_axis[2] * xi_sine };
// output
return MatrixBase<_T, 3, 3>(xi_cosine + xx * oneMinusCosine, xyOne - axisSine2, xzOne + axisSine1,
xyOne + axisSine2, xi_cosine + yy * oneMinusCosine, yzOne - axisSine0,
xzOne - axisSine1, yzOne + axisSine0, xi_cosine + zz * oneMinusCosine );
}
// -----------------
// --- modifiers ---
// -----------------
/**
* \brief swap two rows of a given matrix
*
* @param {MatrixBase, in} matrix whose rows shall be swapped
* @param {size_t, in} row #1
* @param {size_t, in} row #2
* @param {MatrixBase, out} input matrix with swapped rows
**/
template<typename T, std::size_t COL, std::size_t ROW>
constexpr inline MatrixBase<T, COL, ROW> SwapRows(MatrixBase<T, COL, ROW>&& xi_matrix, const std::size_t a, const std::size_t b) {
assert((a < ROW) && (b < ROW) && "MatrixBase::SwapRows - attempting to swap rows which are outside of matrix boundaries.");
for (std::size_t i{}; i < COL; ++i) {
std::swap(xi_matrix(i, a), xi_matrix(i, b));
}
return xi_matrix;
}
template<typename T, std::size_t COL, std::size_t ROW>
constexpr inline MatrixBase<T, COL, ROW> SwapRows(const MatrixBase<T, COL, ROW>& xi_matrix, const std::size_t a, const std::size_t b) {
assert((a < ROW) && (b < ROW) && "MatrixBase::SwapRows - attempting to swap rows which are outside of matrix boundaries.");
auto mat = FWD(xi_matrix);
for (std::size_t i{}; i < COL; ++i) {
std::swap(mat(i, a), mat(i, b));
}
return FWD(mat);
}
/**
* \brief swap two columns of a given matrix
*
* @param {MatrixBase, in} matrix whose columns shall be swapped
* @param {size_t, in} row #1
* @param {size_t, in} row #2
* @param {MatrixBase, out} input matrix with swapped columns
**/
template<typename T, std::size_t COL, std::size_t ROW>
constexpr inline MatrixBase<T, COL, ROW> SwapColumns(MatrixBase<T, COL, ROW>&& xi_matrix, const std::size_t a, const std::size_t b) {
assert((a < ROW) && (b < ROW) && "MatrixBase::SwapColumns - attempting to swap columns which are outside of matrix boundaries.");
for (std::size_t i{}; i < ROW; ++i) {
std::swap(xi_matrix(a, i), xi_matrix(b, i));
}
return xi_matrix;
}
template<typename T, std::size_t COL, std::size_t ROW>
constexpr inline MatrixBase<T, COL, ROW> SwapColumns(const MatrixBase<T, COL, ROW>& xi_matrix, const std::size_t a, const std::size_t b) {
assert((a < ROW) && (b < ROW) && "MatrixBase::SwapColumns - attempting to swap columns which are outside of matrix boundaries.");
auto mat = FWD(xi_matrix);
for (std::size_t i{}; i < ROW; ++i) {
std::swap(mat(a, i), mat(b, i));
}
return FWD(mat);
}
/**
* \brief set the row of a given matrix
*
* @param {MatrixBase, in|out} matrix whose row shall be set
* @param {size_t, in} row index
* @param {VectorBase, in} new row
**/
template<typename T, typename U, REQUIRE(is_MatrixBase_v<T> && Is_VectorOfLength_v<U, Columns_v<T>>)>
constexpr inline void SetRow(T& xio_matrix, const std::size_t xi_index, U&& xi_row) {
using _T = underlying_type_t<T>;
assert(xi_index < Rows_v<T> && "MatrixBase::SetRow - attempting to set a row which doesn't exist.");
for_each(xi_row, [&, i = 0](const auto & elm) mutable {
xio_matrix(i, xi_index) = static_cast<_T>(elm);
++i;
});
}
template<typename T, typename U, REQUIRE(is_MatrixBase_v<T> && Is_VectorOfLength_v<U, Columns_v<T>>)>
constexpr inline void SetRow(T& xio_matrix, const std::size_t xi_index, const U& xi_row) {
using _T = underlying_type_t<T>;
assert(xi_index < Rows_v<T> && "MatrixBase::SetRow - attempting to set a row which doesn't exist.");
auto row = FWD(xi_row);
for_each(row, [&, i = 0](const auto & elm) mutable {
xio_matrix(i, xi_index) = static_cast<_T>(elm);
++i;
});
}
/**
* \brief set the diagonal of a cubic matrix
*
* @param {MatrixBase, in|out} matrix whose diagonal shall be set
* @param {VectorBase/T, in} new diagonal
**/
template<typename T, typename U, REQUIRE(is_Cubic<T>::value && Is_VectorOfLength_v<U, Columns_v<T>>)>
constexpr inline void SetDiagonal(T& xio_matrix, const U& xi_diagonal) {
using _T = underlying_type_t<T>;
for (std::size_t i{}; i < Columns_v<T>; ++i) {
xio_matrix(i, i) = static_cast<_T>(xi_diagonal[i]);
}
}
template<typename T, typename U, REQUIRE(is_Cubic<T>::value&& Is_VectorOfLength_v<U, Columns_v<T>>)>
constexpr inline void SetDiagonal(T& xio_matrix, U&& xi_diagonal) {
using _T = underlying_type_t<T>;
for (std::size_t i{}; i < Columns_v<T>; ++i) {
xio_matrix(i, i) = static_cast<_T>(std::move(xi_diagonal[i]));
}
}
/**
* \brief return the transpose of a given matrix
*
* @param {MatrixBase, in} matrix whose row shall be to be transposed
* @param {MatrixBase, out} transposed matrix
**/
template<typename T, std::size_t COL, std::size_t ROW, REQUIRE((COL == ROW) && (ROW == 2))>
constexpr inline MatrixBase<T, COL, ROW> Transpose(MatrixBase<T, COL, ROW>&& xi_mat) noexcept {
return MatrixBase<T, 2, 2>(xi_mat(0, 0), xi_mat(1, 0),
xi_mat(0, 1), xi_mat(1, 1));
}
template<typename T, std::size_t COL, std::size_t ROW, REQUIRE((COL == ROW) && (ROW == 3))>
constexpr inline MatrixBase<T, COL, ROW> Transpose(MatrixBase<T, COL, ROW>&& xi_mat) noexcept {
return MatrixBase<T, 3, 3>(xi_mat(0, 0), xi_mat(1, 0), xi_mat(2, 0),
xi_mat(0, 1), xi_mat(1, 1), xi_mat(2, 1),
xi_mat(0, 2), xi_mat(1, 2), xi_mat(2, 2));
}
template<typename T, std::size_t COL, std::size_t ROW, REQUIRE((COL == ROW) && (ROW == 4))>
constexpr inline MatrixBase<T, COL, ROW> Transpose(MatrixBase<T, COL, ROW>&& xi_mat) noexcept {
return MatrixBase<T, 4, 4>(xi_mat(0, 0), xi_mat(1, 0), xi_mat(2, 0), xi_mat(3, 0),
xi_mat(0, 1), xi_mat(1, 1), xi_mat(2, 1), xi_mat(3, 1),
xi_mat(0, 2), xi_mat(1, 2), xi_mat(2, 2), xi_mat(3, 2),
xi_mat(0, 3), xi_mat(1, 3), xi_mat(2, 3), xi_mat(3, 3));
}
template<typename T, std::size_t COL, std::size_t ROW, REQUIRE((COL == ROW) && (ROW > 4))>
constexpr inline MatrixBase<T, COL, ROW> Transpose(MatrixBase<T, COL, ROW>&& xi_mat) noexcept {
constexpr std::size_t last{ ROW * COL },
size{ last - 1 },
n{ last / COL };
// housekeeping
std::bitset<last> visited;
// transposing
std::size_t cycle{};
while (++cycle != last) {
// visited this element?
if (visited[cycle]) continue;
// cycle along elements
std::size_t a{ cycle };
do {
a = (a == size) ? (size) : ((n * a) % size);
std::swap(xi_mat.m_data[a], xi_mat.m_data[cycle]);
visited[a] = true;
} while (a != cycle);
}
return xi_mat;
}
template<typename T, std::size_t COL, std::size_t ROW, REQUIRE((COL == ROW) && (ROW == 2))>
constexpr inline MatrixBase<T, COL, ROW> Transpose(const MatrixBase<T, COL, ROW>& xi_mat) noexcept {
return MatrixBase<T, 2, 2>(xi_mat(0, 0), xi_mat(0, 1),
xi_mat(1, 0), xi_mat(1, 1));
}
template<typename T, std::size_t COL, std::size_t ROW, REQUIRE((COL == ROW) && (ROW == 3))>
constexpr inline MatrixBase<T, COL, ROW> Transpose(const MatrixBase<T, COL, ROW>& xi_mat) noexcept {
return MatrixBase<T, 3, 3>(xi_mat(0, 0), xi_mat(1, 0), xi_mat(2, 0),
xi_mat(0, 1), xi_mat(1, 1), xi_mat(2, 1),
xi_mat(0, 2), xi_mat(1, 2), xi_mat(2, 2));
}
template<typename T, std::size_t COL, std::size_t ROW, REQUIRE((COL == ROW) && (ROW == 4))>
constexpr inline MatrixBase<T, COL, ROW> Transpose(const MatrixBase<T, COL, ROW>& xi_mat) noexcept {
return MatrixBase<T, 4, 4>(xi_mat(0, 0), xi_mat(1, 0), xi_mat(2, 0), xi_mat(3, 0),
xi_mat(0, 1), xi_mat(1, 1), xi_mat(2, 1), xi_mat(3, 1),
xi_mat(0, 2), xi_mat(1, 2), xi_mat(2, 2), xi_mat(3, 2),
xi_mat(0, 3), xi_mat(1, 3), xi_mat(2, 3), xi_mat(3, 3));
}
template<typename T, std::size_t COL, std::size_t ROW, REQUIRE((COL == ROW) && (ROW > 4))>
constexpr inline MatrixBase<T, ROW, COL> Transpose(const MatrixBase<T, COL, ROW>& xi_mat) noexcept {
constexpr std::size_t last{ ROW * COL },
size{ last - 1 },
n{ last / COL };
// housekeeping
auto mat = FWD(xi_mat);
std::bitset<last> visited;
// transposing
std::size_t cycle{};
while (++cycle != last) {
// visited this element?
if (visited[cycle]) continue;
// cycle along elements
std::size_t a{ cycle };
do {
a = (a == size) ? (size) : ((n * a) % size);
std::swap(mat.m_data[a], mat.m_data[cycle]);
visited[a] = true;
} while (a != cycle);
}
return FWD(mat);
}
/**
* \brief perform element-wise multiplication between two matrix
*
* @param {MatrixBase, in} matrix #1
* @param {MatrixBase, in} matrix #2
* @param {MatrixBase, out} element wise multiplication of input matrix
**/
template<typename T, typename U, REQUIRE(is_MatrixBase_v<T> && is_MatrixBase_v<U> &&
(Columns_v<T> == Columns_v<U>) && (Rows_v<T> == Rows_v<U>))>
constexpr inline MatrixBase<std::common_type_t<underlying_type_t<T>, underlying_type_t<U>>, Columns_v<T>, Rows_v<T>>
matrixMatrixMul(const T& xi_a, const U& xi_b) noexcept {
MatrixBase<std::common_type_t<underlying_type_t<T>, underlying_type_t<U>>, Columns_v<T>, Rows_v<T>> xo_mat(xi_a);
xo_mat.m_data *= xi_b.m_data;
return xo_mat;
}
// ---------------
// --- getters ---
// ---------------
/**
* \brief return a given row of a given matrix
*
* @param {MatrixBase, in} matrix
* @param {size_t, in} row index
* @param {VectorBase, out} row
**/
template<typename T, REQUIRE(is_MatrixBase_v<T>)>
constexpr inline VectorBase<underlying_type_t<T>, Columns_v<T>> GetRow(const T& xi_matrix, const std::size_t xi_index) {
assert(xi_index < Rows_v<T> && "MatrixBase::GetRow - attempting to get a row which doesn't exist.");
constexpr std::size_t COL{ Columns_v<T> };
VectorBase<underlying_type_t<T>, COL> xo_row;
for (std::size_t i{}; i < COL; ++i) {
xo_row[i] = xi_matrix(i, xi_index);
}
return xo_row;
}
/**
* \brief return the diagonal of a cubic matrix
*
* @param {MatrixBase, in} cubic matrix
* @param {VectorBase, out} diagonal
**/
template<typename T, REQUIRE(is_Cubic<T>::value && (Rows_v<T> == 2))>
constexpr inline VectorBase<underlying_type_t<T>, Rows_v<T>> GetDiagonal(const T& xi_matrix) noexcept {
using _T = underlying_type_t<T>;
constexpr std::size_t N{ Rows_v<T> };
return VectorBase<_T, N>(xi_matrix(0,0), xi_matrix(1,1));
}
template<typename T, REQUIRE(is_Cubic<T>::value && (Rows_v<T> == 3))>
constexpr inline VectorBase<underlying_type_t<T>, Rows_v<T>> GetDiagonal(const T& xi_matrix) noexcept {
using _T = underlying_type_t<T>;
constexpr std::size_t N{ Rows_v<T> };
return VectorBase<_T, N>(xi_matrix(0, 0), xi_matrix(1, 1), xi_matrix(2, 2));
}
template<typename T, REQUIRE(is_Cubic<T>::value && (Rows_v<T> == 4))>
constexpr inline VectorBase<underlying_type_t<T>, Rows_v<T>> GetDiagonal(const T& xi_matrix) noexcept {
using _T = underlying_type_t<T>;
constexpr std::size_t N{ Rows_v<T> };
return VectorBase<_T, N>(xi_matrix(0, 0), xi_matrix(1, 1), xi_matrix(2, 2), xi_matrix(3, 3));
}
template<typename T, REQUIRE(is_Cubic<T>::value && (Rows_v<T> > 4))>
constexpr inline VectorBase<underlying_type_t<T>, Rows_v<T>> GetDiagonal(const T& xi_matrix) noexcept {
using _T = underlying_type_t<T>;
constexpr std::size_t N{ Rows_v<T> };
VectorBase<_T, N> xo_diag;
for (std::size_t i{}; i < N; ++i) {
xo_diag[i] = xi_matrix(i, i);
}
return xo_diag;
}
/**
* \brief return the lower triangular part of a cubic matrix
*
* @param {MatrixBase, in} cubic matrix
* @param {MatrixBase, out} lower triangular part
**/
template<typename T, REQUIRE(is_Cubic<T>::value)>
constexpr inline T GetLowerTriangular(const T& xi_matrix) noexcept {
constexpr std::size_t N{ Rows_v<T> };
T xo_mat(0);
for (std::size_t i{}; i < N; ++i) {
for (std::size_t j{ i }; j < N; ++j) {
xo_mat(i, j) = xi_matrix(i, j);
}
}
return xo_mat;
}
/**
* \brief return the upper triangular part of a cubic matrix
*
* @param {MatrixBase, in} cubic matrix
* @param {MatrixBase, out} upper triangular part
**/
template<typename T, REQUIRE(is_Cubic<T>::value)>
constexpr inline T GetUpperTriangular(const T& xi_matrix) noexcept {
T xo_mat(0);
for (std::size_t i{}; i < Rows_v<T>; ++i) {
for (std::size_t j{}; j <= i; ++j) {
xo_mat(i, j) = xi_matrix(i, j);
}
}
return xo_mat;
}
/**
* \brief return a block of a given matrix, where the bloc size is known at compile time
*
* @param {MatrixBase, in} matrix whos block will be extracted
* @param {std::size_t, in} column start
* @param {std::size_t, in} column end
* @param {std::size_t, in} row start
* @param {std::size_t, in} row end
* @param {MatrixBase, out} block
**/
template<std::size_t COL_MIN, std::size_t COL_MAX,
std::size_t ROW_MIN, std::size_t ROW_MAX,
typename T, std::size_t COL, std::size_t ROW,
REQUIRE((ROW_MIN < ROW_MAX) && (COL_MIN < COL_MAX) && (ROW_MAX <= ROW) && (COL_MAX <= COL))>
constexpr inline MatrixBase<T, COL_MAX - COL_MIN + 1, ROW_MAX - ROW_MIN + 1> GetBlock(const MatrixBase<T, COL, ROW>& xi_matrix) noexcept {
MatrixBase<T, COL_MAX - COL_MIN + 1, ROW_MAX - ROW_MIN + 1> xo_block(T{});
for (std::size_t j{ COL_MIN }; j <= COL_MAX; ++j) {
for (std::size_t i{ ROW_MIN }; i <= ROW_MAX; ++i) {
xo_block(j - COL_MIN, i - ROW_MIN) = xi_matrix(j, i);
}
}
return xo_block;
}
// ---------------------------------
// --- general matrix operations ---
// ---------------------------------
/** \brief return the trace (sum of diagonal elements) of a matrix (cubic)
*
* @param {matrix, in} matrix
* @param {T, out} matrix trace
**/
template<typename T, REQUIRE(is_Cubic<T>::value && (Rows_v<T> == 2))>
constexpr inline underlying_type_t<T> Trace(const T& xi_matrix) noexcept {
return (xi_matrix(0, 0) + xi_matrix(1, 1));
}
template<typename T, REQUIRE(is_Cubic<T>::value && (Rows_v<T> == 3))>
constexpr inline underlying_type_t<T> Trace(const T& xi_matrix) noexcept {
return (xi_matrix(0, 0) + xi_matrix(1, 1) + xi_matrix(2, 2));
}
template<typename T, REQUIRE(is_Cubic<T>::value && (Rows_v<T> == 4))>
constexpr inline underlying_type_t<T> Trace(const T& xi_matrix) noexcept {
return (xi_matrix(0, 0) + xi_matrix(1, 1) + xi_matrix(2, 2) + xi_matrix(3, 3) );
}
template<typename T, REQUIRE(is_Cubic<T>::value && (Rows_v<T> > 4))>
constexpr inline underlying_type_t<T> Trace(const T& xi_matrix) noexcept {
using _T = underlying_type_t<T>;
constexpr std::size_t COL{ Columns_v<T> };
_T xo_trace{};
for (std::size_t i{}; i < COL; ++i) {
xo_trace += xi_matrix(i, i);
}
return xo_trace;
}
/** \brief return the determinant of a given matrix (cubic)
*
* @param {matrix, in} matrix
* @param {T, out} matrix determinant
**/
template<typename T, REQUIRE(is_Cubic<T>::value && (Rows_v<T> == 2))>
constexpr inline underlying_type_t<T> Determinant(const T& xi_matrix) noexcept {
return (xi_matrix(0, 0) * xi_matrix(1, 1) - xi_matrix(0, 1) * xi_matrix(1, 0));
}
template<typename T, REQUIRE(is_Cubic<T>::value && (Rows_v<T> == 3))>
constexpr inline underlying_type_t<T> Determinant(const T& xi_matrix) noexcept {
return (xi_matrix(0, 0) * (xi_matrix(1, 1) * xi_matrix(2, 2) - xi_matrix(2, 1) * xi_matrix(1, 2)) -
xi_matrix(0, 1) * (xi_matrix(1, 0) * xi_matrix(2, 2) - xi_matrix(2, 0) * xi_matrix(1, 2)) +
xi_matrix(0, 2) * (xi_matrix(1, 0) * xi_matrix(2, 1) - xi_matrix(2, 0) * xi_matrix(1, 1)));
}
template<typename T, REQUIRE(is_Cubic<T>::value && (Rows_v<T> == 4))>
constexpr inline underlying_type_t<T> Determinant(const T& xi_matrix) noexcept {
using _T = underlying_type_t<T>;
// minor 0
MatrixBase<_T, 3, 3> minor( xi_matrix(1, 1), xi_matrix(1, 2), xi_matrix(1, 3),
xi_matrix(2, 1), xi_matrix(2, 2), xi_matrix(2, 3),
xi_matrix(3, 1), xi_matrix(3, 2), xi_matrix(3, 3) );
const _T det0{ Determinant(minor) };
// minor 1
minor = MatrixBase<_T, 3, 3>( xi_matrix(1, 0), xi_matrix(1, 2), xi_matrix(1, 3),
xi_matrix(2, 0), xi_matrix(2, 2), xi_matrix(2, 3),
xi_matrix(3, 0), xi_matrix(3, 2), xi_matrix(3, 3) );
const _T det1{ Determinant(minor) };
// minor 2
minor = MatrixBase<_T, 3, 3>( xi_matrix(1, 0), xi_matrix(1, 1), xi_matrix(1, 3),
xi_matrix(2, 0), xi_matrix(2, 1), xi_matrix(2, 3),
xi_matrix(3, 0), xi_matrix(3, 1), xi_matrix(3, 3) );
const _T det2{ Determinant(minor) };
// minor 3
minor = MatrixBase<_T, 3, 3>( xi_matrix(1, 0), xi_matrix(1, 1), xi_matrix(1, 2),
xi_matrix(2, 0), xi_matrix(2, 1), xi_matrix(2, 2),
xi_matrix(3, 0), xi_matrix(3, 1), xi_matrix(3, 2) );
const _T det3{ Determinant(minor) };
// output
return (xi_matrix(0, 0) * det0 - xi_matrix(0, 1) * det1 +
xi_matrix(0, 2) * det2 - xi_matrix(0, 3) * det3);
}
template<typename T, REQUIRE(is_Cubic<T>::value && (Rows_v<T> > 4))>
constexpr inline underlying_type_t<T> Determinant(const T& xi_matrix) noexcept {
using _T = underlying_type_t<T>;
constexpr std::size_t COL{ Columns_v<T> };
// get matrix upper triangular decomposition (dolittle style)
T lu;
std::int32_t sign{};
VectorBase<std::size_t, COL> pivot;
LU(xi_matrix, lu, pivot, sign);
// determinant calculation
_T xo_det{ static_cast<_T>(sign) };
for (std::size_t i{}; i < COL; ++i) {
xo_det *= lu(i, i);
}
return xo_det;
}
/** \brief return the inverse of a given matrix (cubic).
* notice that assertion is the only way to test if matrix is singular (invertible).
*
* @param {matrix, in} matrix
* @param {matrix, out} matrix inverse
**/
template<typename T, REQUIRE(is_Cubic<T>::value && (Rows_v<T> == 2))>
constexpr inline T Inv(const T& xi_matrix) noexcept {
using _T = underlying_type_t<T>;
const _T det{ Determinant(xi_matrix) };
assert(std::abs(det) > std::numeric_limits<_T>::epsilon() && " attempting to inverse a not invertiable matrix.");
const _T detInv{ _T{ 1 } / det };
return MatrixBase<_T, 2, 2>( xi_matrix(1, 1) * detInv, -xi_matrix(0, 1) * detInv,
-xi_matrix(1, 0) * detInv, xi_matrix(0, 0) * detInv );
}
template<typename T, REQUIRE(is_Cubic<T>::value && (Rows_v<T> == 3))>
constexpr inline T Inv(const T& xi_matrix) noexcept {
using _T = underlying_type_t<T>;
const _T det{ Determinant(xi_matrix) };
assert(std::abs(det) > std::numeric_limits<_T>::epsilon() && " attempting to inverse a not invertiable matrix.");
const _T detInv{ _T{ 1 } / det };
return MatrixBase<_T, 3, 3>( (xi_matrix(1, 1) * xi_matrix(2, 2) - xi_matrix(2, 1) * xi_matrix(1, 2)) * detInv,
(xi_matrix(0, 2) * xi_matrix(2, 1) - xi_matrix(2, 2) * xi_matrix(0, 1)) * detInv,
(xi_matrix(0, 1) * xi_matrix(1, 2) - xi_matrix(1, 1) * xi_matrix(0, 2)) * detInv,
(xi_matrix(1, 2) * xi_matrix(2, 0) - xi_matrix(2, 2) * xi_matrix(1, 0)) * detInv,
(xi_matrix(0, 0) * xi_matrix(2, 2) - xi_matrix(2, 0) * xi_matrix(0, 2)) * detInv,
(xi_matrix(0, 2) * xi_matrix(1, 0) - xi_matrix(1, 2) * xi_matrix(0, 0)) * detInv,
(xi_matrix(1, 0) * xi_matrix(2, 1) - xi_matrix(2, 0) * xi_matrix(1, 1)) * detInv,
(xi_matrix(0, 1) * xi_matrix(2, 0) - xi_matrix(2, 1) * xi_matrix(0, 0)) * detInv,
(xi_matrix(0, 0) * xi_matrix(1, 1) - xi_matrix(1, 0) * xi_matrix(0, 1)) * detInv );
}
template<typename T, REQUIRE(is_Cubic<T>::value && (Rows_v<T> == 4))>
constexpr inline T Inv(const T& xi_matrix) noexcept {
using _T = underlying_type_t<T>;
MatrixBase<_T, 4, 4> xo_inv( xi_matrix.m_data[5] * xi_matrix.m_data[10] * xi_matrix.m_data[15] - xi_matrix.m_data[5] * xi_matrix.m_data[11] * xi_matrix.m_data[14] - xi_matrix.m_data[9] * xi_matrix.m_data[6] * xi_matrix.m_data[15] + xi_matrix.m_data[9] * xi_matrix.m_data[7] * xi_matrix.m_data[14] + xi_matrix.m_data[13] * xi_matrix.m_data[6] * xi_matrix.m_data[11] - xi_matrix.m_data[13] * xi_matrix.m_data[7] * xi_matrix.m_data[10],
-xi_matrix.m_data[1] * xi_matrix.m_data[10] * xi_matrix.m_data[15] + xi_matrix.m_data[1] * xi_matrix.m_data[11] * xi_matrix.m_data[14] + xi_matrix.m_data[9] * xi_matrix.m_data[2] * xi_matrix.m_data[15] - xi_matrix.m_data[9] * xi_matrix.m_data[3] * xi_matrix.m_data[14] - xi_matrix.m_data[13] * xi_matrix.m_data[2] * xi_matrix.m_data[11] + xi_matrix.m_data[13] * xi_matrix.m_data[3] * xi_matrix.m_data[10],
xi_matrix.m_data[1] * xi_matrix.m_data[6] * xi_matrix.m_data[15] - xi_matrix.m_data[1] * xi_matrix.m_data[7] * xi_matrix.m_data[14] - xi_matrix.m_data[5] * xi_matrix.m_data[2] * xi_matrix.m_data[15] + xi_matrix.m_data[5] * xi_matrix.m_data[3] * xi_matrix.m_data[14] + xi_matrix.m_data[13] * xi_matrix.m_data[2] * xi_matrix.m_data[7] - xi_matrix.m_data[13] * xi_matrix.m_data[3] * xi_matrix.m_data[6],
-xi_matrix.m_data[1] * xi_matrix.m_data[6] * xi_matrix.m_data[11] + xi_matrix.m_data[1] * xi_matrix.m_data[7] * xi_matrix.m_data[10] + xi_matrix.m_data[5] * xi_matrix.m_data[2] * xi_matrix.m_data[11] - xi_matrix.m_data[5] * xi_matrix.m_data[3] * xi_matrix.m_data[10] - xi_matrix.m_data[9] * xi_matrix.m_data[2] * xi_matrix.m_data[7] + xi_matrix.m_data[9] * xi_matrix.m_data[3] * xi_matrix.m_data[6],
-xi_matrix.m_data[4] * xi_matrix.m_data[10] * xi_matrix.m_data[15] + xi_matrix.m_data[4] * xi_matrix.m_data[11] * xi_matrix.m_data[14] + xi_matrix.m_data[8] * xi_matrix.m_data[6] * xi_matrix.m_data[15] - xi_matrix.m_data[8] * xi_matrix.m_data[7] * xi_matrix.m_data[14] - xi_matrix.m_data[12] * xi_matrix.m_data[6] * xi_matrix.m_data[11] + xi_matrix.m_data[12] * xi_matrix.m_data[7] * xi_matrix.m_data[10],
xi_matrix.m_data[0] * xi_matrix.m_data[10] * xi_matrix.m_data[15] - xi_matrix.m_data[0] * xi_matrix.m_data[11] * xi_matrix.m_data[14] - xi_matrix.m_data[8] * xi_matrix.m_data[2] * xi_matrix.m_data[15] + xi_matrix.m_data[8] * xi_matrix.m_data[3] * xi_matrix.m_data[14] + xi_matrix.m_data[12] * xi_matrix.m_data[2] * xi_matrix.m_data[11] - xi_matrix.m_data[12] * xi_matrix.m_data[3] * xi_matrix.m_data[10],
-xi_matrix.m_data[0] * xi_matrix.m_data[6] * xi_matrix.m_data[15] + xi_matrix.m_data[0] * xi_matrix.m_data[7] * xi_matrix.m_data[14] + xi_matrix.m_data[4] * xi_matrix.m_data[2] * xi_matrix.m_data[15] - xi_matrix.m_data[4] * xi_matrix.m_data[3] * xi_matrix.m_data[14] - xi_matrix.m_data[12] * xi_matrix.m_data[2] * xi_matrix.m_data[7] + xi_matrix.m_data[12] * xi_matrix.m_data[3] * xi_matrix.m_data[6],
xi_matrix.m_data[0] * xi_matrix.m_data[6] * xi_matrix.m_data[11] - xi_matrix.m_data[0] * xi_matrix.m_data[7] * xi_matrix.m_data[10] - xi_matrix.m_data[4] * xi_matrix.m_data[2] * xi_matrix.m_data[11] + xi_matrix.m_data[4] * xi_matrix.m_data[3] * xi_matrix.m_data[10] + xi_matrix.m_data[8] * xi_matrix.m_data[2] * xi_matrix.m_data[7] - xi_matrix.m_data[8] * xi_matrix.m_data[3] * xi_matrix.m_data[6],
xi_matrix.m_data[4] * xi_matrix.m_data[9] * xi_matrix.m_data[15] - xi_matrix.m_data[4] * xi_matrix.m_data[11] * xi_matrix.m_data[13] - xi_matrix.m_data[8] * xi_matrix.m_data[5] * xi_matrix.m_data[15] + xi_matrix.m_data[8] * xi_matrix.m_data[7] * xi_matrix.m_data[13] + xi_matrix.m_data[12] * xi_matrix.m_data[5] * xi_matrix.m_data[11] - xi_matrix.m_data[12] * xi_matrix.m_data[7] * xi_matrix.m_data[9],
-xi_matrix.m_data[0] * xi_matrix.m_data[9] * xi_matrix.m_data[15] + xi_matrix.m_data[0] * xi_matrix.m_data[11] * xi_matrix.m_data[13] + xi_matrix.m_data[8] * xi_matrix.m_data[1] * xi_matrix.m_data[15] - xi_matrix.m_data[8] * xi_matrix.m_data[3] * xi_matrix.m_data[13] - xi_matrix.m_data[12] * xi_matrix.m_data[1] * xi_matrix.m_data[11] + xi_matrix.m_data[12] * xi_matrix.m_data[3] * xi_matrix.m_data[9],
xi_matrix.m_data[0] * xi_matrix.m_data[5] * xi_matrix.m_data[15] - xi_matrix.m_data[0] * xi_matrix.m_data[7] * xi_matrix.m_data[13] - xi_matrix.m_data[4] * xi_matrix.m_data[1] * xi_matrix.m_data[15] + xi_matrix.m_data[4] * xi_matrix.m_data[3] * xi_matrix.m_data[13] + xi_matrix.m_data[12] * xi_matrix.m_data[1] * xi_matrix.m_data[7] - xi_matrix.m_data[12] * xi_matrix.m_data[3] * xi_matrix.m_data[5],
-xi_matrix.m_data[0] * xi_matrix.m_data[5] * xi_matrix.m_data[11] + xi_matrix.m_data[0] * xi_matrix.m_data[7] * xi_matrix.m_data[9] + xi_matrix.m_data[4] * xi_matrix.m_data[1] * xi_matrix.m_data[11] - xi_matrix.m_data[4] * xi_matrix.m_data[3] * xi_matrix.m_data[9] - xi_matrix.m_data[8] * xi_matrix.m_data[1] * xi_matrix.m_data[7] + xi_matrix.m_data[8] * xi_matrix.m_data[3] * xi_matrix.m_data[5],
-xi_matrix.m_data[4] * xi_matrix.m_data[9] * xi_matrix.m_data[14] + xi_matrix.m_data[4] * xi_matrix.m_data[10] * xi_matrix.m_data[13] + xi_matrix.m_data[8] * xi_matrix.m_data[5] * xi_matrix.m_data[14] - xi_matrix.m_data[8] * xi_matrix.m_data[6] * xi_matrix.m_data[13] - xi_matrix.m_data[12] * xi_matrix.m_data[5] * xi_matrix.m_data[10] + xi_matrix.m_data[12] * xi_matrix.m_data[6] * xi_matrix.m_data[9],
xi_matrix.m_data[0] * xi_matrix.m_data[9] * xi_matrix.m_data[14] - xi_matrix.m_data[0] * xi_matrix.m_data[10] * xi_matrix.m_data[13] - xi_matrix.m_data[8] * xi_matrix.m_data[1] * xi_matrix.m_data[14] + xi_matrix.m_data[8] * xi_matrix.m_data[2] * xi_matrix.m_data[13] + xi_matrix.m_data[12] * xi_matrix.m_data[1] * xi_matrix.m_data[10] - xi_matrix.m_data[12] * xi_matrix.m_data[2] * xi_matrix.m_data[9],
-xi_matrix.m_data[0] * xi_matrix.m_data[5] * xi_matrix.m_data[14] + xi_matrix.m_data[0] * xi_matrix.m_data[6] * xi_matrix.m_data[13] + xi_matrix.m_data[4] * xi_matrix.m_data[1] * xi_matrix.m_data[14] - xi_matrix.m_data[4] * xi_matrix.m_data[2] * xi_matrix.m_data[13] - xi_matrix.m_data[12] * xi_matrix.m_data[1] * xi_matrix.m_data[6] + xi_matrix.m_data[12] * xi_matrix.m_data[2] * xi_matrix.m_data[5],
xi_matrix.m_data[0] * xi_matrix.m_data[5] * xi_matrix.m_data[10] - xi_matrix.m_data[0] * xi_matrix.m_data[6] * xi_matrix.m_data[9] - xi_matrix.m_data[4] * xi_matrix.m_data[1] * xi_matrix.m_data[10] + xi_matrix.m_data[4] * xi_matrix.m_data[2] * xi_matrix.m_data[9] + xi_matrix.m_data[8] * xi_matrix.m_data[1] * xi_matrix.m_data[6] - xi_matrix.m_data[8] * xi_matrix.m_data[2] * xi_matrix.m_data[5]);
_T det{ xi_matrix.m_data[0] * xo_inv.m_data[0] + xi_matrix.m_data[1] * xo_inv.m_data[4] +
xi_matrix.m_data[2] * xo_inv.m_data[8] + xi_matrix.m_data[3] * xo_inv.m_data[12] };
assert(std::abs(det) > std::numeric_limits<_T>::epsilon() && " attempting to inverse a not invertiable matrix.");
det = static_cast<_T>(1) / det;
xo_inv *= det;
return xo_inv;
}
template<typename T, REQUIRE(is_Cubic<T>::value && (Rows_v<T> > 4))>
constexpr inline T Inv(const T& xi_matrix) noexcept {
using _T = underlying_type_t<T>;
constexpr std::size_t COL{ Columns_v<T> };
// LU decompose
T lu, xo_inv( _T{} );
VectorBase<std::size_t, COL> P;
std::int32_t sign{};
LU(xi_matrix, lu, P, sign);
// iverted matrix
for (std::size_t j{}; j < COL; ++j) {
// columns
for (std::size_t i{}; i < COL; ++i) {
xo_inv(j, i) = (P[i] == j) * static_cast<_T>(1);
for (std::size_t k{}; k < i; ++k) {
xo_inv(j, i) -= lu(k, i) * xo_inv(j, k);
}
}
// rows
for (std::int32_t i{ static_cast<std::int32_t>(COL) - 1 }; i >= 0; i--) {
for (std::int32_t k{ i + 1 }; k < static_cast<std::int32_t>(COL); ++k) {
xo_inv(j, i) -= lu(static_cast<std::size_t>(k), static_cast<std::size_t>(i)) * xo_inv(static_cast<std::size_t>(j), static_cast<std::size_t>(k));
}
xo_inv(static_cast<std::size_t>(j), static_cast<std::size_t>(i)) /= lu(static_cast<std::size_t>(i), static_cast<std::size_t>(i));
}
}
return xo_inv;
}
/**
* \brief orthonormalize a given matrix (make it columns orthogonal and normalized)
* using modified gram-schmidt process.
*
* @param {matrixBase, in} matrix to be orthonormalized
* @param {matrixBase, out} orthonormalized matrix
**/
template<typename T, REQUIRE(is_MatrixBase_v<T>)>
constexpr T Orthonormalize(const T& xi_matrix) noexcept {
using _T = underlying_type_t<T>;
constexpr std::size_t COL{ Columns_v<T> };
// gram-schmidt
T xo_mat;
for (std::size_t i{}; i < COL; ++i) {
xo_mat[i] = xi_matrix[i];
for (std::size_t j{}; j < i; ++j) {
xo_mat[i] -= ProjectOn(xo_mat[i], xo_mat[j]);
}
xo_mat[i] = Normalize(std::move(xo_mat[i]));
}
return xo_mat;
}
// ----------------------
// --- Decompositions ---
// ----------------------
/**
* \brief perform LU decomposition of a cubic matrix (using Doolittle algorithm),
* i.e. - given matrix A, decompose it to L*P*U, where L is lower traingular with unit diagonal,
* U is an upper triangular and P is a diagonal pivot matrix (given as a vector holding its diagonal)
*
* @param {matrixBase, in} cubic matrix to be decomposed
* @param {matrixBase, out} decomposed matrix, such that its upper triangular portion
* is the 'U' portion, and its lower triangulat portion is the 'L' portion.
* lower triangular matrix should have its diagonal as '1'.
* @param {VectorBase, out} decomposition pivot vector (row vector, i.e - VectorN<std::size_t, COL>)
* @param {INT32_T, out} pivot sign
**/
template<typename T, typename U, REQUIRE(is_Cubic<T>::value && Is_VectorOfLength_v<U, Columns_v<T>> && std::is_same_v<std::size_t, underlying_type_t<U>>)>
constexpr void LU(const T& xi_matrix, T& xo_lu, U& xo_pivot, std::int32_t& xo_sign) noexcept {
using _T = underlying_type_t<T>;
constexpr std::size_t COL{ Columns_v<T> };
xo_lu = xi_matrix;
xo_pivot = [](const std::size_t i) { return i; };
xo_sign = 1;
for (std::size_t c{}; c < COL; ++c) {
// find pivot
std::size_t pivot{ c };
for (std::size_t r{ c + 1 }; r < COL; ++r) {
if (std::abs(xo_lu(c, r)) > std::abs(xo_lu(c, pivot))) {
pivot = r;
}
}
// exchange pivot
if (pivot != c) {
for (std::size_t cc{}; cc < COL; ++cc) {
std::swap(xo_lu(cc, pivot), xo_lu(cc, c));
}
std::swap(xo_pivot[pivot], xo_pivot[c]);
xo_sign = -xo_sign;
}
// calculate multipliers and eliminate c-th column.
if (xo_lu(c, c) != _T{}) {
for (std::size_t r{ c + 1 }; r < COL; ++r) {
xo_lu(c, r) /= xo_lu(c, c);
for (std::size_t cc{ c + 1 }; cc < COL; ++cc) {
xo_lu(cc, r) -= xo_lu(c, r) * xo_lu(cc, c);
}
}
}
}
}
/**
* \brief perform QR decomposition of a cubic using gram-schmidt process.
*
* @param {matrixBase, in} cubic matrix to be decomposed (whose number of rows is either equal or larger then the number of columns)
* @param {matrixBase, out} Q matrix (orthogonal matrix with orthogonal columns, i.e. - Q*Q^T = I; COLxROW)
* @param {matrixBase, out} R matrix (upper triangular matrix; COLxCOL)
**/
template<typename T, REQUIRE(is_MatrixBase_v<T> && (Rows_v<T> >= Columns_v<T>))>
constexpr void QRgramSchmidt(const T& xi_matrix,
MatrixBase<underlying_type_t<T>, Columns_v<T>, Rows_v<T>>& xo_Q,
MatrixBase<underlying_type_t<T>, Columns_v<T>, Columns_v<T>>& xo_R) noexcept {
using _T = underlying_type_t<T>;
constexpr std::size_t COL{ Columns_v<T> };
// Q
xo_Q = Orthonormalize(xi_matrix);
// R
xo_R = _T{};
for (std::size_t i{}; i < COL; ++i) {
for (std::size_t j{ i }; j < COL; ++j) {
xo_R(j, i) = dot(xi_matrix[j], xo_Q[i]);
}
}
}
/**
* \brief perform QR decomposition using "Givens rotations".
*
* @param {matrixBase, in} matrix to be decomposed (whose number of rows is either equal or larger then the number of columns)
* @param {matrixBase, out} Q matrix (orthogonal matrix with orthogonal columns, i.e. - Q*Q^T = I; COLxROW)
* @param {matrixBase, out} R matrix (upper triangular matrix; COLxCOL)
**/
template<typename T, REQUIRE(is_MatrixBase_v<T> && (Rows_v<T> >= Columns_v<T>))>
void QRgivensRotations(const T& xi_matrix, T& xo_Q,
MatrixBase<underlying_type_t<T>, Columns_v<T>, Columns_v<T>>& xo_R) noexcept {
using _T = underlying_type_t<T>;
constexpr std::size_t COL{ Columns_v<T> },
ROW{ Rows_v<T> };
/**
* "givens rotation" (return in order of {cosine, sine, radius})
**/
auto GivensRotationInternal = [](const _T& a, const _T& b) {
constexpr _T one{ static_cast<_T>(1) },
TOL{ static_cast<_T>(2) * std::numeric_limits<_T>::epsilon() };
// a == 0
if (std::abs(a) <= TOL) {
return VectorBase<_T, 3>(_T{}, std::copysign(one, b), std::abs(b));
} // b == 0
else if (std::abs(b) <= TOL) {
return VectorBase<_T, 3>(std::copysign(one, a), _T{}, std::abs(a));
} // a > b
else if (std::abs(a) > std::abs(b)) {
const _T t{ b / a },
u{ std::copysign(one, a) * std::sqrt(one + t * t) },
c{ one / u};
return VectorBase<_T, 3>(c, t * c, a * u);
} // a <= b
else {
const _T t{ a / b },
u{std::copysign(one, b) * std::sqrt(one + t * t)},
s{ one / u};
return VectorBase<_T, 3>(t * s, s, b * u);
}
};
MatrixBase<_T, COL, ROW> R(xi_matrix);
MatrixBase<_T, ROW, ROW> Q;
for (std::size_t j{}; j < COL; ++j) {
for (std::size_t i{ ROW - 1 }; i >= j + 1; --i) {
VectorBase<_T, 3> CSR( GivensRotationInternal(R(j, i - 1), R(j, i)) );
// R' = G * R
for (std::size_t x{}; x < COL; ++x) {
const _T temp1{ R(x, i - 1) },
temp2{ R(x, i) };
R(x, i - 1) = temp1 * CSR[0] + temp2 * CSR[1];
R(x, i) = -temp1 * CSR[1] + temp2 * CSR[0];
}
R(j, i - 1) = CSR[2];
R(j, i) = _T{};
// Q' = Q * G^
for (std::size_t x{}; x < ROW; ++x) {
const _T temp1{ Q(i - 1, x) },
temp2{ Q(i, x) };
Q(i - 1, x) = temp1 * CSR[0] + temp2 * CSR[1];
Q(i, x) = -temp1 * CSR[1] + temp2 * CSR[0];
}
}
}
// adjust Q
if constexpr (ROW != COL) {
Q = Transpose(std::move(Q));
}
xo_Q = GetBlock<0, COL - 1, 0, ROW - 1>(Q);
// adjust R
xo_R = GetBlock<0, COL-1, 0, COL-1>(R);
}