WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
VariableBlock.hpp
Go to the documentation of this file.
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <concepts>
6#include <type_traits>
7#include <utility>
8
9#include <Eigen/Core>
10
15
16namespace sleipnir {
17
18/**
19 * A submatrix of autodiff variables with reference semantics.
20 *
21 * @tparam Mat The type of the matrix whose storage this class points to.
22 */
23template <typename Mat>
25 public:
26 VariableBlock(const VariableBlock<Mat>& values) = default;
27
28 /**
29 * Assigns a VariableBlock to the block.
30 *
31 * @param values VariableBlock of values.
32 */
34 if (this == &values) {
35 return *this;
36 }
37
38 if (m_mat == nullptr) {
39 m_mat = values.m_mat;
40 m_rowSlice = values.m_rowSlice;
41 m_rowSliceLength = values.m_rowSliceLength;
42 m_colSlice = values.m_colSlice;
43 m_colSliceLength = values.m_colSliceLength;
44 } else {
45 Assert(Rows() == values.Rows());
46 Assert(Cols() == values.Cols());
47
48 for (int row = 0; row < Rows(); ++row) {
49 for (int col = 0; col < Cols(); ++col) {
50 (*this)(row, col) = values(row, col);
51 }
52 }
53 }
54
55 return *this;
56 }
57
59
60 /**
61 * Assigns a VariableBlock to the block.
62 *
63 * @param values VariableBlock of values.
64 */
66 if (this == &values) {
67 return *this;
68 }
69
70 if (m_mat == nullptr) {
71 m_mat = values.m_mat;
72 m_rowSlice = values.m_rowSlice;
73 m_rowSliceLength = values.m_rowSliceLength;
74 m_colSlice = values.m_colSlice;
75 m_colSliceLength = values.m_colSliceLength;
76 } else {
77 Assert(Rows() == values.Rows());
78 Assert(Cols() == values.Cols());
79
80 for (int row = 0; row < Rows(); ++row) {
81 for (int col = 0; col < Cols(); ++col) {
82 (*this)(row, col) = values(row, col);
83 }
84 }
85 }
86
87 return *this;
88 }
89
90 /**
91 * Constructs a Variable block pointing to all of the given matrix.
92 *
93 * @param mat The matrix to which to point.
94 */
95 VariableBlock(Mat& mat) // NOLINT
96 : m_mat{&mat},
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())} {}
101
102 /**
103 * Constructs a Variable block pointing to a subset of the given matrix.
104 *
105 * @param mat The matrix to which to point.
106 * @param rowOffset The block's row offset.
107 * @param colOffset The block's column offset.
108 * @param blockRows The number of rows in the block.
109 * @param blockCols The number of columns in the block.
110 */
111 VariableBlock(Mat& mat, int rowOffset, int colOffset, int blockRows,
112 int blockCols)
113 : m_mat{&mat},
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())} {}
118
119 /**
120 * Constructs a Variable block pointing to a subset of the given matrix.
121 *
122 * Note that the slices are taken as is rather than adjusted.
123 *
124 * @param mat The matrix to which to point.
125 * @param rowSlice The block's row slice.
126 * @param rowSliceLength The block's row length.
127 * @param colSlice The block's column slice.
128 * @param colSliceLength The block's column length.
129 */
130 VariableBlock(Mat& mat, Slice rowSlice, int rowSliceLength, Slice colSlice,
131 int colSliceLength)
132 : m_mat{&mat},
133 m_rowSlice{std::move(rowSlice)},
134 m_rowSliceLength{rowSliceLength},
135 m_colSlice{std::move(colSlice)},
136 m_colSliceLength{colSliceLength} {}
137
138 /**
139 * Assigns a double to the block.
140 *
141 * This only works for blocks with one row and one column.
142 */
144 Assert(Rows() == 1 && Cols() == 1);
145
146 (*this)(0, 0) = value;
147
148 return *this;
149 }
150
151 /**
152 * Assigns a double to the block.
153 *
154 * This only works for blocks with one row and one column.
155 *
156 * @param value Value to assign.
157 */
158 void SetValue(double value) {
159 Assert(Rows() == 1 && Cols() == 1);
160
161 (*this)(0, 0).SetValue(value);
162 }
163
164 /**
165 * Assigns an Eigen matrix to the block.
166 *
167 * @param values Eigen matrix of values to assign.
168 */
169 template <typename Derived>
170 VariableBlock<Mat>& operator=(const Eigen::MatrixBase<Derived>& values) {
171 Assert(Rows() == values.rows());
172 Assert(Cols() == values.cols());
173
174 for (int row = 0; row < Rows(); ++row) {
175 for (int col = 0; col < Cols(); ++col) {
176 (*this)(row, col) = values(row, col);
177 }
178 }
179
180 return *this;
181 }
182
183 /**
184 * Sets block's internal values.
185 *
186 * @param values Eigen matrix of values.
187 */
188 template <typename Derived>
189 requires std::same_as<typename Derived::Scalar, double>
190 void SetValue(const Eigen::MatrixBase<Derived>& values) {
191 Assert(Rows() == values.rows());
192 Assert(Cols() == values.cols());
193
194 for (int row = 0; row < Rows(); ++row) {
195 for (int col = 0; col < Cols(); ++col) {
196 (*this)(row, col).SetValue(values(row, col));
197 }
198 }
199 }
200
201 /**
202 * Assigns a VariableMatrix to the block.
203 *
204 * @param values VariableMatrix of values.
205 */
206 VariableBlock<Mat>& operator=(const Mat& values) {
207 Assert(Rows() == values.Rows());
208 Assert(Cols() == values.Cols());
209
210 for (int row = 0; row < Rows(); ++row) {
211 for (int col = 0; col < Cols(); ++col) {
212 (*this)(row, col) = values(row, col);
213 }
214 }
215 return *this;
216 }
217
218 /**
219 * Assigns a VariableMatrix to the block.
220 *
221 * @param values VariableMatrix of values.
222 */
224 Assert(Rows() == values.Rows());
225 Assert(Cols() == values.Cols());
226
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));
230 }
231 }
232 return *this;
233 }
234
235 /**
236 * Returns a scalar subblock at the given row and column.
237 *
238 * @param row The scalar subblock's row.
239 * @param col The scalar subblock's column.
240 */
241 Variable& operator()(int row, int col)
242 requires(!std::is_const_v<Mat>)
243 {
244 Assert(row >= 0 && row < Rows());
245 Assert(col >= 0 && col < Cols());
246 return (*m_mat)(m_rowSlice.start + row * m_rowSlice.step,
247 m_colSlice.start + col * m_colSlice.step);
248 }
249
250 /**
251 * Returns a scalar subblock at the given row and column.
252 *
253 * @param row The scalar subblock's row.
254 * @param col The scalar subblock's column.
255 */
256 const Variable& operator()(int row, int col) const {
257 Assert(row >= 0 && row < Rows());
258 Assert(col >= 0 && col < Cols());
259 return (*m_mat)(m_rowSlice.start + row * m_rowSlice.step,
260 m_colSlice.start + col * m_colSlice.step);
261 }
262
263 /**
264 * Returns a scalar subblock at the given row.
265 *
266 * @param row The scalar subblock's row.
267 */
269 requires(!std::is_const_v<Mat>)
270 {
271 Assert(row >= 0 && row < Rows() * Cols());
272 return (*this)(row / Cols(), row % Cols());
273 }
274
275 /**
276 * Returns a scalar subblock at the given row.
277 *
278 * @param row The scalar subblock's row.
279 */
280 const Variable& operator()(int row) const {
281 Assert(row >= 0 && row < Rows() * Cols());
282 return (*this)(row / Cols(), row % Cols());
283 }
284
285 /**
286 * Returns a block of the variable matrix.
287 *
288 * @param rowOffset The row offset of the block selection.
289 * @param colOffset The column offset of the block selection.
290 * @param blockRows The number of rows in the block selection.
291 * @param blockCols The number of columns in the block selection.
292 */
293 VariableBlock<Mat> Block(int rowOffset, int colOffset, int blockRows,
294 int blockCols) {
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});
301 }
302
303 /**
304 * Returns a block slice of the variable matrix.
305 *
306 * @param rowOffset The row offset of the block selection.
307 * @param colOffset The column offset of the block selection.
308 * @param blockRows The number of rows in the block selection.
309 * @param blockCols The number of columns in the block selection.
310 */
311 const VariableBlock<const Mat> Block(int rowOffset, int colOffset,
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});
319 }
320
321 /**
322 * Returns a slice of the variable matrix.
323 *
324 * @param rowSlice The row slice.
325 * @param colSlice The column slice.
326 */
328 int rowSliceLength = rowSlice.Adjust(m_rowSliceLength);
329 int colSliceLength = colSlice.Adjust(m_colSliceLength);
330 return VariableBlock{
331 *m_mat,
332 {m_rowSlice.start + rowSlice.start * m_rowSlice.step,
333 m_rowSlice.start + rowSlice.stop, m_rowSlice.step * rowSlice.step},
334 rowSliceLength,
335 {m_colSlice.start + colSlice.start * m_colSlice.step,
336 m_colSlice.start + colSlice.stop, m_colSlice.step * colSlice.step},
337 colSliceLength};
338 }
339
340 /**
341 * Returns a slice of the variable matrix.
342 *
343 * @param rowSlice The row slice.
344 * @param colSlice The column slice.
345 */
347 Slice colSlice) const {
348 int rowSliceLength = rowSlice.Adjust(m_rowSliceLength);
349 int colSliceLength = colSlice.Adjust(m_colSliceLength);
350 return VariableBlock{
351 *m_mat,
352 {m_rowSlice.start + rowSlice.start * m_rowSlice.step,
353 m_rowSlice.start + rowSlice.stop, m_rowSlice.step * rowSlice.step},
354 rowSliceLength,
355 {m_colSlice.start + colSlice.start * m_colSlice.step,
356 m_colSlice.start + colSlice.stop, m_colSlice.step * colSlice.step},
357 colSliceLength};
358 }
359
360 /**
361 * Returns a slice of the variable matrix.
362 *
363 * The given slices aren't adjusted. This overload is for Python bindings
364 * only.
365 *
366 * @param rowSlice The row slice.
367 * @param rowSliceLength The row slice length.
368 * @param colSlice The column slice.
369 * @param colSliceLength The column slice length.
370 */
371 VariableBlock<Mat> operator()(Slice rowSlice, int rowSliceLength,
372 Slice colSlice, int colSliceLength) {
373 return VariableBlock{
374 *m_mat,
375 {m_rowSlice.start + rowSlice.start * m_rowSlice.step,
376 m_rowSlice.start + rowSlice.stop, m_rowSlice.step * rowSlice.step},
377 rowSliceLength,
378 {m_colSlice.start + colSlice.start * m_colSlice.step,
379 m_colSlice.start + colSlice.stop, m_colSlice.step * colSlice.step},
380 colSliceLength};
381 }
382
383 /**
384 * Returns a slice of the variable matrix.
385 *
386 * The given slices aren't adjusted. This overload is for Python bindings
387 * only.
388 *
389 * @param rowSlice The row slice.
390 * @param rowSliceLength The row slice length.
391 * @param colSlice The column slice.
392 * @param colSliceLength The column slice length.
393 */
394 const VariableBlock<const Mat> operator()(Slice rowSlice, int rowSliceLength,
395 Slice colSlice,
396 int colSliceLength) const {
397 return VariableBlock{
398 *m_mat,
399 {m_rowSlice.start + rowSlice.start * m_rowSlice.step,
400 m_rowSlice.start + rowSlice.stop, m_rowSlice.step * rowSlice.step},
401 rowSliceLength,
402 {m_colSlice.start + colSlice.start * m_colSlice.step,
403 m_colSlice.start + colSlice.stop, m_colSlice.step * colSlice.step},
404 colSliceLength};
405 }
406
407 /**
408 * Returns a segment of the variable vector.
409 *
410 * @param offset The offset of the segment.
411 * @param length The length of the segment.
412 */
413 VariableBlock<Mat> Segment(int offset, int length) {
414 Assert(offset >= 0 && offset < Rows() * Cols());
415 Assert(length >= 0 && length <= Rows() * Cols() - offset);
416 return Block(offset, 0, length, 1);
417 }
418
419 /**
420 * Returns a segment of the variable vector.
421 *
422 * @param offset The offset of the segment.
423 * @param length The length of the segment.
424 */
425 const VariableBlock<Mat> Segment(int offset, int length) const {
426 Assert(offset >= 0 && offset < Rows() * Cols());
427 Assert(length >= 0 && length <= Rows() * Cols() - offset);
428 return Block(offset, 0, length, 1);
429 }
430
431 /**
432 * Returns a row slice of the variable matrix.
433 *
434 * @param row The row to slice.
435 */
437 Assert(row >= 0 && row < Rows());
438 return Block(row, 0, 1, Cols());
439 }
440
441 /**
442 * Returns a row slice of the variable matrix.
443 *
444 * @param row The row to slice.
445 */
447 Assert(row >= 0 && row < Rows());
448 return Block(row, 0, 1, Cols());
449 }
450
451 /**
452 * Returns a column slice of the variable matrix.
453 *
454 * @param col The column to slice.
455 */
457 Assert(col >= 0 && col < Cols());
458 return Block(0, col, Rows(), 1);
459 }
460
461 /**
462 * Returns a column slice of the variable matrix.
463 *
464 * @param col The column to slice.
465 */
467 Assert(col >= 0 && col < Cols());
468 return Block(0, col, Rows(), 1);
469 }
470
471 /**
472 * Compound matrix multiplication-assignment operator.
473 *
474 * @param rhs Variable to multiply.
475 */
477 Assert(Cols() == rhs.Rows() && Cols() == rhs.Cols());
478
479 for (int i = 0; i < Rows(); ++i) {
480 for (int j = 0; j < rhs.Cols(); ++j) {
481 Variable sum;
482 for (int k = 0; k < Cols(); ++k) {
483 sum += (*this)(i, k) * rhs(k, j);
484 }
485 (*this)(i, j) = sum;
486 }
487 }
488
489 return *this;
490 }
491
492 /**
493 * Compound matrix multiplication-assignment operator (only enabled when lhs
494 * is a scalar).
495 *
496 * @param rhs Variable to multiply.
497 */
499 for (int row = 0; row < Rows(); ++row) {
500 for (int col = 0; col < Cols(); ++col) {
501 (*this)(row, col) *= rhs;
502 }
503 }
504
505 return *this;
506 }
507
508 /**
509 * Compound matrix division-assignment operator (only enabled when rhs
510 * is a scalar).
511 *
512 * @param rhs Variable to divide.
513 */
515 Assert(rhs.Rows() == 1 && rhs.Cols() == 1);
516
517 for (int row = 0; row < Rows(); ++row) {
518 for (int col = 0; col < Cols(); ++col) {
519 (*this)(row, col) /= rhs(0, 0);
520 }
521 }
522
523 return *this;
524 }
525
526 /**
527 * Compound matrix division-assignment operator (only enabled when rhs
528 * is a scalar).
529 *
530 * @param rhs Variable to divide.
531 */
533 for (int row = 0; row < Rows(); ++row) {
534 for (int col = 0; col < Cols(); ++col) {
535 (*this)(row, col) /= rhs;
536 }
537 }
538
539 return *this;
540 }
541
542 /**
543 * Compound addition-assignment operator.
544 *
545 * @param rhs Variable to add.
546 */
548 for (int row = 0; row < Rows(); ++row) {
549 for (int col = 0; col < Cols(); ++col) {
550 (*this)(row, col) += rhs(row, col);
551 }
552 }
553
554 return *this;
555 }
556
557 /**
558 * Compound subtraction-assignment operator.
559 *
560 * @param rhs Variable to subtract.
561 */
563 for (int row = 0; row < Rows(); ++row) {
564 for (int col = 0; col < Cols(); ++col) {
565 (*this)(row, col) -= rhs(row, col);
566 }
567 }
568
569 return *this;
570 }
571
572 /**
573 * Returns the transpose of the variable matrix.
574 */
575 std::remove_cv_t<Mat> T() const {
576 std::remove_cv_t<Mat> result{Cols(), Rows()};
577
578 for (int row = 0; row < Rows(); ++row) {
579 for (int col = 0; col < Cols(); ++col) {
580 result(col, row) = (*this)(row, col);
581 }
582 }
583
584 return result;
585 }
586
587 /**
588 * Returns number of rows in the matrix.
589 */
590 int Rows() const { return m_rowSliceLength; }
591
592 /**
593 * Returns number of columns in the matrix.
594 */
595 int Cols() const { return m_colSliceLength; }
596
597 /**
598 * Returns an element of the variable matrix.
599 *
600 * @param row The row of the element to return.
601 * @param col The column of the element to return.
602 */
603 double Value(int row, int col) {
604 Assert(row >= 0 && row < Rows());
605 Assert(col >= 0 && col < Cols());
606 return (*m_mat)(m_rowSlice.start + row * m_rowSlice.step,
607 m_colSlice.start + col * m_colSlice.step)
608 .Value();
609 }
610
611 /**
612 * Returns a row of the variable column vector.
613 *
614 * @param index The index of the element to return.
615 */
616 double Value(int index) {
617 Assert(index >= 0 && index < Rows() * Cols());
618 return Value(index / Cols(), index % Cols());
619 }
620
621 /**
622 * Returns the contents of the variable matrix.
623 */
624 Eigen::MatrixXd Value() {
625 Eigen::MatrixXd result{Rows(), Cols()};
626
627 for (int row = 0; row < Rows(); ++row) {
628 for (int col = 0; col < Cols(); ++col) {
629 result(row, col) = Value(row, col);
630 }
631 }
632
633 return result;
634 }
635
636 /**
637 * Transforms the matrix coefficient-wise with an unary operator.
638 *
639 * @param unaryOp The unary operator to use for the transform operation.
640 */
641 std::remove_cv_t<Mat> CwiseTransform(
642 function_ref<Variable(const Variable& x)> unaryOp) const {
643 std::remove_cv_t<Mat> result{Rows(), Cols()};
644
645 for (int row = 0; row < Rows(); ++row) {
646 for (int col = 0; col < Cols(); ++col) {
647 result(row, col) = unaryOp((*this)(row, col));
648 }
649 }
650
651 return result;
652 }
653
654 class iterator {
655 public:
656 using iterator_category = std::forward_iterator_tag;
658 using difference_type = std::ptrdiff_t;
661
662 iterator(VariableBlock<Mat>* mat, int row, int col)
663 : m_mat{mat}, m_row{row}, m_col{col} {}
664
666 ++m_col;
667 if (m_col == m_mat->Cols()) {
668 m_col = 0;
669 ++m_row;
670 }
671 return *this;
672 }
674 iterator retval = *this;
675 ++(*this);
676 return retval;
677 }
678 bool operator==(const iterator&) const = default;
679 reference operator*() { return (*m_mat)(m_row, m_col); }
680
681 private:
682 VariableBlock<Mat>* m_mat;
683 int m_row;
684 int m_col;
685 };
686
688 public:
689 using iterator_category = std::forward_iterator_tag;
691 using difference_type = std::ptrdiff_t;
693 using const_reference = const Variable&;
694
695 const_iterator(const VariableBlock<Mat>* mat, int row, int col)
696 : m_mat{mat}, m_row{row}, m_col{col} {}
697
699 ++m_col;
700 if (m_col == m_mat->Cols()) {
701 m_col = 0;
702 ++m_row;
703 }
704 return *this;
705 }
707 const_iterator retval = *this;
708 ++(*this);
709 return retval;
710 }
711 bool operator==(const const_iterator&) const = default;
712 const_reference operator*() const { return (*m_mat)(m_row, m_col); }
713
714 private:
715 const VariableBlock<Mat>* m_mat;
716 int m_row;
717 int m_col;
718 };
719
720 /**
721 * Returns begin iterator.
722 */
723 iterator begin() { return iterator(this, 0, 0); }
724
725 /**
726 * Returns end iterator.
727 */
728 iterator end() { return iterator(this, Rows(), 0); }
729
730 /**
731 * Returns begin iterator.
732 */
733 const_iterator begin() const { return const_iterator(this, 0, 0); }
734
735 /**
736 * Returns end iterator.
737 */
738 const_iterator end() const { return const_iterator(this, Rows(), 0); }
739
740 /**
741 * Returns begin iterator.
742 */
743 const_iterator cbegin() const { return const_iterator(this, 0, 0); }
744
745 /**
746 * Returns end iterator.
747 */
748 const_iterator cend() const { return const_iterator(this, Rows(), 0); }
749
750 /**
751 * Returns number of elements in matrix.
752 */
753 size_t size() const { return Rows() * Cols(); }
754
755 private:
756 Mat* m_mat = nullptr;
757
758 Slice m_rowSlice;
759 int m_rowSliceLength = 0;
760
761 Slice m_colSlice;
762 int m_colSliceLength = 0;
763};
764
765} // namespace sleipnir
Definition Slice.hpp:21
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