testOptionalJacobian.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 <gtsam/base/Matrix.h>
22 
23 #include <optional>
24 #include <functional>
25 
26 using namespace std;
27 using namespace gtsam;
28 
29 //******************************************************************************
30 #define TEST_CONSTRUCTOR(DIM1, DIM2, X, TRUTHY) \
31  { \
32  OptionalJacobian<DIM1, DIM2> H(X); \
33  EXPECT(H == TRUTHY); \
34  }
35 TEST( OptionalJacobian, Constructors ) {
36  Matrix23 fixed;
37  Matrix dynamic;
38  std::optional<std::reference_wrapper<Matrix>> optionalRef(std::ref(dynamic));
39 
41  EXPECT(!H);
42 
43  TEST_CONSTRUCTOR(2, 3, fixed, true);
44  TEST_CONSTRUCTOR(2, 3, &fixed, true);
45  TEST_CONSTRUCTOR(2, 3, dynamic, true);
46  TEST_CONSTRUCTOR(2, 3, &dynamic, true);
47  TEST_CONSTRUCTOR(2, 3, std::nullopt, false);
48  TEST_CONSTRUCTOR(2, 3, optionalRef, true);
49 
50  // Test dynamic
51  OptionalJacobian<-1, -1> H7;
52  EXPECT(!H7);
53 
54  TEST_CONSTRUCTOR(-1, -1, dynamic, true);
55  TEST_CONSTRUCTOR(-1, -1, std::nullopt, false);
56  TEST_CONSTRUCTOR(-1, -1, optionalRef, true);
57 
58 }
59 
60 //******************************************************************************
61 Matrix kTestMatrix = (Matrix23() << 11,12,13,21,22,23).finished();
62 
64  if (H)
65  *H = kTestMatrix;
66 }
67 
69 
70  // Default argument does nothing
71  test();
72 
73  // Fixed size, no copy
74  Matrix23 fixed1;
75  fixed1.setOnes();
76  test(fixed1);
78 
79  // Fixed size, no copy, pointer style
80  Matrix23 fixed2;
81  fixed2.setOnes();
82  test(&fixed2);
84 
85  // Passing in an empty matrix means we want it resized
86  Matrix dynamic0;
87  test(dynamic0);
88  EXPECT(assert_equal(kTestMatrix,dynamic0));
89 
90  // Dynamic wrong size
91  Matrix dynamic1(3, 5);
92  dynamic1.setOnes();
93  test(dynamic1);
94  EXPECT(assert_equal(kTestMatrix,dynamic1));
95 
96  // Dynamic right size
97  Matrix dynamic2(2, 5);
98  dynamic2.setOnes();
99  test(dynamic2);
100  EXPECT(assert_equal(kTestMatrix, dynamic2));
101 
102  { // Dynamic pointer
103  // Passing in an empty matrix means we want it resized
104  Matrix dynamic0;
105  test(&dynamic0);
106  EXPECT(assert_equal(kTestMatrix, dynamic0));
107 
108  // Dynamic wrong size
109  Matrix dynamic1(3, 5);
110  dynamic1.setOnes();
111  test(&dynamic1);
112  EXPECT(assert_equal(kTestMatrix, dynamic1));
113 
114  // Dynamic right size
115  Matrix dynamic2(2, 5);
116  dynamic2.setOnes();
117  test(&dynamic2);
118  EXPECT(assert_equal(kTestMatrix, dynamic2));
119  }
120 }
121 
122 //******************************************************************************
124  if (H)
125  *H = kTestMatrix; // resizes
126 }
127 
129 
130  // Default argument does nothing
131  test2();
132 
133  // Passing in an empty matrix means we want it resized
134  Matrix dynamic0;
135  test2(dynamic0);
136  EXPECT(assert_equal(kTestMatrix,dynamic0));
137 
138  // Dynamic wrong size
139  Matrix dynamic1(3, 5);
140  dynamic1.setOnes();
141  test2(dynamic1);
142  EXPECT(assert_equal(kTestMatrix,dynamic1));
143 
144  // Dynamic right size
145  Matrix dynamic2(2, 5);
146  dynamic2.setOnes();
147  test2(dynamic2);
148  EXPECT(assert_equal(kTestMatrix, dynamic2));
149 }
150 
151 //******************************************************************************
152 void test3(double add, OptionalJacobian<2,1> H = {}) {
153  if (H) *H << add + 10, add + 20;
154 }
155 
156 // This function calls the above function three times, one for each column
158  if (H) {
159  test3(1, H.cols<1>(0));
160  test3(2, H.cols<1>(1));
161  test3(3, H.cols<1>(2));
162  }
163 }
164 
166  // Default argument does nothing
167  test4();
168 
169  Matrix23 fixed;
170  fixed.setOnes();
171  test4(fixed);
173 }
174 
175 //******************************************************************************
176 int main() {
177  TestResult tr;
178  return TestRegistry::runAllTests(tr);
179 }
180 //******************************************************************************
void test3(double add, OptionalJacobian< 2, 1 > H={})
static int runAllTests(TestResult &result)
void test4(OptionalJacobian< 2, 3 > H={})
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:40
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:39
Definition: BFloat16.h:88
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 y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set set set surface set nocontour set clabel set mapping cartesian set nohidden3d set cntrparam order set cntrparam linear set cntrparam levels auto set cntrparam points set size set set xzeroaxis lt lw set x2zeroaxis lt lw set yzeroaxis lt lw set y2zeroaxis lt lw set tics in set ticslevel set tics set mxtics default set mytics default set mx2tics default set my2tics default set xtics border mirror norotate autofreq set ytics border mirror norotate autofreq set ztics border nomirror norotate autofreq set nox2tics set noy2tics set timestamp bottom norotate set rrange [*:*] noreverse nowriteback set trange [*:*] noreverse nowriteback set urange [*:*] noreverse nowriteback set vrange [*:*] noreverse nowriteback set xlabel matrix size set x2label set timefmt d m y n H
#define TEST_CONSTRUCTOR(DIM1, DIM2, X, TRUTHY)
void test2(OptionalJacobian<-1,-1 > H={})
int main()
TEST(OptionalJacobian, Constructors)
#define EXPECT(condition)
Definition: Test.h:150
traits
Definition: chartTesting.h:28
void test(OptionalJacobian< 2, 3 > H={})
graph add(PriorFactor< Pose2 >(1, priorMean, priorNoise))
Special class for optional Jacobian arguments.
const int Dynamic
Definition: Constants.h:22
Matrix kTestMatrix


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:38:52