gtsam
Loading...
Searching...
No Matches
VerticalBlockMatrix.h
Go to the documentation of this file.
1/* ----------------------------------------------------------------------------
2
3 * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4 * Atlanta, Georgia 30332-0415
5 * All Rights Reserved
6 * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7
8 * See LICENSE for the license information
9
10 * -------------------------------------------------------------------------- */
11
18#pragma once
19
21#include <gtsam/base/Matrix.h>
23
24#include <cassert>
25
26namespace gtsam {
27
28// Forward declarations
30
47class GTSAM_EXPORT VerticalBlockMatrix {
48 public:
49 typedef VerticalBlockMatrix This;
50 typedef Eigen::Block<Matrix> Block;
51 typedef Eigen::Block<const Matrix> constBlock;
52
53 protected:
54 Matrix matrix_;
57
64
65 public:
70
71 // Destructor
72 ~VerticalBlockMatrix() = default;
73
74 // Copy constructor (default)
75 VerticalBlockMatrix(const VerticalBlockMatrix& other) = default;
76
77 // Copy assignment operator (default)
78 VerticalBlockMatrix& operator=(const VerticalBlockMatrix& other) = default;
79
80 // Move constructor
82 : matrix_(std::move(other.matrix_)),
83 variableColOffsets_(std::move(other.variableColOffsets_)),
84 rowStart_(other.rowStart_),
85 rowEnd_(other.rowEnd_),
86 blockStart_(other.blockStart_) {
87 other.rowStart_ = 0;
88 other.rowEnd_ = 0;
89 other.blockStart_ = 0;
90 }
91
92 // Move assignment operator
93 VerticalBlockMatrix& operator=(VerticalBlockMatrix&& other) noexcept {
94 if (this != &other) {
95 matrix_ = std::move(other.matrix_);
96 variableColOffsets_ = std::move(other.variableColOffsets_);
97 rowStart_ = other.rowStart_;
98 rowEnd_ = other.rowEnd_;
99 blockStart_ = other.blockStart_;
100
101 other.rowStart_ = 0;
102 other.rowEnd_ = 0;
103 other.blockStart_ = 0;
104 }
105 return *this;
106 }
107
109 template <typename CONTAINER>
110 VerticalBlockMatrix(const CONTAINER& dimensions, DenseIndex height,
111 bool appendOneDimension = false)
112 : variableColOffsets_(dimensions.size() + (appendOneDimension ? 2 : 1)),
113 rowStart_(0),
114 rowEnd_(height),
115 blockStart_(0) {
116 fillOffsets(dimensions.begin(), dimensions.end(), appendOneDimension);
117 matrix_.resize(height, variableColOffsets_.back());
118 assertInvariants();
119 }
120
123 template <typename CONTAINER, typename DERIVED>
124 VerticalBlockMatrix(const CONTAINER& dimensions,
125 const Eigen::MatrixBase<DERIVED>& matrix,
126 bool appendOneDimension = false)
127 : matrix_(matrix),
128 variableColOffsets_(dimensions.size() + (appendOneDimension ? 2 : 1)),
129 rowStart_(0),
131 blockStart_(0) {
132 fillOffsets(dimensions.begin(), dimensions.end(), appendOneDimension);
133 if (variableColOffsets_.back() != matrix_.cols())
134 throw std::invalid_argument(
135 "Requested to create a VerticalBlockMatrix with dimensions that do "
136 "not sum to the total columns of the provided matrix.");
137 assertInvariants();
138 }
139
141 template <typename ITERATOR>
142 VerticalBlockMatrix(ITERATOR firstBlockDim, ITERATOR lastBlockDim,
143 DenseIndex height, bool appendOneDimension = false)
144 : variableColOffsets_((lastBlockDim - firstBlockDim) +
145 (appendOneDimension ? 2 : 1)),
146 rowStart_(0),
147 rowEnd_(height),
148 blockStart_(0) {
149 fillOffsets(firstBlockDim, lastBlockDim, appendOneDimension);
150 matrix_.resize(height, variableColOffsets_.back());
151 assertInvariants();
152 }
153
160 static VerticalBlockMatrix LikeActiveViewOf(const VerticalBlockMatrix& rhs);
161
165 static VerticalBlockMatrix LikeActiveViewOf(const SymmetricBlockMatrix& rhs,
166 DenseIndex height);
167
169 DenseIndex rows() const {
170 assertInvariants();
171 return rowEnd_ - rowStart_;
172 }
173
175 DenseIndex cols() const {
176 assertInvariants();
178 }
179
182 assertInvariants();
183 return variableColOffsets_.size() - 1 - blockStart_;
184 }
185
187 Block operator()(DenseIndex block) { return range(block, block + 1); }
188
190 const constBlock operator()(DenseIndex block) const {
191 return range(block, block + 1);
192 }
193
195 Block range(DenseIndex startBlock, DenseIndex endBlock) {
196 assertInvariants();
197 DenseIndex actualStartBlock = startBlock + blockStart_;
198 DenseIndex actualEndBlock = endBlock + blockStart_;
199 if (startBlock != 0 || endBlock != 0) {
200 checkBlock(actualStartBlock);
201 assert(actualEndBlock < (DenseIndex)variableColOffsets_.size());
202 }
203 const DenseIndex startCol = variableColOffsets_[actualStartBlock];
204 const DenseIndex rangeCols = variableColOffsets_[actualEndBlock] - startCol;
205 return matrix_.block(rowStart_, startCol, this->rows(), rangeCols);
206 }
207
208 const constBlock range(DenseIndex startBlock, DenseIndex endBlock) const {
209 assertInvariants();
210 DenseIndex actualStartBlock = startBlock + blockStart_;
211 DenseIndex actualEndBlock = endBlock + blockStart_;
212 if (startBlock != 0 || endBlock != 0) {
213 checkBlock(actualStartBlock);
214 assert(actualEndBlock < (DenseIndex)variableColOffsets_.size());
215 }
216 const DenseIndex startCol = variableColOffsets_[actualStartBlock];
217 const DenseIndex rangeCols = variableColOffsets_[actualEndBlock] - startCol;
218 return ((const Matrix&)matrix_)
219 .block(rowStart_, startCol, this->rows(), rangeCols);
220 }
221
228 constBlock blockRows(DenseIndex block, DenseIndex beginRow,
229 DenseIndex endRow) const {
230 assertInvariants();
231 assert(beginRow >= 0 && beginRow <= endRow && endRow <= matrix_.rows());
232 const DenseIndex actualBlock = block + blockStart_;
233 checkBlock(actualBlock);
234 const DenseIndex startCol = variableColOffsets_[actualBlock];
235 const DenseIndex blockCols =
236 variableColOffsets_[actualBlock + 1] - startCol;
237 return matrix_.block(beginRow, startCol, endRow - beginRow, blockCols);
238 }
239
242 Block full() { return range(0, nBlocks()); }
243
246 const constBlock full() const { return range(0, nBlocks()); }
247
248 DenseIndex offset(DenseIndex block) const {
249 assertInvariants();
250 DenseIndex actualBlock = block + blockStart_;
251 checkBlock(actualBlock);
252 return variableColOffsets_[actualBlock];
253 }
254
256 const DenseIndex& rowStart() const { return rowStart_; }
257
261
264 const DenseIndex& rowEnd() const { return rowEnd_; }
265
268 DenseIndex& rowEnd() { return rowEnd_; }
269
271 const DenseIndex& firstBlock() const { return blockStart_; }
272
275
278 const Matrix& matrix() const { return matrix_; }
279
282 Matrix& matrix() { return matrix_; }
283
284 protected:
285 void assertInvariants() const {
286 assert(matrix_.cols() == variableColOffsets_.back());
287 assert(blockStart_ < (DenseIndex)variableColOffsets_.size());
288 assert(rowStart_ <= matrix_.rows());
289 assert(rowEnd_ <= matrix_.rows());
290 assert(rowStart_ <= rowEnd_);
291 }
292
293 void checkBlock(DenseIndex block) const {
294 static_cast<void>(block); // Disable unused varibale warnings.
295 assert(matrix_.cols() == variableColOffsets_.back());
296 assert(block < (DenseIndex)variableColOffsets_.size() - 1);
297 assert(variableColOffsets_[block] < matrix_.cols() &&
298 variableColOffsets_[block + 1] <= matrix_.cols());
299 }
300
301 template <typename ITERATOR>
302 void fillOffsets(ITERATOR firstBlockDim, ITERATOR lastBlockDim,
303 bool appendOneDimension) {
304 variableColOffsets_[0] = 0;
305 DenseIndex j = 0;
306 for (ITERATOR dim = firstBlockDim; dim != lastBlockDim; ++dim, ++j)
307 variableColOffsets_[j + 1] = variableColOffsets_[j] + *dim;
308 if (appendOneDimension)
309 variableColOffsets_[j + 1] = variableColOffsets_[j] + 1;
310 }
311
312 friend class SymmetricBlockMatrix;
313
314 private:
315#if GTSAM_ENABLE_BOOST_SERIALIZATION
317 friend class boost::serialization::access;
318 template <class ARCHIVE>
319 void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
320 ar& BOOST_SERIALIZATION_NVP(matrix_);
321 ar& BOOST_SERIALIZATION_NVP(variableColOffsets_);
322 ar& BOOST_SERIALIZATION_NVP(rowStart_);
323 ar& BOOST_SERIALIZATION_NVP(rowEnd_);
324 ar& BOOST_SERIALIZATION_NVP(blockStart_);
325 }
326#endif
327};
328
329} // namespace gtsam
A thin wrapper around std::vector that uses a custom allocator.
typedef and functions to augment Eigen's MatrixXd
Serialization for matrices.
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
FastVector is a type alias to a std::vector with a custom memory allocator.
Definition FastVector.h:33
Global functions in a separate testing namespace.
Definition chartTesting.h:28
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition types.h:49
This class stores a dense matrix and allows it to be accessed as a collection of blocks.
Definition SymmetricBlockMatrix.h:80
This class stores a dense matrix and allows it to be accessed as a collection of vertical blocks.
Definition VerticalBlockMatrix.h:47
VerticalBlockMatrix()
Construct an empty VerticalBlockMatrix.
Definition VerticalBlockMatrix.h:67
Block full()
Return the full matrix, not including any portions excluded by rowStart(), rowEnd(),...
Definition VerticalBlockMatrix.h:242
Block range(DenseIndex startBlock, DenseIndex endBlock)
access ranges of blocks at a time
Definition VerticalBlockMatrix.h:195
constBlock blockRows(DenseIndex block, DenseIndex beginRow, DenseIndex endRow) const
Access one logical block over an explicit underlying row range.
Definition VerticalBlockMatrix.h:228
const DenseIndex & firstBlock() const
Get the apparent first block for all operations.
Definition VerticalBlockMatrix.h:271
const Matrix & matrix() const
Access to full matrix (including any portions excluded by rowStart(), rowEnd(), and firstBlock()).
Definition VerticalBlockMatrix.h:278
VerticalBlockMatrix(const CONTAINER &dimensions, DenseIndex height, bool appendOneDimension=false)
Construct from a container of the sizes of each vertical block.
Definition VerticalBlockMatrix.h:110
DenseIndex rowEnd_
Changes apparent matrix view, see main class comment.
Definition VerticalBlockMatrix.h:61
const DenseIndex & rowEnd() const
Get the apparent last row (exclusive, i.e.
Definition VerticalBlockMatrix.h:264
DenseIndex rows() const
Row size.
Definition VerticalBlockMatrix.h:169
Block operator()(DenseIndex block)
Access a single block in the underlying matrix with read/write access.
Definition VerticalBlockMatrix.h:187
DenseIndex & firstBlock()
Get or set the apparent first block for all operations.
Definition VerticalBlockMatrix.h:274
DenseIndex cols() const
Column size.
Definition VerticalBlockMatrix.h:175
VerticalBlockMatrix(ITERATOR firstBlockDim, ITERATOR lastBlockDim, DenseIndex height, bool appendOneDimension=false)
Construct from iterator over the sizes of each vertical block.
Definition VerticalBlockMatrix.h:142
const constBlock operator()(DenseIndex block) const
Access a const block view.
Definition VerticalBlockMatrix.h:190
const DenseIndex & rowStart() const
Get the apparent first row of the underlying matrix for all operations.
Definition VerticalBlockMatrix.h:256
Matrix matrix_
The full matrix.
Definition VerticalBlockMatrix.h:54
DenseIndex nBlocks() const
Block count.
Definition VerticalBlockMatrix.h:181
const constBlock full() const
Return the full matrix, not including any portions excluded by rowStart(), rowEnd(),...
Definition VerticalBlockMatrix.h:246
DenseIndex & rowStart()
Get or set the apparent first row of the underlying matrix for all operations.
Definition VerticalBlockMatrix.h:260
Matrix & matrix()
Non-const access to full matrix (including any portions excluded by rowStart(), rowEnd(),...
Definition VerticalBlockMatrix.h:282
DenseIndex & rowEnd()
Get or set the apparent last row (exclusive, i.e.
Definition VerticalBlockMatrix.h:268
DenseIndex rowStart_
Changes apparent matrix view, see main class comment.
Definition VerticalBlockMatrix.h:59
VerticalBlockMatrix(const CONTAINER &dimensions, const Eigen::MatrixBase< DERIVED > &matrix, bool appendOneDimension=false)
Construct from a container of the sizes of each vertical block and a pre-prepared matrix.
Definition VerticalBlockMatrix.h:124
DenseIndex blockStart_
Changes apparent matrix view, see main class comment.
Definition VerticalBlockMatrix.h:63
FastVector< DenseIndex > variableColOffsets_
the starting columns of each block (0-based)
Definition VerticalBlockMatrix.h:56