Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API improvements for discrete #990

Merged
merged 20 commits into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
cpp file
  • Loading branch information
dellaert committed Dec 26, 2021
commit 10628a0ddc8acf9502286493abef2f92219cadef
50 changes: 50 additions & 0 deletions gtsam/discrete/DiscretePrior.cpp
Original file line number Diff line number Diff line change
@@ -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 <gtsam/discrete/DiscretePrior.h>

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<double> 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<double> array;
array.reserve(nrValues);
for (size_t v = 0; v < nrValues; v++) {
array.push_back(operator()(v));
}
return array;
}

} // namespace gtsam
29 changes: 5 additions & 24 deletions gtsam/discrete/DiscretePrior.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> 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<double> array;
array.reserve(nrValues);
for (size_t v = 0; v < nrValues; v++) {
array.push_back(operator()(v));
}
return array;
}
std::vector<double> pmf() const;

/// @}
};
// DiscretePrior
Expand Down