gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SymmetricBlockMatrix.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>
23 
24 namespace gtsam {
25 
26  // Forward declarations
27  class VerticalBlockMatrix;
28 
40  class GTSAM_EXPORT SymmetricBlockMatrix
41  {
42  public:
43  typedef SymmetricBlockMatrix This;
46 
47  protected:
48  Matrix matrix_;
50 
52 
53  public:
56  blockStart_(0)
57  {
58  variableColOffsets_.push_back(0);
59  assertInvariants();
60  }
61 
63  template<typename CONTAINER>
64  SymmetricBlockMatrix(const CONTAINER& dimensions, bool appendOneDimension = false) :
65  blockStart_(0)
66  {
67  fillOffsets(dimensions.begin(), dimensions.end(), appendOneDimension);
68  matrix_.resize(variableColOffsets_.back(), variableColOffsets_.back());
69  assertInvariants();
70  }
71 
73  template<typename ITERATOR>
74  SymmetricBlockMatrix(ITERATOR firstBlockDim, ITERATOR lastBlockDim, bool appendOneDimension = false) :
75  blockStart_(0)
76  {
77  fillOffsets(firstBlockDim, lastBlockDim, appendOneDimension);
78  matrix_.resize(variableColOffsets_.back(), variableColOffsets_.back());
79  assertInvariants();
80  }
81 
83  template<typename CONTAINER>
84  SymmetricBlockMatrix(const CONTAINER& dimensions, const Matrix& matrix, bool appendOneDimension = false) :
85  blockStart_(0)
86  {
87  matrix_.resize(matrix.rows(), matrix.cols());
88  matrix_.triangularView<Eigen::Upper>() = matrix.triangularView<Eigen::Upper>();
89  fillOffsets(dimensions.begin(), dimensions.end(), appendOneDimension);
90  if(matrix_.rows() != matrix_.cols())
91  throw std::invalid_argument("Requested to create a SymmetricBlockMatrix from a non-square matrix.");
92  if(variableColOffsets_.back() != matrix_.cols())
93  throw std::invalid_argument("Requested to create a SymmetricBlockMatrix with dimensions that do not sum to the total size of the provided matrix.");
94  assertInvariants();
95  }
96 
100  static SymmetricBlockMatrix LikeActiveViewOf(const SymmetricBlockMatrix& other);
101 
105  static SymmetricBlockMatrix LikeActiveViewOf(const VerticalBlockMatrix& other);
106 
108  DenseIndex rows() const { assertInvariants(); return variableColOffsets_.back() - variableColOffsets_[blockStart_]; }
109 
111  DenseIndex cols() const { return rows(); }
112 
114  DenseIndex nBlocks() const { assertInvariants(); return variableColOffsets_.size() - 1 - blockStart_; }
115 
118  Block operator()(DenseIndex i_block, DenseIndex j_block) {
119  return Block(*this, i_block, j_block);
120  }
121 
124  constBlock operator()(DenseIndex i_block, DenseIndex j_block) const {
125  return constBlock(*this, i_block, j_block);
126  }
127 
132  Block range(DenseIndex i_startBlock, DenseIndex i_endBlock, DenseIndex j_startBlock, DenseIndex j_endBlock) {
133  assertInvariants();
134  return Block(*this, i_startBlock, j_startBlock, i_endBlock - i_startBlock, j_endBlock - j_startBlock);
135  }
136 
141  constBlock range(DenseIndex i_startBlock, DenseIndex i_endBlock, DenseIndex j_startBlock, DenseIndex j_endBlock) const {
142  assertInvariants();
143  return constBlock(*this, i_startBlock, j_startBlock, i_endBlock - i_startBlock, j_endBlock - j_startBlock);
144  }
145 
148  {
149  return Block(*this, 0, nBlocks(), 0);
150  }
151 
153  constBlock full() const
154  {
155  return constBlock(*this, 0, nBlocks(), 0);
156  }
157 
159  Eigen::SelfAdjointView<const Matrix, Eigen::Upper> matrix() const
160  {
161  return matrix_;
162  }
163 
165  Eigen::SelfAdjointView<Matrix, Eigen::Upper> matrix()
166  {
167  return matrix_;
168  }
169 
172  {
173  assertInvariants();
174  DenseIndex actualBlock = block + blockStart_;
175  checkBlock(actualBlock);
176  return variableColOffsets_[actualBlock];
177  }
178 
182  DenseIndex& blockStart() { return blockStart_; }
183 
186  DenseIndex blockStart() const { return blockStart_; }
187 
191 
192  protected:
193  void assertInvariants() const
194  {
195  assert(matrix_.rows() == matrix_.cols());
196  assert(matrix_.cols() == variableColOffsets_.back());
197  assert(blockStart_ < (DenseIndex)variableColOffsets_.size());
198  }
199 
200  void checkBlock(DenseIndex block) const
201  {
202  assert(matrix_.rows() == matrix_.cols());
203  assert(matrix_.cols() == variableColOffsets_.back());
204  assert(block >= 0);
205  assert(block < (DenseIndex)variableColOffsets_.size()-1);
206  assert(variableColOffsets_[block] < matrix_.cols() && variableColOffsets_[block+1] <= matrix_.cols());
207  }
208 
209  DenseIndex offsetUnchecked(DenseIndex block) const
210  {
211  return variableColOffsets_[block + blockStart_];
212  }
213 
214  template<typename ITERATOR>
215  void fillOffsets(ITERATOR firstBlockDim, ITERATOR lastBlockDim, bool appendOneDimension)
216  {
217  variableColOffsets_.resize((lastBlockDim-firstBlockDim) + 1 + (appendOneDimension ? 1 : 0));
218  variableColOffsets_[0] = 0;
219  DenseIndex j=0;
220  for(ITERATOR dim=firstBlockDim; dim!=lastBlockDim; ++dim) {
221  variableColOffsets_[j+1] = variableColOffsets_[j] + *dim;
222  ++ j;
223  }
224  if(appendOneDimension)
225  {
226  variableColOffsets_[j+1] = variableColOffsets_[j] + 1;
227  ++ j;
228  }
229  }
230 
231  friend class VerticalBlockMatrix;
232  template<typename SymmetricBlockMatrixType> friend class SymmetricBlockMatrixBlockExpr;
233 
234  private:
236  friend class boost::serialization::access;
237  template<class ARCHIVE>
238  void serialize(ARCHIVE & ar, const unsigned int version) {
239  // Fill in the lower triangle part of the matrix, so boost::serialization won't
240  // complain about uninitialized data with an input_stream_error exception
241  // http://www.boost.org/doc/libs/1_37_0/libs/serialization/doc/exceptions.html#stream_error
242  matrix_.triangularView<Eigen::Lower>() = matrix_.triangularView<Eigen::Upper>().transpose();
243  ar & BOOST_SERIALIZATION_NVP(matrix_);
244  ar & BOOST_SERIALIZATION_NVP(variableColOffsets_);
245  ar & BOOST_SERIALIZATION_NVP(blockStart_);
246  }
247  };
248 
249  /* ************************************************************************* */
250  class CholeskyFailed : public gtsam::ThreadsafeException<CholeskyFailed>
251  {
252  public:
253  CholeskyFailed() throw() {}
254  virtual ~CholeskyFailed() throw() {}
255  };
256 
257 }
258 
constBlock operator()(DenseIndex i_block, DenseIndex j_block) const
Access the block with vertical block index i_block and horizontal block index j_block.
Definition: SymmetricBlockMatrix.h:124
DenseIndex cols() const
Column size.
Definition: SymmetricBlockMatrix.h:111
Eigen::SelfAdjointView< const Matrix, Eigen::Upper > matrix() const
Access to full matrix, including any portions excluded by firstBlock() to other operations.
Definition: SymmetricBlockMatrix.h:159
SymmetricBlockMatrix(ITERATOR firstBlockDim, ITERATOR lastBlockDim, bool appendOneDimension=false)
Construct from iterator over the sizes of each vertical block.
Definition: SymmetricBlockMatrix.h:74
bool choleskyPartial(Matrix &ABC, size_t nFrontal)
Partial Cholesky computes a factor [R S such that [R' 0 [R S = [A B 0 L] S' I] 0 L] B' C]...
Definition: cholesky.cpp:121
Base exception type that uses tbb_exception if GTSAM is compiled with TBB.
Definition: types.h:154
Block operator()(DenseIndex i_block, DenseIndex j_block)
Access the block with vertical block index i_block and horizontal block index j_block.
Definition: SymmetricBlockMatrix.h:118
Definition: VerticalBlockMatrix.h:41
FastVector< DenseIndex > variableColOffsets_
the starting columns of each block (0-based)
Definition: SymmetricBlockMatrix.h:49
Eigen::SelfAdjointView< Matrix, Eigen::Upper > matrix()
Access to full matrix, including any portions excluded by firstBlock() to other operations.
Definition: SymmetricBlockMatrix.h:165
size_t dim(const Vector &v)
dimensionality == size
Definition: Vector.h:90
DenseIndex rows() const
Row size.
Definition: SymmetricBlockMatrix.h:108
SymmetricBlockMatrix(const CONTAINER &dimensions, bool appendOneDimension=false)
Construct from a container of the sizes of each block.
Definition: SymmetricBlockMatrix.h:64
SymmetricBlockMatrix(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: SymmetricBlockMatrix.h:84
DenseIndex blockStart() const
Retrieve the first logical block, i.e.
Definition: SymmetricBlockMatrix.h:186
constBlock full() const
Return the full matrix, not including any portions excluded by firstBlock().
Definition: SymmetricBlockMatrix.h:153
A matrix expression that references a single block of a SymmetricBlockMatrix.
Definition: SymmetricBlockMatrixBlockExpr.h:22
Definition: SymmetricBlockMatrix.h:250
constBlock range(DenseIndex i_startBlock, DenseIndex i_endBlock, DenseIndex j_startBlock, DenseIndex j_endBlock) const
Access the range of blocks starting with vertical block index i_startBlock, ending with vertical bloc...
Definition: SymmetricBlockMatrix.h:141
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition: types.h:74
typedef and functions to augment Eigen's MatrixXd
Matrix matrix_
The full matrix.
Definition: SymmetricBlockMatrix.h:48
Block range(DenseIndex i_startBlock, DenseIndex i_endBlock, DenseIndex j_startBlock, DenseIndex j_endBlock)
Access the range of blocks starting with vertical block index i_startBlock, ending with vertical bloc...
Definition: SymmetricBlockMatrix.h:132
Definition: SymmetricBlockMatrix.h:40
DenseIndex & blockStart()
Retrieve or modify the first logical block, i.e.
Definition: SymmetricBlockMatrix.h:182
SymmetricBlockMatrix()
Construct from an empty matrix (asserts that the matrix is empty)
Definition: SymmetricBlockMatrix.h:55
Block full()
Return the full matrix, not including any portions excluded by firstBlock().
Definition: SymmetricBlockMatrix.h:147
DenseIndex blockStart_
Changes apparent matrix view, see main class comment.
Definition: SymmetricBlockMatrix.h:51
DenseIndex offset(DenseIndex block) const
Return the absolute offset in the underlying matrix of the start of the specified block...
Definition: SymmetricBlockMatrix.h:171
DenseIndex nBlocks() const
Block count.
Definition: SymmetricBlockMatrix.h:114
Matrix expression for a block of a SymmetricBlockMatrix.
A thin wrapper around std::vector that uses boost's pool_allocator.