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 using namespace std;
24 using namespace gtsam;
25 
26 //******************************************************************************
27 TEST( OptionalJacobian, Constructors ) {
28  Matrix23 fixed;
29 
31  EXPECT(!H1);
32 
33  OptionalJacobian<2, 3> H2(fixed);
34  EXPECT(H2);
35 
36  OptionalJacobian<2, 3> H3(&fixed);
37  EXPECT(H3);
38 
39  Matrix dynamic;
40  OptionalJacobian<2, 3> H4(dynamic);
41  EXPECT(H4);
42 
43  OptionalJacobian<2, 3> H5(boost::none);
44  EXPECT(!H5);
45 
46  boost::optional<Matrix&> optional(dynamic);
47  OptionalJacobian<2, 3> H6(optional);
48  EXPECT(H6);
49 
50  OptionalJacobian<-1, -1> H7;
51  EXPECT(!H7);
52 
53  OptionalJacobian<-1, -1> H8(dynamic);
54  EXPECT(H8);
55 
56  OptionalJacobian<-1, -1> H9(boost::none);
57  EXPECT(!H9);
58 
59  OptionalJacobian<-1, -1> H10(optional);
60  EXPECT(H10);
61 }
62 
63 //******************************************************************************
64 Matrix kTestMatrix = (Matrix23() << 11,12,13,21,22,23).finished();
65 
66 void test(OptionalJacobian<2, 3> H = boost::none) {
67  if (H)
68  *H = kTestMatrix;
69 }
70 
72 
73  // Default argument does nothing
74  test();
75 
76  // Fixed size, no copy
77  Matrix23 fixed1;
78  fixed1.setOnes();
79  test(fixed1);
81 
82  // Fixed size, no copy, pointer style
83  Matrix23 fixed2;
84  fixed2.setOnes();
85  test(&fixed2);
87 
88  // Passing in an empty matrix means we want it resized
89  Matrix dynamic0;
90  test(dynamic0);
91  EXPECT(assert_equal(kTestMatrix,dynamic0));
92 
93  // Dynamic wrong size
94  Matrix dynamic1(3, 5);
95  dynamic1.setOnes();
96  test(dynamic1);
97  EXPECT(assert_equal(kTestMatrix,dynamic1));
98 
99  // Dynamic right size
100  Matrix dynamic2(2, 5);
101  dynamic2.setOnes();
102  test(dynamic2);
103  EXPECT(assert_equal(kTestMatrix, dynamic2));
104 }
105 
106 //******************************************************************************
107 void test2(OptionalJacobian<-1,-1> H = boost::none) {
108  if (H)
109  *H = kTestMatrix; // resizes
110 }
111 
113 
114  // Default argument does nothing
115  test2();
116 
117  // Passing in an empty matrix means we want it resized
118  Matrix dynamic0;
119  test2(dynamic0);
120  EXPECT(assert_equal(kTestMatrix,dynamic0));
121 
122  // Dynamic wrong size
123  Matrix dynamic1(3, 5);
124  dynamic1.setOnes();
125  test2(dynamic1);
126  EXPECT(assert_equal(kTestMatrix,dynamic1));
127 
128  // Dynamic right size
129  Matrix dynamic2(2, 5);
130  dynamic2.setOnes();
131  test2(dynamic2);
132  EXPECT(assert_equal(kTestMatrix, dynamic2));
133 }
134 
135 //******************************************************************************
136 void test3(double add, OptionalJacobian<2,1> H = boost::none) {
137  if (H) *H << add + 10, add + 20;
138 }
139 
140 // This function calls the above function three times, one for each column
141 void test4(OptionalJacobian<2, 3> H = boost::none) {
142  if (H) {
143  test3(1, H.cols<1>(0));
144  test3(2, H.cols<1>(1));
145  test3(3, H.cols<1>(2));
146  }
147 }
148 
150  // Default argument does nothing
151  test4();
152 
153  Matrix23 fixed;
154  fixed.setOnes();
155  test4(fixed);
157 }
158 
159 //******************************************************************************
160 int main() {
161  TestResult tr;
162  return TestRegistry::runAllTests(tr);
163 }
164 //******************************************************************************
static int runAllTests(TestResult &result)
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:43
Definition: Half.h:150
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
int main()
graph add(boost::make_shared< UnaryFactor >(1, 0.0, 0.0, unaryNoise))
TEST(OptionalJacobian, Constructors)
#define EXPECT(condition)
Definition: Test.h:151
traits
Definition: chartTesting.h:28
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:42
void test(OptionalJacobian< 2, 3 > H=boost::none)
Special class for optional Jacobian arguments.
const int Dynamic
Definition: Constants.h:21
void test3(double add, OptionalJacobian< 2, 1 > H=boost::none)
void test4(OptionalJacobian< 2, 3 > H=boost::none)
Matrix kTestMatrix
void test2(OptionalJacobian<-1,-1 > H=boost::none)


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:48:26