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) {
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
gtsam::Signature::cpt
std::vector< double > cpt() const
Definition: Signature.cpp:69
gtsam::Signature::Table
std::vector< Row > Table
Definition: Signature.h:60
gtsam::Signature::key
const DiscreteKey & key() const
Definition: Signature.h:115
s
RealScalar s
Definition: level1_cplx_impl.h:126
gtsam::operator<<
std::ostream & operator<<(std::ostream &os, const Dih6 &m)
Definition: testGroup.cpp:109
keys
const KeyVector keys
Definition: testRegularImplicitSchurFactor.cpp:40
gtsam::Signature::table_
std::optional< Table > table_
Definition: Signature.h:74
SignatureParser.h
Parser for conditional distribution signatures.
gtsam::operator%
Signature operator%(const DiscreteKey &key, const string &parent)
Definition: Signature.cpp:134
gtsam::Signature::spec_
std::optional< std::string > spec_
Definition: Signature.h:71
gtsam::Signature::Signature
Signature(const DiscreteKey &key, const DiscreteKeys &parents, const Table &table)
Definition: Signature.cpp:41
gtsam::Signature::Row
std::vector< double > Row
Definition: Signature.h:59
gtsam::DiscreteKeys
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition: DiscreteKey.h:41
os
ofstream os("timeSchurFactors.csv")
gtsam::KeyVector
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:92
gtsam::Signature::indices
KeyVector indices() const
Definition: Signature.cpp:62
gtsam::normalize
static void normalize(Signature::Row &row)
Definition: Signature.cpp:88
table
ArrayXXf table(10, 4)
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
gtsam::row
const MATRIX::ConstRowXpr row(const MATRIX &A, size_t j)
Definition: base/Matrix.h:221
Signature.h
signatures for conditional densities
key
const gtsam::Symbol key('X', 0)
gtsam::Signature::operator,
Signature & operator,(const DiscreteKey &parent)
Definition: Signature.cpp:83
gtsam::Signature::key_
DiscreteKey key_
Definition: Signature.h:65
gtsam
traits
Definition: SFMdata.h:40
gtsam::operator|
Signature operator|(const DiscreteKey &key, const DiscreteKey &parent)
Definition: Signature.cpp:129
gtsam::DiscreteKey
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
std
Definition: BFloat16.h:88
gtsam::Signature::parents_
DiscreteKeys parents_
Definition: Signature.h:68
gtsam::Signature::operator=
Signature & operator=(const std::string &spec)
Definition: Signature.cpp:94
gtsam::Signature::discreteKeys
DiscreteKeys discreteKeys() const
Definition: Signature.cpp:55
align_3::t
Point2 t(10, 10)
gtsam::SignatureParser::Parse
static std::optional< Table > Parse(const std::string &str)
Definition: SignatureParser.cpp:77
gtsam::Signature
Definition: Signature.h:54
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
gtsam::Signature::table
const std::optional< Table > & table() const
Definition: Signature.h:127


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:13:18