diff --git a/gtsam/discrete/DiscretePrior.cpp b/gtsam/discrete/DiscretePrior.cpp new file mode 100644 index 000000000..3941e0199 --- /dev/null +++ b/gtsam/discrete/DiscretePrior.cpp @@ -0,0 +1,50 @@ +/* ---------------------------------------------------------------------------- + + * GTSAM Copyright 2010, Georgia Tech Research Corporation, + * Atlanta, Georgia 30332-0415 + * All Rights Reserved + * Authors: Frank Dellaert, et al. (see THANKS for the full author list) + + * See LICENSE for the license information + + * -------------------------------------------------------------------------- */ + +/** + * @file DiscretePrior.cpp + * @date December 2021 + * @author Frank Dellaert + */ + +#include + +namespace gtsam { + +void DiscretePrior::print(const std::string& s, + const KeyFormatter& formatter) const { + Base::print(s, formatter); +} + +double DiscretePrior::operator()(size_t value) const { + if (nrFrontals() != 1) + throw std::invalid_argument( + "Single value operator can only be invoked on single-variable " + "priors"); + DiscreteValues values; + values.emplace(keys_[0], value); + return Base::operator()(values); +} + +std::vector DiscretePrior::pmf() const { + if (nrFrontals() != 1) + throw std::invalid_argument( + "DiscretePrior::pmf only defined for single-variable priors"); + const size_t nrValues = cardinalities_.at(keys_[0]); + std::vector array; + array.reserve(nrValues); + for (size_t v = 0; v < nrValues; v++) { + array.push_back(operator()(v)); + } + return array; +} + +} // namespace gtsam diff --git a/gtsam/discrete/DiscretePrior.h b/gtsam/discrete/DiscretePrior.h index 96a0b6f3a..f44df4987 100644 --- a/gtsam/discrete/DiscretePrior.h +++ b/gtsam/discrete/DiscretePrior.h @@ -72,37 +72,18 @@ class GTSAM_EXPORT DiscretePrior : public DiscreteConditional { /// GTSAM-style print void print( const std::string& s = "Discrete Prior: ", - const KeyFormatter& formatter = DefaultKeyFormatter) const override { - Base::print(s, formatter); - } + const KeyFormatter& formatter = DefaultKeyFormatter) const override; + /// @} /// @name Standard interface /// @{ /// Evaluate given a single value. - double operator()(size_t value) const { - if (nrFrontals() != 1) - throw std::invalid_argument( - "Single value operator can only be invoked on single-variable " - "priors"); - DiscreteValues values; - values.emplace(keys_[0], value); - return Base::operator()(values); - } + double operator()(size_t value) const; /// Evaluate given a single value. - std::vector pmf() const { - if (nrFrontals() != 1) - throw std::invalid_argument( - "DiscretePrior::pmf only defined for single-variable priors"); - const size_t nrValues = cardinalities_.at(keys_[0]); - std::vector array; - array.reserve(nrValues); - for (size_t v = 0; v < nrValues; v++) { - array.push_back(operator()(v)); - } - return array; - } + std::vector pmf() const; + /// @} }; // DiscretePrior