aligator  0.16.0
A versatile and efficient C++ library for real-time constrained trajectory optimization.
Loading...
Searching...
No Matches
blk-matrix.hpp
Go to the documentation of this file.
1
3#pragma once
4
5#include "aligator/math.hpp"
6#include <array>
7#include <numeric>
8
9namespace aligator {
10
17template <typename _MatrixType, int N, int M = N> class BlkMatrix;
18
19template <typename _MatrixType, int _N, int _M> class BlkMatrix {
20public:
21 using MatrixType = _MatrixType;
22 using PlainObject = typename MatrixType::PlainObject;
23 using Scalar = typename MatrixType::Scalar;
24 using Index = Eigen::Index;
25 static constexpr int N = _N;
26 static constexpr int M = _M;
27 static constexpr int Options = PlainObject::Options;
28 static constexpr bool IsVectorAtCompileTime =
29 MatrixType::IsVectorAtCompileTime;
30
31 using RowDimsType = std::conditional_t<N != -1, std::array<Index, size_t(N)>,
32 std::vector<Index>>;
33 using ColDimsType = std::conditional_t<M != -1, std::array<Index, size_t(M)>,
34 std::vector<Index>>;
35
36 static_assert(N != 0 && M != 0,
37 "The BlkMatrix template class only supports nonzero numbers of "
38 "blocks in either direction.");
39
40 static_assert(!IsVectorAtCompileTime || (M == 1),
41 "Compile-time vector cannot have more than one column block.");
42
44 : m_data()
45 , m_rowDims()
46 , m_colDims()
47 , m_rowIndices()
48 , m_colIndices()
49 , m_totalRows(0)
50 , m_totalCols(0) {}
51
62
63 template <typename Other>
64 BlkMatrix(const Eigen::MatrixBase<Other> &data, const RowDimsType &rowDims,
65 const ColDimsType &colDims)
66 : m_data(data.derived())
71 , m_totalRows(0)
72 , m_totalCols(0) {
73 initialize();
74 }
75
76 template <typename Other>
77 BlkMatrix(Eigen::MatrixBase<Other> &data, const RowDimsType &rowDims,
78 const ColDimsType &colDims)
79 : m_data(data.derived())
84 , m_totalRows(0)
85 , m_totalCols(0) {
86 initialize();
87 }
88
90 template <typename Other>
91 BlkMatrix(const Eigen::MatrixBase<Other> &data, const RowDimsType &dims)
92 : BlkMatrix(data, dims, {data.cols()}) {}
93
95 template <typename Other>
96 BlkMatrix(Eigen::MatrixBase<Other> &data, const RowDimsType &dims)
97 : BlkMatrix(data, dims, {data.cols()}) {}
98
99 operator Eigen::Ref<PlainObject>() { return m_data; }
100 operator Eigen::Ref<const PlainObject>() const { return m_data; }
101
103 explicit BlkMatrix(const RowDimsType &dims)
104 : BlkMatrix(dims, {1}) {
105 static_assert(IsVectorAtCompileTime,
106 "Constructor only supported for vector types.");
107 }
108
110 inline auto operator()(size_t i, size_t j) {
111 return m_data.block(m_rowIndices[i], m_colIndices[j], m_rowDims[i],
112 m_colDims[j]);
113 }
114
116 inline auto operator()(size_t i, size_t j) const {
117 return m_data.block(m_rowIndices[i], m_colIndices[j], m_rowDims[i],
118 m_colDims[j]);
119 }
120
121 inline auto blockRow(size_t i) {
122 return m_data.middleRows(m_rowIndices[i], m_rowDims[i]);
123 }
124
125 inline auto blockRow(size_t i) const {
126 return m_data.middleRows(m_rowIndices[i], m_rowDims[i]);
127 }
128
129 inline auto blockCol(size_t j) const {
130 return m_data.middleCols(m_colIndices[j], m_colDims[j]);
131 }
132
133 inline auto blockCol(size_t j) {
134 return m_data.middleCols(m_colIndices[j], m_colDims[j]);
135 }
136
137 auto blockSegment(size_t i) {
138 EIGEN_STATIC_ASSERT_VECTOR_ONLY(MatrixType);
139 return m_data.segment(m_rowIndices[i], m_rowDims[i]);
140 }
141
142 auto blockSegment(size_t i) const {
143 EIGEN_STATIC_ASSERT_VECTOR_ONLY(MatrixType);
144 return m_data.segment(m_rowIndices[i], m_rowDims[i]);
145 }
146
147 inline auto operator[](size_t i) { return blockSegment(i); }
148
149 inline auto operator[](size_t i) const { return blockSegment(i); }
150
152 template <typename Other>
153 BlkMatrix &operator=(const Eigen::MatrixBase<Other> &other) {
154 assert(other.rows() == m_data.rows());
155 assert(other.cols() == m_data.cols());
156 m_data = other;
157 return *this;
158 }
159
160 void setZero() { m_data.setZero(); }
162 const ColDimsType &colDims) {
163
165 out.setZero();
166 return out;
167 }
168
169 template <typename Other> inline void swap(BlkMatrix<Other, N, M> &other) {
170 m_data.swap(other.matrix());
171 }
172
173 MatrixType &matrix() { return m_data; }
174 const MatrixType &matrix() const { return m_data; }
175
176 template <typename D>
177 inline bool isApprox(
178 const Eigen::EigenBase<D> &other,
179 const Scalar prec = Eigen::NumTraits<Scalar>::dummy_precision()) const {
180 return matrix().isApprox(other, prec);
181 }
182
183 template <typename D, int N2, int M2>
184 inline bool isApprox(
185 const BlkMatrix<D, N2, M2> &other,
186 const Scalar prec = Eigen::NumTraits<Scalar>::dummy_precision()) const {
187 return matrix().isApprox(other.matrix(), prec);
188 }
189
190 const RowDimsType &rowDims() const { return m_rowDims; }
191 const RowDimsType &rowIndices() const { return m_rowIndices; }
192 const ColDimsType &colDims() const { return m_colDims; }
193 const ColDimsType &colIndices() const { return m_colIndices; }
194
195 Index rows() const { return m_totalRows; }
196 Index cols() const { return m_totalCols; }
197
199 auto topBlkRows(size_t n) {
200 using OutType = BlkMatrix<Eigen::Ref<MatrixType>, -1, M>;
201 std::vector<Index> subRowDims;
202 subRowDims.resize(n);
203 std::copy_n(m_rowDims.cbegin(), n, subRowDims.begin());
204 Index ntr = std::accumulate(subRowDims.begin(), subRowDims.end(), 0);
205 return OutType(m_data.topRows(ntr), subRowDims, m_colDims);
206 }
207
210 template <size_t n> auto topBlkRows() {
211 static_assert(n <= N,
212 "Cannot take n block rows of matrix with <n block rows.");
213 using RefType = Eigen::Ref<MatrixType>;
214 using OutType = BlkMatrix<RefType, n, M>;
215 std::array<Index, n> subRowDims;
216 std::copy_n(m_rowDims.cbegin(), n, subRowDims.begin());
217 Index ntr = std::accumulate(subRowDims.begin(), subRowDims.end(), 0);
218 return OutType(m_data.topRows(ntr), subRowDims, m_colDims);
219 }
220
221 friend std::ostream &operator<<(std::ostream &oss, const BlkMatrix &self) {
222 return oss << self.m_data;
223 }
224
225protected:
233
234 void initialize() {
235 m_totalRows = 0;
236 m_totalCols = 0;
237 for (size_t i = 0; i < m_rowDims.size(); i++) {
240 }
241 for (size_t i = 0; i < m_colDims.size(); i++) {
244 }
246 }
247};
248
249} // namespace aligator
Block matrix class, with a fixed or dynamic-size number of row and column blocks.
auto blockSegment(size_t i)
Eigen::Index Index
void swap(BlkMatrix< Other, N, M > &other)
friend std::ostream & operator<<(std::ostream &oss, const BlkMatrix &self)
auto blockSegment(size_t i) const
Index cols() const
_MatrixType MatrixType
auto topBlkRows(size_t n)
Take the top n block rows of the block matrix.
bool isApprox(const BlkMatrix< D, N2, M2 > &other, const Scalar prec=Eigen::NumTraits< Scalar >::dummy_precision()) const
typename MatrixType::PlainObject PlainObject
Index rows() const
std::conditional_t< N !=-1, std::array< Index, size_t(N)>, std::vector< Index > > RowDimsType
std::conditional_t< M !=-1, std::array< Index, size_t(M)>, std::vector< Index > > ColDimsType
const ColDimsType & colIndices() const
auto blockRow(size_t i)
bool isApprox(const Eigen::EigenBase< D > &other, const Scalar prec=Eigen::NumTraits< Scalar >::dummy_precision()) const
auto topBlkRows()
This version returns a fixed-size block. This version returns a fixed-size block.
MatrixType & matrix()
BlkMatrix(const Eigen::MatrixBase< Other > &data, const RowDimsType &dims)
Only-rows constructor (only for vectors)
static BlkMatrix Zero(const RowDimsType &rowDims, const ColDimsType &colDims)
auto blockCol(size_t j)
auto operator()(size_t i, size_t j)
Get the block in position ( i, j )
auto operator[](size_t i) const
auto blockCol(size_t j) const
auto operator()(size_t i, size_t j) const
typename MatrixType::Scalar Scalar
const RowDimsType & rowIndices() const
const MatrixType & matrix() const
auto blockRow(size_t i) const
BlkMatrix(Eigen::MatrixBase< Other > &data, const RowDimsType &rowDims, const ColDimsType &colDims)
BlkMatrix(const RowDimsType &rowDims, const ColDimsType &colDims)
BlkMatrix(Eigen::MatrixBase< Other > &data, const RowDimsType &dims)
Only-rows constructor (only for vectors)
BlkMatrix & operator=(const Eigen::MatrixBase< Other > &other)
Set the data to be equal to some other Eigen object.
BlkMatrix(const Eigen::MatrixBase< Other > &data, const RowDimsType &rowDims, const ColDimsType &colDims)
BlkMatrix(const RowDimsType &dims)
Only-rows constructor (only for vectors)
auto operator[](size_t i)
Math utilities.
Main package namespace.