7#include <initializer_list>
44 m_storage.reserve(Rows());
45 for (
int row = 0; row < Rows(); ++row) {
46 m_storage.emplace_back();
57 m_storage.reserve(Rows() * Cols());
58 for (
int index = 0; index < Rows() * Cols(); ++index) {
59 m_storage.emplace_back();
70 m_storage.reserve(Rows() * Cols());
71 for (
int index = 0; index < Rows() * Cols(); ++index) {
72 m_storage.emplace_back(
nullptr);
82 std::initializer_list<std::initializer_list<Variable>> list) {
86 if (list.size() > 0) {
87 m_cols = list.begin()->size();
92 const auto& row : list) {
93 Assert(list.begin()->size() == row.size());
96 m_storage.reserve(Rows() * Cols());
97 for (
const auto& row : list) {
98 std::copy(row.begin(), row.end(), std::back_inserter(m_storage));
111 m_rows = list.size();
113 if (list.size() > 0) {
114 m_cols = list.begin()->size();
118 for ([[maybe_unused]]
119 const auto& row : list) {
120 Assert(list.begin()->size() == row.size());
123 m_storage.reserve(Rows() * Cols());
124 for (
const auto& row : list) {
125 std::copy(row.begin(), row.end(), std::back_inserter(m_storage));
138 m_rows = list.size();
140 if (list.size() > 0) {
141 m_cols = list.begin()->size();
145 for ([[maybe_unused]]
146 const auto& row : list) {
147 Assert(list.begin()->size() == row.size());
150 m_storage.reserve(Rows() * Cols());
151 for (
const auto& row : list) {
152 std::copy(row.begin(), row.end(), std::back_inserter(m_storage));
161 template <
typename Derived>
163 : m_rows{static_cast<int>(values.rows())},
164 m_cols{static_cast<int>(values.cols())} {
165 m_storage.reserve(values.rows() * values.cols());
166 for (
int row = 0; row < values.rows(); ++row) {
167 for (
int col = 0; col < values.cols(); ++col) {
168 m_storage.emplace_back(values(row, col));
178 template <
typename Derived>
180 : m_rows{static_cast<int>(values.rows())},
181 m_cols{static_cast<int>(values.cols())} {
182 m_storage.reserve(values.rows() * values.cols());
183 for (
int row = 0; row < values.rows(); ++row) {
184 for (
int col = 0; col < values.cols(); ++col) {
186 m_storage.emplace_back(values.diagonal()(row));
188 m_storage.emplace_back(0.0);
199 template <
typename Derived>
201 Assert(Rows() == values.rows());
202 Assert(Cols() == values.cols());
204 for (
int row = 0; row < values.rows(); ++row) {
205 for (
int col = 0; col < values.cols(); ++col) {
206 (*this)(row, col) = values(row, col);
218 template <
typename Derived>
219 requires std::same_as<typename Derived::Scalar, double>
220 void SetValue(
const Eigen::MatrixBase<Derived>& values) {
221 Assert(Rows() == values.rows());
222 Assert(Cols() == values.cols());
224 for (
int row = 0; row < values.rows(); ++row) {
225 for (
int col = 0; col < values.cols(); ++col) {
226 (*this)(row, col).SetValue(values(row, col));
237 : m_rows{1}, m_cols{1} {
238 m_storage.emplace_back(variable);
247 m_storage.emplace_back(std::move(variable));
256 : m_rows{values.Rows()}, m_cols{values.Cols()} {
257 m_storage.reserve(Rows() * Cols());
258 for (
int row = 0; row < Rows(); ++row) {
259 for (
int col = 0; col < Cols(); ++col) {
260 m_storage.emplace_back(values(row, col));
271 : m_rows{values.Rows()}, m_cols{values.Cols()} {
272 m_storage.reserve(Rows() * Cols());
273 for (
int row = 0; row < Rows(); ++row) {
274 for (
int col = 0; col < Cols(); ++col) {
275 m_storage.emplace_back(values(row, col));
286 : m_rows{static_cast<int>(values.
size())}, m_cols{1} {
287 m_storage.reserve(Rows() * Cols());
288 for (
int row = 0; row < Rows(); ++row) {
289 for (
int col = 0; col < Cols(); ++col) {
290 m_storage.emplace_back(values[row * Cols() + col]);
303 : m_rows{rows}, m_cols{cols} {
304 Assert(
static_cast<int>(values.size()) == Rows() * Cols());
305 m_storage.reserve(Rows() * Cols());
306 for (
int row = 0; row < Rows(); ++row) {
307 for (
int col = 0; col < Cols(); ++col) {
308 m_storage.emplace_back(values[row * Cols() + col]);
320 Assert(row >= 0 && row < Rows());
321 Assert(col >= 0 && col < Cols());
322 return m_storage[row * Cols() + col];
332 Assert(row >= 0 && row < Rows());
333 Assert(col >= 0 && col < Cols());
334 return m_storage[row * Cols() + col];
343 Assert(row >= 0 && row < Rows() * Cols());
344 return m_storage[row];
353 Assert(row >= 0 && row < Rows() * Cols());
354 return m_storage[row];
366 int blockRows,
int blockCols) {
367 Assert(rowOffset >= 0 && rowOffset <= Rows());
368 Assert(colOffset >= 0 && colOffset <= Cols());
369 Assert(blockRows >= 0 && blockRows <= Rows() - rowOffset);
370 Assert(blockCols >= 0 && blockCols <= Cols() - colOffset);
371 return VariableBlock{*
this, rowOffset, colOffset, blockRows, blockCols};
384 int blockCols)
const {
385 Assert(rowOffset >= 0 && rowOffset <= Rows());
386 Assert(colOffset >= 0 && colOffset <= Cols());
387 Assert(blockRows >= 0 && blockRows <= Rows() - rowOffset);
388 Assert(blockCols >= 0 && blockCols <= Cols() - colOffset);
389 return VariableBlock{*
this, rowOffset, colOffset, blockRows, blockCols};
399 int rowSliceLength = rowSlice.
Adjust(Rows());
400 int colSliceLength = colSlice.
Adjust(Cols());
401 return VariableBlock{*
this, std::move(rowSlice), rowSliceLength,
402 std::move(colSlice), colSliceLength};
412 Slice colSlice)
const {
413 int rowSliceLength = rowSlice.
Adjust(Rows());
414 int colSliceLength = colSlice.
Adjust(Cols());
415 return VariableBlock{*
this, std::move(rowSlice), rowSliceLength,
416 std::move(colSlice), colSliceLength};
432 Slice colSlice,
int colSliceLength) {
433 return VariableBlock{*
this, std::move(rowSlice), rowSliceLength,
434 std::move(colSlice), colSliceLength};
449 Slice rowSlice,
int rowSliceLength,
Slice colSlice,
450 int colSliceLength)
const {
451 return VariableBlock{*
this, std::move(rowSlice), rowSliceLength,
452 std::move(colSlice), colSliceLength};
462 Assert(offset >= 0 && offset < Rows() * Cols());
463 Assert(length >= 0 && length <= Rows() * Cols() - offset);
464 return Block(offset, 0, length, 1);
475 Assert(offset >= 0 && offset < Rows() * Cols());
476 Assert(length >= 0 && length <= Rows() * Cols() - offset);
477 return Block(offset, 0, length, 1);
486 Assert(row >= 0 && row < Rows());
487 return Block(row, 0, 1, Cols());
496 Assert(row >= 0 && row < Rows());
497 return Block(row, 0, 1, Cols());
506 Assert(col >= 0 && col < Cols());
507 return Block(0, col, Rows(), 1);
516 Assert(col >= 0 && col < Cols());
517 return Block(0, col, Rows(), 1);
532 for (
int i = 0; i < lhs.
Rows(); ++i) {
533 for (
int j = 0; j < rhs.
Cols(); ++j) {
535 for (
int k = 0; k < lhs.
Cols(); ++k) {
536 sum += lhs(i, k) * rhs(k, j);
555 for (
int row = 0; row < result.Rows(); ++row) {
556 for (
int col = 0; col < result.Cols(); ++col) {
557 result(row, col) = lhs(row, col) * rhs;
585 for (
int row = 0; row < result.Rows(); ++row) {
586 for (
int col = 0; col < result.Cols(); ++col) {
587 result(row, col) = rhs(row, col) * lhs;
613 for (
int i = 0; i < Rows(); ++i) {
614 for (
int j = 0; j < rhs.
Cols(); ++j) {
616 for (
int k = 0; k < Cols(); ++k) {
617 sum += (*this)(i, k) * rhs(k, j);
636 for (
int row = 0; row < result.Rows(); ++row) {
637 for (
int col = 0; col < result.Cols(); ++col) {
638 result(row, col) = lhs(row, col) / rhs;
652 for (
int row = 0; row < Rows(); ++row) {
653 for (
int col = 0; col < Cols(); ++col) {
654 (*this)(row, col) /= rhs;
671 for (
int row = 0; row < result.Rows(); ++row) {
672 for (
int col = 0; col < result.Cols(); ++col) {
673 result(row, col) = lhs(row, col) + rhs(row, col);
686 for (
int row = 0; row < Rows(); ++row) {
687 for (
int col = 0; col < Cols(); ++col) {
688 (*this)(row, col) += rhs(row, col);
705 for (
int row = 0; row < result.Rows(); ++row) {
706 for (
int col = 0; col < result.Cols(); ++col) {
707 result(row, col) = lhs(row, col) - rhs(row, col);
720 for (
int row = 0; row < Rows(); ++row) {
721 for (
int col = 0; col < Cols(); ++col) {
722 (*this)(row, col) -= rhs(row, col);
738 for (
int row = 0; row < result.Rows(); ++row) {
739 for (
int col = 0; col < result.Cols(); ++col) {
740 result(row, col) = -lhs(row, col);
751 Assert(Rows() == 1 && Cols() == 1);
752 return (*
this)(0, 0);
761 for (
int row = 0; row < Rows(); ++row) {
762 for (
int col = 0; col < Cols(); ++col) {
763 result(col, row) = (*this)(row, col);
773 int Rows()
const {
return m_rows; }
778 int Cols()
const {
return m_cols; }
787 Assert(row >= 0 && row < Rows());
788 Assert(col >= 0 && col < Cols());
789 return m_storage[row * Cols() + col].Value();
798 Assert(index >= 0 && index < Rows() * Cols());
799 return m_storage[index].Value();
806 Eigen::MatrixXd result{Rows(), Cols()};
808 for (
int row = 0; row < Rows(); ++row) {
809 for (
int col = 0; col < Cols(); ++col) {
810 result(row, col) = Value(row, col);
826 for (
int row = 0; row < Rows(); ++row) {
827 for (
int col = 0; col < Cols(); ++col) {
828 result(row, col) = unaryOp((*
this)(row, col));
921 size_t size()
const {
return m_storage.size(); }
932 for (
auto& elem : result) {
948 for (
auto& elem : result) {
976 for (
int row = 0; row <
lhs.Rows(); ++row) {
996 std::initializer_list<std::initializer_list<VariableMatrix>>
list) {
1000 for (
const auto& row :
list) {
1001 if (row.size() > 0) {
1007 for (
const auto&
elem : row) {
1026 for (
const auto& row :
list) {
1028 for (
const auto&
elem : row) {
1051 const std::vector<std::vector<VariableMatrix>>&
list) {
1055 for (
const auto& row :
list) {
1056 if (row.size() > 0) {
1062 for (
const auto&
elem : row) {
1081 for (
const auto& row :
list) {
1083 for (
const auto&
elem : row) {
#define Assert(condition)
Definition Assert.hpp:24
#define SLEIPNIR_DLLEXPORT
Definition SymbolExports.hpp:34
constexpr int Adjust(int length)
Definition Slice.hpp:118
Definition VariableBlock.hpp:24
Definition VariableMatrix.hpp:861
std::forward_iterator_tag iterator_category
Definition VariableMatrix.hpp:863
std::ptrdiff_t difference_type
Definition VariableMatrix.hpp:865
bool operator==(const const_iterator &) const =default
const_iterator & operator++()
Definition VariableMatrix.hpp:872
const_reference operator*() const
Definition VariableMatrix.hpp:882
const_iterator(small_vector< Variable >::const_iterator it)
Definition VariableMatrix.hpp:869
const_iterator operator++(int)
Definition VariableMatrix.hpp:876
Definition VariableMatrix.hpp:835
reference operator*()
Definition VariableMatrix.hpp:855
std::ptrdiff_t difference_type
Definition VariableMatrix.hpp:839
bool operator==(const iterator &) const =default
iterator(small_vector< Variable >::iterator it)
Definition VariableMatrix.hpp:843
iterator & operator++()
Definition VariableMatrix.hpp:845
std::forward_iterator_tag iterator_category
Definition VariableMatrix.hpp:837
iterator operator++(int)
Definition VariableMatrix.hpp:849
Definition VariableMatrix.hpp:28
int Cols() const
Definition VariableMatrix.hpp:778
friend SLEIPNIR_DLLEXPORT VariableMatrix operator*(double lhs, const VariableMatrix &rhs)
Definition VariableMatrix.hpp:601
double Value(int row, int col)
Definition VariableMatrix.hpp:786
friend SLEIPNIR_DLLEXPORT VariableMatrix operator/(const VariableMatrix &lhs, const Variable &rhs)
Definition VariableMatrix.hpp:632
VariableBlock< VariableMatrix > Col(int col)
Definition VariableMatrix.hpp:505
VariableMatrix(const std::vector< std::vector< Variable > > &list)
Definition VariableMatrix.hpp:136
const Variable & operator()(int row) const
Definition VariableMatrix.hpp:352
VariableMatrix(empty_t, int rows, int cols)
Definition VariableMatrix.hpp:69
VariableMatrix & operator*=(const VariableMatrix &rhs)
Definition VariableMatrix.hpp:610
VariableMatrix(const Variable &variable)
Definition VariableMatrix.hpp:236
VariableMatrix(std::span< const Variable > values)
Definition VariableMatrix.hpp:285
VariableBlock< VariableMatrix > operator()(Slice rowSlice, Slice colSlice)
Definition VariableMatrix.hpp:398
friend SLEIPNIR_DLLEXPORT VariableMatrix operator*(const VariableMatrix &lhs, const VariableMatrix &rhs)
Definition VariableMatrix.hpp:527
const VariableBlock< const VariableMatrix > Segment(int offset, int length) const
Definition VariableMatrix.hpp:473
static VariableMatrix Zero(int rows, int cols)
Definition VariableMatrix.hpp:929
friend SLEIPNIR_DLLEXPORT VariableMatrix operator*(const VariableMatrix &lhs, double rhs)
Definition VariableMatrix.hpp:570
static VariableMatrix Ones(int rows, int cols)
Definition VariableMatrix.hpp:945
iterator begin()
Definition VariableMatrix.hpp:891
Variable & operator()(int row, int col)
Definition VariableMatrix.hpp:319
const VariableBlock< const VariableMatrix > operator()(Slice rowSlice, Slice colSlice) const
Definition VariableMatrix.hpp:411
VariableMatrix(int rows, int cols)
Definition VariableMatrix.hpp:56
friend SLEIPNIR_DLLEXPORT VariableMatrix operator*(const Variable &lhs, const VariableMatrix &rhs)
Definition VariableMatrix.hpp:582
VariableMatrix(const VariableBlock< const VariableMatrix > &values)
Definition VariableMatrix.hpp:270
VariableMatrix & operator=(const Eigen::MatrixBase< Derived > &values)
Definition VariableMatrix.hpp:200
VariableMatrix(Variable &&variable)
Definition VariableMatrix.hpp:246
Eigen::MatrixXd Value()
Definition VariableMatrix.hpp:805
const VariableBlock< const VariableMatrix > Block(int rowOffset, int colOffset, int blockRows, int blockCols) const
Definition VariableMatrix.hpp:382
VariableMatrix(const Eigen::DiagonalBase< Derived > &values)
Definition VariableMatrix.hpp:179
VariableMatrix(std::initializer_list< std::initializer_list< Variable > > list)
Definition VariableMatrix.hpp:81
const_iterator begin() const
Definition VariableMatrix.hpp:901
VariableMatrix T() const
Definition VariableMatrix.hpp:758
VariableBlock< VariableMatrix > Block(int rowOffset, int colOffset, int blockRows, int blockCols)
Definition VariableMatrix.hpp:365
int Rows() const
Definition VariableMatrix.hpp:773
friend SLEIPNIR_DLLEXPORT VariableMatrix operator+(const VariableMatrix &lhs, const VariableMatrix &rhs)
Definition VariableMatrix.hpp:668
VariableBlock< VariableMatrix > operator()(Slice rowSlice, int rowSliceLength, Slice colSlice, int colSliceLength)
Definition VariableMatrix.hpp:431
VariableMatrix(std::span< const Variable > values, int rows, int cols)
Definition VariableMatrix.hpp:302
size_t size() const
Definition VariableMatrix.hpp:921
const Variable & operator()(int row, int col) const
Definition VariableMatrix.hpp:331
VariableMatrix(int rows)
Definition VariableMatrix.hpp:43
void SetValue(const Eigen::MatrixBase< Derived > &values)
Definition VariableMatrix.hpp:220
const VariableBlock< const VariableMatrix > Col(int col) const
Definition VariableMatrix.hpp:515
VariableMatrix & operator/=(const Variable &rhs)
Definition VariableMatrix.hpp:651
const_iterator cbegin() const
Definition VariableMatrix.hpp:911
VariableMatrix CwiseTransform(function_ref< Variable(const Variable &x)> unaryOp) const
Definition VariableMatrix.hpp:822
VariableBlock< VariableMatrix > Segment(int offset, int length)
Definition VariableMatrix.hpp:461
VariableMatrix(const VariableBlock< VariableMatrix > &values)
Definition VariableMatrix.hpp:255
const VariableBlock< const VariableMatrix > Row(int row) const
Definition VariableMatrix.hpp:495
VariableMatrix & operator+=(const VariableMatrix &rhs)
Definition VariableMatrix.hpp:685
const VariableBlock< const VariableMatrix > operator()(Slice rowSlice, int rowSliceLength, Slice colSlice, int colSliceLength) const
Definition VariableMatrix.hpp:448
VariableMatrix & operator-=(const VariableMatrix &rhs)
Definition VariableMatrix.hpp:719
VariableMatrix(const Eigen::MatrixBase< Derived > &values)
Definition VariableMatrix.hpp:162
friend SLEIPNIR_DLLEXPORT VariableMatrix operator-(const VariableMatrix &lhs, const VariableMatrix &rhs)
Definition VariableMatrix.hpp:702
iterator end()
Definition VariableMatrix.hpp:896
double Value(int index)
Definition VariableMatrix.hpp:797
const_iterator end() const
Definition VariableMatrix.hpp:906
friend SLEIPNIR_DLLEXPORT VariableMatrix operator*(const VariableMatrix &lhs, const Variable &rhs)
Definition VariableMatrix.hpp:551
VariableMatrix(const std::vector< std::vector< double > > &list)
Definition VariableMatrix.hpp:109
VariableBlock< VariableMatrix > Row(int row)
Definition VariableMatrix.hpp:485
Variable & operator()(int row)
Definition VariableMatrix.hpp:342
friend SLEIPNIR_DLLEXPORT VariableMatrix operator-(const VariableMatrix &lhs)
Definition VariableMatrix.hpp:735
const_iterator cend() const
Definition VariableMatrix.hpp:916
Definition Variable.hpp:33
Definition FunctionRef.hpp:17
Definition small_vector.hpp:499
Definition small_vector.hpp:3616
Definition Expression.hpp:18
SLEIPNIR_DLLEXPORT VariableMatrix Solve(const VariableMatrix &A, const VariableMatrix &B)
IntrusiveSharedPtr< T > AllocateIntrusiveShared(Alloc alloc, Args &&... args)
Definition IntrusiveSharedPtr.hpp:275
constexpr bool empty(const small_vector< T, InlineCapacity, Allocator > &v) noexcept
Definition small_vector.hpp:4353
SLEIPNIR_DLLEXPORT VariableMatrix CwiseReduce(const VariableMatrix &lhs, const VariableMatrix &rhs, function_ref< Variable(const Variable &x, const Variable &y)> binaryOp)
Definition VariableMatrix.hpp:968
SLEIPNIR_DLLEXPORT VariableMatrix Block(std::initializer_list< std::initializer_list< VariableMatrix > > list)
Definition VariableMatrix.hpp:995
constexpr small_vector< T, InlineCapacity, Allocator >::size_type size(const small_vector< T, InlineCapacity, Allocator > &v) noexcept
Definition small_vector.hpp:4336
Definition VariableMatrix.hpp:30