testSignature.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/Testable.h>
23 
24 #include <vector>
25 
26 using namespace std;
27 using namespace gtsam;
28 
29 DiscreteKey X(0, 2), Y(1, 3), Z(2, 2);
30 
31 /* ************************************************************************* */
32 TEST(testSignature, SimpleConditional) {
33  Signature sig(X, {Y}, "1/1 2/3 1/4");
34  CHECK(sig.table());
35  Signature::Table table = *sig.table();
36  vector<double> row[3]{{0.5, 0.5}, {0.4, 0.6}, {0.2, 0.8}};
37  LONGS_EQUAL(3, table.size());
38  CHECK(row[0] == table[0]);
39  CHECK(row[1] == table[1]);
40  CHECK(row[2] == table[2]);
41 
42  CHECK(sig.key() == X);
43 
44  DiscreteKeys keys = sig.discreteKeys();
45  LONGS_EQUAL(2, keys.size());
46  CHECK(keys[0] == X);
47  CHECK(keys[1] == Y);
48 
49  DiscreteKeys parents = sig.parents();
50  LONGS_EQUAL(1, parents.size());
51  CHECK(parents[0] == Y);
52 
53  EXPECT_LONGS_EQUAL(6, sig.cpt().size());
54 }
55 
56 /* ************************************************************************* */
57 TEST(testSignature, SimpleConditionalNonparser) {
58  Signature::Row row1{1, 1}, row2{2, 3}, row3{1, 4};
59  Signature::Table table{row1, row2, row3};
60 
61  Signature sig(X | Y = table);
62  CHECK(sig.key() == X);
63 
65  LONGS_EQUAL(2, keys.size());
66  CHECK(keys[0] == X);
67  CHECK(keys[1] == Y);
68 
69  DiscreteKeys parents = sig.parents();
70  LONGS_EQUAL(1, parents.size());
71  CHECK(parents[0] == Y);
72 
73  EXPECT_LONGS_EQUAL(6, sig.cpt().size());
74 }
75 
76 /* ************************************************************************* */
77 DiscreteKey A(0, 2), S(1, 2), T(2, 2), L(3, 2), B(4, 2), E(5, 2), D(7, 2);
78 
79 // Make sure we can create all signatures for Asia network with constructor.
80 TEST(testSignature, AllExamples) {
81  DiscreteKey X(6, 2);
82  Signature a(A, {}, "99/1");
83  Signature s(S, {}, "50/50");
84  Signature t(T, {A}, "99/1 95/5");
85  Signature l(L, {S}, "99/1 90/10");
86  Signature b(B, {S}, "70/30 40/60");
87  Signature e(E, {T, L}, "F F F 1");
88  Signature x(X, {E}, "95/5 2/98");
89 }
90 
91 // Make sure we can create all signatures for Asia network with operator magic.
92 TEST(testSignature, AllExamplesMagic) {
93  DiscreteKey X(6, 2);
94  Signature a(A % "99/1");
95  Signature s(S % "50/50");
96  Signature t(T | A = "99/1 95/5");
97  Signature l(L | S = "99/1 90/10");
98  Signature b(B | S = "70/30 40/60");
99  Signature e((E | T, L) = "F F F 1");
100  Signature x(X | E = "95/5 2/98");
101 }
102 
103 // Check example from docs.
104 TEST(testSignature, DoxygenExample) {
105  Signature::Table table{{0.9, 0.1}, {0.2, 0.8}, {0.3, 0.7}, {0.1, 0.9}};
106  Signature d1(D, {E, B}, table);
107  Signature d2((D | E, B) = "9/1 2/8 3/7 1/9");
108  Signature d3(D, {E, B}, "9/1 2/8 3/7 1/9");
109  EXPECT(*(d1.table()) == table);
110  EXPECT(*(d2.table()) == table);
111  EXPECT(*(d3.table()) == table);
112 }
113 
114 /* ************************************************************************* */
115 int main() {
116  TestResult tr;
117  return TestRegistry::runAllTests(tr);
118 }
119 /* ************************************************************************* */
Matrix< SCALARB, Dynamic, Dynamic, opt_B > B
Definition: bench_gemm.cpp:49
#define CHECK(condition)
Definition: Test.h:108
Scalar * b
Definition: benchVecAdd.cpp:17
Concept check for values that can be used in unit tests.
static int runAllTests(TestResult &result)
signatures for conditional densities
DiscreteKey L(3, 2)
DiscreteKey D(7, 2)
TEST(testSignature, SimpleConditional)
Definition: BFloat16.h:88
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:48
const DiscreteKey & key() const
Definition: Signature.h:115
DiscreteKey S(1, 2)
DiscreteKey Z(2, 2)
int main()
DiscreteKeys discreteKeys() const
Definition: Signature.cpp:55
DiscreteKey T(2, 2)
std::vector< Row > Table
Definition: Signature.h:60
static const Line3 l(Rot3(), 1, 1)
const DiscreteKeys & parents() const
Definition: Signature.h:118
m row(1)
#define EXPECT(condition)
Definition: Test.h:150
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RealScalar s
std::vector< double > Row
Definition: Signature.h:59
DiscreteKey X(0, 2)
#define LONGS_EQUAL(expected, actual)
Definition: Test.h:134
traits
Definition: chartTesting.h:28
DiscreteKey Y(1, 3)
std::vector< double > cpt() const
Definition: Signature.cpp:69
DiscreteKey E(5, 2)
#define EXPECT_LONGS_EQUAL(expected, actual)
Definition: Test.h:154
ArrayXXf table(10, 4)
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
const KeyVector keys
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
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:39:31