Signature.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 
19 #include "Signature.h"
20 
21 #include <cassert>
22 #include <sstream>
23 
25 
26 namespace gtsam {
27 
28 using namespace std;
29 
30 ostream& operator<<(ostream& os, const Signature::Row& row) {
31  os << row[0];
32  for (size_t i = 1; i < row.size(); i++) os << " " << row[i];
33  return os;
34 }
35 
36 ostream& operator<<(ostream& os, const Signature::Table& table) {
37  for (size_t i = 0; i < table.size(); i++) os << table[i] << endl;
38  return os;
39 }
40 
42  const Table& table)
43  : key_(key), parents_(parents) {
44  operator=(table);
45 }
46 
48  const std::string& spec)
49  : key_(key), parents_(parents) {
50  operator=(spec);
51 }
52 
54 
57  keys.push_back(key_);
58  for (const DiscreteKey& key : parents_) keys.push_back(key);
59  return keys;
60 }
61 
63  KeyVector js;
64  js.push_back(key_.first);
65  for (const DiscreteKey& key : parents_) js.push_back(key.first);
66  return js;
67 }
68 
69 vector<double> Signature::cpt() const {
70  vector<double> cpt;
71  if (table_) {
72  const size_t nrStates = table_->at(0).size();
73  for (size_t j = 0; j < nrStates; j++) {
74  for (const Row& row : *table_) {
75  assert(row.size() == nrStates);
76  cpt.push_back(row[j]);
77  }
78  }
79  }
80  return cpt;
81 }
82 
84  parents_.push_back(parent);
85  return *this;
86 }
87 
88 static void normalize(Signature::Row& row) {
89  double sum = 0;
90  for (size_t i = 0; i < row.size(); i++) sum += row[i];
91  for (size_t i = 0; i < row.size(); i++) row[i] /= sum;
92 }
93 
94 Signature& Signature::operator=(const string& spec) {
95  spec_ = spec;
96  auto table = SignatureParser::Parse(spec);
97  if (table) {
98  for (Row& row : *table) normalize(row);
99  table_ = *table;
100  }
101  return *this;
102 }
103 
105  Table table = t;
106  for (Row& row : table) normalize(row);
107  table_ = table;
108  return *this;
109 }
110 
111 ostream& operator<<(ostream& os, const Signature& s) {
112  os << s.key_.first;
113  if (s.parents_.empty()) {
114  os << " % ";
115  } else {
116  os << " | " << s.parents_[0].first;
117  for (size_t i = 1; i < s.parents_.size(); i++)
118  os << " && " << s.parents_[i].first;
119  os << " = ";
120  }
121  os << (s.spec_ ? *s.spec_ : "no spec") << endl;
122  if (s.table_)
123  os << (*s.table_);
124  else
125  os << "spec could not be parsed" << endl;
126  return os;
127 }
128 
130  Signature s(key);
131  return s, parent;
132 }
133 
134 Signature operator%(const DiscreteKey& key, const string& parent) {
135  Signature s(key);
136  return s = parent;
137 }
138 
140  Signature s(key);
141  return s = parent;
142 }
143 
144 } // namespace gtsam
const gtsam::Symbol key('X', 0)
Signature(const DiscreteKey &key, const DiscreteKeys &parents, const Table &table)
Definition: Signature.cpp:41
const MATRIX::ConstRowXpr row(const MATRIX &A, size_t j)
Definition: base/Matrix.h:221
std::ostream & operator<<(std::ostream &os, const Dih6 &m)
Definition: testGroup.cpp:109
signatures for conditional densities
DiscreteKey key_
Definition: Signature.h:65
Signature & operator,(const DiscreteKey &parent)
Definition: Signature.cpp:83
Definition: BFloat16.h:88
Parser for conditional distribution signatures.
const DiscreteKey & key() const
Definition: Signature.h:115
static void normalize(Signature::Row &row)
Definition: Signature.cpp:88
Signature & operator=(const std::string &spec)
Definition: Signature.cpp:94
DiscreteKeys discreteKeys() const
Definition: Signature.cpp:55
DiscreteKeys parents_
Definition: Signature.h:68
std::vector< Row > Table
Definition: Signature.h:60
const DiscreteKeys & parents() const
Definition: Signature.h:118
static std::optional< Table > Parse(const std::string &str)
std::optional< std::string > spec_
Definition: Signature.h:71
RealScalar s
const std::optional< Table > & table() const
Definition: Signature.h:127
Signature operator%(const DiscreteKey &key, const string &parent)
Definition: Signature.cpp:134
std::vector< double > Row
Definition: Signature.h:59
std::optional< Table > table_
Definition: Signature.h:74
traits
Definition: chartTesting.h:28
std::vector< double > cpt() const
Definition: Signature.cpp:69
ofstream os("timeSchurFactors.csv")
ArrayXXf table(10, 4)
GTSAM_EXPORT friend std::ostream & operator<<(std::ostream &os, const Signature &s)
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
Signature operator|(const DiscreteKey &key, const DiscreteKey &parent)
Definition: Signature.cpp:129
const KeyVector keys
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
std::ptrdiff_t j
KeyVector indices() const
Definition: Signature.cpp:62
Point2 t(10, 10)
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:35:43