| 
									
										
										
										
											2014-11-24 01:04:54 +08:00
										 |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * GTSAM Copyright 2010, Georgia Tech Research Corporation,  | 
					
						
							|  |  |  |  * Atlanta, Georgia 30332-0415 | 
					
						
							|  |  |  |  * All Rights Reserved | 
					
						
							|  |  |  |  * Authors: Frank Dellaert, et al. (see THANKS for the full author list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * See LICENSE for the license information | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * @file testBasisDecompositions.cpp | 
					
						
							|  |  |  |  * @date November 23, 2014 | 
					
						
							|  |  |  |  * @author Frank Dellaert | 
					
						
							|  |  |  |  * @brief unit tests for Basis Decompositions w Expressions | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-29 15:48:14 +08:00
										 |  |  | #include <gtsam/base/Matrix.h>
 | 
					
						
							| 
									
										
										
										
											2014-11-24 01:04:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-29 15:48:14 +08:00
										 |  |  | namespace gtsam { | 
					
						
							| 
									
										
										
										
											2014-11-24 01:04:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-24 06:16:04 +08:00
										 |  |  | /// Fourier
 | 
					
						
							|  |  |  | template<int N> | 
					
						
							|  |  |  | class Fourier { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   typedef Eigen::Matrix<double, N, 1> Coefficients; | 
					
						
							|  |  |  |   typedef Eigen::Matrix<double, 1, N> Jacobian; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   double x_; | 
					
						
							|  |  |  |   Jacobian H_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// Constructor
 | 
					
						
							|  |  |  |   Fourier(double x) : | 
					
						
							|  |  |  |       x_(x) { | 
					
						
							|  |  |  |     H_(0, 0) = 1; | 
					
						
							|  |  |  |     for (size_t i = 1; i < N; i += 2) { | 
					
						
							|  |  |  |       H_(0, i) = cos(i * x); | 
					
						
							|  |  |  |       H_(0, i + 1) = sin(i * x); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// Given coefficients c, predict value for x
 | 
					
						
							|  |  |  |   double operator()(const Coefficients& c, boost::optional<Jacobian&> H) { | 
					
						
							| 
									
										
										
										
											2014-11-24 07:08:34 +08:00
										 |  |  |     if (H) | 
					
						
							|  |  |  |       (*H) = H_; | 
					
						
							| 
									
										
										
										
											2014-11-24 06:16:04 +08:00
										 |  |  |     return H_ * c; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-29 15:48:14 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <gtsam_unstable/nonlinear/ExpressionFactor.h>
 | 
					
						
							|  |  |  | #include <gtsam/nonlinear/NonlinearFactorGraph.h>
 | 
					
						
							|  |  |  | #include <gtsam/linear/GaussianFactorGraph.h>
 | 
					
						
							|  |  |  | #include <gtsam/linear/VectorValues.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /// For now, this is our sequence representation
 | 
					
						
							|  |  |  | typedef std::map<double, double> Sequence; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Class that does Fourier Decomposition via least squares | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class FourierDecomposition { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   typedef Vector3 Coefficients; ///< Fourier coefficients
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |   Coefficients c_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// Create nonlinear FG from Sequence
 | 
					
						
							|  |  |  |   static NonlinearFactorGraph NonlinearGraph(const Sequence& sequence, | 
					
						
							|  |  |  |       const SharedNoiseModel& model) { | 
					
						
							|  |  |  |     NonlinearFactorGraph graph; | 
					
						
							|  |  |  |     Expression<Coefficients> c(0); | 
					
						
							|  |  |  |     typedef std::pair<double, double> Sample; | 
					
						
							|  |  |  |     BOOST_FOREACH(Sample sample, sequence) { | 
					
						
							|  |  |  |       Expression<double> expression(Fourier<3>(sample.first), c); | 
					
						
							|  |  |  |       ExpressionFactor<double> factor(model, sample.second, expression); | 
					
						
							|  |  |  |       graph.add(factor); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return graph; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// Create linear FG from Sequence
 | 
					
						
							|  |  |  |   static GaussianFactorGraph::shared_ptr LinearGraph(const Sequence& sequence, | 
					
						
							|  |  |  |       const SharedNoiseModel& model) { | 
					
						
							|  |  |  |     NonlinearFactorGraph graph = NonlinearGraph(sequence, model); | 
					
						
							|  |  |  |     Values values; | 
					
						
							|  |  |  |     values.insert(0, Coefficients()); // does not matter
 | 
					
						
							|  |  |  |     GaussianFactorGraph::shared_ptr gfg = graph.linearize(values); | 
					
						
							|  |  |  |     return gfg; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// Constructor
 | 
					
						
							|  |  |  |   FourierDecomposition(const Sequence& sequence, | 
					
						
							|  |  |  |       const SharedNoiseModel& model) { | 
					
						
							|  |  |  |     GaussianFactorGraph::shared_ptr gfg = LinearGraph(sequence, model); | 
					
						
							|  |  |  |     VectorValues solution = gfg->optimize(); | 
					
						
							|  |  |  |     c_ = solution.at(0); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// Return Fourier coefficients
 | 
					
						
							|  |  |  |   Coefficients coefficients() { | 
					
						
							|  |  |  |     return c_; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <gtsam_unstable/nonlinear/expressionTesting.h>
 | 
					
						
							|  |  |  | #include <gtsam/base/Testable.h>
 | 
					
						
							|  |  |  | #include <CppUnitLite/TestHarness.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | noiseModel::Diagonal::shared_ptr model = noiseModel::Unit::Create(1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-24 01:04:54 +08:00
										 |  |  | //******************************************************************************
 | 
					
						
							|  |  |  | TEST(BasisDecompositions, Fourier) { | 
					
						
							| 
									
										
										
										
											2014-11-24 07:08:34 +08:00
										 |  |  |   Fourier<3> fx(0); | 
					
						
							|  |  |  |   Eigen::Matrix<double, 1, 3> expectedH, actualH; | 
					
						
							|  |  |  |   Vector3 c(1.5661, 1.2717, 1.2717); | 
					
						
							|  |  |  |   expectedH = numericalDerivative11<double, Vector3>( | 
					
						
							|  |  |  |       boost::bind(&Fourier<3>::operator(), fx, _1, boost::none), c); | 
					
						
							|  |  |  |   EXPECT_DOUBLES_EQUAL(c[0]+c[1], fx(c,actualH), 1e-9); | 
					
						
							|  |  |  |   EXPECT(assert_equal((Matrix)expectedH, actualH)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //******************************************************************************
 | 
					
						
							| 
									
										
										
										
											2014-11-29 15:48:14 +08:00
										 |  |  | TEST(BasisDecompositions, ManualFourier) { | 
					
						
							| 
									
										
										
										
											2014-11-24 01:04:54 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Create linear factor graph
 | 
					
						
							|  |  |  |   GaussianFactorGraph g; | 
					
						
							|  |  |  |   Key key(1); | 
					
						
							| 
									
										
										
										
											2014-11-29 15:48:14 +08:00
										 |  |  |   Expression<Vector3> c(key); | 
					
						
							|  |  |  |   Values values; | 
					
						
							|  |  |  |   values.insert(key, Vector3()); // does not matter
 | 
					
						
							| 
									
										
										
										
											2014-11-24 01:04:54 +08:00
										 |  |  |   for (size_t i = 0; i < 16; i++) { | 
					
						
							|  |  |  |     double x = i * M_PI / 8, y = exp(sin(x) + cos(x)); | 
					
						
							| 
									
										
										
										
											2014-11-24 06:16:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Manual JacobianFactor
 | 
					
						
							| 
									
										
										
										
											2014-11-24 01:04:54 +08:00
										 |  |  |     Matrix A(1, 3); | 
					
						
							|  |  |  |     A << 1, cos(x), sin(x); | 
					
						
							|  |  |  |     Vector b(1); | 
					
						
							|  |  |  |     b << y; | 
					
						
							| 
									
										
										
										
											2014-11-29 15:48:14 +08:00
										 |  |  |     JacobianFactor f1(key, A, b); | 
					
						
							|  |  |  |     g.add(f1); | 
					
						
							| 
									
										
										
										
											2014-11-24 06:16:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // With ExpressionFactor
 | 
					
						
							|  |  |  |     Expression<double> expression(Fourier<3>(x), c); | 
					
						
							| 
									
										
										
										
											2014-11-29 15:48:14 +08:00
										 |  |  |     EXPECT_CORRECT_EXPRESSION_JACOBIANS(expression, values, 1e-5, 1e-9); | 
					
						
							| 
									
										
										
										
											2014-11-24 06:16:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-29 15:48:14 +08:00
										 |  |  |     ExpressionFactor<double> f2(model, y, expression); | 
					
						
							|  |  |  |     boost::shared_ptr<GaussianFactor> gf = f2.linearize(values); | 
					
						
							|  |  |  |     boost::shared_ptr<JacobianFactor> jf = //
 | 
					
						
							|  |  |  |         boost::dynamic_pointer_cast<JacobianFactor>(gf); | 
					
						
							|  |  |  |     EXPECT( assert_equal(f1, *jf, 1e-9)); | 
					
						
							| 
									
										
										
										
											2014-11-24 01:04:54 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Solve
 | 
					
						
							|  |  |  |   VectorValues actual = g.optimize(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Check
 | 
					
						
							|  |  |  |   Vector3 expected(1.5661, 1.2717, 1.2717); | 
					
						
							| 
									
										
										
										
											2014-11-24 07:08:34 +08:00
										 |  |  |   EXPECT(assert_equal((Vector) expected, actual.at(key),1e-4)); | 
					
						
							| 
									
										
										
										
											2014-11-24 01:04:54 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-29 15:48:14 +08:00
										 |  |  | //******************************************************************************
 | 
					
						
							|  |  |  | TEST(BasisDecompositions, FourierDecomposition) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Create example sequence
 | 
					
						
							|  |  |  |   Sequence sequence; | 
					
						
							|  |  |  |   for (size_t i = 0; i < 16; i++) { | 
					
						
							|  |  |  |     double x = i * M_PI / 8, y = exp(sin(x) + cos(x)); | 
					
						
							|  |  |  |     sequence[x] = y; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Do Fourier Decomposition
 | 
					
						
							|  |  |  |   FourierDecomposition actual(sequence, model); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Check
 | 
					
						
							|  |  |  |   Vector3 expected(1.5661, 1.2717, 1.2717); | 
					
						
							|  |  |  |   EXPECT(assert_equal((Vector) expected, actual.coefficients(),1e-4)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-24 01:04:54 +08:00
										 |  |  | //******************************************************************************
 | 
					
						
							|  |  |  | int main() { | 
					
						
							|  |  |  |   TestResult tr; | 
					
						
							|  |  |  |   return TestRegistry::runAllTests(tr); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | //******************************************************************************
 | 
					
						
							|  |  |  | 
 |