NumericalDiff.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2009 Thomas Capricelli <orzel@freehackers.org>
00005 
00006 #include <stdio.h>
00007 
00008 #include "main.h"
00009 #include <unsupported/Eigen/NumericalDiff>
00010     
00011 // Generic functor
00012 template<typename _Scalar, int NX=Dynamic, int NY=Dynamic>
00013 struct Functor
00014 {
00015   typedef _Scalar Scalar;
00016   enum {
00017     InputsAtCompileTime = NX,
00018     ValuesAtCompileTime = NY
00019   };
00020   typedef Matrix<Scalar,InputsAtCompileTime,1> InputType;
00021   typedef Matrix<Scalar,ValuesAtCompileTime,1> ValueType;
00022   typedef Matrix<Scalar,ValuesAtCompileTime,InputsAtCompileTime> JacobianType;
00023   
00024   int m_inputs, m_values;
00025   
00026   Functor() : m_inputs(InputsAtCompileTime), m_values(ValuesAtCompileTime) {}
00027   Functor(int inputs, int values) : m_inputs(inputs), m_values(values) {}
00028   
00029   int inputs() const { return m_inputs; }
00030   int values() const { return m_values; }
00031 
00032 };
00033 
00034 struct my_functor : Functor<double>
00035 {
00036     my_functor(void): Functor<double>(3,15) {}
00037     int operator()(const VectorXd &x, VectorXd &fvec) const
00038     {
00039         double tmp1, tmp2, tmp3;
00040         double y[15] = {1.4e-1, 1.8e-1, 2.2e-1, 2.5e-1, 2.9e-1, 3.2e-1, 3.5e-1,
00041             3.9e-1, 3.7e-1, 5.8e-1, 7.3e-1, 9.6e-1, 1.34, 2.1, 4.39};
00042 
00043         for (int i = 0; i < values(); i++)
00044         {
00045             tmp1 = i+1;
00046             tmp2 = 16 - i - 1;
00047             tmp3 = (i>=8)? tmp2 : tmp1;
00048             fvec[i] = y[i] - (x[0] + tmp1/(x[1]*tmp2 + x[2]*tmp3));
00049         }
00050         return 0;
00051     }
00052 
00053     int actual_df(const VectorXd &x, MatrixXd &fjac) const
00054     {
00055         double tmp1, tmp2, tmp3, tmp4;
00056         for (int i = 0; i < values(); i++)
00057         {
00058             tmp1 = i+1;
00059             tmp2 = 16 - i - 1;
00060             tmp3 = (i>=8)? tmp2 : tmp1;
00061             tmp4 = (x[1]*tmp2 + x[2]*tmp3); tmp4 = tmp4*tmp4;
00062             fjac(i,0) = -1;
00063             fjac(i,1) = tmp1*tmp2/tmp4;
00064             fjac(i,2) = tmp1*tmp3/tmp4;
00065         }
00066         return 0;
00067     }
00068 };
00069 
00070 void test_forward()
00071 {
00072     VectorXd x(3);
00073     MatrixXd jac(15,3);
00074     MatrixXd actual_jac(15,3);
00075     my_functor functor;
00076 
00077     x << 0.082, 1.13, 2.35;
00078 
00079     // real one 
00080     functor.actual_df(x, actual_jac);
00081 //    std::cout << actual_jac << std::endl << std::endl;
00082 
00083     // using NumericalDiff
00084     NumericalDiff<my_functor> numDiff(functor);
00085     numDiff.df(x, jac);
00086 //    std::cout << jac << std::endl;
00087 
00088     VERIFY_IS_APPROX(jac, actual_jac);
00089 }
00090 
00091 void test_central()
00092 {
00093     VectorXd x(3);
00094     MatrixXd jac(15,3);
00095     MatrixXd actual_jac(15,3);
00096     my_functor functor;
00097 
00098     x << 0.082, 1.13, 2.35;
00099 
00100     // real one 
00101     functor.actual_df(x, actual_jac);
00102 
00103     // using NumericalDiff
00104     NumericalDiff<my_functor,Central> numDiff(functor);
00105     numDiff.df(x, jac);
00106 
00107     VERIFY_IS_APPROX(jac, actual_jac);
00108 }
00109 
00110 void test_NumericalDiff()
00111 {
00112     CALL_SUBTEST(test_forward());
00113     CALL_SUBTEST(test_central());
00114 }


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:32:01