add scale() method in DiscreteFactor

release/4.3a0
Varun Agrawal 2025-01-28 16:21:26 -05:00
parent 9cc9f0ecf6
commit aa9d775e27
2 changed files with 16 additions and 0 deletions

View File

@ -71,4 +71,12 @@ AlgebraicDecisionTree<Key> DiscreteFactor::errorTree() const {
return AlgebraicDecisionTree<Key>(dkeys, errors); return AlgebraicDecisionTree<Key>(dkeys, errors);
} }
/* ************************************************************************ */
DiscreteFactor::shared_ptr DiscreteFactor::scale() const {
// Max over all the potentials by pretending all keys are frontal:
shared_ptr denominator = this->max(this->size());
// Normalize the product factor to prevent underflow.
return this->operator/(denominator);
}
} // namespace gtsam } // namespace gtsam

View File

@ -158,6 +158,14 @@ class GTSAM_EXPORT DiscreteFactor : public Factor {
/// Create new factor by maximizing over all values with the same separator. /// Create new factor by maximizing over all values with the same separator.
virtual DiscreteFactor::shared_ptr max(const Ordering& keys) const = 0; virtual DiscreteFactor::shared_ptr max(const Ordering& keys) const = 0;
/**
* @brief Scale the factor values by the maximum
* to prevent underflow/overflow.
*
* @return DiscreteFactor::shared_ptr
*/
DiscreteFactor::shared_ptr scale() const;
/** /**
* Get the number of non-zero values contained in this factor. * Get the number of non-zero values contained in this factor.
* It could be much smaller than `prod_{key}(cardinality(key))`. * It could be much smaller than `prod_{key}(cardinality(key))`.