gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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>
21 #include <gtsam/base/FastVector.h>
22 
23 namespace gtsam {
24 
25  // Forward declarations
26  class SymmetricBlockMatrix;
27 
41  class GTSAM_EXPORT VerticalBlockMatrix
42  {
43  public:
44  typedef VerticalBlockMatrix This;
45  typedef Eigen::Block<Matrix> Block;
46  typedef Eigen::Block<const Matrix> constBlock;
47 
48  protected:
49  Matrix matrix_;
51 
55 
56  public:
57 
60  rowStart_(0), rowEnd_(0), blockStart_(0)
61  {
62  variableColOffsets_.push_back(0);
63  assertInvariants();
64  }
65 
67  template<typename CONTAINER>
68  VerticalBlockMatrix(const CONTAINER& dimensions, DenseIndex height, bool appendOneDimension = false) :
69  rowStart_(0), rowEnd_(height), blockStart_(0)
70  {
71  fillOffsets(dimensions.begin(), dimensions.end(), appendOneDimension);
72  matrix_.resize(height, variableColOffsets_.back());
73  assertInvariants();
74  }
75 
77  template<typename CONTAINER>
78  VerticalBlockMatrix(const CONTAINER& dimensions, const Matrix& matrix, bool appendOneDimension = false) :
79  matrix_(matrix), rowStart_(0), rowEnd_(matrix.rows()), blockStart_(0)
80  {
81  fillOffsets(dimensions.begin(), dimensions.end(), appendOneDimension);
82  if(variableColOffsets_.back() != matrix_.cols())
83  throw std::invalid_argument("Requested to create a VerticalBlockMatrix with dimensions that do not sum to the total columns of the provided matrix.");
84  assertInvariants();
85  }
86 
89  template<typename ITERATOR>
90  VerticalBlockMatrix(ITERATOR firstBlockDim, ITERATOR lastBlockDim, DenseIndex height, bool appendOneDimension = false) :
91  rowStart_(0), rowEnd_(height), blockStart_(0)
92  {
93  fillOffsets(firstBlockDim, lastBlockDim, appendOneDimension);
94  matrix_.resize(height, variableColOffsets_.back());
95  assertInvariants();
96  }
97 
103  static VerticalBlockMatrix LikeActiveViewOf(const VerticalBlockMatrix& rhs);
104 
108  static VerticalBlockMatrix LikeActiveViewOf(const SymmetricBlockMatrix& rhs, DenseIndex height);
109 
111  DenseIndex rows() const { assertInvariants(); return rowEnd_ - rowStart_; }
112 
114  DenseIndex cols() const { assertInvariants(); return variableColOffsets_.back() - variableColOffsets_[blockStart_]; }
115 
117  DenseIndex nBlocks() const { assertInvariants(); return variableColOffsets_.size() - 1 - blockStart_; }
118 
120  Block operator()(DenseIndex block) { return range(block, block+1); }
121 
123  const constBlock operator()(DenseIndex block) const { return range(block, block+1); }
124 
126  Block range(DenseIndex startBlock, DenseIndex endBlock) {
127  assertInvariants();
128  DenseIndex actualStartBlock = startBlock + blockStart_;
129  DenseIndex actualEndBlock = endBlock + blockStart_;
130  if(startBlock != 0 || endBlock != 0) {
131  checkBlock(actualStartBlock);
132  assert(actualEndBlock < (DenseIndex)variableColOffsets_.size());
133  }
134  const DenseIndex startCol = variableColOffsets_[actualStartBlock];
135  const DenseIndex rangeCols = variableColOffsets_[actualEndBlock] - startCol;
136  return matrix_.block(rowStart_, startCol, this->rows(), rangeCols);
137  }
138 
139  const constBlock range(DenseIndex startBlock, DenseIndex endBlock) const {
140  assertInvariants();
141  DenseIndex actualStartBlock = startBlock + blockStart_;
142  DenseIndex actualEndBlock = endBlock + blockStart_;
143  if(startBlock != 0 || endBlock != 0) {
144  checkBlock(actualStartBlock);
145  assert(actualEndBlock < (DenseIndex)variableColOffsets_.size());
146  }
147  const DenseIndex startCol = variableColOffsets_[actualStartBlock];
148  const DenseIndex rangeCols = variableColOffsets_[actualEndBlock] - startCol;
149  return ((const Matrix&)matrix_).block(rowStart_, startCol, this->rows(), rangeCols);
150  }
151 
153  Block full() { return range(0, nBlocks()); }
154 
156  const constBlock full() const { return range(0, nBlocks()); }
157 
158  DenseIndex offset(DenseIndex block) const {
159  assertInvariants();
160  DenseIndex actualBlock = block + blockStart_;
161  checkBlock(actualBlock);
162  return variableColOffsets_[actualBlock];
163  }
164 
166  DenseIndex& rowStart() { return rowStart_; }
167 
169  DenseIndex& rowEnd() { return rowEnd_; }
170 
172  DenseIndex& firstBlock() { return blockStart_; }
173 
175  DenseIndex rowStart() const { return rowStart_; }
176 
178  DenseIndex rowEnd() const { return rowEnd_; }
179 
181  DenseIndex firstBlock() const { return blockStart_; }
182 
184  const Matrix& matrix() const { return matrix_; }
185 
187  Matrix& matrix() { return matrix_; }
188 
189  protected:
190  void assertInvariants() const {
191  assert(matrix_.cols() == variableColOffsets_.back());
192  assert(blockStart_ < (DenseIndex)variableColOffsets_.size());
193  assert(rowStart_ <= matrix_.rows());
194  assert(rowEnd_ <= matrix_.rows());
195  assert(rowStart_ <= rowEnd_);
196  }
197 
198  void checkBlock(DenseIndex block) const {
199  assert(matrix_.cols() == variableColOffsets_.back());
200  assert(block < (DenseIndex)variableColOffsets_.size() - 1);
201  assert(variableColOffsets_[block] < matrix_.cols() && variableColOffsets_[block+1] <= matrix_.cols());
202  }
203 
204  template<typename ITERATOR>
205  void fillOffsets(ITERATOR firstBlockDim, ITERATOR lastBlockDim, bool appendOneDimension) {
206  variableColOffsets_.resize((lastBlockDim-firstBlockDim) + 1 + (appendOneDimension ? 1 : 0));
207  variableColOffsets_[0] = 0;
208  DenseIndex j=0;
209  for(ITERATOR dim=firstBlockDim; dim!=lastBlockDim; ++dim) {
210  variableColOffsets_[j+1] = variableColOffsets_[j] + *dim;
211  ++ j;
212  }
213  if(appendOneDimension)
214  {
215  variableColOffsets_[j+1] = variableColOffsets_[j] + 1;
216  ++ j;
217  }
218  }
219 
220  friend class SymmetricBlockMatrix;
221 
222  private:
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  };
234 
235 }
Block full()
Return the full matrix, not including any portions excluded by rowStart(), rowEnd(), and firstBlock()
Definition: VerticalBlockMatrix.h:153
DenseIndex rows() const
Row size.
Definition: VerticalBlockMatrix.h:111
DenseIndex & firstBlock()
Get or set the apparent first block for all operations.
Definition: VerticalBlockMatrix.h:172
Matrix & matrix()
Non-const access to full matrix (including any portions excluded by rowStart(), rowEnd(), and firstBlock())
Definition: VerticalBlockMatrix.h:187
Block operator()(DenseIndex block)
Access a single block in the underlying matrix with read/write access.
Definition: VerticalBlockMatrix.h:120
DenseIndex rowEnd_
Changes apparent matrix view, see main class comment.
Definition: VerticalBlockMatrix.h:53
DenseIndex cols() const
Column size.
Definition: VerticalBlockMatrix.h:114
Definition: VerticalBlockMatrix.h:41
DenseIndex nBlocks() const
Block count.
Definition: VerticalBlockMatrix.h:117
DenseIndex & rowStart()
Get or set the apparent first row of the underlying matrix for all operations.
Definition: VerticalBlockMatrix.h:166
size_t dim(const Vector &v)
dimensionality == size
Definition: Vector.h:90
VerticalBlockMatrix(ITERATOR firstBlockDim, ITERATOR lastBlockDim, DenseIndex height, bool appendOneDimension=false)
Construct from iterator over the sizes of each vertical block.
Definition: VerticalBlockMatrix.h:90
DenseIndex rowStart_
Changes apparent matrix view, see main class comment.
Definition: VerticalBlockMatrix.h:52
const constBlock full() const
Return the full matrix, not including any portions excluded by rowStart(), rowEnd(), and firstBlock()
Definition: VerticalBlockMatrix.h:156
VerticalBlockMatrix()
Construct an empty VerticalBlockMatrix.
Definition: VerticalBlockMatrix.h:59
DenseIndex rowStart() const
Get the apparent first row of the underlying matrix for all operations.
Definition: VerticalBlockMatrix.h:175
Matrix matrix_
The full matrix.
Definition: VerticalBlockMatrix.h:49
VerticalBlockMatrix(const CONTAINER &dimensions, DenseIndex height, bool appendOneDimension=false)
Construct from a container of the sizes of each vertical block.
Definition: VerticalBlockMatrix.h:68
FastVector< DenseIndex > variableColOffsets_
the starting columns of each block (0-based)
Definition: VerticalBlockMatrix.h:50
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition: types.h:74
DenseIndex blockStart_
Changes apparent matrix view, see main class comment.
Definition: VerticalBlockMatrix.h:54
const constBlock operator()(DenseIndex block) const
Access a const block view.
Definition: VerticalBlockMatrix.h:123
typedef and functions to augment Eigen's MatrixXd
Definition: SymmetricBlockMatrix.h:40
DenseIndex firstBlock() const
Get the apparent first block for all operations.
Definition: VerticalBlockMatrix.h:181
const Matrix & matrix() const
Access to full matrix (including any portions excluded by rowStart(), rowEnd(), and firstBlock()) ...
Definition: VerticalBlockMatrix.h:184
VerticalBlockMatrix(const CONTAINER &dimensions, const Matrix &matrix, bool appendOneDimension=false)
Construct from a container of the sizes of each vertical block and a pre-prepared matrix...
Definition: VerticalBlockMatrix.h:78
DenseIndex & rowEnd()
Get or set the apparent last row (exclusive, i.e.
Definition: VerticalBlockMatrix.h:169
DenseIndex rowEnd() const
Get the apparent last row (exclusive, i.e.
Definition: VerticalBlockMatrix.h:178
A thin wrapper around std::vector that uses boost's pool_allocator.
Block range(DenseIndex startBlock, DenseIndex endBlock)
access ranges of blocks at a time
Definition: VerticalBlockMatrix.h:126