g2o
Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | List of all members
g2o::MarginalCovarianceCholesky Class Reference

computing the marginal covariance given a cholesky factor (lower triangle of the factor) More...

#include <marginal_covariance_cholesky.h>

Public Member Functions

 MarginalCovarianceCholesky ()
 
 ~MarginalCovarianceCholesky ()
 
void computeCovariance (double **covBlocks, const std::vector< int > &blockIndices)
 
void computeCovariance (SparseBlockMatrix< MatrixXD > &spinv, const std::vector< int > &rowBlockIndices, const std::vector< std::pair< int, int > > &blockIndices)
 
void setCholeskyFactor (int n, int *Lp, int *Li, double *Lx, int *permInv)
 

Protected Types

typedef std::unordered_map< int, double > LookupMap
 

Protected Member Functions

int computeIndex (int r, int c) const
 compute the index used for hashing More...
 
double computeEntry (int r, int c)
 

Protected Attributes

int _n
 L is an n X n matrix. More...
 
int * _Ap
 column pointer of the CCS storage More...
 
int * _Ai
 row indices of the CCS storage More...
 
double * _Ax
 values of the cholesky factor More...
 
int * _perm
 permutation of the cholesky factor. Variable re-ordering for better fill-in More...
 
LookupMap _map
 hash look up table for the already computed entries More...
 
std::vector< double > _diag
 cache 1 / H_ii to avoid recalculations More...
 

Detailed Description

computing the marginal covariance given a cholesky factor (lower triangle of the factor)

Definition at line 45 of file marginal_covariance_cholesky.h.

Member Typedef Documentation

typedef std::unordered_map<int, double> g2o::MarginalCovarianceCholesky::LookupMap
protected

hash struct for storing the matrix elements needed to compute the covariance

Definition at line 50 of file marginal_covariance_cholesky.h.

Constructor & Destructor Documentation

g2o::MarginalCovarianceCholesky::MarginalCovarianceCholesky ( )

Definition at line 45 of file marginal_covariance_cholesky.cpp.

45  :
46  _n(0), _Ap(0), _Ai(0), _Ax(0), _perm(0)
47 {
48 }
int * _Ai
row indices of the CCS storage
int * _Ap
column pointer of the CCS storage
double * _Ax
values of the cholesky factor
int * _perm
permutation of the cholesky factor. Variable re-ordering for better fill-in
g2o::MarginalCovarianceCholesky::~MarginalCovarianceCholesky ( )

Definition at line 50 of file marginal_covariance_cholesky.cpp.

51 {
52 }

Member Function Documentation

void g2o::MarginalCovarianceCholesky::computeCovariance ( double **  covBlocks,
const std::vector< int > &  blockIndices 
)

compute the marginal cov for the given block indices, write the result to the covBlocks memory (which has to be provided by the caller).

Definition at line 102 of file marginal_covariance_cholesky.cpp.

References _map, _perm, g2o::MatrixElem::c, computeEntry(), computeIndex(), and g2o::MatrixElem::r.

Referenced by g2o::LinearSolverCSparse< MatrixType >::solveBlocks(), g2o::LinearSolverCholmod< MatrixType >::solveBlocks(), g2o::LinearSolverCSparse< MatrixType >::solvePattern(), and g2o::LinearSolverCholmod< MatrixType >::solvePattern().

103 {
104  _map.clear();
105  int base = 0;
106  vector<MatrixElem> elemsToCompute;
107  for (size_t i = 0; i < blockIndices.size(); ++i) {
108  int nbase = blockIndices[i];
109  int vdim = nbase - base;
110  for (int rr = 0; rr < vdim; ++rr)
111  for (int cc = rr; cc < vdim; ++cc) {
112  int r = _perm ? _perm[rr + base] : rr + base; // apply permutation
113  int c = _perm ? _perm[cc + base] : cc + base;
114  if (r > c) // make sure it's still upper triangular after applying the permutation
115  swap(r, c);
116  elemsToCompute.push_back(MatrixElem(r, c));
117  }
118  base = nbase;
119  }
120 
121  // sort the elems to reduce the recursive calls
122  sort(elemsToCompute.begin(), elemsToCompute.end());
123 
124  // compute the inverse elements we need
125  for (size_t i = 0; i < elemsToCompute.size(); ++i) {
126  const MatrixElem& me = elemsToCompute[i];
127  computeEntry(me.r, me.c);
128  }
129 
130  // set the marginal covariance for the vertices, by writing to the blocks memory
131  base = 0;
132  for (size_t i = 0; i < blockIndices.size(); ++i) {
133  int nbase = blockIndices[i];
134  int vdim = nbase - base;
135  double* cov = covBlocks[i];
136  for (int rr = 0; rr < vdim; ++rr)
137  for (int cc = rr; cc < vdim; ++cc) {
138  int r = _perm ? _perm[rr + base] : rr + base; // apply permutation
139  int c = _perm ? _perm[cc + base] : cc + base;
140  if (r > c) // upper triangle
141  swap(r, c);
142  int idx = computeIndex(r, c);
143  LookupMap::const_iterator foundIt = _map.find(idx);
144  assert(foundIt != _map.end());
145  cov[rr*vdim + cc] = foundIt->second;
146  if (rr != cc)
147  cov[cc*vdim + rr] = foundIt->second;
148  }
149  base = nbase;
150  }
151 }
LookupMap _map
hash look up table for the already computed entries
int * _perm
permutation of the cholesky factor. Variable re-ordering for better fill-in
int computeIndex(int r, int c) const
compute the index used for hashing
void g2o::MarginalCovarianceCholesky::computeCovariance ( SparseBlockMatrix< MatrixXD > &  spinv,
const std::vector< int > &  rowBlockIndices,
const std::vector< std::pair< int, int > > &  blockIndices 
)

compute the marginal cov for the given block indices, write the result in spinv).

Definition at line 154 of file marginal_covariance_cholesky.cpp.

References _map, _perm, g2o::SparseBlockMatrix< MatrixType >::block(), g2o::MatrixElem::c, g2o::SparseBlockMatrix< MatrixType >::colBaseOfBlock(), computeEntry(), computeIndex(), g2o::MatrixElem::r, and g2o::SparseBlockMatrix< MatrixType >::rowBaseOfBlock().

155 {
156  // allocate the sparse
157  spinv = SparseBlockMatrix<MatrixXD>(&rowBlockIndices[0],
158  &rowBlockIndices[0],
159  rowBlockIndices.size(),
160  rowBlockIndices.size(), true);
161  _map.clear();
162  vector<MatrixElem> elemsToCompute;
163  for (size_t i = 0; i < blockIndices.size(); ++i) {
164  int blockRow=blockIndices[i].first;
165  int blockCol=blockIndices[i].second;
166  assert(blockRow>=0);
167  assert(blockRow < (int)rowBlockIndices.size());
168  assert(blockCol>=0);
169  assert(blockCol < (int)rowBlockIndices.size());
170 
171  int rowBase=spinv.rowBaseOfBlock(blockRow);
172  int colBase=spinv.colBaseOfBlock(blockCol);
173 
174  MatrixXD *block=spinv.block(blockRow, blockCol, true);
175  assert(block);
176  for (int iRow=0; iRow<block->rows(); ++iRow)
177  for (int iCol=0; iCol<block->cols(); ++iCol){
178  int rr=rowBase+iRow;
179  int cc=colBase+iCol;
180  int r = _perm ? _perm[rr] : rr; // apply permutation
181  int c = _perm ? _perm[cc] : cc;
182  if (r > c)
183  swap(r, c);
184  elemsToCompute.push_back(MatrixElem(r, c));
185  }
186  }
187 
188  // sort the elems to reduce the number of recursive calls
189  sort(elemsToCompute.begin(), elemsToCompute.end());
190 
191  // compute the inverse elements we need
192  for (size_t i = 0; i < elemsToCompute.size(); ++i) {
193  const MatrixElem& me = elemsToCompute[i];
194  computeEntry(me.r, me.c);
195  }
196 
197  // set the marginal covariance
198  for (size_t i = 0; i < blockIndices.size(); ++i) {
199  int blockRow=blockIndices[i].first;
200  int blockCol=blockIndices[i].second;
201  int rowBase=spinv.rowBaseOfBlock(blockRow);
202  int colBase=spinv.colBaseOfBlock(blockCol);
203 
204  MatrixXD *block=spinv.block(blockRow, blockCol);
205  assert(block);
206  for (int iRow=0; iRow<block->rows(); ++iRow)
207  for (int iCol=0; iCol<block->cols(); ++iCol){
208  int rr=rowBase+iRow;
209  int cc=colBase+iCol;
210  int r = _perm ? _perm[rr] : rr; // apply permutation
211  int c = _perm ? _perm[cc] : cc;
212  if (r > c)
213  swap(r, c);
214  int idx = computeIndex(r, c);
215  LookupMap::const_iterator foundIt = _map.find(idx);
216  assert(foundIt != _map.end());
217  (*block)(iRow, iCol) = foundIt->second;
218  }
219  }
220 }
LookupMap _map
hash look up table for the already computed entries
int * _perm
permutation of the cholesky factor. Variable re-ordering for better fill-in
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatrixXD
Definition: eigen_types.h:63
int computeIndex(int r, int c) const
compute the index used for hashing
double g2o::MarginalCovarianceCholesky::computeEntry ( int  r,
int  c 
)
protected

compute one entry in the covariance, r and c are values after applying the permutation, and upper triangular. May issue recursive calls to itself to compute the missing values.

Definition at line 71 of file marginal_covariance_cholesky.cpp.

References _Ai, _Ap, _Ax, _diag, _map, and computeIndex().

Referenced by computeCovariance().

72 {
73  assert(r <= c);
74  int idx = computeIndex(r, c);
75 
76  LookupMap::const_iterator foundIt = _map.find(idx);
77  if (foundIt != _map.end()) {
78  return foundIt->second;
79  }
80 
81  // compute the summation over column r
82  double s = 0.;
83  const int& sc = _Ap[r];
84  const int& ec = _Ap[r+1];
85  for (int j = sc+1; j < ec; ++j) { // sum over row r while skipping the element on the diagonal
86  const int& rr = _Ai[j];
87  double val = rr < c ? computeEntry(rr, c) : computeEntry(c, rr);
88  s += val * _Ax[j];
89  }
90 
91  double result;
92  if (r == c) {
93  const double& diagElem = _diag[r];
94  result = diagElem * (diagElem - s);
95  } else {
96  result = -s * _diag[r];
97  }
98  _map[idx] = result;
99  return result;
100 }
LookupMap _map
hash look up table for the already computed entries
int * _Ai
row indices of the CCS storage
int * _Ap
column pointer of the CCS storage
double * _Ax
values of the cholesky factor
std::vector< double > _diag
cache 1 / H_ii to avoid recalculations
int computeIndex(int r, int c) const
compute the index used for hashing
int g2o::MarginalCovarianceCholesky::computeIndex ( int  r,
int  c 
) const
inlineprotected

compute the index used for hashing

Definition at line 90 of file marginal_covariance_cholesky.h.

Referenced by computeCovariance(), and computeEntry().

90 { /*assert(r <= c);*/ return r*_n + c;}
void g2o::MarginalCovarianceCholesky::setCholeskyFactor ( int  n,
int *  Lp,
int *  Li,
double *  Lx,
int *  permInv 
)

set the CCS representation of the cholesky factor along with the inverse permutation used to reduce the fill-in. permInv might be 0, will then not permute the entries.

The pointers provided by the user need to be still valid when calling computeCovariance(). The pointers are owned by the caller, MarginalCovarianceCholesky does not free the pointers.

Definition at line 54 of file marginal_covariance_cholesky.cpp.

References _Ai, _Ap, _Ax, _diag, _n, and _perm.

Referenced by g2o::LinearSolverCSparse< MatrixType >::solveBlocks(), g2o::LinearSolverCholmod< MatrixType >::solveBlocks(), g2o::LinearSolverCSparse< MatrixType >::solvePattern(), and g2o::LinearSolverCholmod< MatrixType >::solvePattern().

55 {
56  _n = n;
57  _Ap = Lp;
58  _Ai = Li;
59  _Ax = Lx;
60  _perm = permInv;
61 
62  // pre-compute reciprocal values of the diagonal of L
63  _diag.resize(n);
64  for (int r = 0; r < n; ++r) {
65  const int& sc = _Ap[r]; // L is lower triangular, thus the first elem in the column is the diagonal entry
66  assert(r == _Ai[sc] && "Error in CCS storage of L");
67  _diag[r] = 1.0 / _Ax[sc];
68  }
69 }
int * _Ai
row indices of the CCS storage
int * _Ap
column pointer of the CCS storage
double * _Ax
values of the cholesky factor
int * _perm
permutation of the cholesky factor. Variable re-ordering for better fill-in
std::vector< double > _diag
cache 1 / H_ii to avoid recalculations

Member Data Documentation

int* g2o::MarginalCovarianceCholesky::_Ai
protected

row indices of the CCS storage

Definition at line 82 of file marginal_covariance_cholesky.h.

Referenced by computeEntry(), and setCholeskyFactor().

int* g2o::MarginalCovarianceCholesky::_Ap
protected

column pointer of the CCS storage

Definition at line 81 of file marginal_covariance_cholesky.h.

Referenced by computeEntry(), and setCholeskyFactor().

double* g2o::MarginalCovarianceCholesky::_Ax
protected

values of the cholesky factor

Definition at line 83 of file marginal_covariance_cholesky.h.

Referenced by computeEntry(), and setCholeskyFactor().

std::vector<double> g2o::MarginalCovarianceCholesky::_diag
protected

cache 1 / H_ii to avoid recalculations

Definition at line 87 of file marginal_covariance_cholesky.h.

Referenced by computeEntry(), and setCholeskyFactor().

LookupMap g2o::MarginalCovarianceCholesky::_map
protected

hash look up table for the already computed entries

Definition at line 86 of file marginal_covariance_cholesky.h.

Referenced by computeCovariance(), and computeEntry().

int g2o::MarginalCovarianceCholesky::_n
protected

L is an n X n matrix.

Definition at line 80 of file marginal_covariance_cholesky.h.

Referenced by setCholeskyFactor().

int* g2o::MarginalCovarianceCholesky::_perm
protected

permutation of the cholesky factor. Variable re-ordering for better fill-in

Definition at line 84 of file marginal_covariance_cholesky.h.

Referenced by computeCovariance(), and setCholeskyFactor().


The documentation for this class was generated from the following files: