WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
variable_block.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
17
18namespace slp {
19
20/// A submatrix of autodiff variables with reference semantics.
21///
22/// @tparam Mat The type of the matrix whose storage this class points to.
23template <typename Mat>
25 public:
26 /// Scalar type alias.
27 using Scalar = typename Mat::Scalar;
28
29 /// Copy constructor.
31
32 /// Assigns a VariableBlock to the block.
33 ///
34 /// @param values VariableBlock of values.
35 /// @return This VariableBlock.
37 if (this == &values) {
38 return *this;
39 }
40
41 if (m_mat == nullptr) {
42 m_mat = values.m_mat;
43 m_row_slice = values.m_row_slice;
44 m_row_slice_length = values.m_row_slice_length;
45 m_col_slice = values.m_col_slice;
46 m_col_slice_length = values.m_col_slice_length;
47 } else {
48 slp_assert(rows() == values.rows() && cols() == values.cols());
49
50 for (int row = 0; row < rows(); ++row) {
51 for (int col = 0; col < cols(); ++col) {
52 (*this)(row, col) = values(row, col);
53 }
54 }
55 }
56
57 return *this;
58 }
59
60 /// Move constructor.
62
63 /// Assigns a VariableBlock to the block.
64 ///
65 /// @param values VariableBlock of values.
66 /// @return This VariableBlock.
68 if (this == &values) {
69 return *this;
70 }
71
72 if (m_mat == nullptr) {
73 m_mat = values.m_mat;
74 m_row_slice = values.m_row_slice;
75 m_row_slice_length = values.m_row_slice_length;
76 m_col_slice = values.m_col_slice;
77 m_col_slice_length = values.m_col_slice_length;
78 } else {
79 slp_assert(rows() == values.rows() && cols() == values.cols());
80
81 for (int row = 0; row < rows(); ++row) {
82 for (int col = 0; col < cols(); ++col) {
83 (*this)(row, col) = values(row, col);
84 }
85 }
86 }
87
88 return *this;
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 // NOLINTNEXTLINE (google-explicit-constructor)
95 VariableBlock(Mat& mat) : VariableBlock{mat, 0, 0, mat.rows(), mat.cols()} {}
96
97 /// Constructs a Variable block pointing to a subset of the given matrix.
98 ///
99 /// @param mat The matrix to which to point.
100 /// @param row_offset The block's row offset.
101 /// @param col_offset The block's column offset.
102 /// @param block_rows The number of rows in the block.
103 /// @param block_cols The number of columns in the block.
104 VariableBlock(Mat& mat, int row_offset, int col_offset, int block_rows,
105 int block_cols)
106 : m_mat{&mat},
107 m_row_slice{row_offset, row_offset + block_rows, 1},
108 m_row_slice_length{m_row_slice.adjust(mat.rows())},
109 m_col_slice{col_offset, col_offset + block_cols, 1},
110 m_col_slice_length{m_col_slice.adjust(mat.cols())} {}
111
112 /// Constructs a Variable block pointing to a subset of the given matrix.
113 ///
114 /// Note that the slices are taken as is rather than adjusted.
115 ///
116 /// @param mat The matrix to which to point.
117 /// @param row_slice The block's row slice.
118 /// @param row_slice_length The block's row length.
119 /// @param col_slice The block's column slice.
120 /// @param col_slice_length The block's column length.
121 VariableBlock(Mat& mat, Slice row_slice, int row_slice_length,
122 Slice col_slice, int col_slice_length)
123 : m_mat{&mat},
124 m_row_slice{std::move(row_slice)},
125 m_row_slice_length{row_slice_length},
126 m_col_slice{std::move(col_slice)},
127 m_col_slice_length{col_slice_length} {}
128
129 /// Assigns a scalar to the block.
130 ///
131 /// This only works for blocks with one row and one column.
132 ///
133 /// @param value Value to assign.
134 /// @return This VariableBlock.
136 slp_assert(rows() == 1 && cols() == 1);
137
138 (*this)(0, 0) = value;
139
140 return *this;
141 }
142
143 /// Assigns a scalar to the block.
144 ///
145 /// This only works for blocks with one row and one column.
146 ///
147 /// @param value Value to assign.
149 slp_assert(rows() == 1 && cols() == 1);
150
151 (*this)(0, 0).set_value(value);
152 }
153
154 /// Assigns an Eigen matrix to the block.
155 ///
156 /// @param values Eigen matrix of values to assign.
157 /// @return This VariableBlock.
158 template <typename Derived>
159 VariableBlock<Mat>& operator=(const Eigen::MatrixBase<Derived>& values) {
160 slp_assert(rows() == values.rows() && cols() == values.cols());
161
162 for (int row = 0; row < rows(); ++row) {
163 for (int col = 0; col < cols(); ++col) {
164 (*this)(row, col) = values(row, col);
165 }
166 }
167
168 return *this;
169 }
170
171 /// Sets block's internal values.
172 ///
173 /// @param values Eigen matrix of values.
174 template <typename Derived>
175 requires std::same_as<typename Derived::Scalar, Scalar>
176 void set_value(const Eigen::MatrixBase<Derived>& values) {
177 slp_assert(rows() == values.rows() && cols() == values.cols());
178
179 for (int row = 0; row < rows(); ++row) {
180 for (int col = 0; col < cols(); ++col) {
181 (*this)(row, col).set_value(values(row, col));
182 }
183 }
184 }
185
186 /// Assigns a VariableMatrix to the block.
187 ///
188 /// @param values VariableMatrix of values.
189 /// @return This VariableBlock.
190 VariableBlock<Mat>& operator=(const Mat& values) {
191 slp_assert(rows() == values.rows() && cols() == values.cols());
192
193 for (int row = 0; row < rows(); ++row) {
194 for (int col = 0; col < cols(); ++col) {
195 (*this)(row, col) = values(row, col);
196 }
197 }
198 return *this;
199 }
200
201 /// Assigns a VariableMatrix to the block.
202 ///
203 /// @param values VariableMatrix of values.
204 /// @return This VariableBlock.
206 slp_assert(rows() == values.rows() && cols() == values.cols());
207
208 for (int row = 0; row < rows(); ++row) {
209 for (int col = 0; col < cols(); ++col) {
210 (*this)(row, col) = std::move(values(row, col));
211 }
212 }
213 return *this;
214 }
215
216 /// Returns a scalar subblock at the given row and column.
217 ///
218 /// @param row The scalar subblock's row.
219 /// @param col The scalar subblock's column.
220 /// @return A scalar subblock at the given row and column.
222 requires(!std::is_const_v<Mat>)
223 {
224 slp_assert(row >= 0 && row < rows());
225 slp_assert(col >= 0 && col < cols());
226 return (*m_mat)(m_row_slice.start + row * m_row_slice.step,
227 m_col_slice.start + col * m_col_slice.step);
228 }
229
230 /// Returns a scalar subblock at the given row and column.
231 ///
232 /// @param row The scalar subblock's row.
233 /// @param col The scalar subblock's column.
234 /// @return A scalar subblock at the given row and column.
235 const Variable<Scalar>& operator()(int row, int col) const {
236 slp_assert(row >= 0 && row < rows());
237 slp_assert(col >= 0 && col < cols());
238 return (*m_mat)(m_row_slice.start + row * m_row_slice.step,
239 m_col_slice.start + col * m_col_slice.step);
240 }
241
242 /// Returns a scalar subblock at the given index.
243 ///
244 /// @param index The scalar subblock's index.
245 /// @return A scalar subblock at the given index.
247 requires(!std::is_const_v<Mat>)
248 {
249 slp_assert(index >= 0 && index < rows() * cols());
250 return (*this)(index / cols(), index % cols());
251 }
252
253 /// Returns a scalar subblock at the given index.
254 ///
255 /// @param index The scalar subblock's index.
256 /// @return A scalar subblock at the given index.
258 slp_assert(index >= 0 && index < rows() * cols());
259 return (*this)(index / cols(), index % cols());
260 }
261
262 /// Returns a block of the variable matrix.
263 ///
264 /// @param row_offset The row offset of the block selection.
265 /// @param col_offset The column offset of the block selection.
266 /// @param block_rows The number of rows in the block selection.
267 /// @param block_cols The number of columns in the block selection.
268 /// @return A block of the variable matrix.
269 VariableBlock<Mat> block(int row_offset, int col_offset, int block_rows,
270 int block_cols) {
271 slp_assert(row_offset >= 0 && row_offset <= rows());
272 slp_assert(col_offset >= 0 && col_offset <= cols());
273 slp_assert(block_rows >= 0 && block_rows <= rows() - row_offset);
274 slp_assert(block_cols >= 0 && block_cols <= cols() - col_offset);
275 return (*this)({row_offset, row_offset + block_rows, 1},
276 {col_offset, col_offset + block_cols, 1});
277 }
278
279 /// Returns a block slice of the variable matrix.
280 ///
281 /// @param row_offset The row offset of the block selection.
282 /// @param col_offset The column offset of the block selection.
283 /// @param block_rows The number of rows in the block selection.
284 /// @param block_cols The number of columns in the block selection.
285 /// @return A block slice of the variable matrix.
286 const VariableBlock<const Mat> block(int row_offset, int col_offset,
287 int block_rows, int block_cols) const {
288 slp_assert(row_offset >= 0 && row_offset <= rows());
289 slp_assert(col_offset >= 0 && col_offset <= cols());
290 slp_assert(block_rows >= 0 && block_rows <= rows() - row_offset);
291 slp_assert(block_cols >= 0 && block_cols <= cols() - col_offset);
292 return (*this)({row_offset, row_offset + block_rows, 1},
293 {col_offset, col_offset + block_cols, 1});
294 }
295
296 /// Returns a slice of the variable matrix.
297 ///
298 /// @param row_slice The row slice.
299 /// @param col_slice The column slice.
300 /// @return A slice of the variable matrix.
301 VariableBlock<Mat> operator()(Slice row_slice, Slice col_slice) {
302 int row_slice_length = row_slice.adjust(m_row_slice_length);
303 int col_slice_length = col_slice.adjust(m_col_slice_length);
304 return (*this)(row_slice, row_slice_length, col_slice, col_slice_length);
305 }
306
307 /// Returns a slice of the variable matrix.
308 ///
309 /// @param row_slice The row slice.
310 /// @param col_slice The column slice.
311 /// @return A slice of the variable matrix.
313 Slice col_slice) const {
314 int row_slice_length = row_slice.adjust(m_row_slice_length);
315 int col_slice_length = col_slice.adjust(m_col_slice_length);
316 return (*this)(row_slice, row_slice_length, col_slice, col_slice_length);
317 }
318
319 /// Returns a slice of the variable matrix.
320 ///
321 /// The given slices aren't adjusted. This overload is for Python bindings
322 /// only.
323 ///
324 /// @param row_slice The row slice.
325 /// @param row_slice_length The row slice length.
326 /// @param col_slice The column slice.
327 /// @param col_slice_length The column slice length.
328 /// @return A slice of the variable matrix.
329 VariableBlock<Mat> operator()(Slice row_slice, int row_slice_length,
330 Slice col_slice, int col_slice_length) {
331 return VariableBlock{
332 *m_mat,
333 {m_row_slice.start + row_slice.start * m_row_slice.step,
334 m_row_slice.start + row_slice.stop * m_row_slice.step,
335 row_slice.step * m_row_slice.step},
336 row_slice_length,
337 {m_col_slice.start + col_slice.start * m_col_slice.step,
338 m_col_slice.start + col_slice.stop * m_col_slice.step,
339 col_slice.step * m_col_slice.step},
340 col_slice_length};
341 }
342
343 /// Returns a slice of the variable matrix.
344 ///
345 /// The given slices aren't adjusted. This overload is for Python bindings
346 /// only.
347 ///
348 /// @param row_slice The row slice.
349 /// @param row_slice_length The row slice length.
350 /// @param col_slice The column slice.
351 /// @param col_slice_length The column slice length.
352 /// @return A slice of the variable matrix.
354 int row_slice_length,
355 Slice col_slice,
356 int col_slice_length) const {
357 return VariableBlock{
358 *m_mat,
359 {m_row_slice.start + row_slice.start * m_row_slice.step,
360 m_row_slice.start + row_slice.stop * m_row_slice.step,
361 row_slice.step * m_row_slice.step},
362 row_slice_length,
363 {m_col_slice.start + col_slice.start * m_col_slice.step,
364 m_col_slice.start + col_slice.stop * m_col_slice.step,
365 col_slice.step * m_col_slice.step},
366 col_slice_length};
367 }
368
369 /// Returns a segment of the variable vector.
370 ///
371 /// @param offset The offset of the segment.
372 /// @param length The length of the segment.
373 /// @return A segment of the variable vector.
374 VariableBlock<Mat> segment(int offset, int length) {
375 slp_assert(cols() == 1);
376 slp_assert(offset >= 0 && offset < rows());
377 slp_assert(length >= 0 && length <= rows() - offset);
378 return block(offset, 0, length, 1);
379 }
380
381 /// Returns a segment of the variable vector.
382 ///
383 /// @param offset The offset of the segment.
384 /// @param length The length of the segment.
385 /// @return A segment of the variable vector.
386 const VariableBlock<Mat> segment(int offset, int length) const {
387 slp_assert(cols() == 1);
388 slp_assert(offset >= 0 && offset < rows());
389 slp_assert(length >= 0 && length <= rows() - offset);
390 return block(offset, 0, length, 1);
391 }
392
393 /// Returns a row slice of the variable matrix.
394 ///
395 /// @param row The row to slice.
396 /// @return A row slice of the variable matrix.
398 slp_assert(row >= 0 && row < rows());
399 return block(row, 0, 1, cols());
400 }
401
402 /// Returns a row slice of the variable matrix.
403 ///
404 /// @param row The row to slice.
405 /// @return A row slice of the variable matrix.
407 slp_assert(row >= 0 && row < rows());
408 return block(row, 0, 1, cols());
409 }
410
411 /// Returns a column slice of the variable matrix.
412 ///
413 /// @param col The column to slice.
414 /// @return A column slice of the variable matrix.
416 slp_assert(col >= 0 && col < cols());
417 return block(0, col, rows(), 1);
418 }
419
420 /// Returns a column slice of the variable matrix.
421 ///
422 /// @param col The column to slice.
423 /// @return A column slice of the variable matrix.
425 slp_assert(col >= 0 && col < cols());
426 return block(0, col, rows(), 1);
427 }
428
429 /// Compound matrix multiplication-assignment operator.
430 ///
431 /// @param rhs Variable to multiply.
432 /// @return Result of multiplication.
434 slp_assert(cols() == rhs.rows() && cols() == rhs.cols());
435
436 for (int i = 0; i < rows(); ++i) {
437 for (int j = 0; j < rhs.cols(); ++j) {
438 Variable sum{Scalar(0)};
439 for (int k = 0; k < cols(); ++k) {
440 sum += (*this)(i, k) * rhs(k, j);
441 }
442 (*this)(i, j) = sum;
443 }
444 }
445
446 return *this;
447 }
448
449 /// Compound matrix multiplication-assignment operator.
450 ///
451 /// @param rhs Variable to multiply.
452 /// @return Result of multiplication.
454 for (int row = 0; row < rows(); ++row) {
455 for (int col = 0; col < cols(); ++col) {
456 (*this)(row, col) *= rhs;
457 }
458 }
459
460 return *this;
461 }
462
463 /// Compound matrix division-assignment operator.
464 ///
465 /// @param rhs Variable to divide.
466 /// @return Result of division.
468 slp_assert(rhs.rows() == 1 && rhs.cols() == 1);
469
470 for (int row = 0; row < rows(); ++row) {
471 for (int col = 0; col < cols(); ++col) {
472 (*this)(row, col) /= rhs(0, 0);
473 }
474 }
475
476 return *this;
477 }
478
479 /// Compound matrix division-assignment operator.
480 ///
481 /// @param rhs Variable to divide.
482 /// @return Result of division.
484 for (int row = 0; row < rows(); ++row) {
485 for (int col = 0; col < cols(); ++col) {
486 (*this)(row, col) /= rhs;
487 }
488 }
489
490 return *this;
491 }
492
493 /// Compound addition-assignment operator.
494 ///
495 /// @param rhs Variable to add.
496 /// @return Result of addition.
498 slp_assert(rows() == rhs.rows() && cols() == rhs.cols());
499
500 for (int row = 0; row < rows(); ++row) {
501 for (int col = 0; col < cols(); ++col) {
502 (*this)(row, col) += rhs(row, col);
503 }
504 }
505
506 return *this;
507 }
508
509 /// Compound addition-assignment operator.
510 ///
511 /// @param rhs Variable to add.
512 /// @return Result of addition.
514 slp_assert(rows() == 1 && cols() == 1);
515
516 for (int row = 0; row < rows(); ++row) {
517 for (int col = 0; col < cols(); ++col) {
518 (*this)(row, col) += rhs;
519 }
520 }
521
522 return *this;
523 }
524
525 /// Compound subtraction-assignment operator.
526 ///
527 /// @param rhs Variable to subtract.
528 /// @return Result of subtraction.
530 slp_assert(rows() == rhs.rows() && cols() == rhs.cols());
531
532 for (int row = 0; row < rows(); ++row) {
533 for (int col = 0; col < cols(); ++col) {
534 (*this)(row, col) -= rhs(row, col);
535 }
536 }
537
538 return *this;
539 }
540
541 /// Compound subtraction-assignment operator.
542 ///
543 /// @param rhs Variable to subtract.
544 /// @return Result of subtraction.
546 slp_assert(rows() == 1 && cols() == 1);
547
548 for (int row = 0; row < rows(); ++row) {
549 for (int col = 0; col < cols(); ++col) {
550 (*this)(row, col) -= rhs;
551 }
552 }
553
554 return *this;
555 }
556
557 /// Implicit conversion operator from 1x1 VariableBlock to Variable.
558 // NOLINTNEXTLINE (google-explicit-constructor)
559 operator Variable<Scalar>() const {
560 slp_assert(rows() == 1 && cols() == 1);
561 return (*this)(0, 0);
562 }
563
564 /// Returns the transpose of the variable matrix.
565 ///
566 /// @return The transpose of the variable matrix.
567 std::remove_cv_t<Mat> T() const {
568 std::remove_cv_t<Mat> result{detail::empty, cols(), rows()};
569
570 for (int row = 0; row < rows(); ++row) {
571 for (int col = 0; col < cols(); ++col) {
572 result(col, row) = (*this)(row, col);
573 }
574 }
575
576 return result;
577 }
578
579 /// Returns the number of rows in the matrix.
580 ///
581 /// @return The number of rows in the matrix.
582 int rows() const { return m_row_slice_length; }
583
584 /// Returns the number of columns in the matrix.
585 ///
586 /// @return The number of columns in the matrix.
587 int cols() const { return m_col_slice_length; }
588
589 /// Returns an element of the variable matrix.
590 ///
591 /// @param row The row of the element to return.
592 /// @param col The column of the element to return.
593 /// @return An element of the variable matrix.
594 Scalar value(int row, int col) { return (*this)(row, col).value(); }
595
596 /// Returns an element of the variable block.
597 ///
598 /// @param index The index of the element to return.
599 /// @return An element of the variable block.
601 slp_assert(index >= 0 && index < rows() * cols());
602 return value(index / cols(), index % cols());
603 }
604
605 /// Returns the contents of the variable matrix.
606 ///
607 /// @return The contents of the variable matrix.
608 Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> value() {
609 Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> result{rows(),
610 cols()};
611
612 for (int row = 0; row < rows(); ++row) {
613 for (int col = 0; col < cols(); ++col) {
614 result(row, col) = value(row, col);
615 }
616 }
617
618 return result;
619 }
620
621 /// Transforms the matrix coefficient-wise with an unary operator.
622 ///
623 /// @param unary_op The unary operator to use for the transform operation.
624 /// @return Result of the unary operator.
625 std::remove_cv_t<Mat> cwise_transform(
627 const {
628 std::remove_cv_t<Mat> result{detail::empty, rows(), cols()};
629
630 for (int row = 0; row < rows(); ++row) {
631 for (int col = 0; col < cols(); ++col) {
632 result(row, col) = unary_op((*this)(row, col));
633 }
634 }
635
636 return result;
637 }
638
639#ifndef DOXYGEN_SHOULD_SKIP_THIS
640
641 class iterator {
642 public:
643 using iterator_category = std::bidirectional_iterator_tag;
645 using difference_type = std::ptrdiff_t;
648
649 constexpr iterator() noexcept = default;
650
651 constexpr iterator(VariableBlock<Mat>* mat, int index) noexcept
652 : m_mat{mat}, m_index{index} {}
653
654 constexpr iterator& operator++() noexcept {
655 ++m_index;
656 return *this;
657 }
658
659 constexpr iterator operator++(int) noexcept {
660 iterator retval = *this;
661 ++(*this);
662 return retval;
663 }
664
665 constexpr iterator& operator--() noexcept {
666 --m_index;
667 return *this;
668 }
669
670 constexpr iterator operator--(int) noexcept {
671 iterator retval = *this;
672 --(*this);
673 return retval;
674 }
675
676 constexpr bool operator==(const iterator&) const noexcept = default;
677
678 constexpr reference operator*() const noexcept { return (*m_mat)[m_index]; }
679
680 private:
681 VariableBlock<Mat>* m_mat = nullptr;
682 int m_index = 0;
683 };
684
686 public:
687 using iterator_category = std::bidirectional_iterator_tag;
689 using difference_type = std::ptrdiff_t;
692
693 constexpr const_iterator() noexcept = default;
694
695 constexpr const_iterator(const VariableBlock<Mat>* mat, int index) noexcept
696 : m_mat{mat}, m_index{index} {}
697
698 constexpr const_iterator& operator++() noexcept {
699 ++m_index;
700 return *this;
701 }
702
703 constexpr const_iterator operator++(int) noexcept {
704 const_iterator retval = *this;
705 ++(*this);
706 return retval;
707 }
708
709 constexpr const_iterator& operator--() noexcept {
710 --m_index;
711 return *this;
712 }
713
714 constexpr const_iterator operator--(int) noexcept {
715 iterator retval = *this;
716 --(*this);
717 return retval;
718 }
719
720 constexpr bool operator==(const const_iterator&) const noexcept = default;
721
722 constexpr const_reference operator*() const noexcept {
723 return (*m_mat)[m_index];
724 }
725
726 private:
727 const VariableBlock<Mat>* m_mat = nullptr;
728 int m_index = 0;
729 };
730
731 using reverse_iterator = std::reverse_iterator<iterator>;
732 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
733
734#endif // DOXYGEN_SHOULD_SKIP_THIS
735
736 /// Returns begin iterator.
737 ///
738 /// @return Begin iterator.
739 iterator begin() { return iterator(this, 0); }
740
741 /// Returns end iterator.
742 ///
743 /// @return End iterator.
744 iterator end() { return iterator(this, rows() * cols()); }
745
746 /// Returns begin iterator.
747 ///
748 /// @return Begin iterator.
749 const_iterator begin() const { return const_iterator(this, 0); }
750
751 /// Returns end iterator.
752 ///
753 /// @return End iterator.
754 const_iterator end() const { return const_iterator(this, rows() * cols()); }
755
756 /// Returns const begin iterator.
757 ///
758 /// @return Const begin iterator.
759 const_iterator cbegin() const { return const_iterator(this, 0); }
760
761 /// Returns const end iterator.
762 ///
763 /// @return Const end iterator.
764 const_iterator cend() const { return const_iterator(this, rows() * cols()); }
765
766 /// Returns reverse begin iterator.
767 ///
768 /// @return Reverse begin iterator.
770
771 /// Returns reverse end iterator.
772 ///
773 /// @return Reverse end iterator.
775
776 /// Returns const reverse begin iterator.
777 ///
778 /// @return Const reverse begin iterator.
782
783 /// Returns const reverse end iterator.
784 ///
785 /// @return Const reverse end iterator.
789
790 /// Returns const reverse begin iterator.
791 ///
792 /// @return Const reverse begin iterator.
796
797 /// Returns const reverse end iterator.
798 ///
799 /// @return Const reverse end iterator.
803
804 /// Returns number of elements in matrix.
805 ///
806 /// @return Number of elements in matrix.
807 size_t size() const { return rows() * cols(); }
808
809 private:
810 Mat* m_mat = nullptr;
811
812 Slice m_row_slice;
813 int m_row_slice_length = 0;
814
815 Slice m_col_slice;
816 int m_col_slice_length = 0;
817};
818
819} // namespace slp
#define slp_assert(condition)
Abort in C++.
Definition assert.hpp:25
@ index
Definition base.h:690
Marker interface for concepts to determine whether a given scalar or matrix type belongs to Sleipnir.
Definition sleipnir_base.hpp:9
Represents a sequence of elements in an iterable object.
Definition slice.hpp:25
int step
Step.
Definition slice.hpp:34
int stop
Stop index (exclusive).
Definition slice.hpp:31
constexpr int adjust(int length)
Adjusts start and end slice indices assuming a sequence of the specified length.
Definition slice.hpp:118
int start
Start index (inclusive).
Definition slice.hpp:28
Definition variable_block.hpp:685
Variable< Scalar > * pointer
Definition variable_block.hpp:690
constexpr bool operator==(const const_iterator &) const noexcept=default
constexpr const_iterator() noexcept=default
Variable< Scalar > value_type
Definition variable_block.hpp:688
std::bidirectional_iterator_tag iterator_category
Definition variable_block.hpp:687
constexpr const_iterator & operator--() noexcept
Definition variable_block.hpp:709
const Variable< Scalar > & const_reference
Definition variable_block.hpp:691
constexpr const_reference operator*() const noexcept
Definition variable_block.hpp:722
std::ptrdiff_t difference_type
Definition variable_block.hpp:689
constexpr const_iterator operator--(int) noexcept
Definition variable_block.hpp:714
constexpr const_iterator operator++(int) noexcept
Definition variable_block.hpp:703
constexpr const_iterator & operator++() noexcept
Definition variable_block.hpp:698
Definition variable_block.hpp:641
Variable< Scalar > * pointer
Definition variable_block.hpp:646
std::bidirectional_iterator_tag iterator_category
Definition variable_block.hpp:643
constexpr iterator operator++(int) noexcept
Definition variable_block.hpp:659
constexpr bool operator==(const iterator &) const noexcept=default
std::ptrdiff_t difference_type
Definition variable_block.hpp:645
constexpr iterator & operator--() noexcept
Definition variable_block.hpp:665
constexpr iterator & operator++() noexcept
Definition variable_block.hpp:654
Variable< Scalar > value_type
Definition variable_block.hpp:644
constexpr iterator() noexcept=default
Variable< Scalar > & reference
Definition variable_block.hpp:647
constexpr reference operator*() const noexcept
Definition variable_block.hpp:678
constexpr iterator operator--(int) noexcept
Definition variable_block.hpp:670
VariableBlock< Mat > & operator=(const Mat &values)
Assigns a VariableMatrix to the block.
Definition variable_block.hpp:190
VariableBlock< Mat > & operator*=(const ScalarLike auto &rhs)
Compound matrix multiplication-assignment operator.
Definition variable_block.hpp:453
const_iterator begin() const
Returns begin iterator.
Definition variable_block.hpp:749
const_iterator end() const
Returns end iterator.
Definition variable_block.hpp:754
void set_value(const Eigen::MatrixBase< Derived > &values)
Sets block's internal values.
Definition variable_block.hpp:176
const_iterator cbegin() const
Returns const begin iterator.
Definition variable_block.hpp:759
void set_value(Scalar value)
Assigns a scalar to the block.
Definition variable_block.hpp:148
VariableBlock< Mat > row(int row)
Returns a row slice of the variable matrix.
Definition variable_block.hpp:397
VariableBlock< Mat > & operator=(ScalarLike auto value)
Assigns a scalar to the block.
Definition variable_block.hpp:135
Variable< Scalar > & operator[](int index)
Returns a scalar subblock at the given index.
Definition variable_block.hpp:246
VariableBlock< Mat > & operator+=(const MatrixLike auto &rhs)
Compound addition-assignment operator.
Definition variable_block.hpp:497
VariableBlock< Mat > & operator*=(const MatrixLike auto &rhs)
Compound matrix multiplication-assignment operator.
Definition variable_block.hpp:433
iterator begin()
Returns begin iterator.
Definition variable_block.hpp:739
VariableBlock(VariableBlock< Mat > &&)=default
Move constructor.
VariableBlock< Mat > & operator=(const VariableBlock< Mat > &values)
Assigns a VariableBlock to the block.
Definition variable_block.hpp:36
const Variable< Scalar > & operator[](int index) const
Returns a scalar subblock at the given index.
Definition variable_block.hpp:257
VariableBlock< const Mat > col(int col) const
Returns a column slice of the variable matrix.
Definition variable_block.hpp:424
int rows() const
Returns the number of rows in the matrix.
Definition variable_block.hpp:582
const VariableBlock< const Mat > operator()(Slice row_slice, int row_slice_length, Slice col_slice, int col_slice_length) const
Returns a slice of the variable matrix.
Definition variable_block.hpp:353
VariableBlock< Mat > col(int col)
Returns a column slice of the variable matrix.
Definition variable_block.hpp:415
VariableBlock< Mat > & operator=(const Eigen::MatrixBase< Derived > &values)
Assigns an Eigen matrix to the block.
Definition variable_block.hpp:159
Scalar value(int index)
Returns an element of the variable block.
Definition variable_block.hpp:600
VariableBlock< Mat > operator()(Slice row_slice, int row_slice_length, Slice col_slice, int col_slice_length)
Returns a slice of the variable matrix.
Definition variable_block.hpp:329
reverse_iterator rbegin()
Returns reverse begin iterator.
Definition variable_block.hpp:769
VariableBlock< Mat > & operator=(Mat &&values)
Assigns a VariableMatrix to the block.
Definition variable_block.hpp:205
const_iterator cend() const
Returns const end iterator.
Definition variable_block.hpp:764
const Variable< Scalar > & operator()(int row, int col) const
Returns a scalar subblock at the given row and column.
Definition variable_block.hpp:235
VariableBlock< Mat > & operator/=(const MatrixLike auto &rhs)
Compound matrix division-assignment operator.
Definition variable_block.hpp:467
const_reverse_iterator crbegin() const
Returns const reverse begin iterator.
Definition variable_block.hpp:793
VariableBlock< Mat > & operator-=(const ScalarLike auto &rhs)
Compound subtraction-assignment operator.
Definition variable_block.hpp:545
VariableBlock(Mat &mat, int row_offset, int col_offset, int block_rows, int block_cols)
Constructs a Variable block pointing to a subset of the given matrix.
Definition variable_block.hpp:104
VariableBlock< Mat > & operator/=(const ScalarLike auto &rhs)
Compound matrix division-assignment operator.
Definition variable_block.hpp:483
VariableBlock< Mat > segment(int offset, int length)
Returns a segment of the variable vector.
Definition variable_block.hpp:374
const_reverse_iterator rend() const
Returns const reverse end iterator.
Definition variable_block.hpp:786
VariableBlock< Mat > & operator-=(const MatrixLike auto &rhs)
Compound subtraction-assignment operator.
Definition variable_block.hpp:529
VariableBlock< Mat > & operator+=(const ScalarLike auto &rhs)
Compound addition-assignment operator.
Definition variable_block.hpp:513
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > value()
Returns the contents of the variable matrix.
Definition variable_block.hpp:608
iterator end()
Returns end iterator.
Definition variable_block.hpp:744
VariableBlock(Mat &mat)
Constructs a Variable block pointing to all of the given matrix.
Definition variable_block.hpp:95
VariableBlock< const Mat > row(int row) const
Returns a row slice of the variable matrix.
Definition variable_block.hpp:406
const VariableBlock< const Mat > operator()(Slice row_slice, Slice col_slice) const
Returns a slice of the variable matrix.
Definition variable_block.hpp:312
const VariableBlock< Mat > segment(int offset, int length) const
Returns a segment of the variable vector.
Definition variable_block.hpp:386
Scalar value(int row, int col)
Returns an element of the variable matrix.
Definition variable_block.hpp:594
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition variable_block.hpp:732
int cols() const
Returns the number of columns in the matrix.
Definition variable_block.hpp:587
VariableBlock< Mat > block(int row_offset, int col_offset, int block_rows, int block_cols)
Returns a block of the variable matrix.
Definition variable_block.hpp:269
reverse_iterator rend()
Returns reverse end iterator.
Definition variable_block.hpp:774
size_t size() const
Returns number of elements in matrix.
Definition variable_block.hpp:807
std::remove_cv_t< Mat > T() const
Returns the transpose of the variable matrix.
Definition variable_block.hpp:567
VariableBlock(const VariableBlock< Mat > &)=default
Copy constructor.
std::remove_cv_t< Mat > cwise_transform(function_ref< Variable< Scalar >(const Variable< Scalar > &x)> unary_op) const
Transforms the matrix coefficient-wise with an unary operator.
Definition variable_block.hpp:625
VariableBlock< Mat > & operator=(VariableBlock< Mat > &&values)
Assigns a VariableBlock to the block.
Definition variable_block.hpp:67
Variable< Scalar > & operator()(int row, int col)
Returns a scalar subblock at the given row and column.
Definition variable_block.hpp:221
VariableBlock(Mat &mat, Slice row_slice, int row_slice_length, Slice col_slice, int col_slice_length)
Constructs a Variable block pointing to a subset of the given matrix.
Definition variable_block.hpp:121
const_reverse_iterator crend() const
Returns const reverse end iterator.
Definition variable_block.hpp:800
VariableBlock< Mat > operator()(Slice row_slice, Slice col_slice)
Returns a slice of the variable matrix.
Definition variable_block.hpp:301
const_reverse_iterator rbegin() const
Returns const reverse begin iterator.
Definition variable_block.hpp:779
const VariableBlock< const Mat > block(int row_offset, int col_offset, int block_rows, int block_cols) const
Returns a block slice of the variable matrix.
Definition variable_block.hpp:286
typename Mat::Scalar Scalar
Scalar type alias.
Definition variable_block.hpp:27
std::reverse_iterator< iterator > reverse_iterator
Definition variable_block.hpp:731
An autodiff variable pointing to an expression node.
Definition variable.hpp:47
Definition function_ref.hpp:13
Definition concepts.hpp:18
Definition concepts.hpp:24
static constexpr empty_t empty
Designates an uninitialized VariableMatrix.
Definition empty.hpp:11
Definition expression_graph.hpp:11
Definition StringMap.hpp:773