Conditional.h
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 
18 // \callgraph
19 #pragma once
20 
21 #include <gtsam/inference/Key.h>
22 
23 namespace gtsam {
24 
25  class HybridValues; // forward declaration.
26 
60  template<class FACTOR, class DERIVEDCONDITIONAL>
62  {
63  protected:
65  size_t nrFrontals_;
66 
67  private:
70 
71 
72  public:
75  typedef std::pair<typename FACTOR::const_iterator, typename FACTOR::const_iterator> ConstFactorRange;
77  ConstFactorRange range_;
78  // Delete default constructor
79  ConstFactorRangeIterator() = delete;
80  ConstFactorRangeIterator(ConstFactorRange const& x) : range_(x) {}
81  // Implement begin and end for iteration
82  typename FACTOR::const_iterator begin() const { return range_.first; }
83  typename FACTOR::const_iterator end() const { return range_.second; }
84  size_t size() const { return std::distance(range_.first, range_.second); }
85  const auto& front() const { return *begin(); }
86  // == operator overload for comparison with another iterator
87  template<class OTHER>
88  bool operator==(const OTHER& rhs) const {
89  return std::equal(begin(), end(), rhs.begin());
90  }
91  };
92 
95 
98 
99  protected:
102 
104  Conditional() : nrFrontals_(0) {}
105 
107  Conditional(size_t nrFrontals) : nrFrontals_(nrFrontals) {}
108 
110 
111  public:
114 
116  void print(const std::string& s = "Conditional", const KeyFormatter& formatter = DefaultKeyFormatter) const;
117 
119  bool equals(const This& c, double tol = 1e-9) const;
120 
122 
125 
126  virtual ~Conditional() {}
127 
129  size_t nrFrontals() const { return nrFrontals_; }
130 
132  size_t nrParents() const { return asFactor().size() - nrFrontals_; }
133 
136  if(nrFrontals_ > 0)
137  return asFactor().front();
138  else
139  throw std::invalid_argument("Requested Conditional::firstFrontalKey from a conditional with zero frontal keys");
140  }
141 
143  Frontals frontals() const { return ConstFactorRangeIterator({beginFrontals(), endFrontals()});}
144 
146  Parents parents() const { return ConstFactorRangeIterator({beginParents(), endParents()}); }
147 
152  virtual double logProbability(const HybridValues& c) const;
153 
158  virtual double evaluate(const HybridValues& c) const;
159 
161  double operator()(const HybridValues& x) const {
162  return evaluate(x);
163  }
164 
169  virtual double logNormalizationConstant() const;
170 
172  double normalizationConstant() const;
173 
177 
179  typename FACTOR::const_iterator beginFrontals() const { return asFactor().begin(); }
180 
182  typename FACTOR::const_iterator endFrontals() const { return asFactor().begin() + nrFrontals_; }
183 
185  typename FACTOR::const_iterator beginParents() const { return endFrontals(); }
186 
188  typename FACTOR::const_iterator endParents() const { return asFactor().end(); }
189 
191  size_t& nrFrontals() { return nrFrontals_; }
192 
194  typename FACTOR::iterator beginFrontals() { return asFactor().begin(); }
195 
197  typename FACTOR::iterator endFrontals() { return asFactor().begin() + nrFrontals_; }
198 
200  typename FACTOR::iterator beginParents() { return asFactor().begin() + nrFrontals_; }
201 
203  typename FACTOR::iterator endParents() { return asFactor().end(); }
204 
218  template <class VALUES>
219  static bool CheckInvariants(const DERIVEDCONDITIONAL& conditional,
220  const VALUES& x);
221 
223 
224  private:
225 
228 
229  // Cast to factor type (non-const) (casts down to derived conditional type, then up to factor type)
230  FACTOR& asFactor() { return static_cast<FACTOR&>(static_cast<DERIVEDCONDITIONAL&>(*this)); }
231 
232  // Cast to derived type (const) (casts down to derived conditional type, then up to factor type)
233  const FACTOR& asFactor() const { return static_cast<const FACTOR&>(static_cast<const DERIVEDCONDITIONAL&>(*this)); }
234 
235 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
236 
237  friend class boost::serialization::access;
238  template<class ARCHIVE>
239  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
240  ar & BOOST_SERIALIZATION_NVP(nrFrontals_);
241  }
242 #endif
243 
245 
246  };
247 
248 } // gtsam
FACTOR::const_iterator end() const
Definition: Conditional.h:83
const FACTOR & asFactor() const
Definition: Conditional.h:233
Conditional(size_t nrFrontals)
Definition: Conditional.h:107
ConstFactorRangeIterator Parents
Definition: Conditional.h:97
std::string serialize(const T &input)
serializes to a string
virtual double logProbability(const HybridValues &c) const
bool operator==(const OTHER &rhs) const
Definition: Conditional.h:88
FACTOR::const_iterator endFrontals() const
Definition: Conditional.h:182
static bool CheckInvariants(const DERIVEDCONDITIONAL &conditional, const VALUES &x)
ConstFactorRangeIterator(ConstFactorRange const &x)
Definition: Conditional.h:80
size_t & nrFrontals()
Definition: Conditional.h:191
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
FACTOR::const_iterator beginParents() const
Definition: Conditional.h:185
Double_ distance(const OrientedPlane3_ &p)
virtual double evaluate(const HybridValues &c) const
size_t nrFrontals() const
Definition: Conditional.h:129
Frontals frontals() const
Definition: Conditional.h:143
ConstFactorRangeIterator Frontals
Definition: Conditional.h:94
static const KeyFormatter DefaultKeyFormatter
Definition: Key.h:43
double normalizationConstant() const
const KeyFormatter & formatter
FACTOR::const_iterator beginFrontals() const
Definition: Conditional.h:179
std::pair< typename FACTOR::const_iterator, typename FACTOR::const_iterator > ConstFactorRange
Definition: Conditional.h:75
FACTOR::iterator endFrontals()
Definition: Conditional.h:197
FACTOR::iterator endParents()
Definition: Conditional.h:203
FACTOR::const_iterator endParents() const
Definition: Conditional.h:188
FACTOR::const_iterator begin() const
Definition: Conditional.h:82
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RealScalar s
virtual ~Conditional()
Definition: Conditional.h:126
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
FACTOR::iterator beginFrontals()
Definition: Conditional.h:194
traits
Definition: chartTesting.h:28
FACTOR::iterator beginParents()
Definition: Conditional.h:200
FACTOR & asFactor()
Definition: Conditional.h:230
void print(const std::string &s="Conditional", const KeyFormatter &formatter=DefaultKeyFormatter) const
double operator()(const HybridValues &x) const
Evaluate probability density, sugar.
Definition: Conditional.h:161
const G double tol
Definition: Group.h:86
Key firstFrontalKey() const
Definition: Conditional.h:135
Parents parents() const
Definition: Conditional.h:146
bool equal(const T &obj1, const T &obj2, double tol)
Definition: Testable.h:85
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
virtual double logNormalizationConstant() const
Conditional< FACTOR, DERIVEDCONDITIONAL > This
Typedef to this class.
Definition: Conditional.h:69
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102
bool equals(const This &c, double tol=1e-9) const
size_t nrParents() const
Definition: Conditional.h:132


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