g2o
Loading...
Searching...
No Matches
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< MatrixX > &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
 
double computeEntry (int r, int c)
 

Protected Attributes

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

Detailed Description

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

Definition at line 42 of file marginal_covariance_cholesky.h.

Member Typedef Documentation

◆ LookupMap

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 48 of file marginal_covariance_cholesky.h.

Constructor & Destructor Documentation

◆ MarginalCovarianceCholesky()

g2o::MarginalCovarianceCholesky::MarginalCovarianceCholesky ( )

Definition at line 46 of file marginal_covariance_cholesky.cpp.

47 : _n(0), _Ap(0), _Ai(0), _Ax(0), _perm(0) {}
double * _Ax
values of the cholesky factor
int * _Ai
row indices of the CCS storage
int * _Ap
column pointer of the CCS storage

◆ ~MarginalCovarianceCholesky()

g2o::MarginalCovarianceCholesky::~MarginalCovarianceCholesky ( )

Definition at line 49 of file marginal_covariance_cholesky.cpp.

49{}

Member Function Documentation

◆ computeCovariance() [1/2]

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 100 of file marginal_covariance_cholesky.cpp.

101 {
102 _map.clear();
103 int base = 0;
104 vector<MatrixElem> elemsToCompute;
105 for (size_t i = 0; i < blockIndices.size(); ++i) {
106 int nbase = blockIndices[i];
107 int vdim = nbase - base;
108 for (int rr = 0; rr < vdim; ++rr)
109 for (int cc = rr; cc < vdim; ++cc) {
110 int r = _perm ? _perm[rr + base] : rr + base; // apply permutation
111 int c = _perm ? _perm[cc + base] : cc + base;
112 if (r > c) // make sure it's still upper triangular after applying the
113 // permutation
114 swap(r, c);
115 elemsToCompute.push_back(MatrixElem(r, c));
116 }
117 base = nbase;
118 }
119
120 // sort the elems to reduce the recursive calls
121 sort(elemsToCompute.begin(), elemsToCompute.end());
122
123 // compute the inverse elements we need
124 for (size_t i = 0; i < elemsToCompute.size(); ++i) {
125 const MatrixElem& me = elemsToCompute[i];
126 computeEntry(me.r, me.c);
127 }
128
129 // set the marginal covariance for the vertices, by writing to the blocks
130 // 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) cov[cc * vdim + rr] = foundIt->second;
147 }
148 base = nbase;
149 }
150}
LookupMap _map
hash look up table for the already computed entries
int computeIndex(int r, int c) const
compute the index used for hashing

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

◆ computeCovariance() [2/2]

void g2o::MarginalCovarianceCholesky::computeCovariance ( SparseBlockMatrix< MatrixX > &  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 152 of file marginal_covariance_cholesky.cpp.

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

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

◆ computeEntry()

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 69 of file marginal_covariance_cholesky.cpp.

69 {
70 assert(r <= c);
71 int idx = computeIndex(r, c);
72
73 LookupMap::const_iterator foundIt = _map.find(idx);
74 if (foundIt != _map.end()) {
75 return foundIt->second;
76 }
77
78 // compute the summation over column r
79 double s = 0.;
80 const int& sc = _Ap[r];
81 const int& ec = _Ap[r + 1];
82 for (int j = sc + 1; j < ec;
83 ++j) { // sum over row r while skipping the element on the diagonal
84 const int& rr = _Ai[j];
85 double val = rr < c ? computeEntry(rr, c) : computeEntry(c, rr);
86 s += val * _Ax[j];
87 }
88
89 double result;
90 if (r == c) {
91 const double& diagElem = _diag[r];
92 result = diagElem * (diagElem - s);
93 } else {
94 result = -s * _diag[r];
95 }
96 _map[idx] = result;
97 return result;
98}
std::vector< double > _diag
cache 1 / H_ii to avoid recalculations

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

Referenced by computeCovariance(), computeCovariance(), and computeEntry().

◆ computeIndex()

int g2o::MarginalCovarianceCholesky::computeIndex ( int  r,
int  c 
) const
inlineprotected

compute the index used for hashing

Definition at line 93 of file marginal_covariance_cholesky.h.

93 { /*assert(r <= c);*/
94 return r * _n + c;
95 }

Referenced by computeCovariance(), computeCovariance(), and computeEntry().

◆ setCholeskyFactor()

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 51 of file marginal_covariance_cholesky.cpp.

52 {
53 _n = n;
54 _Ap = Lp;
55 _Ai = Li;
56 _Ax = Lx;
57 _perm = permInv;
58
59 // pre-compute reciprocal values of the diagonal of L
60 _diag.resize(n);
61 for (int r = 0; r < n; ++r) {
62 const int& sc = _Ap[r]; // L is lower triangular, thus the first elem in
63 // the column is the diagonal entry
64 assert(r == _Ai[sc] && "Error in CCS storage of L");
65 _diag[r] = 1.0 / _Ax[sc];
66 }
67}

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

Referenced by g2o::LinearSolverCholmod< MatrixType >::solveBlocks_impl(), g2o::LinearSolverCSparse< MatrixType >::solveBlocks_impl(), and g2o::LinearSolverEigen< MatrixType >::solveBlocks_impl().

Member Data Documentation

◆ _Ai

int* g2o::MarginalCovarianceCholesky::_Ai
protected

row indices of the CCS storage

Definition at line 84 of file marginal_covariance_cholesky.h.

Referenced by computeEntry(), and setCholeskyFactor().

◆ _Ap

int* g2o::MarginalCovarianceCholesky::_Ap
protected

column pointer of the CCS storage

Definition at line 83 of file marginal_covariance_cholesky.h.

Referenced by computeEntry(), and setCholeskyFactor().

◆ _Ax

double* g2o::MarginalCovarianceCholesky::_Ax
protected

values of the cholesky factor

Definition at line 85 of file marginal_covariance_cholesky.h.

Referenced by computeEntry(), and setCholeskyFactor().

◆ _diag

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

cache 1 / H_ii to avoid recalculations

Definition at line 90 of file marginal_covariance_cholesky.h.

Referenced by computeEntry(), and setCholeskyFactor().

◆ _map

LookupMap g2o::MarginalCovarianceCholesky::_map
protected

hash look up table for the already computed entries

Definition at line 89 of file marginal_covariance_cholesky.h.

Referenced by computeCovariance(), computeCovariance(), and computeEntry().

◆ _n

int g2o::MarginalCovarianceCholesky::_n
protected

L is an n X n matrix.

Definition at line 82 of file marginal_covariance_cholesky.h.

Referenced by setCholeskyFactor().

◆ _perm

int* g2o::MarginalCovarianceCholesky::_perm
protected

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

Definition at line 86 of file marginal_covariance_cholesky.h.

Referenced by computeCovariance(), computeCovariance(), and setCholeskyFactor().


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