testChebyshev.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 
20 #include <gtsam/base/Testable.h>
21 #include <gtsam/basis/Chebyshev.h>
22 #include <gtsam/basis/FitBasis.h>
24 
25 using namespace std;
26 using namespace gtsam;
27 
28 namespace {
29 auto model = noiseModel::Unit::Create(1);
30 const size_t N = 3;
31 } // namespace
32 
33 //******************************************************************************
34 TEST(Chebyshev, Chebyshev1) {
35  using Synth = Chebyshev1Basis::EvaluationFunctor;
36  Vector c(N);
37  double x;
38  c << 12, 3, 1;
39  x = -1.0;
40  EXPECT_DOUBLES_EQUAL(12 + 3 * x + 2 * x * x - 1, Synth(N, x)(c), 1e-9);
41  x = -0.5;
42  EXPECT_DOUBLES_EQUAL(12 + 3 * x + 2 * x * x - 1, Synth(N, x)(c), 1e-9);
43  x = 0.3;
44  EXPECT_DOUBLES_EQUAL(12 + 3 * x + 2 * x * x - 1, Synth(N, x)(c), 1e-9);
45 }
46 
47 //******************************************************************************
48 TEST(Chebyshev, Chebyshev2) {
49  using Synth = Chebyshev2Basis::EvaluationFunctor;
50  Vector c(N);
51  double x;
52  c << 12, 3, 1;
53  x = -1.0;
54  EXPECT_DOUBLES_EQUAL(12 + 6 * x + 4 * x * x - 1, Synth(N, x)(c), 1e-9);
55  x = -0.5;
56  EXPECT_DOUBLES_EQUAL(12 + 6 * x + 4 * x * x - 1, Synth(N, x)(c), 1e-9);
57  x = 0.3;
58  EXPECT_DOUBLES_EQUAL(12 + 6 * x + 4 * x * x - 1, Synth(N, x)(c), 1e-9);
59 }
60 
61 //******************************************************************************
62 TEST(Chebyshev, Evaluation) {
63  Chebyshev1Basis::EvaluationFunctor fx(N, 0.5);
64  Vector c(N);
65  c << 3, 5, -12;
66  EXPECT_DOUBLES_EQUAL(11.5, fx(c), 1e-9);
67 }
68 
69 //******************************************************************************
72 TEST(Chebyshev, Expression) {
73  // Create linear factor graph
75  Key key(1);
76 
77  // Let's pretend we have 6 GPS measurements (we just do x coordinate)
78  // at times
79  const size_t m = 6;
80  Vector t(m);
81  t << -0.7, -0.4, 0.1, 0.3, 0.7, 0.9;
82  Vector x(m);
83  x << -0.7, -0.4, 0.1, 0.3, 0.7, 0.9;
84 
85  for (size_t i = 0; i < m; i++) {
87  t(i));
88  }
89 
90  // Solve
92  initial.insert<Vector>(key, Vector::Zero(N)); // initial does not matter
93 
94  // ... and optimize
97  Values result = optimizer.optimize();
98 
99  // Check
100  Vector expected(N);
101  expected << 0, 1, 0;
102  Vector actual_c = result.at<Vector>(key);
103  EXPECT(assert_equal(expected, actual_c));
104 
105  // Calculate and print covariances
107  Matrix3 cov = marginals.marginalCovariance(key);
108  EXPECT_DOUBLES_EQUAL(0.626, cov(1, 1), 1e-3);
109 
110  // Predict x at time 1.0
111  Chebyshev1Basis::EvaluationFunctor f(N, 1.0);
112  Matrix H;
113  double actual = f(actual_c, H);
114  EXPECT_DOUBLES_EQUAL(1.0, actual, 1e-9);
115 
116  // Calculate predictive variance on prediction
117  double actual_variance_on_prediction = (H * cov * H.transpose())(0);
118  EXPECT_DOUBLES_EQUAL(1.1494, actual_variance_on_prediction, 1e-4);
119 }
120 
121 //******************************************************************************
122 TEST(Chebyshev, Decomposition) {
123  const size_t M = 16;
124 
125  // Create example sequence
127  for (size_t i = 0; i < M; i++) {
128  double x = ((double)i / M); // - 0.99;
129  double y = x;
130  sequence[x] = y;
131  }
132 
133  // Do Chebyshev Decomposition
135 
136  // Check
137  Vector expected = Vector::Zero(N);
138  expected(1) = 1;
139  EXPECT(assert_equal(expected, (Vector)actual.parameters(), 1e-4));
140 }
141 
142 //******************************************************************************
143 TEST(Chebyshev1, Derivative) {
144  Vector c(N);
145  c << 12, 3, 2;
146 
147  Weights D;
148 
149  double x = -1.0;
150  D = Chebyshev1Basis::DerivativeWeights(N, x);
151  // regression
152  EXPECT_DOUBLES_EQUAL(-5, (D * c)(0), 1e-9);
153 
154  x = -0.5;
155  D = Chebyshev1Basis::DerivativeWeights(N, x);
156  // regression
157  EXPECT_DOUBLES_EQUAL(-1, (D * c)(0), 1e-9);
158 
159  x = 0.3;
160  D = Chebyshev1Basis::DerivativeWeights(N, x);
161  // regression
162  EXPECT_DOUBLES_EQUAL(5.4, (D * c)(0), 1e-9);
163 }
164 
165 //******************************************************************************
166 Vector3 f(-6, 1, 0.5);
167 
168 double proxy1(double x, size_t N) {
169  return Chebyshev1Basis::EvaluationFunctor(N, x)(Vector(f));
170 }
171 
172 TEST(Chebyshev1, Derivative2) {
173  const double x = 0.5;
174  auto D = Chebyshev1Basis::DerivativeWeights(N, x);
175 
176  Matrix numeric_dTdx =
177  numericalDerivative21<double, double, double>(proxy1, x, N);
178  // regression
179  EXPECT_DOUBLES_EQUAL(2, numeric_dTdx(0, 0), 1e-9);
180  EXPECT_DOUBLES_EQUAL(2, (D * f)(0), 1e-9);
181 }
182 
183 //******************************************************************************
184 TEST(Chebyshev2, Derivative) {
185  Vector c(N);
186  c << 12, 6, 2;
187 
188  Weights D;
189 
190  double x = -1.0;
191  CHECK_EXCEPTION(Chebyshev2Basis::DerivativeWeights(N, x), std::runtime_error);
192  x = 1.0;
193  CHECK_EXCEPTION(Chebyshev2Basis::DerivativeWeights(N, x), std::runtime_error);
194 
195  x = -0.5;
196  D = Chebyshev2Basis::DerivativeWeights(N, x);
197  // regression
198  EXPECT_DOUBLES_EQUAL(4, (D * c)(0), 1e-9);
199 
200  x = 0.3;
201  D = Chebyshev2Basis::DerivativeWeights(N, x);
202  // regression
203  EXPECT_DOUBLES_EQUAL(16.8, (D * c)(0), 1e-9);
204 
205  x = 0.75;
206  D = Chebyshev2Basis::DerivativeWeights(N, x);
207  // regression
208  EXPECT_DOUBLES_EQUAL(24, (D * c)(0), 1e-9);
209 
210  x = 10;
211  D = Chebyshev2Basis::DerivativeWeights(N, x, 0, 20);
212  // regression
213  EXPECT_DOUBLES_EQUAL(12, (D * c)(0), 1e-9);
214 }
215 
216 //******************************************************************************
217 double proxy2(double x, size_t N) {
218  return Chebyshev2Basis::EvaluationFunctor(N, x)(Vector(f));
219 }
220 
221 TEST(Chebyshev2, Derivative2) {
222  const double x = 0.5;
223  auto D = Chebyshev2Basis::DerivativeWeights(N, x);
224 
225  Matrix numeric_dTdx =
226  numericalDerivative21<double, double, double>(proxy2, x, N);
227  // regression
228  EXPECT_DOUBLES_EQUAL(4, numeric_dTdx(0, 0), 1e-9);
229  EXPECT_DOUBLES_EQUAL(4, (D * f)(0), 1e-9);
230 }
231 
232 //******************************************************************************
233 int main() {
234  TestResult tr;
235  return TestRegistry::runAllTests(tr);
236 }
237 //******************************************************************************
TestRegistry::runAllTests
static int runAllTests(TestResult &result)
Definition: TestRegistry.cpp:27
H
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
Definition: gnuplot_common_settings.hh:74
CHECK_EXCEPTION
#define CHECK_EXCEPTION(condition, exception_name)
Definition: Test.h:118
gtsam::NonlinearOptimizer::optimize
virtual const Values & optimize()
Definition: NonlinearOptimizer.h:98
TEST
TEST(Chebyshev, Chebyshev1)
Definition: testChebyshev.cpp:34
main
int main()
Definition: testChebyshev.cpp:233
D
MatrixXcd D
Definition: EigenSolver_EigenSolver_MatrixType.cpp:14
gtsam::GaussNewtonOptimizer
Definition: GaussNewtonOptimizer.h:38
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
Testable.h
Concept check for values that can be used in unit tests.
EXPECT
#define EXPECT(condition)
Definition: Test.h:150
TestHarness.h
fx
const double fx
Definition: testSmartStereoFactor_iSAM2.cpp:47
c
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
initial
Values initial
Definition: OdometryOptimize.cpp:2
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
gtsam::EvaluationFactor
Factor for enforcing the scalar value of the polynomial BASIS representation at x is the same as the ...
Definition: BasisFactors.h:39
gtsam::Marginals
Definition: Marginals.h:32
gtsam::Matrix
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:39
gtsam::FitBasis
Definition: FitBasis.h:52
gtsam::Vector3
Eigen::Vector3d Vector3
Definition: Vector.h:43
proxy1
double proxy1(double x, size_t N)
Definition: testChebyshev.cpp:168
gtsam::Vector
Eigen::VectorXd Vector
Definition: Vector.h:38
result
Values result
Definition: OdometryOptimize.cpp:8
f
Vector3 f(-6, 1, 0.5)
gtsam::Marginals::marginalCovariance
Matrix marginalCovariance(Key variable) const
Definition: Marginals.cpp:133
gtsam::Chebyshev2
Definition: Chebyshev2.h:46
gtsam::Expression
Definition: Expression.h:47
GaussNewtonOptimizer.h
gtsam::Values::at
const ValueType at(Key j) const
Definition: Values-inl.h:261
sequence
Definition: pytypes.h:2146
parameters
static ConjugateGradientParameters parameters
Definition: testIterative.cpp:33
gtsam::GaussNewtonParams
Definition: GaussNewtonOptimizer.h:30
gtsam::Sequence
std::map< double, double > Sequence
Our sequence representation is a map of {x: y} values where y = f(x)
Definition: FitBasis.h:36
gtsam::NonlinearFactorGraph
Definition: NonlinearFactorGraph.h:55
cholesky::expected
Matrix expected
Definition: testMatrix.cpp:971
factorTesting.h
Evaluate derivatives of a nonlinear factor numerically.
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
model
noiseModel::Diagonal::shared_ptr model
Definition: doc/Code/Pose2SLAMExample.cpp:7
EXPECT_DOUBLES_EQUAL
#define EXPECT_DOUBLES_EQUAL(expected, actual, threshold)
Definition: Test.h:161
TestResult
Definition: TestResult.h:26
y
Scalar * y
Definition: level1_cplx_impl.h:124
key
const gtsam::Symbol key('X', 0)
Chebyshev.h
Chebyshev basis decompositions.
gtsam
traits
Definition: SFMdata.h:40
Marginals.h
A class for computing marginals in a NonlinearFactorGraph.
gtsam::Values
Definition: Values.h:65
std
Definition: BFloat16.h:88
FitBasis.h
Fit a Basis using least-squares.
gtsam::assert_equal
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:40
initial
Definition: testScenarioRunner.cpp:148
Eigen::Matrix< double, 1, -1 >
proxy2
double proxy2(double x, size_t N)
Definition: testChebyshev.cpp:217
N
#define N
Definition: igam.h:9
graph
NonlinearFactorGraph graph
Definition: doc/Code/OdometryExample.cpp:2
align_3::t
Point2 t(10, 10)
marginals
Marginals marginals(graph, result)
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
ceres::Vector
Eigen::Matrix< double, Eigen::Dynamic, 1 > Vector
Definition: gtsam/3rdparty/ceres/eigen.h:38
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
gtsam::FactorGraph::emplace_shared
IsDerived< DERIVEDFACTOR > emplace_shared(Args &&... args)
Emplace a shared pointer to factor of given type.
Definition: FactorGraph.h:153
gtsam::FitBasis::parameters
Parameters parameters() const
Return Fourier coefficients.
Definition: FitBasis.h:96
M
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:51


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:07:19