gtsam 4.2.0
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
20#include <gtsam/base/Matrix.h>
23
24namespace gtsam {
25
26 // Forward declarations
27 class SymmetricBlockMatrix;
28
42 class GTSAM_EXPORT VerticalBlockMatrix
43 {
44 public:
46 typedef Eigen::Block<Matrix> Block;
47 typedef Eigen::Block<const Matrix> constBlock;
48
49 protected:
50 Matrix matrix_;
52
56
57 public:
58
61 rowStart_(0), rowEnd_(0), blockStart_(0)
62 {
63 variableColOffsets_.push_back(0);
64 assertInvariants();
65 }
66
68 template<typename CONTAINER>
69 VerticalBlockMatrix(const CONTAINER& dimensions, DenseIndex height,
70 bool appendOneDimension = false) :
71 variableColOffsets_(dimensions.size() + (appendOneDimension ? 2 : 1)),
72 rowStart_(0), rowEnd_(height), blockStart_(0) {
73 fillOffsets(dimensions.begin(), dimensions.end(), appendOneDimension);
74 matrix_.resize(height, variableColOffsets_.back());
75 assertInvariants();
76 }
77
79 template<typename CONTAINER, typename DERIVED>
80 VerticalBlockMatrix(const CONTAINER& dimensions,
81 const Eigen::MatrixBase<DERIVED>& matrix, bool appendOneDimension = false) :
82 matrix_(matrix), variableColOffsets_(dimensions.size() + (appendOneDimension ? 2 : 1)),
83 rowStart_(0), rowEnd_(matrix.rows()), blockStart_(0) {
84 fillOffsets(dimensions.begin(), dimensions.end(), appendOneDimension);
85 if (variableColOffsets_.back() != matrix_.cols())
86 throw std::invalid_argument(
87 "Requested to create a VerticalBlockMatrix with dimensions that do not sum to the total columns of the provided matrix.");
88 assertInvariants();
89 }
90
92 template<typename ITERATOR>
93 VerticalBlockMatrix(ITERATOR firstBlockDim, ITERATOR lastBlockDim,
94 DenseIndex height, bool appendOneDimension = false) :
95 variableColOffsets_((lastBlockDim-firstBlockDim) + (appendOneDimension ? 2 : 1)),
96 rowStart_(0), rowEnd_(height), blockStart_(0) {
97 fillOffsets(firstBlockDim, lastBlockDim, appendOneDimension);
98 matrix_.resize(height, variableColOffsets_.back());
99 assertInvariants();
100 }
101
107 static VerticalBlockMatrix LikeActiveViewOf(const VerticalBlockMatrix& rhs);
108
112 static VerticalBlockMatrix LikeActiveViewOf(const SymmetricBlockMatrix& rhs, DenseIndex height);
113
115 DenseIndex rows() const { assertInvariants(); return rowEnd_ - rowStart_; }
116
118 DenseIndex cols() const { assertInvariants(); return variableColOffsets_.back() - variableColOffsets_[blockStart_]; }
119
121 DenseIndex nBlocks() const { assertInvariants(); return variableColOffsets_.size() - 1 - blockStart_; }
122
124 Block operator()(DenseIndex block) { return range(block, block+1); }
125
127 const constBlock operator()(DenseIndex block) const { return range(block, block+1); }
128
130 Block range(DenseIndex startBlock, DenseIndex endBlock) {
131 assertInvariants();
132 DenseIndex actualStartBlock = startBlock + blockStart_;
133 DenseIndex actualEndBlock = endBlock + blockStart_;
134 if(startBlock != 0 || endBlock != 0) {
135 checkBlock(actualStartBlock);
136 assert(actualEndBlock < (DenseIndex)variableColOffsets_.size());
137 }
138 const DenseIndex startCol = variableColOffsets_[actualStartBlock];
139 const DenseIndex rangeCols = variableColOffsets_[actualEndBlock] - startCol;
140 return matrix_.block(rowStart_, startCol, this->rows(), rangeCols);
141 }
142
143 const constBlock range(DenseIndex startBlock, DenseIndex endBlock) const {
144 assertInvariants();
145 DenseIndex actualStartBlock = startBlock + blockStart_;
146 DenseIndex actualEndBlock = endBlock + blockStart_;
147 if(startBlock != 0 || endBlock != 0) {
148 checkBlock(actualStartBlock);
149 assert(actualEndBlock < (DenseIndex)variableColOffsets_.size());
150 }
151 const DenseIndex startCol = variableColOffsets_[actualStartBlock];
152 const DenseIndex rangeCols = variableColOffsets_[actualEndBlock] - startCol;
153 return ((const Matrix&)matrix_).block(rowStart_, startCol, this->rows(), rangeCols);
154 }
155
157 Block full() { return range(0, nBlocks()); }
158
160 const constBlock full() const { return range(0, nBlocks()); }
161
162 DenseIndex offset(DenseIndex block) const {
163 assertInvariants();
164 DenseIndex actualBlock = block + blockStart_;
165 checkBlock(actualBlock);
166 return variableColOffsets_[actualBlock];
167 }
168
170 const DenseIndex& rowStart() const { return rowStart_; }
171
173 DenseIndex& rowStart() { return rowStart_; }
174
176 const DenseIndex& rowEnd() const { return rowEnd_; }
177
179 DenseIndex& rowEnd() { return rowEnd_; }
180
182 const DenseIndex& firstBlock() const { return blockStart_; }
183
185 DenseIndex& firstBlock() { return blockStart_; }
186
188 const Matrix& matrix() const { return matrix_; }
189
191 Matrix& matrix() { return matrix_; }
192
193 protected:
194 void assertInvariants() const {
195 assert(matrix_.cols() == variableColOffsets_.back());
196 assert(blockStart_ < (DenseIndex)variableColOffsets_.size());
197 assert(rowStart_ <= matrix_.rows());
198 assert(rowEnd_ <= matrix_.rows());
199 assert(rowStart_ <= rowEnd_);
200 }
201
202 void checkBlock(DenseIndex block) const {
203 static_cast<void>(block); //Disable unused varibale warnings.
204 assert(matrix_.cols() == variableColOffsets_.back());
205 assert(block < (DenseIndex)variableColOffsets_.size() - 1);
206 assert(variableColOffsets_[block] < matrix_.cols() && variableColOffsets_[block+1] <= matrix_.cols());
207 }
208
209 template<typename ITERATOR>
210 void fillOffsets(ITERATOR firstBlockDim, ITERATOR lastBlockDim, bool appendOneDimension) {
211 variableColOffsets_[0] = 0;
212 DenseIndex j=0;
213 for(ITERATOR dim=firstBlockDim; dim!=lastBlockDim; ++dim, ++j)
214 variableColOffsets_[j+1] = variableColOffsets_[j] + *dim;
215 if(appendOneDimension)
216 variableColOffsets_[j+1] = variableColOffsets_[j] + 1;
217 }
218
219 friend class SymmetricBlockMatrix;
220
221 private:
223 friend class boost::serialization::access;
224 template<class ARCHIVE>
225 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
226 ar & BOOST_SERIALIZATION_NVP(matrix_);
227 ar & BOOST_SERIALIZATION_NVP(variableColOffsets_);
228 ar & BOOST_SERIALIZATION_NVP(rowStart_);
229 ar & BOOST_SERIALIZATION_NVP(rowEnd_);
230 ar & BOOST_SERIALIZATION_NVP(blockStart_);
231 }
232 };
233
234}
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:34
Global functions in a separate testing namespace.
Definition chartTesting.h:28
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition types.h:106
This class stores a dense matrix and allows it to be accessed as a collection of blocks.
Definition SymmetricBlockMatrix.h:52
This class stores a dense matrix and allows it to be accessed as a collection of vertical blocks.
Definition VerticalBlockMatrix.h:43
VerticalBlockMatrix()
Construct an empty VerticalBlockMatrix.
Definition VerticalBlockMatrix.h:60
Block full()
Return the full matrix, not including any portions excluded by rowStart(), rowEnd(),...
Definition VerticalBlockMatrix.h:157
Block range(DenseIndex startBlock, DenseIndex endBlock)
access ranges of blocks at a time
Definition VerticalBlockMatrix.h:130
const DenseIndex & firstBlock() const
Get the apparent first block for all operations.
Definition VerticalBlockMatrix.h:182
const Matrix & matrix() const
Access to full matrix (including any portions excluded by rowStart(), rowEnd(), and firstBlock())
Definition VerticalBlockMatrix.h:188
VerticalBlockMatrix(const CONTAINER &dimensions, DenseIndex height, bool appendOneDimension=false)
Construct from a container of the sizes of each vertical block.
Definition VerticalBlockMatrix.h:69
DenseIndex rowEnd_
Changes apparent matrix view, see main class comment.
Definition VerticalBlockMatrix.h:54
const DenseIndex & rowEnd() const
Get the apparent last row (exclusive, i.e.
Definition VerticalBlockMatrix.h:176
DenseIndex rows() const
Row size.
Definition VerticalBlockMatrix.h:115
Block operator()(DenseIndex block)
Access a single block in the underlying matrix with read/write access.
Definition VerticalBlockMatrix.h:124
DenseIndex & firstBlock()
Get or set the apparent first block for all operations.
Definition VerticalBlockMatrix.h:185
DenseIndex cols() const
Column size.
Definition VerticalBlockMatrix.h:118
VerticalBlockMatrix(ITERATOR firstBlockDim, ITERATOR lastBlockDim, DenseIndex height, bool appendOneDimension=false)
Construct from iterator over the sizes of each vertical block.
Definition VerticalBlockMatrix.h:93
const constBlock operator()(DenseIndex block) const
Access a const block view.
Definition VerticalBlockMatrix.h:127
const DenseIndex & rowStart() const
Get the apparent first row of the underlying matrix for all operations.
Definition VerticalBlockMatrix.h:170
Matrix matrix_
The full matrix.
Definition VerticalBlockMatrix.h:50
DenseIndex nBlocks() const
Block count.
Definition VerticalBlockMatrix.h:121
const constBlock full() const
Return the full matrix, not including any portions excluded by rowStart(), rowEnd(),...
Definition VerticalBlockMatrix.h:160
DenseIndex & rowStart()
Get or set the apparent first row of the underlying matrix for all operations.
Definition VerticalBlockMatrix.h:173
Matrix & matrix()
Non-const access to full matrix (including any portions excluded by rowStart(), rowEnd(),...
Definition VerticalBlockMatrix.h:191
DenseIndex & rowEnd()
Get or set the apparent last row (exclusive, i.e.
Definition VerticalBlockMatrix.h:179
DenseIndex rowStart_
Changes apparent matrix view, see main class comment.
Definition VerticalBlockMatrix.h:53
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:80
DenseIndex blockStart_
Changes apparent matrix view, see main class comment.
Definition VerticalBlockMatrix.h:55
FastVector< DenseIndex > variableColOffsets_
the starting columns of each block (0-based)
Definition VerticalBlockMatrix.h:51