g2o
sparse_block_matrix.h
Go to the documentation of this file.
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #ifndef G2O_SPARSE_BLOCK_MATRIX_
28 #define G2O_SPARSE_BLOCK_MATRIX_
29 
30 #include <map>
31 #include <vector>
32 #include <fstream>
33 #include <iostream>
34 #include <iomanip>
35 #include <cassert>
36 #include <Eigen/Core>
37 
39 #include "matrix_structure.h"
40 #include "matrix_operations.h"
41 #include "g2o/config.h"
42 
43 namespace g2o {
60 template <class MatrixType = MatrixXD >
62 
63  public:
65  typedef MatrixType SparseMatrixBlock;
66 
68  inline int cols() const {return _colBlockIndices.size() ? _colBlockIndices.back() : 0;}
70  inline int rows() const {return _rowBlockIndices.size() ? _rowBlockIndices.back() : 0;}
71 
72  typedef std::map<int, SparseMatrixBlock*> IntBlockMap;
73 
85  SparseBlockMatrix( const int * rbi, const int* cbi, int rb, int cb, bool hasStorage=true);
86 
88 
90 
91 
93  void clear(bool dealloc=false) ;
94 
96  SparseMatrixBlock* block(int r, int c, bool alloc=false);
98  const SparseMatrixBlock* block(int r, int c) const;
99 
101  inline int rowsOfBlock(int r) const { return r ? _rowBlockIndices[r] - _rowBlockIndices[r-1] : _rowBlockIndices[0] ; }
102 
104  inline int colsOfBlock(int c) const { return c ? _colBlockIndices[c] - _colBlockIndices[c-1] : _colBlockIndices[0]; }
105 
107  inline int rowBaseOfBlock(int r) const { return r ? _rowBlockIndices[r-1] : 0 ; }
108 
110  inline int colBaseOfBlock(int c) const { return c ? _colBlockIndices[c-1] : 0 ; }
111 
113  size_t nonZeros() const;
115  size_t nonZeroBlocks() const;
116 
118  SparseBlockMatrix* clone() const ;
119 
128  SparseBlockMatrix* slice(int rmin, int rmax, int cmin, int cmax, bool alloc=true) const;
129 
131  template <class MatrixTransposedType>
133 
135  bool add(SparseBlockMatrix<MatrixType>*& dest) const ;
136 
138  template <class MatrixResultType, class MatrixFactorType>
140 
142  void multiply(double*& dest, const double* src) const;
143 
148  void multiplySymmetricUpperTriangle(double*& dest, const double* src) const;
149 
151  void rightMultiply(double*& dest, const double* src) const;
152 
154  void scale( double a);
155 
160  bool symmPermutation(SparseBlockMatrix<MatrixType>*& dest, const int* pinv, bool onlyUpper=false) const;
161 
165  int fillCCS(int* Cp, int* Ci, double* Cx, bool upperTriangle = false) const;
166 
171  int fillCCS(double* Cx, bool upperTriangle = false) const;
172 
174  void fillBlockStructure(MatrixStructure& ms) const;
175 
177  const std::vector<IntBlockMap>& blockCols() const { return _blockCols;}
178  std::vector<IntBlockMap>& blockCols() { return _blockCols;}
179 
181  const std::vector<int>& rowBlockIndices() const { return _rowBlockIndices;}
182  std::vector<int>& rowBlockIndices() { return _rowBlockIndices;}
183 
185  const std::vector<int>& colBlockIndices() const { return _colBlockIndices;}
186  std::vector<int>& colBlockIndices() { return _colBlockIndices;}
187 
192  bool writeOctave(const char* filename, bool upperTriangle = true) const;
193 
199 
205 
211 
212  protected:
213  std::vector<int> _rowBlockIndices;
214  std::vector<int> _colBlockIndices;
215  std::vector <IntBlockMap> _blockCols;
219 };
220 
221 template < class MatrixType >
222 std::ostream& operator << (std::ostream&, const SparseBlockMatrix<MatrixType>& m);
223 
225 
226 } //end namespace
227 
228 #include "sparse_block_matrix.hpp"
229 
230 #endif
const std::vector< int > & rowBlockIndices() const
indices of the row blocks
std::vector< int > _rowBlockIndices
vector of the indices of the blocks along the rows.
std::vector< int > _colBlockIndices
int fillSparseBlockMatrixCCS(SparseBlockMatrixCCS< MatrixType > &blockCCS) const
bool transpose(SparseBlockMatrix< MatrixTransposedType > *&dest) const
transposes a block matrix, The transposed type should match the argument false on failure ...
int rowsOfBlock(int r) const
how many rows does the block at block-row r has?
int cols() const
columns of the matrix
bool writeOctave(const char *filename, bool upperTriangle=true) const
int rows() const
rows of the matrix
SparseMatrixBlock * block(int r, int c, bool alloc=false)
returns the block at location r,c. if alloc=true he block is created if it does not exist ...
representing the structure of a matrix in column compressed structure (only the upper triangular part...
size_t nonZeroBlocks() const
number of allocated blocks
int colsOfBlock(int c) const
how many cols does the block at block-col c has?
void clear(bool dealloc=false)
this zeroes all the blocks. If dealloc=true the blocks are removed from memory
MatrixType SparseMatrixBlock
this is the type of the elementary block, it is an Eigen::Matrix.
size_t nonZeros() const
number of non-zero elements
void fillBlockStructure(MatrixStructure &ms) const
exports the non zero blocks in the structure matrix ms
std::vector< IntBlockMap > _blockCols
const std::vector< int > & colBlockIndices() const
indices of the column blocks
SparseBlockMatrix * slice(int rmin, int rmax, int cmin, int cmax, bool alloc=true) const
std::vector< IntBlockMap > & blockCols()
void takePatternFromHash(SparseBlockMatrixHashMap< MatrixType > &hashMatrix)
std::vector< int > & colBlockIndices()
void multiplySymmetricUpperTriangle(double *&dest, const double *src) const
Sparse matrix which uses blocks.
int fillSparseBlockMatrixCCSTransposed(SparseBlockMatrixCCS< MatrixType > &blockCCS) const
void rightMultiply(double *&dest, const double *src) const
dest = M * (*this)
std::vector< int > & rowBlockIndices()
bool symmPermutation(SparseBlockMatrix< MatrixType > *&dest, const int *pinv, bool onlyUpper=false) const
std::map< int, SparseMatrixBlock * > IntBlockMap
int colBaseOfBlock(int c) const
where does the col at block-col r starts?
const std::vector< IntBlockMap > & blockCols() const
the block matrices per block-column
bool multiply(SparseBlockMatrix< MatrixResultType > *&dest, const SparseBlockMatrix< MatrixFactorType > *M) const
dest = (*this) * M
void scale(double a)
*this *= a
SparseBlockMatrix * clone() const
deep copy of a sparse-block-matrix;
SparseBlockMatrix< MatrixXD > SparseBlockMatrixXd
Sparse matrix which uses blocks based on hash structures.
bool add(SparseBlockMatrix< MatrixType > *&dest) const
adds the current matrix to the destination
int rowBaseOfBlock(int r) const
where does the row at block-row r starts?
Sparse matrix which uses blocks.
int fillCCS(int *Cp, int *Ci, double *Cx, bool upperTriangle=false) const