25#include <Eigen/Sparse>
32#if GTSAM_ENABLE_BOOST_SERIALIZATION
35#include <boost/serialization/nvp.hpp>
58 std::map<Key, size_t> denominators_;
76 size_t keyValueForIndex(
Key target_key, uint64_t index)
const;
92 static Eigen::SparseVector<double> Convert(
const DiscreteKeys& keys,
93 const std::vector<double>& table);
96 static Eigen::SparseVector<double> Convert(
const DiscreteKeys& keys,
97 const std::string& table);
102 typedef std::shared_ptr<TableFactor> shared_ptr;
103 typedef Eigen::SparseVector<double>::InnerIterator SparseIt;
104 typedef std::vector<std::pair<DiscreteValues, double>> AssignValList;
117 const Eigen::SparseVector<double>& table);
128 template <
class SOURCE>
155 const std::string& s =
"TableFactor:\n",
173 return std::make_shared<TableFactor>(
174 apply([s](
const double& a) {
return Ring::mul(a, s); }));
179 return apply(f, Ring::mul);
202 static double safe_div(
const double& a,
const double& b);
206 return apply(f, safe_div);
230 double max()
const override;
263 const uint64_t idx)
const;
267 std::unordered_map<uint64_t, AssignValList> createMap(
271 uint64_t uniqueRep(
const DiscreteKeys& keys,
const uint64_t idx)
const;
288 shared_ptr combine(
size_t nrFrontals, Binary op)
const;
296 shared_ptr combine(
const Ordering& keys, Binary op)
const;
299 std::vector<std::pair<DiscreteValues, double>> enumerate()
const;
343 const Names& names = {})
const override;
352 std::string html(
const KeyFormatter& keyFormatter = DefaultKeyFormatter,
353 const Names& names = {})
const override;
363 double error(
const HybridValues& values)
const override;
367#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V43
373 DiscreteKeys contractDkeys(
const TableFactor& factor)
const;
377 DiscreteKeys freeDkeys(
const TableFactor& factor)
const;
381 DiscreteKeys unionDkeys(
const TableFactor& factor)
const;
387#if GTSAM_ENABLE_BOOST_SERIALIZATION
389 friend class boost::serialization::access;
390 template <
class ARCHIVE>
391 void serialize(ARCHIVE& ar,
const unsigned int ) {
392 ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
393 ar& BOOST_SERIALIZATION_NVP(sparse_table_);
394 ar& BOOST_SERIALIZATION_NVP(denominators_);
395 ar& BOOST_SERIALIZATION_NVP(sorted_dkeys_);
Serialization for matrices.
specialized key for discrete variables
std::pair< Key, size_t > DiscreteKey
Key type for discrete variables.
Definition DiscreteKey.h:38
Global functions in a separate testing namespace.
Definition chartTesting.h:28
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition Key.cpp:30
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition Matrix.cpp:143
string markdown(const DiscreteValues &values, const KeyFormatter &keyFormatter, const DiscreteValues::Names &names)
Free version of markdown.
Definition DiscreteValues.cpp:155
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition Point2.h:52
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition Key.h:35
DecisionTree< L, Y > apply(const DecisionTree< L, Y > &f, const typename DecisionTree< L, Y >::Unary &op)
free versions of apply
Definition DecisionTree.h:467
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
Template to create a binary predicate.
Definition Testable.h:112
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152
An assignment from labels to value index (size_t).
Definition Assignment.h:37
a decision tree is a function from assignments to values.
Definition DecisionTree.h:62
A discrete probabilistic factor.
Definition DecisionTreeFactor.h:42
Discrete Conditional Density Derives from DecisionTreeFactor.
Definition DiscreteConditional.h:40
Base class for discrete probabilistic factors The most general one is the derived DecisionTreeFactor.
Definition DiscreteFactor.h:41
std::shared_ptr< DiscreteFactor > shared_ptr
shared_ptr to this class
Definition DiscreteFactor.h:46
std::map< Key, size_t > cardinalities_
Map of Keys and their cardinalities.
Definition DiscreteFactor.h:58
DiscreteFactor()
Default constructor creates empty factor.
Definition DiscreteFactor.h:65
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition DiscreteKey.h:41
A map from keys to values.
Definition DiscreteValues.h:34
A discrete probabilistic factor optimized for sparsity.
Definition TableFactor.h:51
TableFactor operator*(const TableFactor &f) const
multiply two TableFactors
Definition TableFactor.h:178
TableFactor(const DiscreteKey &key, SOURCE table)
Single-key specialization.
Definition TableFactor.h:129
TableFactor(const DiscreteKeys &keys, const std::string &table)
Constructor from string.
Definition TableFactor.h:124
TableFactor apply(Unary op) const
Apply unary operator op(*this) where op accepts the discrete value.
Definition TableFactor.cpp:506
TableFactor(const DiscreteKey &key, const std::vector< double > &row)
Single-key specialization, with vector of doubles.
Definition TableFactor.h:133
TableFactor(const DiscreteKeys &keys, const std::vector< double > &table)
Constructor from doubles.
Definition TableFactor.h:120
Eigen::SparseVector< double > sparse_table_
SparseVector of nonzero probabilities.
Definition TableFactor.h:54
TableFactor operator/(const TableFactor &f) const
divide by factor f (safely)
Definition TableFactor.h:205
DiscreteFactor::shared_ptr operator*(double s) const override
multiply with a scalar
Definition TableFactor.h:172
TableFactor()
Default constructor for I/O.
Definition TableFactor.cpp:118
DiscreteFactor Base
Typedef to base class.
Definition TableFactor.h:101
TableFactor toTableFactor() const override
Return this sparse table representation.
Definition TableFactor.h:217
uint64_t nrValues() const override
Get the number of non-zero values contained in this factor.
Definition TableFactor.h:325
Eigen::SparseVector< double > sparseTable() const
Getter for the underlying sparse vector.
Definition TableFactor.h:163
static Eigen::SparseVector< double > Convert(const DiscreteKeys &keys, const std::vector< double > &table)
Convert probability table given as doubles to SparseVector.
Definition TableFactor.cpp:257
HybridValues represents a collection of DiscreteValues and VectorValues.
Definition HybridValues.h:37
const KeyVector & keys() const
Access the factor's involved variable keys.
Definition Factor.h:143
KeyVector keys_
The keys involved in this factor.
Definition Factor.h:88