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 namespace gtsam {
25 
26  // Forward declarations
27  class SymmetricBlockMatrix;
28 
42  class GTSAM_EXPORT VerticalBlockMatrix
43  {
44  public:
48 
49  protected:
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 
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 
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:
222 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
223 
224  friend class boost::serialization::access;
225  template<class ARCHIVE>
226  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
227  ar & BOOST_SERIALIZATION_NVP(matrix_);
228  ar & BOOST_SERIALIZATION_NVP(variableColOffsets_);
229  ar & BOOST_SERIALIZATION_NVP(rowStart_);
230  ar & BOOST_SERIALIZATION_NVP(rowEnd_);
231  ar & BOOST_SERIALIZATION_NVP(blockStart_);
232  }
233 #endif
234  };
235 
236 }
const constBlock range(DenseIndex startBlock, DenseIndex endBlock) const
DenseIndex cols() const
Column size.
VerticalBlockMatrix(const CONTAINER &dimensions, const Eigen::MatrixBase< DERIVED > &matrix, bool appendOneDimension=false)
m m block(1, 0, 2, 2)<< 4
std::string serialize(const T &input)
serializes to a string
DenseIndex nBlocks() const
Block count.
const constBlock full() const
DenseIndex rows() const
Row size.
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
Definition: FastVector.h:34
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:39
const Matrix & matrix() const
Block operator()(DenseIndex block)
DenseIndex offset(DenseIndex block) const
Eigen::Block< const Matrix > constBlock
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition: types.h:108
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
void checkBlock(DenseIndex block) const
Matrix matrix_
The full matrix.
VerticalBlockMatrix(ITERATOR firstBlockDim, ITERATOR lastBlockDim, DenseIndex height, bool appendOneDimension=false)
void fillOffsets(ITERATOR firstBlockDim, ITERATOR lastBlockDim, bool appendOneDimension)
const DenseIndex & rowEnd() const
DenseIndex rowStart_
Changes apparent matrix view, see main class comment.
Serialization for matrices.
Block range(DenseIndex startBlock, DenseIndex endBlock)
A thin wrapper around std::vector that uses a custom allocator.
traits
Definition: chartTesting.h:28
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:103
const constBlock operator()(DenseIndex block) const
Double_ range(const Point2_ &p, const Point2_ &q)
Eigen::Block< Matrix > Block
FastVector< DenseIndex > variableColOffsets_
the starting columns of each block (0-based)
VerticalBlockMatrix(const CONTAINER &dimensions, DenseIndex height, bool appendOneDimension=false)
const DenseIndex & firstBlock() const
DenseIndex blockStart_
Changes apparent matrix view, see main class comment.
DenseIndex rowEnd_
Changes apparent matrix view, see main class comment.
Map< Matrix< T, Dynamic, Dynamic, ColMajor >, 0, OuterStride<> > matrix(T *data, int rows, int cols, int stride)
const std::vector< size_t > dimensions
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
const DenseIndex & rowStart() const
std::ptrdiff_t j


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:40:44