Skip to content

Commit 6ec49ac

Browse files
authored
Merge pull request #2482 from borglab/doc/Marginals
Simplify Marginals and add docs
2 parents 5723155 + 636ead9 commit 6ec49ac

File tree

10 files changed

+329
-503
lines changed

10 files changed

+329
-503
lines changed

doc/CovarianceRecovery.pdf

-13.6 KB
Binary file not shown.

gtsam/nonlinear/Marginals.cpp

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,6 @@ KeyVector uniqueSortedKeys(const KeyVector& keys) {
4040
return result;
4141
}
4242

43-
/* ************************************************************************* */
44-
// Return keys in first-seen order with duplicates removed.
45-
KeyVector uniqueStableKeys(const KeyVector& keys) {
46-
KeyVector result;
47-
result.reserve(keys.size());
48-
KeySet seen;
49-
for (Key key : keys) {
50-
if (seen.insert(key).second) {
51-
result.push_back(key);
52-
}
53-
}
54-
return result;
55-
}
56-
5743
/* ************************************************************************* */
5844
// Look up the block dimension of each requested variable.
5945
std::vector<size_t> dimsForKeys(const KeyVector& keys, const Values& values) {
@@ -147,45 +133,6 @@ Matrix covarianceColumns(const GaussianBayesNet& bayesNet,
147133
return selectors;
148134
}
149135

150-
/* ************************************************************************* */
151-
// Assemble a left-by-right block from packed selected covariance columns.
152-
Matrix assembleCrossBlock(const Matrix& selectedColumns,
153-
const KeyVector& orderedKeys,
154-
const std::vector<size_t>& dims,
155-
const KeyVector& left, const KeyVector& right) {
156-
FastMap<Key, size_t> keyIndex = Ordering(orderedKeys).invert();
157-
const std::vector<size_t> offsets = blockOffsets(dims);
158-
159-
size_t leftDim = 0;
160-
for (Key key : left) {
161-
leftDim += dims.at(keyIndex.at(key));
162-
}
163-
size_t rightDim = 0;
164-
for (Key key : right) {
165-
rightDim += dims.at(keyIndex.at(key));
166-
}
167-
168-
Matrix result(leftDim, rightDim);
169-
size_t rowOffset = 0;
170-
for (Key leftKey : left) {
171-
const size_t leftBlock = keyIndex.at(leftKey);
172-
const size_t leftSize = dims[leftBlock];
173-
const size_t leftBegin = offsets[leftBlock];
174-
size_t columnOffset = 0;
175-
size_t selectedOffset = 0;
176-
for (Key rightKey : right) {
177-
const size_t rightBlock = keyIndex.at(rightKey);
178-
const size_t rightSize = dims[rightBlock];
179-
result.block(rowOffset, columnOffset, leftSize, rightSize) =
180-
selectedColumns.block(leftBegin, selectedOffset, leftSize, rightSize);
181-
columnOffset += rightSize;
182-
selectedOffset += rightSize;
183-
}
184-
rowOffset += leftSize;
185-
}
186-
return result;
187-
}
188-
189136
} // namespace
190137

191138
/* ************************************************************************* */
@@ -374,46 +321,6 @@ JointMarginal Marginals::jointMarginalInformation(
374321
}
375322
}
376323

377-
/* ************************************************************************* */
378-
Matrix Marginals::crossCovariance(const KeyVector& left,
379-
const KeyVector& right) const {
380-
// Preserve the requested left/right block order while removing duplicates.
381-
const KeyVector leftUnique = uniqueStableKeys(left);
382-
const KeyVector rightUnique = uniqueStableKeys(right);
383-
KeyVector unionKeys = leftUnique;
384-
unionKeys.insert(unionKeys.end(), rightUnique.begin(), rightUnique.end());
385-
unionKeys = uniqueSortedKeys(unionKeys);
386-
387-
if (unionKeys.empty()) {
388-
return Matrix();
389-
}
390-
391-
if (unionKeys.size() == 1 && leftUnique == rightUnique) {
392-
return marginalCovariance(unionKeys.front());
393-
}
394-
395-
// Reduce to the union query once, then recover only the requested RHS blocks.
396-
const GaussianFactorGraph jointFG =
397-
reducedJointFactorGraph(bayesTree_, factorization_, unionKeys);
398-
const GaussianBayesNet bayesNet =
399-
queryBayesNet(jointFG, factorization_, unionKeys);
400-
const std::vector<size_t> dims = dimsForKeys(unionKeys, values_);
401-
FastMap<Key, size_t> keyIndex = Ordering(unionKeys).invert();
402-
403-
// Map each requested right-hand variable to its block in the reduced system.
404-
std::vector<size_t> rightBlocks;
405-
rightBlocks.reserve(rightUnique.size());
406-
for (Key key : rightUnique) {
407-
rightBlocks.push_back(keyIndex.at(key));
408-
}
409-
410-
// Recover the selected columns and pack them back into the requested block layout.
411-
const Matrix selectedColumns =
412-
covarianceColumns(bayesNet, unionKeys, dims, rightBlocks);
413-
return assembleCrossBlock(selectedColumns, unionKeys, dims, leftUnique,
414-
rightUnique);
415-
}
416-
417324
/* ************************************************************************* */
418325
VectorValues Marginals::optimize() const {
419326
return bayesTree_.optimize();

gtsam/nonlinear/Marginals.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,6 @@ class GTSAM_EXPORT Marginals {
130130
/** Compute the joint marginal information of several variables */
131131
JointMarginal jointMarginalInformation(const KeyVector& variables) const;
132132

133-
/** Compute the cross-covariance block between two variable sets. */
134-
Matrix crossCovariance(const KeyVector& left, const KeyVector& right) const;
135-
136133
/** Delete cached Bayes tree shortcuts created while computing marginals */
137134
void deleteCachedShortcuts();
138135

0 commit comments

Comments
 (0)