7#include <initializer_list>
41 for (
int row = 0; row < rows; ++row) {
42 m_storage.emplace_back();
53 for (
int row = 0; row < rows; ++row) {
54 for (
int col = 0; col < cols; ++col) {
55 m_storage.emplace_back();
66 std::initializer_list<std::initializer_list<Variable>> list) {
70 if (list.size() > 0) {
71 m_cols = list.begin()->size();
76 const auto& row : list) {
77 Assert(list.begin()->size() == row.size());
80 m_storage.reserve(Rows() * Cols());
81 for (
const auto& row : list) {
82 std::copy(row.begin(), row.end(), std::back_inserter(m_storage));
97 if (list.size() > 0) {
98 m_cols = list.begin()->size();
102 for ([[maybe_unused]]
103 const auto& row : list) {
104 Assert(list.begin()->size() == row.size());
107 m_storage.reserve(Rows() * Cols());
108 for (
const auto& row : list) {
109 std::copy(row.begin(), row.end(), std::back_inserter(m_storage));
122 m_rows = list.size();
124 if (list.size() > 0) {
125 m_cols = list.begin()->size();
129 for ([[maybe_unused]]
130 const auto& row : list) {
131 Assert(list.begin()->size() == row.size());
134 m_storage.reserve(Rows() * Cols());
135 for (
const auto& row : list) {
136 std::copy(row.begin(), row.end(), std::back_inserter(m_storage));
145 template <
typename Derived>
147 : m_rows{static_cast<int>(values.rows())},
148 m_cols{static_cast<int>(values.cols())} {
149 m_storage.reserve(values.rows() * values.cols());
150 for (
int row = 0; row < values.rows(); ++row) {
151 for (
int col = 0; col < values.cols(); ++col) {
152 m_storage.emplace_back(values(row, col));
162 template <
typename Derived>
164 : m_rows{static_cast<int>(values.rows())},
165 m_cols{static_cast<int>(values.cols())} {
166 m_storage.reserve(values.rows() * values.cols());
167 for (
int row = 0; row < values.rows(); ++row) {
168 for (
int col = 0; col < values.cols(); ++col) {
170 m_storage.emplace_back(values.diagonal()(row));
172 m_storage.emplace_back(0.0);
183 template <
typename Derived>
185 Assert(Rows() == values.rows());
186 Assert(Cols() == values.cols());
188 for (
int row = 0; row < values.rows(); ++row) {
189 for (
int col = 0; col < values.cols(); ++col) {
190 (*this)(row, col) = values(row, col);
202 template <
typename Derived>
203 requires std::same_as<typename Derived::Scalar, double>
204 void SetValue(
const Eigen::MatrixBase<Derived>& values) {
205 Assert(Rows() == values.rows());
206 Assert(Cols() == values.cols());
208 for (
int row = 0; row < values.rows(); ++row) {
209 for (
int col = 0; col < values.cols(); ++col) {
210 (*this)(row, col).SetValue(values(row, col));
221 : m_rows{1}, m_cols{1} {
222 m_storage.emplace_back(variable);
231 m_storage.emplace_back(std::move(variable));
240 : m_rows{values.Rows()}, m_cols{values.Cols()} {
241 for (
int row = 0; row < Rows(); ++row) {
242 for (
int col = 0; col < Cols(); ++col) {
243 m_storage.emplace_back(values(row, col));
254 : m_rows{values.Rows()}, m_cols{values.Cols()} {
255 for (
int row = 0; row < Rows(); ++row) {
256 for (
int col = 0; col < Cols(); ++col) {
257 m_storage.emplace_back(values(row, col));
268 : m_rows{static_cast<int>(values.size())}, m_cols{1} {
269 for (
int row = 0; row < Rows(); ++row) {
270 for (
int col = 0; col < Cols(); ++col) {
271 m_storage.emplace_back(values[row * Cols() + col]);
284 : m_rows{rows}, m_cols{cols} {
285 Assert(
static_cast<int>(values.size()) == Rows() * Cols());
286 for (
int row = 0; row < Rows(); ++row) {
287 for (
int col = 0; col < Cols(); ++col) {
288 m_storage.emplace_back(values[row * Cols() + col]);
300 Assert(row >= 0 && row < Rows());
301 Assert(col >= 0 && col < Cols());
302 return m_storage[row * Cols() + col];
312 Assert(row >= 0 && row < Rows());
313 Assert(col >= 0 && col < Cols());
314 return m_storage[row * Cols() + col];
323 Assert(row >= 0 && row < Rows() * Cols());
324 return m_storage[row];
333 Assert(row >= 0 && row < Rows() * Cols());
334 return m_storage[row];
346 int blockRows,
int blockCols) {
347 Assert(rowOffset >= 0 && rowOffset <= Rows());
348 Assert(colOffset >= 0 && colOffset <= Cols());
349 Assert(blockRows >= 0 && blockRows <= Rows() - rowOffset);
350 Assert(blockCols >= 0 && blockCols <= Cols() - colOffset);
351 return VariableBlock{*
this, rowOffset, colOffset, blockRows, blockCols};
364 int blockCols)
const {
365 Assert(rowOffset >= 0 && rowOffset <= Rows());
366 Assert(colOffset >= 0 && colOffset <= Cols());
367 Assert(blockRows >= 0 && blockRows <= Rows() - rowOffset);
368 Assert(blockCols >= 0 && blockCols <= Cols() - colOffset);
369 return VariableBlock{*
this, rowOffset, colOffset, blockRows, blockCols};
379 int rowSliceLength = rowSlice.
Adjust(Rows());
380 int colSliceLength = colSlice.
Adjust(Cols());
381 return VariableBlock{*
this, std::move(rowSlice), rowSliceLength,
382 std::move(colSlice), colSliceLength};
392 Slice colSlice)
const {
393 int rowSliceLength = rowSlice.
Adjust(Rows());
394 int colSliceLength = colSlice.
Adjust(Cols());
395 return VariableBlock{*
this, std::move(rowSlice), rowSliceLength,
396 std::move(colSlice), colSliceLength};
412 Slice colSlice,
int colSliceLength) {
413 return VariableBlock{*
this, std::move(rowSlice), rowSliceLength,
414 std::move(colSlice), colSliceLength};
429 Slice rowSlice,
int rowSliceLength,
Slice colSlice,
430 int colSliceLength)
const {
431 return VariableBlock{*
this, std::move(rowSlice), rowSliceLength,
432 std::move(colSlice), colSliceLength};
442 Assert(offset >= 0 && offset < Rows() * Cols());
443 Assert(length >= 0 && length <= Rows() * Cols() - offset);
444 return Block(offset, 0, length, 1);
455 Assert(offset >= 0 && offset < Rows() * Cols());
456 Assert(length >= 0 && length <= Rows() * Cols() - offset);
457 return Block(offset, 0, length, 1);
466 Assert(row >= 0 && row < Rows());
467 return Block(row, 0, 1, Cols());
476 Assert(row >= 0 && row < Rows());
477 return Block(row, 0, 1, Cols());
486 Assert(col >= 0 && col < Cols());
487 return Block(0, col, Rows(), 1);
496 Assert(col >= 0 && col < Cols());
497 return Block(0, col, Rows(), 1);
512 for (
int i = 0; i < lhs.
Rows(); ++i) {
513 for (
int j = 0; j < rhs.
Cols(); ++j) {
515 for (
int k = 0; k < lhs.
Cols(); ++k) {
516 sum += lhs(i, k) * rhs(k, j);
535 for (
int row = 0; row < result.Rows(); ++row) {
536 for (
int col = 0; col < result.Cols(); ++col) {
537 result(row, col) = lhs(row, col) * rhs;
565 for (
int row = 0; row < result.Rows(); ++row) {
566 for (
int col = 0; col < result.Cols(); ++col) {
567 result(row, col) = rhs(row, col) * lhs;
593 for (
int i = 0; i < Rows(); ++i) {
594 for (
int j = 0; j < rhs.
Cols(); ++j) {
596 for (
int k = 0; k < Cols(); ++k) {
597 sum += (*this)(i, k) * rhs(k, j);
616 for (
int row = 0; row < result.Rows(); ++row) {
617 for (
int col = 0; col < result.Cols(); ++col) {
618 result(row, col) = lhs(row, col) / rhs;
632 for (
int row = 0; row < Rows(); ++row) {
633 for (
int col = 0; col < Cols(); ++col) {
634 (*this)(row, col) /= rhs;
651 for (
int row = 0; row < result.Rows(); ++row) {
652 for (
int col = 0; col < result.Cols(); ++col) {
653 result(row, col) = lhs(row, col) + rhs(row, col);
666 for (
int row = 0; row < Rows(); ++row) {
667 for (
int col = 0; col < Cols(); ++col) {
668 (*this)(row, col) += rhs(row, col);
685 for (
int row = 0; row < result.Rows(); ++row) {
686 for (
int col = 0; col < result.Cols(); ++col) {
687 result(row, col) = lhs(row, col) - rhs(row, col);
700 for (
int row = 0; row < Rows(); ++row) {
701 for (
int col = 0; col < Cols(); ++col) {
702 (*this)(row, col) -= rhs(row, col);
718 for (
int row = 0; row < result.Rows(); ++row) {
719 for (
int col = 0; col < result.Cols(); ++col) {
720 result(row, col) = -lhs(row, col);
731 Assert(Rows() == 1 && Cols() == 1);
732 return (*
this)(0, 0);
741 for (
int row = 0; row < Rows(); ++row) {
742 for (
int col = 0; col < Cols(); ++col) {
743 result(col, row) = (*this)(row, col);
753 int Rows()
const {
return m_rows; }
758 int Cols()
const {
return m_cols; }
767 Assert(row >= 0 && row < Rows());
768 Assert(col >= 0 && col < Cols());
769 return m_storage[row * Cols() + col].Value();
779 return m_storage[
index].Value();
786 Eigen::MatrixXd result{Rows(), Cols()};
788 for (
int row = 0; row < Rows(); ++row) {
789 for (
int col = 0; col < Cols(); ++col) {
790 result(row, col) = Value(row, col);
806 for (
int row = 0; row < Rows(); ++row) {
807 for (
int col = 0; col < Cols(); ++col) {
808 result(row, col) = unaryOp((*
this)(row, col));
824 : m_mat{mat}, m_row{row}, m_col{col} {}
828 if (m_col == m_mat->Cols()) {
857 : m_mat{mat}, m_row{row}, m_col{col} {}
861 if (m_col == m_mat->Cols()) {
914 size_t size()
const {
return m_rows * m_cols; }
925 for (
auto& elem : result) {
941 for (
auto& elem : result) {
969 for (
int row = 0; row < lhs.
Rows(); ++row) {
970 for (
int col = 0; col < lhs.
Cols(); ++col) {
971 result(row, col) = binaryOp(lhs(row, col), rhs(row, col));
989 std::initializer_list<std::initializer_list<VariableMatrix>> list) {
993 for (
const auto& row : list) {
994 if (row.size() > 0) {
995 rows += row.begin()->Rows();
1000 for (
const auto& elem : row) {
1002 Assert(row.begin()->Rows() == elem.Rows());
1004 latestCols += elem.Cols();
1012 Assert(cols == latestCols);
1019 for (
const auto& row : list) {
1021 for (
const auto& elem : row) {
1022 result.Block(rowOffset, colOffset, elem.Rows(), elem.Cols()) = elem;
1023 colOffset += elem.Cols();
1025 rowOffset += row.begin()->Rows();
1044 const std::vector<std::vector<VariableMatrix>>& list) {
1048 for (
const auto& row : list) {
1049 if (row.size() > 0) {
1050 rows += row.begin()->Rows();
1055 for (
const auto& elem : row) {
1057 Assert(row.begin()->Rows() == elem.Rows());
1059 latestCols += elem.Cols();
1067 Assert(cols == latestCols);
1074 for (
const auto& row : list) {
1076 for (
const auto& elem : row) {
1077 result.Block(rowOffset, colOffset, elem.Rows(), elem.Cols()) = elem;
1078 colOffset += elem.Cols();
1080 rowOffset += row.begin()->Rows();
This file defines the SmallVector class.
#define SLEIPNIR_DLLEXPORT
Definition SymbolExports.hpp:34
constexpr int Adjust(int length)
Adjusts start and end slice indices assuming a sequence of the specified length.
Definition Slice.hpp:118
A submatrix of autodiff variables with reference semantics.
Definition VariableBlock.hpp:24
An autodiff variable pointing to an expression node.
Definition Variable.hpp:31
Definition VariableMatrix.hpp:848
std::forward_iterator_tag iterator_category
Definition VariableMatrix.hpp:850
const_iterator(const VariableMatrix *mat, int row, int col)
Definition VariableMatrix.hpp:856
std::ptrdiff_t difference_type
Definition VariableMatrix.hpp:852
bool operator==(const const_iterator &) const =default
const_iterator & operator++()
Definition VariableMatrix.hpp:859
const_reference operator*() const
Definition VariableMatrix.hpp:873
const_iterator operator++(int)
Definition VariableMatrix.hpp:867
Definition VariableMatrix.hpp:815
reference operator*()
Definition VariableMatrix.hpp:840
std::ptrdiff_t difference_type
Definition VariableMatrix.hpp:819
bool operator==(const iterator &) const =default
iterator & operator++()
Definition VariableMatrix.hpp:826
std::forward_iterator_tag iterator_category
Definition VariableMatrix.hpp:817
iterator operator++(int)
Definition VariableMatrix.hpp:834
iterator(VariableMatrix *mat, int row, int col)
Definition VariableMatrix.hpp:823
A matrix of autodiff variables.
Definition VariableMatrix.hpp:28
int Cols() const
Returns number of columns in the matrix.
Definition VariableMatrix.hpp:758
friend SLEIPNIR_DLLEXPORT VariableMatrix operator*(double lhs, const VariableMatrix &rhs)
Scalar-matrix multiplication operator.
Definition VariableMatrix.hpp:581
double Value(int row, int col)
Returns an element of the variable matrix.
Definition VariableMatrix.hpp:766
friend SLEIPNIR_DLLEXPORT VariableMatrix operator/(const VariableMatrix &lhs, const Variable &rhs)
Binary division operator (only enabled when rhs is a scalar).
Definition VariableMatrix.hpp:612
VariableBlock< VariableMatrix > Col(int col)
Returns a column slice of the variable matrix.
Definition VariableMatrix.hpp:485
VariableMatrix(const std::vector< std::vector< Variable > > &list)
Constructs a scalar VariableMatrix from a nested list of Variables.
Definition VariableMatrix.hpp:120
const Variable & operator()(int row) const
Returns a block pointing to the given row.
Definition VariableMatrix.hpp:332
VariableMatrix & operator*=(const VariableMatrix &rhs)
Compound matrix multiplication-assignment operator.
Definition VariableMatrix.hpp:590
VariableMatrix(const Variable &variable)
Constructs a scalar VariableMatrix from a Variable.
Definition VariableMatrix.hpp:220
VariableMatrix(std::span< const Variable > values)
Constructs a column vector wrapper around a Variable array.
Definition VariableMatrix.hpp:267
VariableBlock< VariableMatrix > operator()(Slice rowSlice, Slice colSlice)
Returns a slice of the variable matrix.
Definition VariableMatrix.hpp:378
friend SLEIPNIR_DLLEXPORT VariableMatrix operator*(const VariableMatrix &lhs, const VariableMatrix &rhs)
Matrix multiplication operator.
Definition VariableMatrix.hpp:507
const VariableBlock< const VariableMatrix > Segment(int offset, int length) const
Returns a segment of the variable vector.
Definition VariableMatrix.hpp:453
static VariableMatrix Zero(int rows, int cols)
Returns a variable matrix filled with zeroes.
Definition VariableMatrix.hpp:922
friend SLEIPNIR_DLLEXPORT VariableMatrix operator*(const VariableMatrix &lhs, double rhs)
Matrix-scalar multiplication operator.
Definition VariableMatrix.hpp:550
static VariableMatrix Ones(int rows, int cols)
Returns a variable matrix filled with ones.
Definition VariableMatrix.hpp:938
iterator begin()
Returns begin iterator.
Definition VariableMatrix.hpp:884
Variable & operator()(int row, int col)
Returns a block pointing to the given row and column.
Definition VariableMatrix.hpp:299
const VariableBlock< const VariableMatrix > operator()(Slice rowSlice, Slice colSlice) const
Returns a slice of the variable matrix.
Definition VariableMatrix.hpp:391
VariableMatrix(int rows, int cols)
Constructs a VariableMatrix with the given dimensions.
Definition VariableMatrix.hpp:52
friend SLEIPNIR_DLLEXPORT VariableMatrix operator*(const Variable &lhs, const VariableMatrix &rhs)
Scalar-matrix multiplication operator.
Definition VariableMatrix.hpp:562
VariableMatrix()=default
Constructs an empty VariableMatrix.
VariableMatrix(const VariableBlock< const VariableMatrix > &values)
Constructs a VariableMatrix from a VariableBlock.
Definition VariableMatrix.hpp:253
VariableMatrix & operator=(const Eigen::MatrixBase< Derived > &values)
Assigns an Eigen matrix to a VariableMatrix.
Definition VariableMatrix.hpp:184
VariableMatrix(Variable &&variable)
Constructs a scalar VariableMatrix from a Variable.
Definition VariableMatrix.hpp:230
Eigen::MatrixXd Value()
Returns the contents of the variable matrix.
Definition VariableMatrix.hpp:785
const VariableBlock< const VariableMatrix > Block(int rowOffset, int colOffset, int blockRows, int blockCols) const
Returns a block of the variable matrix.
Definition VariableMatrix.hpp:362
VariableMatrix(const Eigen::DiagonalBase< Derived > &values)
Constructs a VariableMatrix from an Eigen diagonal matrix.
Definition VariableMatrix.hpp:163
VariableMatrix(std::initializer_list< std::initializer_list< Variable > > list)
Constructs a scalar VariableMatrix from a nested list of Variables.
Definition VariableMatrix.hpp:65
const_iterator begin() const
Returns begin iterator.
Definition VariableMatrix.hpp:894
VariableMatrix T() const
Returns the transpose of the variable matrix.
Definition VariableMatrix.hpp:738
VariableBlock< VariableMatrix > Block(int rowOffset, int colOffset, int blockRows, int blockCols)
Returns a block of the variable matrix.
Definition VariableMatrix.hpp:345
int Rows() const
Returns number of rows in the matrix.
Definition VariableMatrix.hpp:753
friend SLEIPNIR_DLLEXPORT VariableMatrix operator+(const VariableMatrix &lhs, const VariableMatrix &rhs)
Binary addition operator.
Definition VariableMatrix.hpp:648
VariableBlock< VariableMatrix > operator()(Slice rowSlice, int rowSliceLength, Slice colSlice, int colSliceLength)
Returns a slice of the variable matrix.
Definition VariableMatrix.hpp:411
VariableMatrix(std::span< const Variable > values, int rows, int cols)
Constructs a matrix wrapper around a Variable array.
Definition VariableMatrix.hpp:283
size_t size() const
Returns number of elements in matrix.
Definition VariableMatrix.hpp:914
const Variable & operator()(int row, int col) const
Returns a block pointing to the given row and column.
Definition VariableMatrix.hpp:311
VariableMatrix(int rows)
Constructs a VariableMatrix column vector with the given rows.
Definition VariableMatrix.hpp:40
void SetValue(const Eigen::MatrixBase< Derived > &values)
Sets the VariableMatrix's internal values.
Definition VariableMatrix.hpp:204
const VariableBlock< const VariableMatrix > Col(int col) const
Returns a column slice of the variable matrix.
Definition VariableMatrix.hpp:495
VariableMatrix & operator/=(const Variable &rhs)
Compound matrix division-assignment operator (only enabled when rhs is a scalar).
Definition VariableMatrix.hpp:631
const_iterator cbegin() const
Returns begin iterator.
Definition VariableMatrix.hpp:904
VariableMatrix CwiseTransform(function_ref< Variable(const Variable &x)> unaryOp) const
Transforms the matrix coefficient-wise with an unary operator.
Definition VariableMatrix.hpp:802
VariableBlock< VariableMatrix > Segment(int offset, int length)
Returns a segment of the variable vector.
Definition VariableMatrix.hpp:441
VariableMatrix(const VariableBlock< VariableMatrix > &values)
Constructs a VariableMatrix from a VariableBlock.
Definition VariableMatrix.hpp:239
const VariableBlock< const VariableMatrix > Row(int row) const
Returns a row slice of the variable matrix.
Definition VariableMatrix.hpp:475
VariableMatrix & operator+=(const VariableMatrix &rhs)
Compound addition-assignment operator.
Definition VariableMatrix.hpp:665
const VariableBlock< const VariableMatrix > operator()(Slice rowSlice, int rowSliceLength, Slice colSlice, int colSliceLength) const
Returns a slice of the variable matrix.
Definition VariableMatrix.hpp:428
VariableMatrix & operator-=(const VariableMatrix &rhs)
Compound subtraction-assignment operator.
Definition VariableMatrix.hpp:699
VariableMatrix(const Eigen::MatrixBase< Derived > &values)
Constructs a VariableMatrix from an Eigen matrix.
Definition VariableMatrix.hpp:146
friend SLEIPNIR_DLLEXPORT VariableMatrix operator-(const VariableMatrix &lhs, const VariableMatrix &rhs)
Binary subtraction operator.
Definition VariableMatrix.hpp:682
iterator end()
Returns end iterator.
Definition VariableMatrix.hpp:889
double Value(int index)
Returns a row of the variable column vector.
Definition VariableMatrix.hpp:777
const_iterator end() const
Returns end iterator.
Definition VariableMatrix.hpp:899
friend SLEIPNIR_DLLEXPORT VariableMatrix operator*(const VariableMatrix &lhs, const Variable &rhs)
Matrix-scalar multiplication operator.
Definition VariableMatrix.hpp:531
VariableMatrix(const std::vector< std::vector< double > > &list)
Constructs a scalar VariableMatrix from a nested list of doubles.
Definition VariableMatrix.hpp:93
VariableBlock< VariableMatrix > Row(int row)
Returns a row slice of the variable matrix.
Definition VariableMatrix.hpp:465
Variable & operator()(int row)
Returns a block pointing to the given row.
Definition VariableMatrix.hpp:322
friend SLEIPNIR_DLLEXPORT VariableMatrix operator-(const VariableMatrix &lhs)
Unary minus operator.
Definition VariableMatrix.hpp:715
const_iterator cend() const
Returns end iterator.
Definition VariableMatrix.hpp:909
An implementation of std::function_ref, a lightweight non-owning reference to a callable.
Definition FunctionRef.hpp:17
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition SmallVector.h:1212
Definition Hessian.hpp:18
SLEIPNIR_DLLEXPORT VariableMatrix Solve(const VariableMatrix &A, const VariableMatrix &B)
Solves the VariableMatrix equation AX = B for X.
SLEIPNIR_DLLEXPORT VariableMatrix CwiseReduce(const VariableMatrix &lhs, const VariableMatrix &rhs, function_ref< Variable(const Variable &x, const Variable &y)> binaryOp)
Applies a coefficient-wise reduce operation to two matrices.
Definition VariableMatrix.hpp:961
SLEIPNIR_DLLEXPORT VariableMatrix Block(std::initializer_list< std::initializer_list< VariableMatrix > > list)
Assemble a VariableMatrix from a nested list of blocks.
Definition VariableMatrix.hpp:988
#define Assert(condition)
Abort in C++.
Definition Assert.hpp:24