aligator  0.10.0
A primal-dual augmented Lagrangian-type solver for nonlinear 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
13template <typename _MatrixType, int _N, int _M = _N> class BlkMatrix {
14public:
15 using MatrixType = _MatrixType;
16 using PlainObject = typename MatrixType::PlainObject;
17 using Scalar = typename MatrixType::Scalar;
18 enum { N = _N, M = _M, Options = PlainObject::Options };
19
20 using row_dim_t = std::conditional_t<N != -1, std::array<long, size_t(N)>,
21 std::vector<long>>;
22 using col_dim_t = std::conditional_t<M != -1, std::array<long, size_t(M)>,
23 std::vector<long>>;
24
25 static_assert(N != 0 && M != 0,
26 "The BlkMatrix template class only supports nonzero numbers of "
27 "blocks in either direction.");
28
29 static_assert(!MatrixType::IsVectorAtCompileTime || (M == 1),
30 "Compile-time vector cannot have more than one column block.");
31
35
42
43 template <typename Other>
44 BlkMatrix(const Eigen::MatrixBase<Other> &data, const row_dim_t &rowDims,
45 const col_dim_t &colDims)
46 : m_data(data.derived()), //
49 initialize();
50 }
51
52 template <typename Other>
53 BlkMatrix(Eigen::MatrixBase<Other> &data, const row_dim_t &rowDims,
54 const col_dim_t &colDims)
55 : m_data(data.derived()), //
58 initialize();
59 }
60
62 template <typename Other>
63 BlkMatrix(const Eigen::MatrixBase<Other> &data, const row_dim_t &dims)
64 : BlkMatrix(data, dims, {data.cols()}) {}
65
67 template <typename Other>
68 BlkMatrix(Eigen::MatrixBase<Other> &data, const row_dim_t &dims)
69 : BlkMatrix(data, dims, {data.cols()}) {}
70
72 explicit BlkMatrix(const row_dim_t &dims) : BlkMatrix(dims, {1}) {}
73
75 inline auto operator()(size_t i, size_t j) {
76 return m_data.block(m_rowIndices[i], m_colIndices[j], m_rowDims[i],
77 m_colDims[j]);
78 }
79
81 inline auto operator()(size_t i, size_t j) const {
82 return m_data.block(m_rowIndices[i], m_colIndices[j], m_rowDims[i],
83 m_colDims[j]);
84 }
85
86 inline auto blockRow(size_t i) {
87 return m_data.middleRows(m_rowIndices[i], m_rowDims[i]);
88 }
89
90 inline auto blockRow(size_t i) const {
91 return m_data.middleRows(m_rowIndices[i], m_rowDims[i]);
92 }
93
94 inline auto blockCol(size_t j) const {
95 return m_data.middleCols(m_colIndices[j], m_colDims[j]);
96 }
97
98 inline auto blockCol(size_t j) {
99 return m_data.middleCols(m_colIndices[j], m_colDims[j]);
100 }
101
102 auto blockSegment(size_t i) {
103 EIGEN_STATIC_ASSERT_VECTOR_ONLY(MatrixType);
104 return m_data.segment(m_rowIndices[i], m_rowDims[i]);
105 }
106
107 auto blockSegment(size_t i) const {
108 EIGEN_STATIC_ASSERT_VECTOR_ONLY(MatrixType);
109 return m_data.segment(m_rowIndices[i], m_rowDims[i]);
110 }
111
112 inline auto operator[](size_t i) { return blockSegment(i); }
113
114 inline auto operator[](size_t i) const { return blockSegment(i); }
115
117 template <typename Other>
118 BlkMatrix &operator=(const Eigen::MatrixBase<Other> &other) {
119 assert(other.rows() == m_data.rows());
120 assert(other.cols() == m_data.cols());
121 m_data = other;
122 return *this;
123 }
124
125 void setZero() { m_data.setZero(); }
127
129 out.setZero();
130 return out;
131 }
132
133 template <typename Other> inline void swap(BlkMatrix<Other, N, M> &other) {
134 m_data.swap(other.matrix());
135 }
136
137 MatrixType &matrix() { return m_data; }
138 const MatrixType &matrix() const { return m_data; }
139
140 const row_dim_t &rowDims() const { return m_rowDims; }
141 const row_dim_t &rowIndices() const { return m_rowIndices; }
142 const col_dim_t &colDims() const { return m_colDims; }
143 const col_dim_t &colIndices() const { return m_colIndices; }
144
145 long rows() const { return m_totalRows; }
146 long cols() const { return m_totalCols; }
147
148 auto topBlkRows(size_t n) {
149 using OutType = BlkMatrix<Eigen::Ref<MatrixType>, -1, M>;
150 std::vector<long> subRowDims;
151 subRowDims.resize(n);
152 std::copy_n(m_rowDims.cbegin(), n, subRowDims.begin());
153 long ntr = std::accumulate(subRowDims.begin(), subRowDims.end(), 0);
154 return OutType(m_data.topRows(ntr), subRowDims, m_colDims);
155 }
156
157 template <size_t n> auto topBlkRows() {
158 static_assert(n <= N,
159 "Cannot take n block rows of matrix with <n block rows.");
160 using RefType = Eigen::Ref<MatrixType>;
161 using OutType = BlkMatrix<RefType, n, M>;
162 std::array<long, n> subRowDims;
163 std::copy_n(m_rowDims.cbegin(), n, subRowDims.begin());
164 long ntr = std::accumulate(subRowDims.begin(), subRowDims.end(), 0);
165 return OutType(m_data.topRows(ntr), subRowDims, m_colDims);
166 }
167
168 friend std::ostream &operator<<(std::ostream &oss, const BlkMatrix &self) {
169 return oss << self.m_data;
170 }
171
172protected:
180
181 explicit BlkMatrix(const row_dim_t &dims, std::true_type)
182 : BlkMatrix(dims, dims) {}
183 explicit BlkMatrix(const row_dim_t &dims, std::false_type)
184 : BlkMatrix(dims, {1}) {}
185
186 void initialize() {
187 for (size_t i = 0; i < m_rowDims.size(); i++) {
190 }
191 for (size_t i = 0; i < m_colDims.size(); i++) {
194 }
196 }
197};
198
199} // namespace aligator
Block matrix class, with a fixed-size number of row and column blocks.
auto blockSegment(size_t i)
void swap(BlkMatrix< Other, N, M > &other)
friend std::ostream & operator<<(std::ostream &oss, const BlkMatrix &self)
auto blockSegment(size_t i) const
_MatrixType MatrixType
auto topBlkRows(size_t n)
typename MatrixType::PlainObject PlainObject
const row_dim_t & rowDims() const
BlkMatrix(const row_dim_t &dims, std::true_type)
BlkMatrix(const Eigen::MatrixBase< Other > &data, const row_dim_t &dims)
Only-rows constructor (only for vectors)
auto blockRow(size_t i)
MatrixType & matrix()
BlkMatrix(const row_dim_t &dims)
Only-rows constructor (only for vectors)
auto blockCol(size_t j)
auto operator()(size_t i, size_t j)
Get the block in position ( i, j )
BlkMatrix(const row_dim_t &dims, std::false_type)
BlkMatrix(const Eigen::MatrixBase< Other > &data, const row_dim_t &rowDims, const col_dim_t &colDims)
static BlkMatrix Zero(const row_dim_t &rowDims, const col_dim_t &colDims)
auto operator[](size_t i) const
auto blockCol(size_t j) const
auto operator()(size_t i, size_t j) const
const row_dim_t & rowIndices() const
BlkMatrix(Eigen::MatrixBase< Other > &data, const row_dim_t &dims)
Only-rows constructor (only for vectors)
typename MatrixType::Scalar Scalar
const MatrixType & matrix() const
auto blockRow(size_t i) const
const col_dim_t & colDims() const
std::conditional_t< M !=-1, std::array< long, size_t(M)>, std::vector< long > > col_dim_t
const col_dim_t & colIndices() const
BlkMatrix & operator=(const Eigen::MatrixBase< Other > &other)
Set the data to be equal to some other Eigen object.
BlkMatrix(const row_dim_t &rowDims, const col_dim_t &colDims)
std::conditional_t< N !=-1, std::array< long, size_t(N)>, std::vector< long > > row_dim_t
BlkMatrix(Eigen::MatrixBase< Other > &data, const row_dim_t &rowDims, const col_dim_t &colDims)
auto operator[](size_t i)
Math utilities.
Main package namespace.