GaussianMixtureFactor.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
21 #include <gtsam/base/utilities.h>
28 
29 namespace gtsam {
30 
31 /* *******************************************************************************/
33  const DiscreteKeys &discreteKeys,
34  const Factors &factors)
35  : Base(continuousKeys, discreteKeys), factors_(factors) {}
36 
37 /* *******************************************************************************/
38 bool GaussianMixtureFactor::equals(const HybridFactor &lf, double tol) const {
39  const This *e = dynamic_cast<const This *>(&lf);
40  if (e == nullptr) return false;
41 
42  // This will return false if either factors_ is empty or e->factors_ is empty,
43  // but not if both are empty or both are not empty:
44  if (factors_.empty() ^ e->factors_.empty()) return false;
45 
46  // Check the base and the factors:
47  return Base::equals(*e, tol) &&
49  [tol](const sharedFactor &f1, const sharedFactor &f2) {
50  return f1->equals(*f2, tol);
51  });
52 }
53 
54 /* *******************************************************************************/
55 void GaussianMixtureFactor::print(const std::string &s,
56  const KeyFormatter &formatter) const {
57  HybridFactor::print(s, formatter);
58  std::cout << "{\n";
59  if (factors_.empty()) {
60  std::cout << " empty" << std::endl;
61  } else {
63  "", [&](Key k) { return formatter(k); },
64  [&](const sharedFactor &gf) -> std::string {
65  RedirectCout rd;
66  std::cout << ":\n";
67  if (gf && !gf->empty()) {
68  gf->print("", formatter);
69  return rd.str();
70  } else {
71  return "nullptr";
72  }
73  });
74  }
75  std::cout << "}" << std::endl;
76 }
77 
78 /* *******************************************************************************/
80  const DiscreteValues &assignment) const {
81  return factors_(assignment);
82 }
83 
84 /* *******************************************************************************/
86  const GaussianFactorGraphTree &sum) const {
87  using Y = GaussianFactorGraph;
88  auto add = [](const Y &graph1, const Y &graph2) {
89  auto result = graph1;
90  result.push_back(graph2);
91  return result;
92  };
93  const auto tree = asGaussianFactorGraphTree();
94  return sum.empty() ? tree : sum.apply(tree, add);
95 }
96 
97 /* *******************************************************************************/
99  const {
100  auto wrap = [](const sharedFactor &gf) { return GaussianFactorGraph{gf}; };
101  return {factors_, wrap};
102 }
103 
104 /* *******************************************************************************/
106  const VectorValues &continuousValues) const {
107  // functor to convert from sharedFactor to double error value.
108  auto errorFunc = [&continuousValues](const sharedFactor &gf) {
109  return gf->error(continuousValues);
110  };
111  DecisionTree<Key, double> errorTree(factors_, errorFunc);
112  return errorTree;
113 }
114 
115 /* *******************************************************************************/
117  const sharedFactor gf = factors_(values.discrete());
118  return gf->error(values.continuous());
119 }
120 /* *******************************************************************************/
121 
122 } // namespace gtsam
A set of GaussianFactors, indexed by a set of discrete keys.
Decision Tree for use in DiscreteFactors.
GaussianMixtureFactor()=default
Default constructor, mainly for serialization.
GaussianFactorGraphTree asGaussianFactorGraphTree() const
Helper function to return factors and functional to create a DecisionTree of Gaussian Factor Graphs...
const char Y
Implementation of a discrete conditional mixture factor. Implements a joint discrete-continuous facto...
A factor with a quadratic error function - a Gaussian.
leaf::MyValues values
const GaussianFactorGraph factors
const VectorValues & continuous() const
Return the multi-dimensional vector values.
Definition: HybridValues.h:89
double f2(const Vector2 &x)
const KeyFormatter & formatter
AlgebraicDecisionTree< Key > error(const VectorValues &continuousValues) const
Compute error of the GaussianMixtureFactor as a tree.
void print(const std::string &s="HybridFactor\, const KeyFormatter &formatter=DefaultKeyFormatter) const override
print
Values result
std::string str() const
return the string
Definition: utilities.cpp:5
virtual bool equals(const HybridFactor &lf, double tol=1e-9) const
equals
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RealScalar s
std::shared_ptr< GaussianFactor > sharedFactor
Linear Factor Graph where all factors are Gaussians.
bool equals(const DecisionTree &other, const CompareFunc &compare=&DefaultCompare) const
DecisionTree apply(const Unary &op) const
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
NonlinearFactorGraph graph2()
traits
Definition: chartTesting.h:28
void print(const std::string &s, const LabelFormatter &labelFormatter, const ValueFormatter &valueFormatter) const
GTSAM-style print.
bool empty() const
Check if tree is empty.
Definition: DecisionTree.h:240
bool equals(const HybridFactor &lf, double tol=1e-9) const override
equals
Point2 f1(const Point3 &p, OptionalJacobian< 2, 3 > H)
mxArray * wrap(const Class &value)
Definition: matlab.h:126
const DiscreteValues & discrete() const
Return the discrete values.
Definition: HybridValues.h:92
const G double tol
Definition: Group.h:86
void print(const std::string &s="GaussianMixtureFactor\, const KeyFormatter &formatter=DefaultKeyFormatter) const override
print
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
GaussianFactorGraphTree add(const GaussianFactorGraphTree &sum) const
Combine the Gaussian Factor Graphs in sum and this while maintaining the original tree structure...
sharedFactor operator()(const DiscreteValues &assignment) const
Get factor at a given discrete assignment.
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102
Factors factors_
Decision tree of Gaussian factors indexed by discrete keys.
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition: DiscreteKey.h:41


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:15