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 
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 }
gtsam::VerticalBlockMatrix::rowEnd
const DenseIndex & rowEnd() const
Definition: VerticalBlockMatrix.h:176
gtsam::VerticalBlockMatrix::firstBlock
DenseIndex & firstBlock()
Definition: VerticalBlockMatrix.h:185
gtsam::VerticalBlockMatrix::matrix_
Matrix matrix_
The full matrix.
Definition: VerticalBlockMatrix.h:50
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:127
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:130
gtsam::VerticalBlockMatrix::blockStart_
DenseIndex blockStart_
Changes apparent matrix view, see main class comment.
Definition: VerticalBlockMatrix.h:55
gtsam::VerticalBlockMatrix::full
Block full()
Definition: VerticalBlockMatrix.h:157
gtsam::Matrix
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:39
gtsam::VerticalBlockMatrix::Block
Eigen::Block< Matrix > Block
Definition: VerticalBlockMatrix.h:46
gtsam::VerticalBlockMatrix::VerticalBlockMatrix
VerticalBlockMatrix(const CONTAINER &dimensions, DenseIndex height, bool appendOneDimension=false)
Definition: VerticalBlockMatrix.h:69
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:143
gtsam::VerticalBlockMatrix::rowEnd
DenseIndex & rowEnd()
Definition: VerticalBlockMatrix.h:179
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:182
gtsam::VerticalBlockMatrix::rowStart
const DenseIndex & rowStart() const
Definition: VerticalBlockMatrix.h:170
gtsam::VerticalBlockMatrix::rowEnd_
DenseIndex rowEnd_
Changes apparent matrix view, see main class comment.
Definition: VerticalBlockMatrix.h:54
gtsam::VerticalBlockMatrix::variableColOffsets_
FastVector< DenseIndex > variableColOffsets_
the starting columns of each block (0-based)
Definition: VerticalBlockMatrix.h:51
gtsam::VerticalBlockMatrix
Definition: VerticalBlockMatrix.h:42
gtsam::VerticalBlockMatrix::rowStart
DenseIndex & rowStart()
Definition: VerticalBlockMatrix.h:173
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
gtsam::VerticalBlockMatrix::VerticalBlockMatrix
VerticalBlockMatrix()
Definition: VerticalBlockMatrix.h:60
gtsam::VerticalBlockMatrix::nBlocks
DenseIndex nBlocks() const
Block count.
Definition: VerticalBlockMatrix.h:121
gtsam::VerticalBlockMatrix::VerticalBlockMatrix
VerticalBlockMatrix(const CONTAINER &dimensions, const Eigen::MatrixBase< DERIVED > &matrix, bool appendOneDimension=false)
Definition: VerticalBlockMatrix.h:80
gtsam::VerticalBlockMatrix::offset
DenseIndex offset(DenseIndex block) const
Definition: VerticalBlockMatrix.h:162
gtsam::VerticalBlockMatrix::This
VerticalBlockMatrix This
Definition: VerticalBlockMatrix.h:45
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:191
gtsam::VerticalBlockMatrix::constBlock
Eigen::Block< const Matrix > constBlock
Definition: VerticalBlockMatrix.h:47
gtsam
traits
Definition: SFMdata.h:40
gtsam::VerticalBlockMatrix::cols
DenseIndex cols() const
Column size.
Definition: VerticalBlockMatrix.h:118
gtsam::VerticalBlockMatrix::full
const constBlock full() const
Definition: VerticalBlockMatrix.h:160
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:188
gtsam::VerticalBlockMatrix::rows
DenseIndex rows() const
Row size.
Definition: VerticalBlockMatrix.h:115
gtsam::VerticalBlockMatrix::assertInvariants
void assertInvariants() const
Definition: VerticalBlockMatrix.h:194
gtsam::VerticalBlockMatrix::VerticalBlockMatrix
VerticalBlockMatrix(ITERATOR firstBlockDim, ITERATOR lastBlockDim, DenseIndex height, bool appendOneDimension=false)
Definition: VerticalBlockMatrix.h:93
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:202
gtsam::VerticalBlockMatrix::fillOffsets
void fillOffsets(ITERATOR firstBlockDim, ITERATOR lastBlockDim, bool appendOneDimension)
Definition: VerticalBlockMatrix.h:210
gtsam::VerticalBlockMatrix::operator()
Block operator()(DenseIndex block)
Definition: VerticalBlockMatrix.h:124
gtsam::VerticalBlockMatrix::rowStart_
DenseIndex rowStart_
Changes apparent matrix view, see main class comment.
Definition: VerticalBlockMatrix.h:53


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:09:42