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
96  GaussNewtonOptimizer optimizer(graph, initial, parameters);
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
106  Marginals marginals(graph, result);
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
134  FitBasis<Chebyshev1Basis> actual(sequence, model, N);
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 //******************************************************************************
const gtsam::Symbol key('X', 0)
Matrix3f m
double proxy1(double x, size_t N)
Matrix marginalCovariance(Key variable) const
Definition: Marginals.cpp:133
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:51
Scalar * y
virtual const Values & optimize()
Concept check for values that can be used in unit tests.
IsDerived< DERIVEDFACTOR > emplace_shared(Args &&... args)
Emplace a shared pointer to factor of given type.
Definition: FactorGraph.h:196
static int runAllTests(TestResult &result)
Eigen::Vector3d Vector3
Definition: Vector.h:43
noiseModel::Diagonal::shared_ptr model
const ValueType at(Key j) const
Definition: Values-inl.h:204
Matrix expected
Definition: testMatrix.cpp:971
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:40
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:39
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
Chebyshev basis decompositions.
Values initial
Definition: BFloat16.h:88
Evaluate derivatives of a nonlinear factor numerically.
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 N
Definition: gksort.c:12
#define CHECK_EXCEPTION(condition, exception_name)
Definition: Test.h:118
NonlinearFactorGraph graph
#define EXPECT_DOUBLES_EQUAL(expected, actual, threshold)
Definition: Test.h:161
std::map< double, double > Sequence
Our sequence representation is a map of {x: y} values where y = f(x)
Definition: FitBasis.h:36
Eigen::VectorXd Vector
Definition: Vector.h:38
Values result
Vector3 f(-6, 1, 0.5)
Factor for enforcing the scalar value of the polynomial BASIS representation at x is the same as the ...
Definition: BasisFactors.h:39
#define EXPECT(condition)
Definition: Test.h:150
Array< double, 1, 3 > e(1./3., 0.5, 2.)
double proxy2(double x, size_t N)
static ConjugateGradientParameters parameters
traits
Definition: chartTesting.h:28
A class for computing marginals in a NonlinearFactorGraph.
const double fx
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
Parameters parameters() const
Return Fourier coefficients.
Definition: FitBasis.h:96
Eigen::Matrix< double, Eigen::Dynamic, 1 > Vector
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102
Marginals marginals(graph, result)
Point2 t(10, 10)
Fit a Basis using least-squares.
TEST(Chebyshev, Chebyshev1)
int main()


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:37:59