Sleipnir C++ API
Loading...
Searching...
No Matches
variable_matrix.hpp
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <algorithm>
6#include <array>
7#include <concepts>
8#include <cstddef>
9#include <initializer_list>
10#include <iterator>
11#include <span>
12#include <type_traits>
13#include <utility>
14#include <vector>
15
16#include <Eigen/Core>
17#include <Eigen/LU> // IWYU pragma: keep
18#include <Eigen/QR> // IWYU pragma: keep
19#include <gch/small_vector.hpp>
20
21#include "sleipnir/autodiff/expression_graph.hpp"
22#include "sleipnir/autodiff/sleipnir_base.hpp"
23#include "sleipnir/autodiff/slice.hpp"
24#include "sleipnir/autodiff/variable.hpp"
25#include "sleipnir/autodiff/variable_block.hpp"
26#include "sleipnir/util/assert.hpp"
27#include "sleipnir/util/concepts.hpp"
28#include "sleipnir/util/empty.hpp"
29#include "sleipnir/util/function_ref.hpp"
30#include "sleipnir/util/symbol_exports.hpp"
31
32namespace slp {
33
37template <typename Scalar_>
39 public:
41 using Scalar = Scalar_;
42
44 VariableMatrix() = default;
45
50 explicit VariableMatrix(int rows) : VariableMatrix{rows, 1} {}
51
56 VariableMatrix(int rows, int cols) : m_rows{rows}, m_cols{cols} {
57 m_storage.reserve(rows * cols);
58 for (int index = 0; index < rows * cols; ++index) {
59 m_storage.emplace_back();
60 }
61 }
62
68 : m_rows{rows}, m_cols{cols} {
69 m_storage.reserve(rows * cols);
70 for (int index = 0; index < rows * cols; ++index) {
71 m_storage.emplace_back(nullptr);
72 }
73 }
74
79 std::initializer_list<std::initializer_list<Variable<Scalar>>> list) {
80 // Get row and column counts for destination matrix
81 m_rows = list.size();
82 m_cols = 0;
83 if (list.size() > 0) {
84 m_cols = list.begin()->size();
85 }
86
87 // Assert all column counts are the same
88 for ([[maybe_unused]]
89 const auto& row : list) {
90 slp_assert(static_cast<int>(row.size()) == m_cols);
91 }
92
93 m_storage.reserve(rows() * cols());
94 for (const auto& row : list) {
95 std::ranges::copy(row, std::back_inserter(m_storage));
96 }
97 }
98
104 // NOLINTNEXTLINE (google-explicit-constructor)
105 VariableMatrix(const std::vector<std::vector<Scalar>>& list) {
106 // Get row and column counts for destination matrix
107 m_rows = list.size();
108 m_cols = 0;
109 if (list.size() > 0) {
110 m_cols = list.begin()->size();
111 }
112
113 // Assert all column counts are the same
114 for ([[maybe_unused]]
115 const auto& row : list) {
116 slp_assert(static_cast<int>(row.size()) == m_cols);
117 }
118
119 m_storage.reserve(rows() * cols());
120 for (const auto& row : list) {
121 std::ranges::copy(row, std::back_inserter(m_storage));
122 }
123 }
124
130 // NOLINTNEXTLINE (google-explicit-constructor)
131 VariableMatrix(const std::vector<std::vector<Variable<Scalar>>>& list) {
132 // Get row and column counts for destination matrix
133 m_rows = list.size();
134 m_cols = 0;
135 if (list.size() > 0) {
136 m_cols = list.begin()->size();
137 }
138
139 // Assert all column counts are the same
140 for ([[maybe_unused]]
141 const auto& row : list) {
142 slp_assert(static_cast<int>(row.size()) == m_cols);
143 }
144
145 m_storage.reserve(rows() * cols());
146 for (const auto& row : list) {
147 std::ranges::copy(row, std::back_inserter(m_storage));
148 }
149 }
150
154 template <typename Derived>
155 // NOLINTNEXTLINE (google-explicit-constructor)
156 VariableMatrix(const Eigen::MatrixBase<Derived>& values)
157 : m_rows{static_cast<int>(values.rows())},
158 m_cols{static_cast<int>(values.cols())} {
159 m_storage.reserve(values.rows() * values.cols());
160 for (int row = 0; row < values.rows(); ++row) {
161 for (int col = 0; col < values.cols(); ++col) {
162 m_storage.emplace_back(values[row, col]);
163 }
164 }
165 }
166
170 template <typename Derived>
171 // NOLINTNEXTLINE (google-explicit-constructor)
172 VariableMatrix(const Eigen::DiagonalBase<Derived>& values)
173 : m_rows{static_cast<int>(values.rows())},
174 m_cols{static_cast<int>(values.cols())} {
175 m_storage.reserve(values.rows() * values.cols());
176 for (int row = 0; row < values.rows(); ++row) {
177 for (int col = 0; col < values.cols(); ++col) {
178 if (row == col) {
179 m_storage.emplace_back(values.diagonal()[row]);
180 } else {
181 m_storage.emplace_back(Scalar(0));
182 }
183 }
184 }
185 }
186
190 // NOLINTNEXTLINE (google-explicit-constructor)
191 VariableMatrix(const Variable<Scalar>& variable) : m_rows{1}, m_cols{1} {
192 m_storage.emplace_back(variable);
193 }
194
198 // NOLINTNEXTLINE (google-explicit-constructor)
199 VariableMatrix(Variable<Scalar>&& variable) : m_rows{1}, m_cols{1} {
200 m_storage.emplace_back(std::move(variable));
201 }
202
206 // NOLINTNEXTLINE (google-explicit-constructor)
208 : m_rows{values.rows()}, m_cols{values.cols()} {
209 m_storage.reserve(rows() * cols());
210 for (int row = 0; row < rows(); ++row) {
211 for (int col = 0; col < cols(); ++col) {
212 m_storage.emplace_back(values[row, col]);
213 }
214 }
215 }
216
220 // NOLINTNEXTLINE (google-explicit-constructor)
222 : m_rows{values.rows()}, m_cols{values.cols()} {
223 m_storage.reserve(rows() * cols());
224 for (int row = 0; row < rows(); ++row) {
225 for (int col = 0; col < cols(); ++col) {
226 m_storage.emplace_back(values[row, col]);
227 }
228 }
229 }
230
234 explicit VariableMatrix(std::span<const Variable<Scalar>> values)
235 : m_rows{static_cast<int>(values.size())}, m_cols{1} {
236 m_storage.reserve(rows() * cols());
237 for (int row = 0; row < rows(); ++row) {
238 for (int col = 0; col < cols(); ++col) {
239 m_storage.emplace_back(values[row * cols() + col]);
240 }
241 }
242 }
243
249 VariableMatrix(std::span<const Variable<Scalar>> values, int rows, int cols)
250 : m_rows{rows}, m_cols{cols} {
251 slp_assert(static_cast<int>(values.size()) == rows * cols);
252 m_storage.reserve(rows * cols);
253 for (int row = 0; row < rows; ++row) {
254 for (int col = 0; col < cols; ++col) {
255 m_storage.emplace_back(values[row * cols + col]);
256 }
257 }
258 }
259
264 template <typename Derived>
265 VariableMatrix& operator=(const Eigen::MatrixBase<Derived>& values) {
266 slp_assert(rows() == values.rows() && cols() == values.cols());
267
268 for (int row = 0; row < values.rows(); ++row) {
269 for (int col = 0; col < values.cols(); ++col) {
270 (*this)[row, col] = values[row, col];
271 }
272 }
273
274 return *this;
275 }
276
284 slp_assert(rows() == 1 && cols() == 1);
285
286 (*this)[0, 0] = value;
287
288 return *this;
289 }
290
294 template <typename Derived>
295 requires std::same_as<typename Derived::Scalar, Scalar>
296 void set_value(const Eigen::MatrixBase<Derived>& values) {
297 slp_assert(rows() == values.rows() && cols() == values.cols());
298
299 for (int row = 0; row < values.rows(); ++row) {
300 for (int col = 0; col < values.cols(); ++col) {
301 (*this)[row, col].set_value(values[row, col]);
302 }
303 }
304 }
305
312 slp_assert(row >= 0 && row < rows());
313 slp_assert(col >= 0 && col < cols());
314 return m_storage[row * cols() + col];
315 }
316
322 const Variable<Scalar>& operator[](int row, int col) const {
323 slp_assert(row >= 0 && row < rows());
324 slp_assert(col >= 0 && col < cols());
325 return m_storage[row * cols() + col];
326 }
327
333 slp_assert(index >= 0 && index < rows() * cols());
334 return m_storage[index];
335 }
336
341 const Variable<Scalar>& operator[](int index) const {
342 slp_assert(index >= 0 && index < rows() * cols());
343 return m_storage[index];
344 }
345
354 int block_rows, int block_cols) {
355 slp_assert(row_offset >= 0 && row_offset <= rows());
356 slp_assert(col_offset >= 0 && col_offset <= cols());
357 slp_assert(block_rows >= 0 && block_rows <= rows() - row_offset);
358 slp_assert(block_cols >= 0 && block_cols <= cols() - col_offset);
360 }
361
370 int col_offset,
371 int block_rows,
372 int block_cols) const {
373 slp_assert(row_offset >= 0 && row_offset <= rows());
374 slp_assert(col_offset >= 0 && col_offset <= cols());
375 slp_assert(block_rows >= 0 && block_rows <= rows() - row_offset);
376 slp_assert(block_cols >= 0 && block_cols <= cols() - col_offset);
378 }
379
391
398 Slice col_slice) const {
399 int row_slice_length = row_slice.adjust(rows());
400 int col_slice_length = col_slice.adjust(cols());
401 return VariableBlock{*this, std::move(row_slice), row_slice_length,
402 std::move(col_slice), col_slice_length};
403 }
404
422
439
446 slp_assert(cols() == 1);
447 slp_assert(offset >= 0 && offset < rows());
448 slp_assert(length >= 0 && length <= rows() - offset);
449 return block(offset, 0, length, 1);
450 }
451
458 int length) const {
459 slp_assert(cols() == 1);
460 slp_assert(offset >= 0 && offset < rows());
461 slp_assert(length >= 0 && length <= rows() - offset);
462 return block(offset, 0, length, 1);
463 }
464
470 slp_assert(row >= 0 && row < rows());
471 return block(row, 0, 1, cols());
472 }
473
479 slp_assert(row >= 0 && row < rows());
480 return block(row, 0, 1, cols());
481 }
482
488 slp_assert(col >= 0 && col < cols());
489 return block(0, col, rows(), 1);
490 }
491
497 slp_assert(col >= 0 && col < cols());
498 return block(0, col, rows(), 1);
499 }
500
505 template <EigenMatrixLike LHS, SleipnirMatrixLike<Scalar> RHS>
507 slp_assert(lhs.cols() == rhs.rows());
508
509 VariableMatrix<Scalar> result(detail::empty, lhs.rows(), rhs.cols());
510
511 for (int i = 0; i < lhs.rows(); ++i) {
512 for (int j = 0; j < rhs.cols(); ++j) {
513 Variable sum{Scalar(0)};
514 for (int k = 0; k < lhs.cols(); ++k) {
515 sum += lhs[i, k] * rhs[k, j];
516 }
517 result[i, j] = sum;
518 }
519 }
520
521 return result;
522 }
523
528 template <SleipnirMatrixLike<Scalar> LHS, EigenMatrixLike RHS>
530 slp_assert(lhs.cols() == rhs.rows());
531
532 VariableMatrix<Scalar> result(detail::empty, lhs.rows(), rhs.cols());
533
534 for (int i = 0; i < lhs.rows(); ++i) {
535 for (int j = 0; j < rhs.cols(); ++j) {
536 Variable sum{Scalar(0)};
537 for (int k = 0; k < lhs.cols(); ++k) {
538 sum += lhs[i, k] * rhs[k, j];
539 }
540 result[i, j] = sum;
541 }
542 }
543
544 return result;
545 }
546
551 template <SleipnirMatrixLike<Scalar> LHS, SleipnirMatrixLike<Scalar> RHS>
553 slp_assert(lhs.cols() == rhs.rows());
554
555 VariableMatrix<Scalar> result(detail::empty, lhs.rows(), rhs.cols());
556
557 for (int i = 0; i < lhs.rows(); ++i) {
558 for (int j = 0; j < rhs.cols(); ++j) {
559 Variable sum{Scalar(0)};
560 for (int k = 0; k < lhs.cols(); ++k) {
561 sum += lhs[i, k] * rhs[k, j];
562 }
563 result[i, j] = sum;
564 }
565 }
566
567 return result;
568 }
569
574 template <SleipnirMatrixLike<Scalar> LHS, ScalarLike RHS>
576 VariableMatrix<Scalar> result(detail::empty, lhs.rows(), lhs.cols());
577
578 for (int row = 0; row < result.rows(); ++row) {
579 for (int col = 0; col < result.cols(); ++col) {
580 result[row, col] = lhs[row, col] * rhs;
581 }
582 }
583
584 return result;
585 }
586
591 template <ScalarLike LHS, SleipnirMatrixLike<Scalar> RHS>
593 VariableMatrix<Scalar> result(detail::empty, rhs.rows(), rhs.cols());
594
595 for (int row = 0; row < result.rows(); ++row) {
596 for (int col = 0; col < result.cols(); ++col) {
597 result[row, col] = rhs[row, col] * lhs;
598 }
599 }
600
601 return result;
602 }
603
609 slp_assert(cols() == rhs.rows() && cols() == rhs.cols());
610
611 for (int i = 0; i < rows(); ++i) {
613 for (int j = 0; j < rhs.cols(); ++j) {
614 Variable sum{Scalar(0)};
615 for (int k = 0; k < cols(); ++k) {
616 sum += lhs_old_row[k] * rhs[k, j];
617 }
618 (*this)[i, j] = sum;
619 }
620 }
621
622 return *this;
623 }
624
630 for (int row = 0; row < rows(); ++row) {
631 for (int col = 0; col < cols(); ++col) {
632 (*this)[row, col] *= rhs;
633 }
634 }
635
636 return *this;
637 }
638
644 template <SleipnirMatrixLike<Scalar> LHS, ScalarLike RHS>
646 friend VariableMatrix<Scalar> operator/(const LHS& lhs, const RHS& rhs) {
647 VariableMatrix<Scalar> result(detail::empty, lhs.rows(), lhs.cols());
648
649 for (int row = 0; row < result.rows(); ++row) {
650 for (int col = 0; col < result.cols(); ++col) {
651 result[row, col] = lhs[row, col] / rhs;
652 }
653 }
654
655 return result;
656 }
657
663 template <SleipnirMatrixLike<Scalar> LHS>
665 const Variable<Scalar>& rhs) {
666 VariableMatrix<Scalar> result(detail::empty, lhs.rows(), lhs.cols());
667
668 for (int row = 0; row < result.rows(); ++row) {
669 for (int col = 0; col < result.cols(); ++col) {
670 result[row, col] = lhs[row, col] / rhs;
671 }
672 }
673
674 return result;
675 }
676
682 for (int row = 0; row < rows(); ++row) {
683 for (int col = 0; col < cols(); ++col) {
684 (*this)[row, col] /= rhs;
685 }
686 }
687
688 return *this;
689 }
690
696 template <EigenMatrixLike LHS, SleipnirMatrixLike<Scalar> RHS>
698 slp_assert(lhs.rows() == rhs.rows() && lhs.cols() == rhs.cols());
699
700 VariableMatrix<Scalar> result(detail::empty, lhs.rows(), lhs.cols());
701
702 for (int row = 0; row < result.rows(); ++row) {
703 for (int col = 0; col < result.cols(); ++col) {
704 result[row, col] = lhs[row, col] + rhs[row, col];
705 }
706 }
707
708 return result;
709 }
710
716 template <SleipnirMatrixLike<Scalar> LHS, EigenMatrixLike RHS>
718 slp_assert(lhs.rows() == rhs.rows() && lhs.cols() == rhs.cols());
719
720 VariableMatrix<Scalar> result(detail::empty, lhs.rows(), lhs.cols());
721
722 for (int row = 0; row < result.rows(); ++row) {
723 for (int col = 0; col < result.cols(); ++col) {
724 result[row, col] = lhs[row, col] + rhs[row, col];
725 }
726 }
727
728 return result;
729 }
730
736 template <SleipnirMatrixLike<Scalar> LHS, SleipnirMatrixLike<Scalar> RHS>
738 slp_assert(lhs.rows() == rhs.rows() && lhs.cols() == rhs.cols());
739
740 VariableMatrix<Scalar> result(detail::empty, lhs.rows(), lhs.cols());
741
742 for (int row = 0; row < result.rows(); ++row) {
743 for (int col = 0; col < result.cols(); ++col) {
744 result[row, col] = lhs[row, col] + rhs[row, col];
745 }
746 }
747
748 return result;
749 }
750
756 slp_assert(rows() == rhs.rows() && cols() == rhs.cols());
757
758 for (int row = 0; row < rows(); ++row) {
759 for (int col = 0; col < cols(); ++col) {
760 (*this)[row, col] += rhs[row, col];
761 }
762 }
763
764 return *this;
765 }
766
772 slp_assert(rows() == 1 && cols() == 1);
773
774 for (int row = 0; row < rows(); ++row) {
775 for (int col = 0; col < cols(); ++col) {
776 (*this)[row, col] += rhs;
777 }
778 }
779
780 return *this;
781 }
782
788 template <EigenMatrixLike LHS, SleipnirMatrixLike<Scalar> RHS>
790 slp_assert(lhs.rows() == rhs.rows() && lhs.cols() == rhs.cols());
791
792 VariableMatrix<Scalar> result(detail::empty, lhs.rows(), lhs.cols());
793
794 for (int row = 0; row < result.rows(); ++row) {
795 for (int col = 0; col < result.cols(); ++col) {
796 result[row, col] = lhs[row, col] - rhs[row, col];
797 }
798 }
799
800 return result;
801 }
802
808 template <SleipnirMatrixLike<Scalar> LHS, EigenMatrixLike RHS>
810 slp_assert(lhs.rows() == rhs.rows() && lhs.cols() == rhs.cols());
811
812 VariableMatrix<Scalar> result(detail::empty, lhs.rows(), lhs.cols());
813
814 for (int row = 0; row < result.rows(); ++row) {
815 for (int col = 0; col < result.cols(); ++col) {
816 result[row, col] = lhs[row, col] - rhs[row, col];
817 }
818 }
819
820 return result;
821 }
822
828 template <SleipnirMatrixLike<Scalar> LHS, SleipnirMatrixLike<Scalar> RHS>
830 slp_assert(lhs.rows() == rhs.rows() && lhs.cols() == rhs.cols());
831
832 VariableMatrix<Scalar> result(detail::empty, lhs.rows(), lhs.cols());
833
834 for (int row = 0; row < result.rows(); ++row) {
835 for (int col = 0; col < result.cols(); ++col) {
836 result[row, col] = lhs[row, col] - rhs[row, col];
837 }
838 }
839
840 return result;
841 }
842
848 slp_assert(rows() == rhs.rows() && cols() == rhs.cols());
849
850 for (int row = 0; row < rows(); ++row) {
851 for (int col = 0; col < cols(); ++col) {
852 (*this)[row, col] -= rhs[row, col];
853 }
854 }
855
856 return *this;
857 }
858
864 slp_assert(rows() == 1 && cols() == 1);
865
866 for (int row = 0; row < rows(); ++row) {
867 for (int col = 0; col < cols(); ++col) {
868 (*this)[row, col] -= rhs;
869 }
870 }
871
872 return *this;
873 }
874
879 const SleipnirMatrixLike<Scalar> auto& lhs) {
880 VariableMatrix<Scalar> result{detail::empty, lhs.rows(), lhs.cols()};
881
882 for (int row = 0; row < result.rows(); ++row) {
883 for (int col = 0; col < result.cols(); ++col) {
884 result[row, col] = -lhs[row, col];
885 }
886 }
887
888 return result;
889 }
890
892 // NOLINTNEXTLINE (google-explicit-constructor)
893 operator Variable<Scalar>() const {
894 slp_assert(rows() == 1 && cols() == 1);
895 return (*this)[0, 0];
896 }
897
902 VariableMatrix<Scalar> result{detail::empty, cols(), rows()};
903
904 for (int row = 0; row < rows(); ++row) {
905 for (int col = 0; col < cols(); ++col) {
906 result[col, row] = (*this)[row, col];
907 }
908 }
909
910 return result;
911 }
912
916 int rows() const { return m_rows; }
917
921 int cols() const { return m_cols; }
922
928 Scalar value(int row, int col) { return (*this)[row, col].value(); }
929
934 Scalar value(int index) { return (*this)[index].value(); }
935
939 Eigen::MatrixX<Scalar> value() {
940 Eigen::MatrixX<Scalar> result{rows(), cols()};
941
942 for (int row = 0; row < rows(); ++row) {
943 for (int col = 0; col < cols(); ++col) {
944 result[row, col] = value(row, col);
945 }
946 }
947
948 return result;
949 }
950
954 Eigen::MatrixX<Variable<Scalar>> to_eigen() const {
955 Eigen::MatrixX<Variable<Scalar>> result{rows(), cols()};
956
957 for (int row = 0; row < rows(); ++row) {
958 for (int col = 0; col < cols(); ++col) {
959 result[row, col] = (*this)[row, col];
960 }
961 }
962
963 return result;
964 }
965
972 const {
973 VariableMatrix<Scalar> result{detail::empty, rows(), cols()};
974
975 for (int row = 0; row < rows(); ++row) {
976 for (int col = 0; col < cols(); ++col) {
977 result[row, col] = unary_op((*this)[row, col]);
978 }
979 }
980
981 return result;
982 }
983
988 slp_assert(rows() == cols());
989
990 // Coefficients for (13, 13) Padé approximant of exp(A) are from the
991 // following program:
992 //
993 // ```python
994 // #!/usr/bin/env python
995 //
996 // import mpmath as mp
997 //
998 // # https://en.wikipedia.org/wiki/IEEE_754#Basic_and_interchange_formats
999 // mp.mp.prec = 113 # quad precision
1000 //
1001 // L = 13
1002 // M = 13
1003 // p, q = mp.pade(mp.taylor(mp.exp, 0, L + M), L, M)
1004 //
1005 // print("constexpr std::array p{")
1006 // for k, p_k in enumerate(p):
1007 // print(f"Scalar({p_k}L){',' if k < len(p) - 1 else '};'}")
1008 // print("constexpr std::array q{")
1009 // for k, q_k in enumerate(q):
1010 // print(f"Scalar({q_k}L){',' if k < len(q) - 1 else '};'}")
1011 // ```
1012 constexpr size_t NUM_COEFFS = 14;
1013 constexpr std::array p{Scalar(1.0L),
1014 Scalar(0.499999999999999999999987615564159L),
1015 Scalar(0.119999999999999999999993719796468L),
1016 Scalar(0.0183333333333333333333318078905823L),
1017 Scalar(0.00199275362318840579710121408775205L),
1018 Scalar(0.00016304347826086956521736560868541L),
1019 Scalar(1.03519668737060041407846479512981e-05L),
1020 Scalar(5.17598343685300207039205144279119e-07L),
1021 Scalar(2.04315135665250081725989398668247e-08L),
1022 Scalar(6.30602270571759511499920806570486e-10L),
1023 Scalar(1.48377004840414002705850442238441e-11L),
1024 Scalar(2.52915349159796595521307855480881e-13L),
1025 Scalar(2.81017054621996217245857568431558e-15L),
1026 Scalar(1.54404975067030888596595580141692e-17L)};
1027 constexpr std::array q{Scalar(1.0L),
1028 Scalar(-0.500000000000000000000012384435841L),
1029 Scalar(0.120000000000000000000006104232309L),
1030 Scalar(-0.0183333333333333333333347707904729L),
1031 Scalar(0.00199275362318840579710166350137732L),
1032 Scalar(-0.000163043478260869565217413851346892L),
1033 Scalar(1.03519668737060041407885187519331e-05L),
1034 Scalar(-5.17598343685300207039443859549262e-07L),
1035 Scalar(2.04315135665250081726103781506842e-08L),
1036 Scalar(-6.30602270571759511500345035969254e-10L),
1037 Scalar(1.48377004840414002705969744674248e-11L),
1038 Scalar(-2.52915349159796595521550648156349e-13L),
1039 Scalar(2.81017054621996217246180850207342e-15L),
1040 Scalar(-1.54404975067030888596810607146175e-17L)};
1041 static_assert(p.size() == NUM_COEFFS);
1042 static_assert(q.size() == NUM_COEFFS);
1043
1044 // 13
1045 // P = Σ pₖAᵏ
1046 // k=0
1047 //
1048 // 13
1049 // Q = Σ qₖAᵏ
1050 // k=0
1052 Eigen::VectorX<Scalar>::Constant(rows(), p[0]).asDiagonal()};
1054 Eigen::VectorX<Scalar>::Constant(rows(), q[0]).asDiagonal()};
1055 auto A_pow = *this;
1056 size_t k = 1;
1057 while (true) {
1058 P += p[k] * A_pow;
1059 Q += q[k] * A_pow;
1060
1061 ++k;
1062 if (k < NUM_COEFFS) {
1063 A_pow *= *this;
1064 } else {
1065 break;
1066 }
1067 }
1068
1069 // https://mpmath.org/doc/current/calculus/approximation.html#mpmath.pade
1070 // defines the Padé approximant as exp(A)Q ≈ P, so:
1071 //
1072 // exp(A) ≈ P / Q
1073 // exp(A) ≈ (Qᵀ \ Pᵀ)ᵀ
1075 Q.T().to_eigen().lu().solve(P.T().to_eigen()).transpose()};
1076 }
1077
1078#ifndef DOXYGEN_SHOULD_SKIP_THIS
1079
1080 class iterator {
1081 public:
1082 using iterator_category = std::bidirectional_iterator_tag;
1083 using value_type = Variable<Scalar>;
1084 using difference_type = std::ptrdiff_t;
1085 using pointer = Variable<Scalar>*;
1086 using reference = Variable<Scalar>&;
1087
1088 constexpr iterator() noexcept = default;
1089
1090 explicit constexpr iterator(
1091 gch::small_vector<Variable<Scalar>>::iterator it) noexcept
1092 : m_it{it} {}
1093
1094 constexpr iterator& operator++() noexcept {
1095 ++m_it;
1096 return *this;
1097 }
1098
1099 constexpr iterator operator++(int) noexcept {
1100 iterator retval = *this;
1101 ++(*this);
1102 return retval;
1103 }
1104
1105 constexpr iterator& operator--() noexcept {
1106 --m_it;
1107 return *this;
1108 }
1109
1110 constexpr iterator operator--(int) noexcept {
1111 iterator retval = *this;
1112 --(*this);
1113 return retval;
1114 }
1115
1116 constexpr bool operator==(const iterator&) const noexcept = default;
1117
1118 constexpr reference operator*() const noexcept { return *m_it; }
1119
1120 private:
1121 gch::small_vector<Variable<Scalar>>::iterator m_it;
1122 };
1123
1124 class const_iterator {
1125 public:
1126 using iterator_category = std::bidirectional_iterator_tag;
1127 using value_type = Variable<Scalar>;
1128 using difference_type = std::ptrdiff_t;
1129 using pointer = Variable<Scalar>*;
1130 using const_reference = const Variable<Scalar>&;
1131
1132 constexpr const_iterator() noexcept = default;
1133
1134 explicit constexpr const_iterator(
1135 gch::small_vector<Variable<Scalar>>::const_iterator it) noexcept
1136 : m_it{it} {}
1137
1138 constexpr const_iterator& operator++() noexcept {
1139 ++m_it;
1140 return *this;
1141 }
1142
1143 constexpr const_iterator operator++(int) noexcept {
1144 const_iterator retval = *this;
1145 ++(*this);
1146 return retval;
1147 }
1148
1149 constexpr const_iterator& operator--() noexcept {
1150 --m_it;
1151 return *this;
1152 }
1153
1154 constexpr const_iterator operator--(int) noexcept {
1155 const_iterator retval = *this;
1156 --(*this);
1157 return retval;
1158 }
1159
1160 constexpr bool operator==(const const_iterator&) const noexcept = default;
1161
1162 constexpr const_reference operator*() const noexcept { return *m_it; }
1163
1164 private:
1165 gch::small_vector<Variable<Scalar>>::const_iterator m_it;
1166 };
1167
1168 using reverse_iterator = std::reverse_iterator<iterator>;
1169 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
1170
1171#endif // DOXYGEN_SHOULD_SKIP_THIS
1172
1176 iterator begin() { return iterator{m_storage.begin()}; }
1177
1181 iterator end() { return iterator{m_storage.end()}; }
1182
1186 const_iterator begin() const { return const_iterator{m_storage.begin()}; }
1187
1191 const_iterator end() const { return const_iterator{m_storage.end()}; }
1192
1196 const_iterator cbegin() const { return const_iterator{m_storage.cbegin()}; }
1197
1201 const_iterator cend() const { return const_iterator{m_storage.cend()}; }
1202
1206 reverse_iterator rbegin() { return reverse_iterator{end()}; }
1207
1211 reverse_iterator rend() { return reverse_iterator{begin()}; }
1212
1216 const_reverse_iterator rbegin() const {
1217 return const_reverse_iterator{end()};
1218 }
1219
1223 const_reverse_iterator rend() const {
1224 return const_reverse_iterator{begin()};
1225 }
1226
1230 const_reverse_iterator crbegin() const {
1231 return const_reverse_iterator{cend()};
1232 }
1233
1237 const_reverse_iterator crend() const {
1238 return const_reverse_iterator{cbegin()};
1239 }
1240
1244 size_t size() const { return m_storage.size(); }
1245
1251 VariableMatrix<Scalar> result{detail::empty, rows, rows};
1252
1253 for (int row = 0; row < rows; ++row) {
1254 for (int col = 0; col < rows; ++col) {
1255 result[row, col] = row == col ? Scalar(1) : Scalar(0);
1256 }
1257 }
1258
1259 return result;
1260 }
1261
1268 VariableMatrix<Scalar> result{detail::empty, rows, cols};
1269
1270 for (auto& elem : result) {
1271 elem = Scalar(0);
1272 }
1273
1274 return result;
1275 }
1276
1283 VariableMatrix<Scalar> result{detail::empty, rows, cols};
1284
1285 for (auto& elem : result) {
1286 elem = Scalar(1);
1287 }
1288
1289 return result;
1290 }
1291
1299 VariableMatrix<Scalar> result{detail::empty, rows, cols};
1300
1301 for (auto& elem : result) {
1302 elem = constant;
1303 }
1304
1305 return result;
1306 }
1307
1308 private:
1309 gch::small_vector<Variable<Scalar>> m_storage;
1310 int m_rows = 0;
1311 int m_cols = 0;
1312};
1313
1314template <typename Derived>
1315VariableMatrix(const Eigen::MatrixBase<Derived>&)
1316 -> VariableMatrix<typename Derived::Scalar>;
1317
1318template <typename Derived>
1319VariableMatrix(const Eigen::DiagonalBase<Derived>&)
1320 -> VariableMatrix<typename Derived::Scalar>;
1321
1327template <typename Scalar, EigenMatrixLike LHS>
1328 requires std::same_as<typename std::decay_t<LHS>::Scalar, Scalar>
1329VariableMatrix<Scalar> operator*(const LHS& lhs, const Variable<Scalar>& rhs) {
1330 VariableMatrix<Scalar> result(detail::empty, lhs.rows(), lhs.cols());
1331
1332 for (int row = 0; row < result.rows(); ++row) {
1333 for (int col = 0; col < result.cols(); ++col) {
1334 result[row, col] = lhs[row, col] * rhs;
1335 }
1336 }
1337
1338 return result;
1339}
1340
1346template <typename Scalar, EigenMatrixLike RHS>
1347 requires std::same_as<typename std::decay_t<RHS>::Scalar, Scalar>
1348VariableMatrix<Scalar> operator*(const Variable<Scalar>& lhs, const RHS& rhs) {
1349 VariableMatrix<Scalar> result(detail::empty, rhs.rows(), rhs.cols());
1350
1351 for (int row = 0; row < result.rows(); ++row) {
1352 for (int col = 0; col < result.cols(); ++col) {
1353 result[row, col] = rhs[row, col] * lhs;
1354 }
1355 }
1356
1357 return result;
1358}
1359
1366template <typename Scalar, EigenMatrixLike LHS>
1367 requires std::same_as<typename std::decay_t<LHS>::Scalar, Scalar>
1368VariableMatrix<Scalar> operator/(const LHS& lhs, const Variable<Scalar>& rhs) {
1369 VariableMatrix<Scalar> result(detail::empty, lhs.rows(), lhs.cols());
1370
1371 for (int row = 0; row < result.rows(); ++row) {
1372 for (int col = 0; col < result.cols(); ++col) {
1373 result[row, col] = lhs[row, col] / rhs;
1374 }
1375 }
1376
1377 return result;
1378}
1379
1386template <typename Scalar>
1387VariableMatrix<Scalar> cwise_reduce(
1388 const VariableMatrix<Scalar>& lhs, const VariableMatrix<Scalar>& rhs,
1389 function_ref<Variable<Scalar>(const Variable<Scalar>& x,
1390 const Variable<Scalar>& y)>
1391 binary_op) {
1392 slp_assert(lhs.rows() == rhs.rows() && lhs.cols() == rhs.cols());
1393
1394 VariableMatrix<Scalar> result{detail::empty, lhs.rows(), lhs.cols()};
1395
1396 for (int row = 0; row < lhs.rows(); ++row) {
1397 for (int col = 0; col < lhs.cols(); ++col) {
1398 result[row, col] = binary_op(lhs[row, col], rhs[row, col]);
1399 }
1400 }
1401
1402 return result;
1403}
1404
1414template <typename Scalar>
1415VariableMatrix<Scalar> block(
1416 std::initializer_list<std::initializer_list<VariableMatrix<Scalar>>> list) {
1417 // Get row and column counts for destination matrix
1418 int rows = 0;
1419 int cols = -1;
1420 for (const auto& row : list) {
1421 if (row.size() > 0) {
1422 rows += row.begin()->rows();
1423 }
1424
1425 // Get number of columns in this row
1426 int latest_cols = 0;
1427 for (const auto& elem : row) {
1428 // Assert the first and latest row have the same height
1429 slp_assert(row.begin()->rows() == elem.rows());
1430
1431 latest_cols += elem.cols();
1432 }
1433
1434 // If this is the first row, record the column count. Otherwise, assert the
1435 // first and latest column counts are the same.
1436 if (cols == -1) {
1437 cols = latest_cols;
1438 } else {
1439 slp_assert(cols == latest_cols);
1440 }
1441 }
1442
1443 VariableMatrix<Scalar> result{detail::empty, rows, cols};
1444
1445 int row_offset = 0;
1446 for (const auto& row : list) {
1447 int col_offset = 0;
1448 for (const auto& elem : row) {
1449 result.block(row_offset, col_offset, elem.rows(), elem.cols()) = elem;
1450 col_offset += elem.cols();
1451 }
1452 if (row.size() > 0) {
1453 row_offset += row.begin()->rows();
1454 }
1455 }
1456
1457 return result;
1458}
1459
1471template <typename Scalar>
1472VariableMatrix<Scalar> block(
1473 const std::vector<std::vector<VariableMatrix<Scalar>>>& list) {
1474 // Get row and column counts for destination matrix
1475 int rows = 0;
1476 int cols = -1;
1477 for (const auto& row : list) {
1478 if (row.size() > 0) {
1479 rows += row.begin()->rows();
1480 }
1481
1482 // Get number of columns in this row
1483 int latest_cols = 0;
1484 for (const auto& elem : row) {
1485 // Assert the first and latest row have the same height
1486 slp_assert(row.begin()->rows() == elem.rows());
1487
1488 latest_cols += elem.cols();
1489 }
1490
1491 // If this is the first row, record the column count. Otherwise, assert the
1492 // first and latest column counts are the same.
1493 if (cols == -1) {
1494 cols = latest_cols;
1495 } else {
1496 slp_assert(cols == latest_cols);
1497 }
1498 }
1499
1500 VariableMatrix<Scalar> result{detail::empty, rows, cols};
1501
1502 int row_offset = 0;
1503 for (const auto& row : list) {
1504 int col_offset = 0;
1505 for (const auto& elem : row) {
1506 result.block(row_offset, col_offset, elem.rows(), elem.cols()) = elem;
1507 col_offset += elem.cols();
1508 }
1509 if (row.size() > 0) {
1510 row_offset += row.begin()->rows();
1511 }
1512 }
1513
1514 return result;
1515}
1516
1523template <typename Scalar>
1524VariableMatrix<Scalar> solve(const VariableMatrix<Scalar>& A,
1525 const VariableMatrix<Scalar>& B) {
1526 // m x n * n x p = m x p
1527 slp_assert(A.rows() == B.rows());
1528
1529 if (A.rows() == 1 && A.cols() == 1) {
1530 // Compute optimal inverse instead of using Eigen's general solver
1531 return B[0, 0] / A[0, 0];
1532 } else if (A.rows() == 2 && A.cols() == 2) {
1533 // Compute optimal inverse instead of using Eigen's general solver
1534 //
1535 // [a b]⁻¹ ___1___ [ d −b]
1536 // [c d] = ad − bc [−c a]
1537
1538 const auto& a = A[0, 0];
1539 const auto& b = A[0, 1];
1540 const auto& c = A[1, 0];
1541 const auto& d = A[1, 1];
1542
1543 VariableMatrix adj_A{{d, -b}, {-c, a}};
1544 auto det_A = a * d - b * c;
1545 return adj_A / det_A * B;
1546 } else if (A.rows() == 3 && A.cols() == 3) {
1547 // Compute optimal inverse instead of using Eigen's general solver
1548 //
1549 // [a b c]⁻¹
1550 // [d e f]
1551 // [g h i]
1552 // 1 [ei − fh ch − bi bf − ce]
1553 // = ------------------------------------ [fg − di ai − cg cd − af]
1554 // a(ei − fh) + b(fg − di) + c(dh − eg) [dh − eg bg − ah ae − bd]
1555 //
1556 // https://www.wolframalpha.com/input?i=inverse+%7B%7Ba%2C+b%2C+c%7D%2C+%7Bd%2C+e%2C+f%7D%2C+%7Bg%2C+h%2C+i%7D%7D
1557
1558 const auto& a = A[0, 0];
1559 const auto& b = A[0, 1];
1560 const auto& c = A[0, 2];
1561 const auto& d = A[1, 0];
1562 const auto& e = A[1, 1];
1563 const auto& f = A[1, 2];
1564 const auto& g = A[2, 0];
1565 const auto& h = A[2, 1];
1566 const auto& i = A[2, 2];
1567
1568 auto ae = a * e;
1569 auto af = a * f;
1570 auto ah = a * h;
1571 auto ai = a * i;
1572 auto bd = b * d;
1573 auto bf = b * f;
1574 auto bg = b * g;
1575 auto bi = b * i;
1576 auto cd = c * d;
1577 auto ce = c * e;
1578 auto cg = c * g;
1579 auto ch = c * h;
1580 auto dh = d * h;
1581 auto di = d * i;
1582 auto eg = e * g;
1583 auto ei = e * i;
1584 auto fg = f * g;
1585 auto fh = f * h;
1586
1587 auto adj_A00 = ei - fh;
1588 auto adj_A10 = fg - di;
1589 auto adj_A20 = dh - eg;
1590
1591 VariableMatrix adj_A{{adj_A00, ch - bi, bf - ce},
1592 {adj_A10, ai - cg, cd - af},
1593 {adj_A20, bg - ah, ae - bd}};
1594 auto det_A = a * adj_A00 + b * adj_A10 + c * adj_A20;
1595 return adj_A / det_A * B;
1596 } else if (A.rows() == 4 && A.cols() == 4) {
1597 // Compute optimal inverse instead of using Eigen's general solver
1598 //
1599 // [a b c d]⁻¹
1600 // [e f g h]
1601 // [i j k l]
1602 // [m n o p]
1603 //
1604 // https://www.wolframalpha.com/input?i=inverse+%7B%7Ba%2C+b%2C+c%2C+d%7D%2C+%7Be%2C+f%2C+g%2C+h%7D%2C+%7Bi%2C+j%2C+k%2C+l%7D%2C+%7Bm%2C+n%2C+o%2C+p%7D%7D
1605
1606 const auto& a = A[0, 0];
1607 const auto& b = A[0, 1];
1608 const auto& c = A[0, 2];
1609 const auto& d = A[0, 3];
1610 const auto& e = A[1, 0];
1611 const auto& f = A[1, 1];
1612 const auto& g = A[1, 2];
1613 const auto& h = A[1, 3];
1614 const auto& i = A[2, 0];
1615 const auto& j = A[2, 1];
1616 const auto& k = A[2, 2];
1617 const auto& l = A[2, 3];
1618 const auto& m = A[3, 0];
1619 const auto& n = A[3, 1];
1620 const auto& o = A[3, 2];
1621 const auto& p = A[3, 3];
1622
1623 auto afk = a * f * k;
1624 auto afl = a * f * l;
1625 auto afo = a * f * o;
1626 auto afp = a * f * p;
1627 auto agj = a * g * j;
1628 auto agl = a * g * l;
1629 auto agn = a * g * n;
1630 auto agp = a * g * p;
1631 auto ahj = a * h * j;
1632 auto ahk = a * h * k;
1633 auto ahn = a * h * n;
1634 auto aho = a * h * o;
1635 auto ajo = a * j * o;
1636 auto ajp = a * j * p;
1637 auto akn = a * k * n;
1638 auto akp = a * k * p;
1639 auto aln = a * l * n;
1640 auto alo = a * l * o;
1641 auto bek = b * e * k;
1642 auto bel = b * e * l;
1643 auto beo = b * e * o;
1644 auto bep = b * e * p;
1645 auto bgi = b * g * i;
1646 auto bgl = b * g * l;
1647 auto bgm = b * g * m;
1648 auto bgp = b * g * p;
1649 auto bhi = b * h * i;
1650 auto bhk = b * h * k;
1651 auto bhm = b * h * m;
1652 auto bho = b * h * o;
1653 auto bio = b * i * o;
1654 auto bip = b * i * p;
1655 auto bjp = b * j * p;
1656 auto bkm = b * k * m;
1657 auto bkp = b * k * p;
1658 auto blm = b * l * m;
1659 auto blo = b * l * o;
1660 auto cej = c * e * j;
1661 auto cel = c * e * l;
1662 auto cen = c * e * n;
1663 auto cep = c * e * p;
1664 auto cfi = c * f * i;
1665 auto cfl = c * f * l;
1666 auto cfm = c * f * m;
1667 auto cfp = c * f * p;
1668 auto chi = c * h * i;
1669 auto chj = c * h * j;
1670 auto chm = c * h * m;
1671 auto chn = c * h * n;
1672 auto cin = c * i * n;
1673 auto cip = c * i * p;
1674 auto cjm = c * j * m;
1675 auto cjp = c * j * p;
1676 auto clm = c * l * m;
1677 auto cln = c * l * n;
1678 auto dej = d * e * j;
1679 auto dek = d * e * k;
1680 auto den = d * e * n;
1681 auto deo = d * e * o;
1682 auto dfi = d * f * i;
1683 auto dfk = d * f * k;
1684 auto dfm = d * f * m;
1685 auto dfo = d * f * o;
1686 auto dgi = d * g * i;
1687 auto dgj = d * g * j;
1688 auto dgm = d * g * m;
1689 auto dgn = d * g * n;
1690 auto din = d * i * n;
1691 auto dio = d * i * o;
1692 auto djm = d * j * m;
1693 auto djo = d * j * o;
1694 auto dkm = d * k * m;
1695 auto dkn = d * k * n;
1696 auto ejo = e * j * o;
1697 auto ejp = e * j * p;
1698 auto ekn = e * k * n;
1699 auto ekp = e * k * p;
1700 auto eln = e * l * n;
1701 auto elo = e * l * o;
1702 auto fio = f * i * o;
1703 auto fip = f * i * p;
1704 auto fkm = f * k * m;
1705 auto fkp = f * k * p;
1706 auto flm = f * l * m;
1707 auto flo = f * l * o;
1708 auto gin = g * i * n;
1709 auto gip = g * i * p;
1710 auto gjm = g * j * m;
1711 auto gjp = g * j * p;
1712 auto glm = g * l * m;
1713 auto gln = g * l * n;
1714 auto hin = h * i * n;
1715 auto hio = h * i * o;
1716 auto hjm = h * j * m;
1717 auto hjo = h * j * o;
1718 auto hkm = h * k * m;
1719 auto hkn = h * k * n;
1720
1721 auto adj_A00 = fkp - flo - gjp + gln + hjo - hkn;
1722 auto adj_A01 = -bkp + blo + cjp - cln - djo + dkn;
1723 auto adj_A02 = bgp - bho - cfp + chn + dfo - dgn;
1724 auto adj_A03 = -bgl + bhk + cfl - chj - dfk + dgj;
1725 auto adj_A10 = -ekp + elo + gip - glm - hio + hkm;
1726 auto adj_A11 = akp - alo - cip + clm + dio - dkm;
1727 auto adj_A12 = -agp + aho + cep - chm - deo + dgm;
1728 auto adj_A13 = agl - ahk - cel + chi + dek - dgi;
1729 auto adj_A20 = ejp - eln - fip + flm + hin - hjm;
1730 auto adj_A21 = -ajp + aln + bip - blm - din + djm;
1731 auto adj_A22 = afp - ahn - bep + bhm + den - dfm;
1732 auto adj_A23 = -afl + ahj + bel - bhi - dej + dfi;
1733 auto adj_A30 = -ejo + ekn + fio - fkm - gin + gjm;
1734 // NOLINTNEXTLINE(build/include_what_you_use)
1735 auto adj_A31 = ajo - akn - bio + bkm + cin - cjm;
1736 auto adj_A32 = -afo + agn + beo - bgm - cen + cfm;
1737 auto adj_A33 = afk - agj - bek + bgi + cej - cfi;
1738
1739 VariableMatrix adj_A{{adj_A00, adj_A01, adj_A02, adj_A03},
1740 {adj_A10, adj_A11, adj_A12, adj_A13},
1741 {adj_A20, adj_A21, adj_A22, adj_A23},
1742 {adj_A30, adj_A31, adj_A32, adj_A33}};
1743 auto det_A = a * adj_A00 + b * adj_A10 + c * adj_A20 + d * adj_A30;
1744 return adj_A / det_A * B;
1745 } else {
1746 return VariableMatrix<Scalar>{
1747 A.to_eigen().householderQr().solve(B.to_eigen())};
1748 }
1749}
1750
1751namespace detail {
1752
1764template <typename Scalar>
1765VariableMatrix<Scalar> gradient_tree(const ExpressionGraph<Scalar>& top_list,
1766 const VariableMatrix<Scalar>& wrt) {
1767 slp_assert(wrt.cols() == 1);
1768
1769 // Read docs/algorithms.md#Reverse_accumulation_automatic_differentiation
1770 // for background on reverse accumulation automatic differentiation.
1771
1772 if (top_list.empty()) {
1773 return VariableMatrix<Scalar>{detail::empty, wrt.rows(), 1};
1774 }
1775
1776 // Set root node's adjoint to 1 since df/df is 1
1777 top_list[0]->adj_expr = constant_ptr(Scalar(1));
1778
1779 // df/dx = (df/dy)(dy/dx). The adjoint of x is equal to the adjoint of y
1780 // multiplied by dy/dx. If there are multiple "paths" from the root node to
1781 // variable; the variable's adjoint is the sum of each path's adjoint
1782 // contribution.
1783 for (auto& node : top_list) {
1784 // Leaf nodes have no children to propagate adjoints to, and a null (zero)
1785 // adjoint has nothing to contribute
1786 if (!node->is_leaf && node->adj_expr != nullptr) {
1787 node->accumulate_adjoints_expr();
1788 }
1789 }
1790
1791 // Move gradient tree to return value
1792 VariableMatrix<Scalar> grad{detail::empty, wrt.rows(), 1};
1793 for (int row = 0; row < grad.rows(); ++row) {
1794 grad[row] = Variable{std::move(wrt[row].expr->adj_expr)};
1795 }
1796
1797 // Unlink adjoints to avoid circular references between them and their
1798 // parent expressions. This ensures all expressions are returned to the free
1799 // list.
1800 for (auto& node : top_list) {
1801 node->adj_expr = nullptr;
1802 }
1803
1804 return grad;
1805}
1806
1807} // namespace detail
1808
1809extern template class EXPORT_TEMPLATE_DECLARE(SLEIPNIR_DLLEXPORT)
1810VariableMatrix<double>;
1811
1812extern template SLEIPNIR_DLLEXPORT VariableMatrix<double> solve(
1813 const VariableMatrix<double>& A, const VariableMatrix<double>& B);
1814
1815} // namespace slp
Definition intrusive_shared_ptr.hpp:27
Definition sleipnir_base.hpp:9
Represents a sequence of elements in an iterable object.
Definition slice.hpp:25
Definition variable_block.hpp:27
Definition variable_matrix.hpp:38
const_reverse_iterator crbegin() const
Definition variable_matrix.hpp:1230
VariableMatrix(std::initializer_list< std::initializer_list< Variable< Scalar > > > list)
Definition variable_matrix.hpp:78
const_reverse_iterator crend() const
Definition variable_matrix.hpp:1237
Scalar_ Scalar
Scalar type alias.
Definition variable_matrix.hpp:41
iterator end()
Definition variable_matrix.hpp:1181
const Variable< Scalar > & operator[](int row, int col) const
Definition variable_matrix.hpp:322
VariableMatrix(Variable< Scalar > &&variable)
Definition variable_matrix.hpp:199
const VariableBlock< const VariableMatrix > block(int row_offset, int col_offset, int block_rows, int block_cols) const
Definition variable_matrix.hpp:369
size_t size() const
Definition variable_matrix.hpp:1244
static VariableMatrix< Scalar > zero(int rows, int cols)
Definition variable_matrix.hpp:1267
VariableBlock< VariableMatrix > block(int row_offset, int col_offset, int block_rows, int block_cols)
Definition variable_matrix.hpp:353
VariableMatrix(const std::vector< std::vector< Scalar > > &list)
Definition variable_matrix.hpp:105
VariableMatrix & operator=(ScalarLike auto value)
Definition variable_matrix.hpp:283
Eigen::MatrixX< Scalar > value()
Definition variable_matrix.hpp:939
VariableMatrix & operator-=(const MatrixLike auto &rhs)
Definition variable_matrix.hpp:847
Variable< Scalar > & operator[](int index)
Definition variable_matrix.hpp:332
VariableMatrix & operator=(const Eigen::MatrixBase< Derived > &values)
Definition variable_matrix.hpp:265
friend VariableMatrix< Scalar > operator-(const LHS &lhs, const RHS &rhs)
Definition variable_matrix.hpp:789
VariableMatrix & operator-=(const ScalarLike auto &rhs)
Definition variable_matrix.hpp:863
VariableMatrix & operator*=(const ScalarLike auto &rhs)
Definition variable_matrix.hpp:629
Scalar value(int index)
Definition variable_matrix.hpp:934
VariableMatrix(const VariableBlock< VariableMatrix > &values)
Definition variable_matrix.hpp:207
VariableMatrix(int rows, int cols)
Definition variable_matrix.hpp:56
const_iterator cbegin() const
Definition variable_matrix.hpp:1196
const_iterator begin() const
Definition variable_matrix.hpp:1186
const_iterator cend() const
Definition variable_matrix.hpp:1201
const VariableBlock< const VariableMatrix > operator[](Slice row_slice, Slice col_slice) const
Definition variable_matrix.hpp:397
friend VariableMatrix< Scalar > operator/(const LHS &lhs, const Variable< Scalar > &rhs)
Definition variable_matrix.hpp:664
VariableMatrix(int rows)
Definition variable_matrix.hpp:50
VariableMatrix(std::span< const Variable< Scalar > > values, int rows, int cols)
Definition variable_matrix.hpp:249
VariableBlock< VariableMatrix > col(int col)
Definition variable_matrix.hpp:487
static VariableMatrix< Scalar > identity(int rows)
Definition variable_matrix.hpp:1250
VariableMatrix(const Eigen::MatrixBase< Derived > &values)
Definition variable_matrix.hpp:156
VariableMatrix< Scalar > exp() const
Definition variable_matrix.hpp:987
const VariableBlock< const VariableMatrix > operator[](Slice row_slice, int row_slice_length, Slice col_slice, int col_slice_length) const
Definition variable_matrix.hpp:433
const VariableBlock< const VariableMatrix > col(int col) const
Definition variable_matrix.hpp:496
VariableMatrix & operator+=(const ScalarLike auto &rhs)
Definition variable_matrix.hpp:771
VariableBlock< VariableMatrix > segment(int offset, int length)
Definition variable_matrix.hpp:445
Scalar value(int row, int col)
Definition variable_matrix.hpp:928
const VariableBlock< const VariableMatrix > row(int row) const
Definition variable_matrix.hpp:478
VariableMatrix()=default
Constructs an empty VariableMatrix.
Eigen::MatrixX< Variable< Scalar > > to_eigen() const
Definition variable_matrix.hpp:954
VariableMatrix & operator+=(const MatrixLike auto &rhs)
Definition variable_matrix.hpp:755
VariableMatrix(std::span< const Variable< Scalar > > values)
Definition variable_matrix.hpp:234
friend VariableMatrix< Scalar > operator-(const SleipnirMatrixLike< Scalar > auto &lhs)
Definition variable_matrix.hpp:878
static VariableMatrix< Scalar > constant(int rows, int cols, Scalar constant)
Definition variable_matrix.hpp:1298
VariableMatrix< Scalar > T() const
Definition variable_matrix.hpp:901
VariableMatrix< Scalar > cwise_transform(function_ref< Variable< Scalar >(const Variable< Scalar > &x)> unary_op) const
Definition variable_matrix.hpp:970
friend VariableMatrix< Scalar > operator*(const LHS &lhs, const RHS &rhs)
Definition variable_matrix.hpp:506
const_reverse_iterator rbegin() const
Definition variable_matrix.hpp:1216
VariableMatrix(const Variable< Scalar > &variable)
Definition variable_matrix.hpp:191
reverse_iterator rend()
Definition variable_matrix.hpp:1211
VariableMatrix(const VariableBlock< const VariableMatrix > &values)
Definition variable_matrix.hpp:221
int rows() const
Definition variable_matrix.hpp:916
static VariableMatrix< Scalar > one(int rows, int cols)
Definition variable_matrix.hpp:1282
VariableMatrix(const Eigen::DiagonalBase< Derived > &values)
Definition variable_matrix.hpp:172
VariableBlock< VariableMatrix > operator[](Slice row_slice, Slice col_slice)
Definition variable_matrix.hpp:385
const_reverse_iterator rend() const
Definition variable_matrix.hpp:1223
VariableMatrix & operator*=(const MatrixLike auto &rhs)
Definition variable_matrix.hpp:608
VariableMatrix(detail::empty_t, int rows, int cols)
Definition variable_matrix.hpp:67
const_iterator end() const
Definition variable_matrix.hpp:1191
void set_value(const Eigen::MatrixBase< Derived > &values)
Definition variable_matrix.hpp:296
Variable< Scalar > & operator[](int row, int col)
Definition variable_matrix.hpp:311
iterator begin()
Definition variable_matrix.hpp:1176
const VariableBlock< const VariableMatrix > segment(int offset, int length) const
Definition variable_matrix.hpp:457
reverse_iterator rbegin()
Definition variable_matrix.hpp:1206
VariableBlock< VariableMatrix > row(int row)
Definition variable_matrix.hpp:469
friend VariableMatrix< Scalar > operator+(const LHS &lhs, const RHS &rhs)
Definition variable_matrix.hpp:697
VariableMatrix(const std::vector< std::vector< Variable< Scalar > > > &list)
Definition variable_matrix.hpp:131
VariableBlock< VariableMatrix > operator[](Slice row_slice, int row_slice_length, Slice col_slice, int col_slice_length)
Definition variable_matrix.hpp:415
VariableMatrix & operator/=(const ScalarLike auto &rhs)
Definition variable_matrix.hpp:681
const Variable< Scalar > & operator[](int index) const
Definition variable_matrix.hpp:341
int cols() const
Definition variable_matrix.hpp:921
Definition variable.hpp:63
Definition function_ref.hpp:13
Definition concepts.hpp:18
Definition concepts.hpp:24
Definition concepts.hpp:33
Definition concepts.hpp:38
Type tag used to designate an uninitialized VariableMatrix.
Definition empty.hpp:8