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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
// Copyright (c) Sleipnir contributors
#pragma once
#include <algorithm>
#include <concepts>
#include <initializer_list>
#include <iterator>
#include <span>
#include <utility>
#include <vector>
#include <Eigen/Core>
#include <gch/small_vector.hpp>
#include "sleipnir/autodiff/slice.hpp"
#include "sleipnir/autodiff/variable.hpp"
#include "sleipnir/autodiff/variable_block.hpp"
#include "sleipnir/util/assert.hpp"
#include "sleipnir/util/concepts.hpp"
#include "sleipnir/util/function_ref.hpp"
#include "sleipnir/util/symbol_exports.hpp"
namespace slp {
/**
* A matrix of autodiff variables.
*/
class SLEIPNIR_DLLEXPORT VariableMatrix {
public:
/**
* Type tag used to designate an uninitialized VariableMatrix.
*/
struct empty_t {};
/**
* Designates an uninitialized VariableMatrix.
*/
static constexpr empty_t empty{};
/**
* Constructs an empty VariableMatrix.
*/
VariableMatrix() = default;
/**
* Constructs a VariableMatrix column vector with the given rows.
*
* @param rows The number of matrix rows.
*/
explicit VariableMatrix(int rows) : m_rows{rows}, m_cols{1} {
m_storage.reserve(rows);
for (int row = 0; row < rows; ++row) {
m_storage.emplace_back();
}
}
/**
* Constructs a zero-initialized VariableMatrix with the given dimensions.
*
* @param rows The number of matrix rows.
* @param cols The number of matrix columns.
*/
VariableMatrix(int rows, int cols) : m_rows{rows}, m_cols{cols} {
m_storage.reserve(rows * cols);
for (int index = 0; index < rows * cols; ++index) {
m_storage.emplace_back();
}
}
/**
* Constructs an empty VariableMatrix with the given dimensions.
*
* @param rows The number of matrix rows.
* @param cols The number of matrix columns.
*/
VariableMatrix(empty_t, int rows, int cols) : m_rows{rows}, m_cols{cols} {
m_storage.reserve(rows * cols);
for (int index = 0; index < rows * cols; ++index) {
m_storage.emplace_back(nullptr);
}
}
/**
* Constructs a scalar VariableMatrix from a nested list of Variables.
*
* @param list The nested list of Variables.
*/
VariableMatrix( // NOLINT
std::initializer_list<std::initializer_list<Variable>> list) {
// Get row and column counts for destination matrix
m_rows = list.size();
m_cols = 0;
if (list.size() > 0) {
m_cols = list.begin()->size();
}
// Assert the first and latest column counts are the same
for ([[maybe_unused]]
const auto& row : list) {
slp_assert(list.begin()->size() == row.size());
}
m_storage.reserve(rows() * cols());
for (const auto& row : list) {
std::ranges::copy(row, std::back_inserter(m_storage));
}
}
/**
* Constructs a scalar VariableMatrix from a nested list of doubles.
*
* This overload is for Python bindings only.
*
* @param list The nested list of Variables.
*/
VariableMatrix(const std::vector<std::vector<double>>& list) { // NOLINT
// Get row and column counts for destination matrix
m_rows = list.size();
m_cols = 0;
if (list.size() > 0) {
m_cols = list.begin()->size();
}
// Assert the first and latest column counts are the same
for ([[maybe_unused]]
const auto& row : list) {
slp_assert(list.begin()->size() == row.size());
}
m_storage.reserve(rows() * cols());
for (const auto& row : list) {
std::ranges::copy(row, std::back_inserter(m_storage));
}
}
/**
* Constructs a scalar VariableMatrix from a nested list of Variables.
*
* This overload is for Python bindings only.
*
* @param list The nested list of Variables.
*/
VariableMatrix(const std::vector<std::vector<Variable>>& list) { // NOLINT
// Get row and column counts for destination matrix
m_rows = list.size();
m_cols = 0;
if (list.size() > 0) {
m_cols = list.begin()->size();
}
// Assert the first and latest column counts are the same
for ([[maybe_unused]]
const auto& row : list) {
slp_assert(list.begin()->size() == row.size());
}
m_storage.reserve(rows() * cols());
for (const auto& row : list) {
std::ranges::copy(row, std::back_inserter(m_storage));
}
}
/**
* Constructs a VariableMatrix from an Eigen matrix.
*
* @param values Eigen matrix of values.
*/
template <typename Derived>
VariableMatrix(const Eigen::MatrixBase<Derived>& values) // NOLINT
: m_rows{static_cast<int>(values.rows())},
m_cols{static_cast<int>(values.cols())} {
m_storage.reserve(values.rows() * values.cols());
for (int row = 0; row < values.rows(); ++row) {
for (int col = 0; col < values.cols(); ++col) {
m_storage.emplace_back(values(row, col));
}
}
}
/**
* Constructs a VariableMatrix from an Eigen diagonal matrix.
*
* @param values Diagonal matrix of values.
*/
template <typename Derived>
VariableMatrix(const Eigen::DiagonalBase<Derived>& values) // NOLINT
: m_rows{static_cast<int>(values.rows())},
m_cols{static_cast<int>(values.cols())} {
m_storage.reserve(values.rows() * values.cols());
for (int row = 0; row < values.rows(); ++row) {
for (int col = 0; col < values.cols(); ++col) {
if (row == col) {
m_storage.emplace_back(values.diagonal()[row]);
} else {
m_storage.emplace_back(0.0);
}
}
}
}
/**
* Assigns an Eigen matrix to a VariableMatrix.
*
* @param values Eigen matrix of values.
* @return This VariableMatrix.
*/
template <typename Derived>
VariableMatrix& operator=(const Eigen::MatrixBase<Derived>& values) {
slp_assert(rows() == values.rows() && cols() == values.cols());
for (int row = 0; row < values.rows(); ++row) {
for (int col = 0; col < values.cols(); ++col) {
(*this)(row, col) = values(row, col);
}
}
return *this;
}
/**
* Assigns a double to the matrix.
*
* This only works for matrices with one row and one column.
*
* @param value Value to assign.
* @return This VariableMatrix.
*/
VariableMatrix& operator=(ScalarLike auto value) {
slp_assert(rows() == 1 && cols() == 1);
(*this)(0, 0) = value;
return *this;
}
/**
* Sets the VariableMatrix's internal values.
*
* @param values Eigen matrix of values.
*/
template <typename Derived>
requires std::same_as<typename Derived::Scalar, double>
void set_value(const Eigen::MatrixBase<Derived>& values) {
slp_assert(rows() == values.rows() && cols() == values.cols());
for (int row = 0; row < values.rows(); ++row) {
for (int col = 0; col < values.cols(); ++col) {
(*this)(row, col).set_value(values(row, col));
}
}
}
/**
* Constructs a scalar VariableMatrix from a Variable.
*
* @param variable Variable.
*/
VariableMatrix(const Variable& variable) // NOLINT
: m_rows{1}, m_cols{1} {
m_storage.emplace_back(variable);
}
/**
* Constructs a scalar VariableMatrix from a Variable.
*
* @param variable Variable.
*/
VariableMatrix(Variable&& variable) : m_rows{1}, m_cols{1} { // NOLINT
m_storage.emplace_back(std::move(variable));
}
/**
* Constructs a VariableMatrix from a VariableBlock.
*
* @param values VariableBlock of values.
*/
VariableMatrix(const VariableBlock<VariableMatrix>& values) // NOLINT
: m_rows{values.rows()}, m_cols{values.cols()} {
m_storage.reserve(rows() * cols());
for (int row = 0; row < rows(); ++row) {
for (int col = 0; col < cols(); ++col) {
m_storage.emplace_back(values(row, col));
}
}
}
/**
* Constructs a VariableMatrix from a VariableBlock.
*
* @param values VariableBlock of values.
*/
VariableMatrix(const VariableBlock<const VariableMatrix>& values) // NOLINT
: m_rows{values.rows()}, m_cols{values.cols()} {
m_storage.reserve(rows() * cols());
for (int row = 0; row < rows(); ++row) {
for (int col = 0; col < cols(); ++col) {
m_storage.emplace_back(values(row, col));
}
}
}
/**
* Constructs a column vector wrapper around a Variable array.
*
* @param values Variable array to wrap.
*/
explicit VariableMatrix(std::span<const Variable> values)
: m_rows{static_cast<int>(values.size())}, m_cols{1} {
m_storage.reserve(rows() * cols());
for (int row = 0; row < rows(); ++row) {
for (int col = 0; col < cols(); ++col) {
m_storage.emplace_back(values[row * cols() + col]);
}
}
}
/**
* Constructs a matrix wrapper around a Variable array.
*
* @param values Variable array to wrap.
* @param rows The number of matrix rows.
* @param cols The number of matrix columns.
*/
VariableMatrix(std::span<const Variable> values, int rows, int cols)
: m_rows{rows}, m_cols{cols} {
slp_assert(static_cast<int>(values.size()) == rows * cols);
m_storage.reserve(rows * cols);
for (int row = 0; row < rows; ++row) {
for (int col = 0; col < cols; ++col) {
m_storage.emplace_back(values[row * cols + col]);
}
}
}
/**
* Returns a block pointing to the given row and column.
*
* @param row The block row.
* @param col The block column.
* @return A block pointing to the given row and column.
*/
Variable& operator()(int row, int col) {
slp_assert(row >= 0 && row < rows());
slp_assert(col >= 0 && col < cols());
return m_storage[row * cols() + col];
}
/**
* Returns a block pointing to the given row and column.
*
* @param row The block row.
* @param col The block column.
* @return A block pointing to the given row and column.
*/
const Variable& operator()(int row, int col) const {
slp_assert(row >= 0 && row < rows());
slp_assert(col >= 0 && col < cols());
return m_storage[row * cols() + col];
}
/**
* Returns a block pointing to the given row.
*
* @param row The block row.
* @return A block pointing to the given row.
*/
Variable& operator[](int row) {
slp_assert(row >= 0 && row < rows() * cols());
return m_storage[row];
}
/**
* Returns a block pointing to the given row.
*
* @param row The block row.
* @return A block pointing to the given row.
*/
const Variable& operator[](int row) const {
slp_assert(row >= 0 && row < rows() * cols());
return m_storage[row];
}
/**
* Returns a block of the variable matrix.
*
* @param row_offset The row offset of the block selection.
* @param col_offset The column offset of the block selection.
* @param block_rows The number of rows in the block selection.
* @param block_cols The number of columns in the block selection.
* @return A block of the variable matrix.
*/
VariableBlock<VariableMatrix> block(int row_offset, int col_offset,
int block_rows, int block_cols) {
slp_assert(row_offset >= 0 && row_offset <= rows());
slp_assert(col_offset >= 0 && col_offset <= cols());
slp_assert(block_rows >= 0 && block_rows <= rows() - row_offset);
slp_assert(block_cols >= 0 && block_cols <= cols() - col_offset);
return VariableBlock{*this, row_offset, col_offset, block_rows, block_cols};
}
/**
* Returns a block of the variable matrix.
*
* @param row_offset The row offset of the block selection.
* @param col_offset The column offset of the block selection.
* @param block_rows The number of rows in the block selection.
* @param block_cols The number of columns in the block selection.
* @return A block of the variable matrix.
*/
const VariableBlock<const VariableMatrix> block(int row_offset,
int col_offset,
int block_rows,
int block_cols) const {
slp_assert(row_offset >= 0 && row_offset <= rows());
slp_assert(col_offset >= 0 && col_offset <= cols());
slp_assert(block_rows >= 0 && block_rows <= rows() - row_offset);
slp_assert(block_cols >= 0 && block_cols <= cols() - col_offset);
return VariableBlock{*this, row_offset, col_offset, block_rows, block_cols};
}
/**
* Returns a slice of the variable matrix.
*
* @param row_slice The row slice.
* @param col_slice The column slice.
* @return A slice of the variable matrix.
*/
VariableBlock<VariableMatrix> operator()(Slice row_slice, Slice col_slice) {
int row_slice_length = row_slice.adjust(rows());
int col_slice_length = col_slice.adjust(cols());
return VariableBlock{*this, std::move(row_slice), row_slice_length,
std::move(col_slice), col_slice_length};
}
/**
* Returns a slice of the variable matrix.
*
* @param row_slice The row slice.
* @param col_slice The column slice.
* @return A slice of the variable matrix.
*/
const VariableBlock<const VariableMatrix> operator()(Slice row_slice,
Slice col_slice) const {
int row_slice_length = row_slice.adjust(rows());
int col_slice_length = col_slice.adjust(cols());
return VariableBlock{*this, std::move(row_slice), row_slice_length,
std::move(col_slice), col_slice_length};
}
/**
* Returns a slice of the variable matrix.
*
* The given slices aren't adjusted. This overload is for Python bindings
* only.
*
* @param row_slice The row slice.
* @param row_slice_length The row slice length.
* @param col_slice The column slice.
* @param col_slice_length The column slice length.
* @return A slice of the variable matrix.
*
*/
VariableBlock<VariableMatrix> operator()(Slice row_slice,
int row_slice_length,
Slice col_slice,
int col_slice_length) {
return VariableBlock{*this, std::move(row_slice), row_slice_length,
std::move(col_slice), col_slice_length};
}
/**
* Returns a slice of the variable matrix.
*
* The given slices aren't adjusted. This overload is for Python bindings
* only.
*
* @param row_slice The row slice.
* @param row_slice_length The row slice length.
* @param col_slice The column slice.
* @param col_slice_length The column slice length.
* @return A slice of the variable matrix.
*/
const VariableBlock<const VariableMatrix> operator()(
Slice row_slice, int row_slice_length, Slice col_slice,
int col_slice_length) const {
return VariableBlock{*this, std::move(row_slice), row_slice_length,
std::move(col_slice), col_slice_length};
}
/**
* Returns a segment of the variable vector.
*
* @param offset The offset of the segment.
* @param length The length of the segment.
* @return A segment of the variable vector.
*/
VariableBlock<VariableMatrix> segment(int offset, int length) {
slp_assert(offset >= 0 && offset < rows() * cols());
slp_assert(length >= 0 && length <= rows() * cols() - offset);
return block(offset, 0, length, 1);
}
/**
* Returns a segment of the variable vector.
*
* @param offset The offset of the segment.
* @param length The length of the segment.
* @return A segment of the variable vector.
*/
const VariableBlock<const VariableMatrix> segment(int offset,
int length) const {
slp_assert(offset >= 0 && offset < rows() * cols());
slp_assert(length >= 0 && length <= rows() * cols() - offset);
return block(offset, 0, length, 1);
}
/**
* Returns a row slice of the variable matrix.
*
* @param row The row to slice.
* @return A row slice of the variable matrix.
*/
VariableBlock<VariableMatrix> row(int row) {
slp_assert(row >= 0 && row < rows());
return block(row, 0, 1, cols());
}
/**
* Returns a row slice of the variable matrix.
*
* @param row The row to slice.
* @return A row slice of the variable matrix.
*/
const VariableBlock<const VariableMatrix> row(int row) const {
slp_assert(row >= 0 && row < rows());
return block(row, 0, 1, cols());
}
/**
* Returns a column slice of the variable matrix.
*
* @param col The column to slice.
* @return A column slice of the variable matrix.
*/
VariableBlock<VariableMatrix> col(int col) {
slp_assert(col >= 0 && col < cols());
return block(0, col, rows(), 1);
}
/**
* Returns a column slice of the variable matrix.
*
* @param col The column to slice.
* @return A column slice of the variable matrix.
*/
const VariableBlock<const VariableMatrix> col(int col) const {
slp_assert(col >= 0 && col < cols());
return block(0, col, rows(), 1);
}
/**
* Matrix multiplication operator.
*
* @param lhs Operator left-hand side.
* @param rhs Operator right-hand side.
*/
template <MatrixLike LHS, MatrixLike RHS>
requires SleipnirMatrixLike<LHS> || SleipnirMatrixLike<RHS>
friend SLEIPNIR_DLLEXPORT VariableMatrix operator*(const LHS& lhs,
const RHS& rhs) {
slp_assert(lhs.cols() == rhs.rows());
VariableMatrix result(VariableMatrix::empty, lhs.rows(), rhs.cols());
#if __GNUC__ >= 12
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
for (int i = 0; i < lhs.rows(); ++i) {
for (int j = 0; j < rhs.cols(); ++j) {
Variable sum;
for (int k = 0; k < lhs.cols(); ++k) {
sum += lhs(i, k) * rhs(k, j);
}
result(i, j) = sum;
}
}
#if __GNUC__ >= 12
#pragma GCC diagnostic pop
#endif
return result;
}
/**
* Matrix-scalar multiplication operator.
*
* @param lhs Operator left-hand side.
* @param rhs Operator right-hand side.
*/
friend SLEIPNIR_DLLEXPORT VariableMatrix
operator*(const SleipnirMatrixLike auto& lhs, const ScalarLike auto& rhs) {
VariableMatrix result{VariableMatrix::empty, lhs.rows(), lhs.cols()};
for (int row = 0; row < result.rows(); ++row) {
for (int col = 0; col < result.cols(); ++col) {
result(row, col) = lhs(row, col) * rhs;
}
}
return result;
}
/**
* Matrix-scalar multiplication operator.
*
* @param lhs Operator left-hand side.
* @param rhs Operator right-hand side.
*/
friend SLEIPNIR_DLLEXPORT VariableMatrix operator*(const MatrixLike auto& lhs,
const Variable& rhs) {
VariableMatrix result(VariableMatrix::empty, lhs.rows(), lhs.cols());
for (int row = 0; row < result.rows(); ++row) {
for (int col = 0; col < result.cols(); ++col) {
result(row, col) = lhs(row, col) * rhs;
}
}
return result;
}
/**
* Scalar-matrix multiplication operator.
*
* @param lhs Operator left-hand side.
* @param rhs Operator right-hand side.
*/
friend SLEIPNIR_DLLEXPORT VariableMatrix
operator*(const ScalarLike auto& lhs, const SleipnirMatrixLike auto& rhs) {
VariableMatrix result{VariableMatrix::empty, rhs.rows(), rhs.cols()};
for (int row = 0; row < result.rows(); ++row) {
for (int col = 0; col < result.cols(); ++col) {
result(row, col) = rhs(row, col) * lhs;
}
}
return result;
}
/**
* Scalar-matrix multiplication operator.
*
* @param lhs Operator left-hand side.
* @param rhs Operator right-hand side.
*/
friend SLEIPNIR_DLLEXPORT VariableMatrix
operator*(const Variable& lhs, const MatrixLike auto& rhs) {
VariableMatrix result(VariableMatrix::empty, rhs.rows(), rhs.cols());
for (int row = 0; row < result.rows(); ++row) {
for (int col = 0; col < result.cols(); ++col) {
result(row, col) = rhs(row, col) * lhs;
}
}
return result;
}
/**
* Compound matrix multiplication-assignment operator.
*
* @param rhs Variable to multiply.
* @return Result of multiplication.
*/
VariableMatrix& operator*=(const MatrixLike auto& rhs) {
slp_assert(cols() == rhs.rows() && cols() == rhs.cols());
for (int i = 0; i < rows(); ++i) {
for (int j = 0; j < rhs.cols(); ++j) {
Variable sum;
for (int k = 0; k < cols(); ++k) {
sum += (*this)(i, k) * rhs(k, j);
}
(*this)(i, j) = sum;
}
}
return *this;
}
/**
* Compound matrix-scalar multiplication-assignment operator.
*
* @param rhs Variable to multiply.
* @return Result of multiplication.
*/
VariableMatrix& operator*=(const ScalarLike auto& rhs) {
for (int row = 0; row < rows(); ++row) {
for (int col = 0; col < rhs.cols(); ++col) {
(*this)(row, col) *= rhs;
}
}
return *this;
}
/**
* Binary division operator.
*
* @param lhs Operator left-hand side.
* @param rhs Operator right-hand side.
* @return Result of division.
*/
friend SLEIPNIR_DLLEXPORT VariableMatrix
operator/(const MatrixLike auto& lhs, const ScalarLike auto& rhs) {
VariableMatrix result(VariableMatrix::empty, lhs.rows(), lhs.cols());
for (int row = 0; row < result.rows(); ++row) {
for (int col = 0; col < result.cols(); ++col) {
result(row, col) = lhs(row, col) / rhs;
}
}
return result;
}
/**
* Compound matrix division-assignment operator.
*
* @param rhs Variable to divide.
* @return Result of division.
*/
VariableMatrix& operator/=(const ScalarLike auto& rhs) {
for (int row = 0; row < rows(); ++row) {
for (int col = 0; col < cols(); ++col) {
(*this)(row, col) /= rhs;
}
}
return *this;
}
/**
* Binary addition operator.
*
* @param lhs Operator left-hand side.
* @param rhs Operator right-hand side.
* @return Result of addition.
*/
template <MatrixLike LHS, MatrixLike RHS>
requires SleipnirMatrixLike<LHS> || SleipnirMatrixLike<RHS>
friend SLEIPNIR_DLLEXPORT VariableMatrix operator+(const LHS& lhs,
const RHS& rhs) {
slp_assert(lhs.rows() == rhs.rows() && lhs.cols() == rhs.cols());
VariableMatrix result(VariableMatrix::empty, lhs.rows(), lhs.cols());
for (int row = 0; row < result.rows(); ++row) {
for (int col = 0; col < result.cols(); ++col) {
result(row, col) = lhs(row, col) + rhs(row, col);
}
}
return result;
}
/**
* Compound addition-assignment operator.
*
* @param rhs Variable to add.
* @return Result of addition.
*/
VariableMatrix& operator+=(const MatrixLike auto& rhs) {
slp_assert(rows() == rhs.rows() && cols() == rhs.cols());
for (int row = 0; row < rows(); ++row) {
for (int col = 0; col < cols(); ++col) {
(*this)(row, col) += rhs(row, col);
}
}
return *this;
}
/**
* Compound addition-assignment operator.
*
* @param rhs Variable to add.
* @return Result of addition.
*/
VariableMatrix& operator+=(const ScalarLike auto& rhs) {
slp_assert(rows() == 1 && cols() == 1);
for (int row = 0; row < rows(); ++row) {
for (int col = 0; col < cols(); ++col) {
(*this)(row, col) += rhs;
}
}
return *this;
}
/**
* Binary subtraction operator.
*
* @param lhs Operator left-hand side.
* @param rhs Operator right-hand side.
* @return Result of subtraction.
*/
template <MatrixLike LHS, MatrixLike RHS>
requires SleipnirMatrixLike<LHS> || SleipnirMatrixLike<RHS>
friend SLEIPNIR_DLLEXPORT VariableMatrix operator-(const LHS& lhs,
const RHS& rhs) {
slp_assert(lhs.rows() == rhs.rows() && lhs.cols() == rhs.cols());
VariableMatrix result(VariableMatrix::empty, lhs.rows(), lhs.cols());
for (int row = 0; row < result.rows(); ++row) {
for (int col = 0; col < result.cols(); ++col) {
result(row, col) = lhs(row, col) - rhs(row, col);
}
}
return result;
}
/**
* Compound subtraction-assignment operator.
*
* @param rhs Variable to subtract.
* @return Result of subtraction.
*/
VariableMatrix& operator-=(const MatrixLike auto& rhs) {
slp_assert(rows() == rhs.rows() && cols() == rhs.cols());
for (int row = 0; row < rows(); ++row) {
for (int col = 0; col < cols(); ++col) {
(*this)(row, col) -= rhs(row, col);
}
}
return *this;
}
/**
* Compound subtraction-assignment operator.
*
* @param rhs Variable to subtract.
* @return Result of subtraction.
*/
VariableMatrix& operator-=(const ScalarLike auto& rhs) {
slp_assert(rows() == 1 && cols() == 1);
for (int row = 0; row < rows(); ++row) {
for (int col = 0; col < cols(); ++col) {
(*this)(row, col) -= rhs;
}
}
return *this;
}
/**
* Unary minus operator.
*
* @param lhs Operand for unary minus.
*/
friend SLEIPNIR_DLLEXPORT VariableMatrix
operator-(const SleipnirMatrixLike auto& lhs) {
VariableMatrix result{VariableMatrix::empty, lhs.rows(), lhs.cols()};
for (int row = 0; row < result.rows(); ++row) {
for (int col = 0; col < result.cols(); ++col) {
result(row, col) = -lhs(row, col);
}
}
return result;
}
/**
* Implicit conversion operator from 1x1 VariableMatrix to Variable.
*/
operator Variable() const { // NOLINT
slp_assert(rows() == 1 && cols() == 1);
return (*this)(0, 0);
}
/**
* Returns the transpose of the variable matrix.
*
* @return The transpose of the variable matrix.
*/
VariableMatrix T() const {
VariableMatrix result{VariableMatrix::empty, cols(), rows()};
for (int row = 0; row < rows(); ++row) {
for (int col = 0; col < cols(); ++col) {
result(col, row) = (*this)(row, col);
}
}
return result;
}
/**
* Returns the number of rows in the matrix.
*
* @return The number of rows in the matrix.
*/
int rows() const { return m_rows; }
/**
* Returns the number of columns in the matrix.
*
* @return The number of columns in the matrix.
*/
int cols() const { return m_cols; }
/**
* Returns an element of the variable matrix.
*
* @param row The row of the element to return.
* @param col The column of the element to return.
* @return An element of the variable matrix.
*/
double value(int row, int col) {
slp_assert(row >= 0 && row < rows());
slp_assert(col >= 0 && col < cols());
return m_storage[row * cols() + col].value();
}
/**
* Returns a row of the variable column vector.
*
* @param index The index of the element to return.
* @return A row of the variable column vector.
*/
double value(int index) {
slp_assert(index >= 0 && index < rows() * cols());
return m_storage[index].value();
}
/**
* Returns the contents of the variable matrix.
*
* @return The contents of the variable matrix.
*/
Eigen::MatrixXd value() {
Eigen::MatrixXd result{rows(), cols()};
for (int row = 0; row < rows(); ++row) {
for (int col = 0; col < cols(); ++col) {
result(row, col) = value(row, col);
}
}
return result;
}
/**
* Transforms the matrix coefficient-wise with an unary operator.
*
* @param unary_op The unary operator to use for the transform operation.
* @return Result of the unary operator.
*/
VariableMatrix cwise_transform(
function_ref<Variable(const Variable& x)> unary_op) const {
VariableMatrix result{VariableMatrix::empty, rows(), cols()};
for (int row = 0; row < rows(); ++row) {
for (int col = 0; col < cols(); ++col) {
result(row, col) = unary_op((*this)(row, col));
}
}
return result;
}
#ifndef DOXYGEN_SHOULD_SKIP_THIS
class iterator {
public:
using iterator_category = std::forward_iterator_tag;
using value_type = Variable;
using difference_type = std::ptrdiff_t;
using pointer = Variable*;
using reference = Variable&;
constexpr iterator() noexcept = default;
explicit constexpr iterator(
gch::small_vector<Variable>::iterator it) noexcept
: m_it{it} {}
constexpr iterator& operator++() noexcept {
++m_it;
return *this;
}
constexpr iterator operator++(int) noexcept {
iterator retval = *this;
++(*this);
return retval;
}
constexpr bool operator==(const iterator&) const noexcept = default;
constexpr reference operator*() const noexcept { return *m_it; }
private:
gch::small_vector<Variable>::iterator m_it;
};
class const_iterator {
public:
using iterator_category = std::forward_iterator_tag;
using value_type = Variable;
using difference_type = std::ptrdiff_t;
using pointer = Variable*;
using const_reference = const Variable&;
constexpr const_iterator() noexcept = default;
explicit constexpr const_iterator(
gch::small_vector<Variable>::const_iterator it) noexcept
: m_it{it} {}
constexpr const_iterator& operator++() noexcept {
++m_it;
return *this;
}
constexpr const_iterator operator++(int) noexcept {
const_iterator retval = *this;
++(*this);
return retval;
}
constexpr bool operator==(const const_iterator&) const noexcept = default;
constexpr const_reference operator*() const noexcept { return *m_it; }
private:
gch::small_vector<Variable>::const_iterator m_it;
};
#endif // DOXYGEN_SHOULD_SKIP_THIS
/**
* Returns begin iterator.
*
* @return Begin iterator.
*/
iterator begin() { return iterator{m_storage.begin()}; }
/**
* Returns end iterator.
*
* @return End iterator.
*/
iterator end() { return iterator{m_storage.end()}; }
/**
* Returns begin iterator.
*
* @return Begin iterator.
*/
const_iterator begin() const { return const_iterator{m_storage.begin()}; }
/**
* Returns end iterator.
*
* @return End iterator.
*/
const_iterator end() const { return const_iterator{m_storage.end()}; }
/**
* Returns begin iterator.
*
* @return Begin iterator.
*/
const_iterator cbegin() const { return const_iterator{m_storage.begin()}; }
/**
* Returns end iterator.
*
* @return End iterator.
*/
const_iterator cend() const { return const_iterator{m_storage.end()}; }
/**
* Returns number of elements in matrix.
*
* @return Number of elements in matrix.
*/
size_t size() const { return m_storage.size(); }
/**
* Returns a variable matrix filled with zeroes.
*
* @param rows The number of matrix rows.
* @param cols The number of matrix columns.
* @return A variable matrix filled with zeroes.
*/
static VariableMatrix zero(int rows, int cols) {
VariableMatrix result{VariableMatrix::empty, rows, cols};
for (auto& elem : result) {
elem = 0.0;
}
return result;
}
/**
* Returns a variable matrix filled with ones.
*
* @param rows The number of matrix rows.
* @param cols The number of matrix columns.
* @return A variable matrix filled with ones.
*/
static VariableMatrix ones(int rows, int cols) {
VariableMatrix result{VariableMatrix::empty, rows, cols};
for (auto& elem : result) {
elem = 1.0;
}
return result;
}
private:
gch::small_vector<Variable> m_storage;
int m_rows = 0;
int m_cols = 0;
};
/**
* Applies a coefficient-wise reduce operation to two matrices.
*
* @param lhs The left-hand side of the binary operator.
* @param rhs The right-hand side of the binary operator.
* @param binary_op The binary operator to use for the reduce operation.
*/
SLEIPNIR_DLLEXPORT inline VariableMatrix cwise_reduce(
const VariableMatrix& lhs, const VariableMatrix& rhs,
function_ref<Variable(const Variable& x, const Variable& y)> binary_op) {
slp_assert(lhs.rows() == rhs.rows() && lhs.cols() == rhs.cols());
VariableMatrix result{VariableMatrix::empty, lhs.rows(), lhs.cols()};
for (int row = 0; row < lhs.rows(); ++row) {
for (int col = 0; col < lhs.cols(); ++col) {
result(row, col) = binary_op(lhs(row, col), rhs(row, col));
}
}
return result;
}
/**
* Assemble a VariableMatrix from a nested list of blocks.
*
* Each row's blocks must have the same height, and the assembled block rows
* must have the same width. For example, for the block matrix [[A, B], [C]] to
* be constructible, the number of rows in A and B must match, and the number of
* columns in [A, B] and [C] must match.
*
* @param list The nested list of blocks.
*/
SLEIPNIR_DLLEXPORT inline VariableMatrix block(
std::initializer_list<std::initializer_list<VariableMatrix>> list) {
// Get row and column counts for destination matrix
int rows = 0;
int cols = -1;
for (const auto& row : list) {
if (row.size() > 0) {
rows += row.begin()->rows();
}
// Get number of columns in this row
int latest_cols = 0;
for (const auto& elem : row) {
// Assert the first and latest row have the same height
slp_assert(row.begin()->rows() == elem.rows());
latest_cols += elem.cols();
}
// If this is the first row, record the column count. Otherwise, assert the
// first and latest column counts are the same.
if (cols == -1) {
cols = latest_cols;
} else {
slp_assert(cols == latest_cols);
}
}
VariableMatrix result{VariableMatrix::empty, rows, cols};
int row_offset = 0;
for (const auto& row : list) {
int col_offset = 0;
for (const auto& elem : row) {
result.block(row_offset, col_offset, elem.rows(), elem.cols()) = elem;
col_offset += elem.cols();
}
row_offset += row.begin()->rows();
}
return result;
}
/**
* Assemble a VariableMatrix from a nested list of blocks.
*
* Each row's blocks must have the same height, and the assembled block rows
* must have the same width. For example, for the block matrix [[A, B], [C]] to
* be constructible, the number of rows in A and B must match, and the number of
* columns in [A, B] and [C] must match.
*
* This overload is for Python bindings only.
*
* @param list The nested list of blocks.
*/
SLEIPNIR_DLLEXPORT inline VariableMatrix block(
const std::vector<std::vector<VariableMatrix>>& list) {
// Get row and column counts for destination matrix
int rows = 0;
int cols = -1;
for (const auto& row : list) {
if (row.size() > 0) {
rows += row.begin()->rows();
}
// Get number of columns in this row
int latest_cols = 0;
for (const auto& elem : row) {
// Assert the first and latest row have the same height
slp_assert(row.begin()->rows() == elem.rows());
latest_cols += elem.cols();
}
// If this is the first row, record the column count. Otherwise, assert the
// first and latest column counts are the same.
if (cols == -1) {
cols = latest_cols;
} else {
slp_assert(cols == latest_cols);
}
}
VariableMatrix result{VariableMatrix::empty, rows, cols};
int row_offset = 0;
for (const auto& row : list) {
int col_offset = 0;
for (const auto& elem : row) {
result.block(row_offset, col_offset, elem.rows(), elem.cols()) = elem;
col_offset += elem.cols();
}
row_offset += row.begin()->rows();
}
return result;
}
/**
* Solves the VariableMatrix equation AX = B for X.
*
* @param A The left-hand side.
* @param B The right-hand side.
* @return The solution X.
*/
SLEIPNIR_DLLEXPORT VariableMatrix solve(const VariableMatrix& A,
const VariableMatrix& B);
} // namespace slp