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 /* ************************************************************************* */
gtsam::Signature::cpt
std::vector< double > cpt() const
Definition: Signature.cpp:69
TestRegistry::runAllTests
static int runAllTests(TestResult &result)
Definition: TestRegistry.cpp:27
gtsam::Signature::Table
std::vector< Row > Table
Definition: Signature.h:60
gtsam::Signature::key
const DiscreteKey & key() const
Definition: Signature.h:115
D
DiscreteKey D(7, 2)
B
Matrix< SCALARB, Dynamic, Dynamic, opt_B > B
Definition: bench_gemm.cpp:49
s
RealScalar s
Definition: level1_cplx_impl.h:126
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
EXPECT_LONGS_EQUAL
#define EXPECT_LONGS_EQUAL(expected, actual)
Definition: Test.h:154
Testable.h
Concept check for values that can be used in unit tests.
EXPECT
#define EXPECT(condition)
Definition: Test.h:150
TestHarness.h
keys
const KeyVector keys
Definition: testRegularImplicitSchurFactor.cpp:40
b
Scalar * b
Definition: benchVecAdd.cpp:17
gtsam::Y
GaussianFactorGraphValuePair Y
Definition: HybridGaussianProductFactor.cpp:29
x
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
Definition: gnuplot_common_settings.hh:12
X
DiscreteKey X(0, 2)
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
A
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:48
Z
DiscreteKey Z(2, 2)
table
ArrayXXf table(10, 4)
Signature.h
signatures for conditional densities
l
static const Line3 l(Rot3(), 1, 1)
T
DiscreteKey T(2, 2)
L
DiscreteKey L(3, 2)
Eigen::Triplet< double >
TestResult
Definition: TestResult.h:26
E
DiscreteKey E(5, 2)
test_docs.d2
d2
Definition: test_docs.py:29
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
gtsam
traits
Definition: SFMdata.h:40
CHECK
#define CHECK(condition)
Definition: Test.h:108
row
m row(1)
gtsam::DiscreteKey
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
std
Definition: BFloat16.h:88
Y
DiscreteKey Y(1, 3)
gtsam::Signature::discreteKeys
DiscreteKeys discreteKeys() const
Definition: Signature.cpp:55
align_3::t
Point2 t(10, 10)
gtsam::Signature::parents
const DiscreteKeys & parents() const
Definition: Signature.h:118
gtsam::Signature
Definition: Signature.h:54
LONGS_EQUAL
#define LONGS_EQUAL(expected, actual)
Definition: Test.h:134
main
int main()
Definition: testSignature.cpp:115
S
DiscreteKey S(1, 2)
TEST
TEST(testSignature, SimpleConditional)
Definition: testSignature.cpp:32


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:08:36