23template <
typename Mat>
34 if (
this == &values) {
38 if (m_mat ==
nullptr) {
40 m_rowSlice = values.m_rowSlice;
41 m_rowSliceLength = values.m_rowSliceLength;
42 m_colSlice = values.m_colSlice;
43 m_colSliceLength = values.m_colSliceLength;
48 for (
int row = 0; row <
Rows(); ++row) {
49 for (
int col = 0; col <
Cols(); ++col) {
50 (*this)(row, col) = values(row, col);
66 if (
this == &values) {
70 if (m_mat ==
nullptr) {
72 m_rowSlice = values.m_rowSlice;
73 m_rowSliceLength = values.m_rowSliceLength;
74 m_colSlice = values.m_colSlice;
75 m_colSliceLength = values.m_colSliceLength;
80 for (
int row = 0; row <
Rows(); ++row) {
81 for (
int col = 0; col <
Cols(); ++col) {
82 (*this)(row, col) = values(row, col);
97 m_rowSlice{0, mat.
Rows(), 1},
98 m_rowSliceLength{m_rowSlice.Adjust(mat.
Rows())},
99 m_colSlice{0, mat.
Cols(), 1},
100 m_colSliceLength{m_colSlice.Adjust(mat.
Cols())} {}
114 m_rowSlice{rowOffset, rowOffset + blockRows, 1},
115 m_rowSliceLength{m_rowSlice.Adjust(mat.
Rows())},
116 m_colSlice{colOffset, colOffset + blockCols, 1},
117 m_colSliceLength{m_colSlice.Adjust(mat.
Cols())} {}
133 m_rowSlice{
std::move(rowSlice)},
134 m_rowSliceLength{rowSliceLength},
135 m_colSlice{
std::move(colSlice)},
136 m_colSliceLength{colSliceLength} {}
146 (*this)(0, 0) = value;
169 template <
typename Derived>
174 for (
int row = 0; row <
Rows(); ++row) {
175 for (
int col = 0; col <
Cols(); ++col) {
176 (*this)(row, col) = values(row, col);
188 template <
typename Derived>
189 requires std::same_as<typename Derived::Scalar, double>
190 void SetValue(
const Eigen::MatrixBase<Derived>& values) {
194 for (
int row = 0; row <
Rows(); ++row) {
195 for (
int col = 0; col <
Cols(); ++col) {
196 (*this)(row, col).
SetValue(values(row, col));
210 for (
int row = 0; row <
Rows(); ++row) {
211 for (
int col = 0; col <
Cols(); ++col) {
212 (*this)(row, col) = values(row, col);
227 for (
int row = 0; row <
Rows(); ++row) {
228 for (
int col = 0; col <
Cols(); ++col) {
229 (*this)(row, col) = std::move(values(row, col));
242 requires(!std::is_const_v<Mat>)
246 return (*m_mat)(m_rowSlice.
start + row * m_rowSlice.
step,
247 m_colSlice.
start + col * m_colSlice.
step);
259 return (*m_mat)(m_rowSlice.
start + row * m_rowSlice.
step,
260 m_colSlice.
start + col * m_colSlice.
step);
269 requires(!std::is_const_v<Mat>)
272 return (*
this)(row /
Cols(), row %
Cols());
282 return (*
this)(row /
Cols(), row %
Cols());
295 Assert(rowOffset >= 0 && rowOffset <=
Rows());
296 Assert(colOffset >= 0 && colOffset <=
Cols());
297 Assert(blockRows >= 0 && blockRows <=
Rows() - rowOffset);
298 Assert(blockCols >= 0 && blockCols <=
Cols() - colOffset);
299 return (*
this)({rowOffset, rowOffset + blockRows, 1},
300 {colOffset, colOffset + blockCols, 1});
312 int blockRows,
int blockCols)
const {
313 Assert(rowOffset >= 0 && rowOffset <=
Rows());
314 Assert(colOffset >= 0 && colOffset <=
Cols());
315 Assert(blockRows >= 0 && blockRows <=
Rows() - rowOffset);
316 Assert(blockCols >= 0 && blockCols <=
Cols() - colOffset);
317 return (*
this)({rowOffset, rowOffset + blockRows, 1},
318 {colOffset, colOffset + blockCols, 1});
328 int rowSliceLength = rowSlice.
Adjust(m_rowSliceLength);
329 int colSliceLength = colSlice.
Adjust(m_colSliceLength);
347 Slice colSlice)
const {
348 int rowSliceLength = rowSlice.
Adjust(m_rowSliceLength);
349 int colSliceLength = colSlice.
Adjust(m_colSliceLength);
372 Slice colSlice,
int colSliceLength) {
396 int colSliceLength)
const {
416 return Block(offset, 0, length, 1);
428 return Block(offset, 0, length, 1);
479 for (
int i = 0; i <
Rows(); ++i) {
480 for (
int j = 0; j < rhs.
Cols(); ++j) {
482 for (
int k = 0; k <
Cols(); ++k) {
483 sum += (*this)(i, k) * rhs(k, j);
499 for (
int row = 0; row <
Rows(); ++row) {
500 for (
int col = 0; col <
Cols(); ++col) {
501 (*this)(row, col) *= rhs;
517 for (
int row = 0; row <
Rows(); ++row) {
518 for (
int col = 0; col <
Cols(); ++col) {
519 (*this)(row, col) /= rhs(0, 0);
533 for (
int row = 0; row <
Rows(); ++row) {
534 for (
int col = 0; col <
Cols(); ++col) {
535 (*this)(row, col) /= rhs;
548 for (
int row = 0; row <
Rows(); ++row) {
549 for (
int col = 0; col <
Cols(); ++col) {
550 (*this)(row, col) += rhs(row, col);
563 for (
int row = 0; row <
Rows(); ++row) {
564 for (
int col = 0; col <
Cols(); ++col) {
565 (*this)(row, col) -= rhs(row, col);
575 std::remove_cv_t<Mat>
T()
const {
576 std::remove_cv_t<Mat> result{
Cols(),
Rows()};
578 for (
int row = 0; row <
Rows(); ++row) {
579 for (
int col = 0; col <
Cols(); ++col) {
580 result(col, row) = (*this)(row, col);
590 int Rows()
const {
return m_rowSliceLength; }
595 int Cols()
const {
return m_colSliceLength; }
606 return (*m_mat)(m_rowSlice.
start + row * m_rowSlice.
step,
607 m_colSlice.
start + col * m_colSlice.
step)
625 Eigen::MatrixXd result{
Rows(),
Cols()};
627 for (
int row = 0; row <
Rows(); ++row) {
628 for (
int col = 0; col <
Cols(); ++col) {
629 result(row, col) =
Value(row, col);
643 std::remove_cv_t<Mat> result{
Rows(),
Cols()};
645 for (
int row = 0; row <
Rows(); ++row) {
646 for (
int col = 0; col <
Cols(); ++col) {
647 result(row, col) = unaryOp((*
this)(row, col));
663 : m_mat{mat}, m_row{row}, m_col{col} {}
667 if (m_col == m_mat->Cols()) {
696 : m_mat{mat}, m_row{row}, m_col{col} {}
700 if (m_col == m_mat->Cols()) {
756 Mat* m_mat =
nullptr;
759 int m_rowSliceLength = 0;
762 int m_colSliceLength = 0;
constexpr int Adjust(int length)
Adjusts start and end slice indices assuming a sequence of the specified length.
Definition Slice.hpp:118
int step
Step.
Definition Slice.hpp:30
int start
Start index (inclusive).
Definition Slice.hpp:24
int stop
Stop index (exclusive).
Definition Slice.hpp:27
Definition VariableBlock.hpp:687
const_iterator(const VariableBlock< Mat > *mat, int row, int col)
Definition VariableBlock.hpp:695
const_reference operator*() const
Definition VariableBlock.hpp:712
const_iterator operator++(int)
Definition VariableBlock.hpp:706
bool operator==(const const_iterator &) const =default
std::ptrdiff_t difference_type
Definition VariableBlock.hpp:691
const_iterator & operator++()
Definition VariableBlock.hpp:698
std::forward_iterator_tag iterator_category
Definition VariableBlock.hpp:689
Definition VariableBlock.hpp:654
reference operator*()
Definition VariableBlock.hpp:679
bool operator==(const iterator &) const =default
std::forward_iterator_tag iterator_category
Definition VariableBlock.hpp:656
iterator(VariableBlock< Mat > *mat, int row, int col)
Definition VariableBlock.hpp:662
std::ptrdiff_t difference_type
Definition VariableBlock.hpp:658
iterator & operator++()
Definition VariableBlock.hpp:665
iterator operator++(int)
Definition VariableBlock.hpp:673
A submatrix of autodiff variables with reference semantics.
Definition VariableBlock.hpp:24
int Rows() const
Returns number of rows in the matrix.
Definition VariableBlock.hpp:590
VariableBlock< const Mat > Col(int col) const
Returns a column slice of the variable matrix.
Definition VariableBlock.hpp:466
const VariableBlock< const Mat > Block(int rowOffset, int colOffset, int blockRows, int blockCols) const
Returns a block slice of the variable matrix.
Definition VariableBlock.hpp:311
const_iterator cend() const
Returns end iterator.
Definition VariableBlock.hpp:748
const VariableBlock< const Mat > operator()(Slice rowSlice, Slice colSlice) const
Returns a slice of the variable matrix.
Definition VariableBlock.hpp:346
const_iterator end() const
Returns end iterator.
Definition VariableBlock.hpp:738
iterator begin()
Returns begin iterator.
Definition VariableBlock.hpp:723
void SetValue(double value)
Assigns a double to the block.
Definition VariableBlock.hpp:158
VariableBlock(const VariableBlock< Mat > &values)=default
VariableBlock< const Mat > Row(int row) const
Returns a row slice of the variable matrix.
Definition VariableBlock.hpp:446
VariableBlock< Mat > & operator/=(const VariableBlock< Mat > &rhs)
Compound matrix division-assignment operator (only enabled when rhs is a scalar).
Definition VariableBlock.hpp:514
VariableBlock< Mat > Row(int row)
Returns a row slice of the variable matrix.
Definition VariableBlock.hpp:436
iterator end()
Returns end iterator.
Definition VariableBlock.hpp:728
size_t size() const
Returns number of elements in matrix.
Definition VariableBlock.hpp:753
VariableBlock(Mat &mat)
Constructs a Variable block pointing to all of the given matrix.
Definition VariableBlock.hpp:95
VariableBlock & operator*=(double rhs)
Compound matrix multiplication-assignment operator (only enabled when lhs is a scalar).
Definition VariableBlock.hpp:498
VariableBlock< Mat > & operator-=(const VariableBlock< Mat > &rhs)
Compound subtraction-assignment operator.
Definition VariableBlock.hpp:562
void SetValue(const Eigen::MatrixBase< Derived > &values)
Sets block's internal values.
Definition VariableBlock.hpp:190
const VariableBlock< Mat > Segment(int offset, int length) const
Returns a segment of the variable vector.
Definition VariableBlock.hpp:425
int Cols() const
Returns number of columns in the matrix.
Definition VariableBlock.hpp:595
VariableBlock< Mat > & operator=(const Eigen::MatrixBase< Derived > &values)
Assigns an Eigen matrix to the block.
Definition VariableBlock.hpp:170
const Variable & operator()(int row) const
Returns a scalar subblock at the given row.
Definition VariableBlock.hpp:280
VariableBlock< Mat > & operator=(VariableBlock< Mat > &&values)
Assigns a VariableBlock to the block.
Definition VariableBlock.hpp:65
const Variable & operator()(int row, int col) const
Returns a scalar subblock at the given row and column.
Definition VariableBlock.hpp:256
VariableBlock< Mat > & operator=(double value)
Assigns a double to the block.
Definition VariableBlock.hpp:143
VariableBlock< Mat > & operator/=(double rhs)
Compound matrix division-assignment operator (only enabled when rhs is a scalar).
Definition VariableBlock.hpp:532
std::remove_cv_t< Mat > T() const
Returns the transpose of the variable matrix.
Definition VariableBlock.hpp:575
Variable & operator()(int row)
Returns a scalar subblock at the given row.
Definition VariableBlock.hpp:268
const_iterator begin() const
Returns begin iterator.
Definition VariableBlock.hpp:733
VariableBlock(Mat &mat, Slice rowSlice, int rowSliceLength, Slice colSlice, int colSliceLength)
Constructs a Variable block pointing to a subset of the given matrix.
Definition VariableBlock.hpp:130
const_iterator cbegin() const
Returns begin iterator.
Definition VariableBlock.hpp:743
VariableBlock< Mat > operator()(Slice rowSlice, Slice colSlice)
Returns a slice of the variable matrix.
Definition VariableBlock.hpp:327
Variable & operator()(int row, int col)
Returns a scalar subblock at the given row and column.
Definition VariableBlock.hpp:241
VariableBlock< Mat > & operator=(const VariableBlock< Mat > &values)
Assigns a VariableBlock to the block.
Definition VariableBlock.hpp:33
double Value(int row, int col)
Returns an element of the variable matrix.
Definition VariableBlock.hpp:603
VariableBlock< Mat > Col(int col)
Returns a column slice of the variable matrix.
Definition VariableBlock.hpp:456
VariableBlock< Mat > & operator=(Mat &&values)
Assigns a VariableMatrix to the block.
Definition VariableBlock.hpp:223
VariableBlock< Mat > Block(int rowOffset, int colOffset, int blockRows, int blockCols)
Returns a block of the variable matrix.
Definition VariableBlock.hpp:293
Eigen::MatrixXd Value()
Returns the contents of the variable matrix.
Definition VariableBlock.hpp:624
VariableBlock< Mat > & operator*=(const VariableBlock< Mat > &rhs)
Compound matrix multiplication-assignment operator.
Definition VariableBlock.hpp:476
VariableBlock< Mat > Segment(int offset, int length)
Returns a segment of the variable vector.
Definition VariableBlock.hpp:413
VariableBlock< Mat > & operator+=(const VariableBlock< Mat > &rhs)
Compound addition-assignment operator.
Definition VariableBlock.hpp:547
const VariableBlock< const Mat > operator()(Slice rowSlice, int rowSliceLength, Slice colSlice, int colSliceLength) const
Returns a slice of the variable matrix.
Definition VariableBlock.hpp:394
std::remove_cv_t< Mat > CwiseTransform(function_ref< Variable(const Variable &x)> unaryOp) const
Transforms the matrix coefficient-wise with an unary operator.
Definition VariableBlock.hpp:641
double Value(int index)
Returns a row of the variable column vector.
Definition VariableBlock.hpp:616
VariableBlock< Mat > & operator=(const Mat &values)
Assigns a VariableMatrix to the block.
Definition VariableBlock.hpp:206
VariableBlock(VariableBlock< Mat > &&)=default
VariableBlock(Mat &mat, int rowOffset, int colOffset, int blockRows, int blockCols)
Constructs a Variable block pointing to a subset of the given matrix.
Definition VariableBlock.hpp:111
VariableBlock< Mat > operator()(Slice rowSlice, int rowSliceLength, Slice colSlice, int colSliceLength)
Returns a slice of the variable matrix.
Definition VariableBlock.hpp:371
An autodiff variable pointing to an expression node.
Definition Variable.hpp:31
An implementation of std::function_ref, a lightweight non-owning reference to a callable.
Definition FunctionRef.hpp:17
Definition Hessian.hpp:18
Implement std::hash so that hash_code can be used in STL containers.
Definition PointerIntPair.h:280
#define Assert(condition)
Abort in C++.
Definition Assert.hpp:24