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>
22 #include <gtsam/base/FastVector.h>
23 
24 #include <cassert>
25 
26 namespace gtsam {
27 
28  // Forward declarations
29  class SymmetricBlockMatrix;
30 
44  class GTSAM_EXPORT VerticalBlockMatrix
45  {
46  public:
50 
51  protected:
54 
58 
59  public:
60 
63  rowStart_(0), rowEnd_(0), blockStart_(0)
64  {
65  variableColOffsets_.push_back(0);
66  assertInvariants();
67  }
68 
70  template<typename CONTAINER>
71  VerticalBlockMatrix(const CONTAINER& dimensions, DenseIndex height,
72  bool appendOneDimension = false) :
73  variableColOffsets_(dimensions.size() + (appendOneDimension ? 2 : 1)),
74  rowStart_(0), rowEnd_(height), blockStart_(0) {
75  fillOffsets(dimensions.begin(), dimensions.end(), appendOneDimension);
76  matrix_.resize(height, variableColOffsets_.back());
77  assertInvariants();
78  }
79 
81  template<typename CONTAINER, typename DERIVED>
82  VerticalBlockMatrix(const CONTAINER& dimensions,
83  const Eigen::MatrixBase<DERIVED>& matrix, bool appendOneDimension = false) :
84  matrix_(matrix), variableColOffsets_(dimensions.size() + (appendOneDimension ? 2 : 1)),
85  rowStart_(0), rowEnd_(matrix.rows()), blockStart_(0) {
86  fillOffsets(dimensions.begin(), dimensions.end(), appendOneDimension);
87  if (variableColOffsets_.back() != matrix_.cols())
88  throw std::invalid_argument(
89  "Requested to create a VerticalBlockMatrix with dimensions that do not sum to the total columns of the provided matrix.");
90  assertInvariants();
91  }
92 
94  template<typename ITERATOR>
95  VerticalBlockMatrix(ITERATOR firstBlockDim, ITERATOR lastBlockDim,
96  DenseIndex height, bool appendOneDimension = false) :
97  variableColOffsets_((lastBlockDim-firstBlockDim) + (appendOneDimension ? 2 : 1)),
98  rowStart_(0), rowEnd_(height), blockStart_(0) {
99  fillOffsets(firstBlockDim, lastBlockDim, appendOneDimension);
100  matrix_.resize(height, variableColOffsets_.back());
101  assertInvariants();
102  }
103 
109  static VerticalBlockMatrix LikeActiveViewOf(const VerticalBlockMatrix& rhs);
110 
114  static VerticalBlockMatrix LikeActiveViewOf(const SymmetricBlockMatrix& rhs, DenseIndex height);
115 
117  DenseIndex rows() const { assertInvariants(); return rowEnd_ - rowStart_; }
118 
120  DenseIndex cols() const { assertInvariants(); return variableColOffsets_.back() - variableColOffsets_[blockStart_]; }
121 
123  DenseIndex nBlocks() const { assertInvariants(); return variableColOffsets_.size() - 1 - blockStart_; }
124 
127 
129  const constBlock operator()(DenseIndex block) const { return range(block, block+1); }
130 
132  Block range(DenseIndex startBlock, DenseIndex endBlock) {
133  assertInvariants();
134  DenseIndex actualStartBlock = startBlock + blockStart_;
135  DenseIndex actualEndBlock = endBlock + blockStart_;
136  if(startBlock != 0 || endBlock != 0) {
137  checkBlock(actualStartBlock);
138  assert(actualEndBlock < (DenseIndex)variableColOffsets_.size());
139  }
140  const DenseIndex startCol = variableColOffsets_[actualStartBlock];
141  const DenseIndex rangeCols = variableColOffsets_[actualEndBlock] - startCol;
142  return matrix_.block(rowStart_, startCol, this->rows(), rangeCols);
143  }
144 
145  const constBlock range(DenseIndex startBlock, DenseIndex endBlock) const {
146  assertInvariants();
147  DenseIndex actualStartBlock = startBlock + blockStart_;
148  DenseIndex actualEndBlock = endBlock + blockStart_;
149  if(startBlock != 0 || endBlock != 0) {
150  checkBlock(actualStartBlock);
151  assert(actualEndBlock < (DenseIndex)variableColOffsets_.size());
152  }
153  const DenseIndex startCol = variableColOffsets_[actualStartBlock];
154  const DenseIndex rangeCols = variableColOffsets_[actualEndBlock] - startCol;
155  return ((const Matrix&)matrix_).block(rowStart_, startCol, this->rows(), rangeCols);
156  }
157 
159  Block full() { return range(0, nBlocks()); }
160 
162  const constBlock full() const { return range(0, nBlocks()); }
163 
165  assertInvariants();
166  DenseIndex actualBlock = block + blockStart_;
167  checkBlock(actualBlock);
168  return variableColOffsets_[actualBlock];
169  }
170 
172  const DenseIndex& rowStart() const { return rowStart_; }
173 
175  DenseIndex& rowStart() { return rowStart_; }
176 
178  const DenseIndex& rowEnd() const { return rowEnd_; }
179 
181  DenseIndex& rowEnd() { return rowEnd_; }
182 
184  const DenseIndex& firstBlock() const { return blockStart_; }
185 
187  DenseIndex& firstBlock() { return blockStart_; }
188 
190  const Matrix& matrix() const { return matrix_; }
191 
193  Matrix& matrix() { return matrix_; }
194 
195  protected:
196  void assertInvariants() const {
197  assert(matrix_.cols() == variableColOffsets_.back());
198  assert(blockStart_ < (DenseIndex)variableColOffsets_.size());
199  assert(rowStart_ <= matrix_.rows());
200  assert(rowEnd_ <= matrix_.rows());
201  assert(rowStart_ <= rowEnd_);
202  }
203 
205  static_cast<void>(block); //Disable unused varibale warnings.
206  assert(matrix_.cols() == variableColOffsets_.back());
207  assert(block < (DenseIndex)variableColOffsets_.size() - 1);
208  assert(variableColOffsets_[block] < matrix_.cols() && variableColOffsets_[block+1] <= matrix_.cols());
209  }
210 
211  template<typename ITERATOR>
212  void fillOffsets(ITERATOR firstBlockDim, ITERATOR lastBlockDim, bool appendOneDimension) {
213  variableColOffsets_[0] = 0;
214  DenseIndex j=0;
215  for(ITERATOR dim=firstBlockDim; dim!=lastBlockDim; ++dim, ++j)
216  variableColOffsets_[j+1] = variableColOffsets_[j] + *dim;
217  if(appendOneDimension)
218  variableColOffsets_[j+1] = variableColOffsets_[j] + 1;
219  }
220 
221  friend class SymmetricBlockMatrix;
222 
223  private:
224 #if GTSAM_ENABLE_BOOST_SERIALIZATION
225 
226  friend class boost::serialization::access;
227  template<class ARCHIVE>
228  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
229  ar & BOOST_SERIALIZATION_NVP(matrix_);
230  ar & BOOST_SERIALIZATION_NVP(variableColOffsets_);
231  ar & BOOST_SERIALIZATION_NVP(rowStart_);
232  ar & BOOST_SERIALIZATION_NVP(rowEnd_);
233  ar & BOOST_SERIALIZATION_NVP(blockStart_);
234  }
235 #endif
236  };
237 
238 }
gtsam::VerticalBlockMatrix::rowEnd
const DenseIndex & rowEnd() const
Definition: VerticalBlockMatrix.h:178
gtsam::VerticalBlockMatrix::firstBlock
DenseIndex & firstBlock()
Definition: VerticalBlockMatrix.h:187
gtsam::VerticalBlockMatrix::matrix_
Matrix matrix_
The full matrix.
Definition: VerticalBlockMatrix.h:52
MatrixSerialization.h
Serialization for matrices.
dimensions
const std::vector< size_t > dimensions
Definition: testVerticalBlockMatrix.cpp:27
Eigen::Block
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:103
FastVector.h
A thin wrapper around std::vector that uses a custom allocator.
gtsam::VerticalBlockMatrix::operator()
const constBlock operator()(DenseIndex block) const
Definition: VerticalBlockMatrix.h:129
gtsam::FastVector
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
Definition: FastVector.h:34
Matrix.h
typedef and functions to augment Eigen's MatrixXd
gtsam::VerticalBlockMatrix::range
Block range(DenseIndex startBlock, DenseIndex endBlock)
Definition: VerticalBlockMatrix.h:132
gtsam::VerticalBlockMatrix::blockStart_
DenseIndex blockStart_
Changes apparent matrix view, see main class comment.
Definition: VerticalBlockMatrix.h:57
gtsam::VerticalBlockMatrix::full
Block full()
Definition: VerticalBlockMatrix.h:159
gtsam::Matrix
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:39
gtsam::VerticalBlockMatrix::Block
Eigen::Block< Matrix > Block
Definition: VerticalBlockMatrix.h:48
gtsam::VerticalBlockMatrix::VerticalBlockMatrix
VerticalBlockMatrix(const CONTAINER &dimensions, DenseIndex height, bool appendOneDimension=false)
Definition: VerticalBlockMatrix.h:71
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
block
m m block(1, 0, 2, 2)<< 4
gtsam::VerticalBlockMatrix::range
const constBlock range(DenseIndex startBlock, DenseIndex endBlock) const
Definition: VerticalBlockMatrix.h:145
gtsam::VerticalBlockMatrix::rowEnd
DenseIndex & rowEnd()
Definition: VerticalBlockMatrix.h:181
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
gtsam::range
Double_ range(const Point2_ &p, const Point2_ &q)
Definition: slam/expressions.h:30
gtsam::VerticalBlockMatrix::firstBlock
const DenseIndex & firstBlock() const
Definition: VerticalBlockMatrix.h:184
gtsam::VerticalBlockMatrix::rowStart
const DenseIndex & rowStart() const
Definition: VerticalBlockMatrix.h:172
gtsam::VerticalBlockMatrix::rowEnd_
DenseIndex rowEnd_
Changes apparent matrix view, see main class comment.
Definition: VerticalBlockMatrix.h:56
gtsam::VerticalBlockMatrix::variableColOffsets_
FastVector< DenseIndex > variableColOffsets_
the starting columns of each block (0-based)
Definition: VerticalBlockMatrix.h:53
gtsam::VerticalBlockMatrix
Definition: VerticalBlockMatrix.h:44
gtsam::VerticalBlockMatrix::rowStart
DenseIndex & rowStart()
Definition: VerticalBlockMatrix.h:175
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
gtsam::VerticalBlockMatrix::VerticalBlockMatrix
VerticalBlockMatrix()
Definition: VerticalBlockMatrix.h:62
gtsam::VerticalBlockMatrix::nBlocks
DenseIndex nBlocks() const
Block count.
Definition: VerticalBlockMatrix.h:123
gtsam::VerticalBlockMatrix::VerticalBlockMatrix
VerticalBlockMatrix(const CONTAINER &dimensions, const Eigen::MatrixBase< DERIVED > &matrix, bool appendOneDimension=false)
Definition: VerticalBlockMatrix.h:82
gtsam::VerticalBlockMatrix::offset
DenseIndex offset(DenseIndex block) const
Definition: VerticalBlockMatrix.h:164
gtsam::VerticalBlockMatrix::This
VerticalBlockMatrix This
Definition: VerticalBlockMatrix.h:47
matrix
Map< Matrix< T, Dynamic, Dynamic, ColMajor >, 0, OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Definition: gtsam/3rdparty/Eigen/blas/common.h:110
gtsam::VerticalBlockMatrix::matrix
Matrix & matrix()
Definition: VerticalBlockMatrix.h:193
gtsam::VerticalBlockMatrix::constBlock
Eigen::Block< const Matrix > constBlock
Definition: VerticalBlockMatrix.h:49
gtsam
traits
Definition: SFMdata.h:40
gtsam::VerticalBlockMatrix::cols
DenseIndex cols() const
Column size.
Definition: VerticalBlockMatrix.h:120
gtsam::VerticalBlockMatrix::full
const constBlock full() const
Definition: VerticalBlockMatrix.h:162
gtsam::DenseIndex
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition: types.h:103
gtsam::VerticalBlockMatrix::matrix
const Matrix & matrix() const
Definition: VerticalBlockMatrix.h:190
gtsam::VerticalBlockMatrix::rows
DenseIndex rows() const
Row size.
Definition: VerticalBlockMatrix.h:117
gtsam::VerticalBlockMatrix::assertInvariants
void assertInvariants() const
Definition: VerticalBlockMatrix.h:196
gtsam::VerticalBlockMatrix::VerticalBlockMatrix
VerticalBlockMatrix(ITERATOR firstBlockDim, ITERATOR lastBlockDim, DenseIndex height, bool appendOneDimension=false)
Definition: VerticalBlockMatrix.h:95
Eigen::MatrixBase
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
gtsam::SymmetricBlockMatrix
Definition: SymmetricBlockMatrix.h:53
gtsam::VerticalBlockMatrix::checkBlock
void checkBlock(DenseIndex block) const
Definition: VerticalBlockMatrix.h:204
gtsam::VerticalBlockMatrix::fillOffsets
void fillOffsets(ITERATOR firstBlockDim, ITERATOR lastBlockDim, bool appendOneDimension)
Definition: VerticalBlockMatrix.h:212
gtsam::VerticalBlockMatrix::operator()
Block operator()(DenseIndex block)
Definition: VerticalBlockMatrix.h:126
gtsam::VerticalBlockMatrix::rowStart_
DenseIndex rowStart_
Changes apparent matrix view, see main class comment.
Definition: VerticalBlockMatrix.h:55


gtsam
Author(s):
autogenerated on Wed Jan 1 2025 04:08:23