autodiff.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 Gael Guennebaud <g.gael@free.fr>
00005 //
00006 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 #include "main.h"
00011 #include <unsupported/Eigen/AutoDiff>
00012 
00013 template<typename Scalar>
00014 EIGEN_DONT_INLINE Scalar foo(const Scalar& x, const Scalar& y)
00015 {
00016   using namespace std;
00017 //   return x+std::sin(y);
00018   EIGEN_ASM_COMMENT("mybegin");
00019   return static_cast<Scalar>(x*2 - pow(x,2) + 2*sqrt(y*y) - 4 * sin(x) + 2 * cos(y) - exp(-0.5*x*x));
00020   //return x+2*y*x;//x*2 -std::pow(x,2);//(2*y/x);// - y*2;
00021   EIGEN_ASM_COMMENT("myend");
00022 }
00023 
00024 template<typename Vector>
00025 EIGEN_DONT_INLINE typename Vector::Scalar foo(const Vector& p)
00026 {
00027   typedef typename Vector::Scalar Scalar;
00028   return (p-Vector(Scalar(-1),Scalar(1.))).norm() + (p.array() * p.array()).sum() + p.dot(p);
00029 }
00030 
00031 template<typename _Scalar, int NX=Dynamic, int NY=Dynamic>
00032 struct TestFunc1
00033 {
00034   typedef _Scalar Scalar;
00035   enum {
00036     InputsAtCompileTime = NX,
00037     ValuesAtCompileTime = NY
00038   };
00039   typedef Matrix<Scalar,InputsAtCompileTime,1> InputType;
00040   typedef Matrix<Scalar,ValuesAtCompileTime,1> ValueType;
00041   typedef Matrix<Scalar,ValuesAtCompileTime,InputsAtCompileTime> JacobianType;
00042 
00043   int m_inputs, m_values;
00044 
00045   TestFunc1() : m_inputs(InputsAtCompileTime), m_values(ValuesAtCompileTime) {}
00046   TestFunc1(int inputs, int values) : m_inputs(inputs), m_values(values) {}
00047 
00048   int inputs() const { return m_inputs; }
00049   int values() const { return m_values; }
00050 
00051   template<typename T>
00052   void operator() (const Matrix<T,InputsAtCompileTime,1>& x, Matrix<T,ValuesAtCompileTime,1>* _v) const
00053   {
00054     Matrix<T,ValuesAtCompileTime,1>& v = *_v;
00055 
00056     v[0] = 2 * x[0] * x[0] + x[0] * x[1];
00057     v[1] = 3 * x[1] * x[0] + 0.5 * x[1] * x[1];
00058     if(inputs()>2)
00059     {
00060       v[0] += 0.5 * x[2];
00061       v[1] += x[2];
00062     }
00063     if(values()>2)
00064     {
00065       v[2] = 3 * x[1] * x[0] * x[0];
00066     }
00067     if (inputs()>2 && values()>2)
00068       v[2] *= x[2];
00069   }
00070 
00071   void operator() (const InputType& x, ValueType* v, JacobianType* _j) const
00072   {
00073     (*this)(x, v);
00074 
00075     if(_j)
00076     {
00077       JacobianType& j = *_j;
00078 
00079       j(0,0) = 4 * x[0] + x[1];
00080       j(1,0) = 3 * x[1];
00081 
00082       j(0,1) = x[0];
00083       j(1,1) = 3 * x[0] + 2 * 0.5 * x[1];
00084 
00085       if (inputs()>2)
00086       {
00087         j(0,2) = 0.5;
00088         j(1,2) = 1;
00089       }
00090       if(values()>2)
00091       {
00092         j(2,0) = 3 * x[1] * 2 * x[0];
00093         j(2,1) = 3 * x[0] * x[0];
00094       }
00095       if (inputs()>2 && values()>2)
00096       {
00097         j(2,0) *= x[2];
00098         j(2,1) *= x[2];
00099 
00100         j(2,2) = 3 * x[1] * x[0] * x[0];
00101         j(2,2) = 3 * x[1] * x[0] * x[0];
00102       }
00103     }
00104   }
00105 };
00106 
00107 template<typename Func> void forward_jacobian(const Func& f)
00108 {
00109     typename Func::InputType x = Func::InputType::Random(f.inputs());
00110     typename Func::ValueType y(f.values()), yref(f.values());
00111     typename Func::JacobianType j(f.values(),f.inputs()), jref(f.values(),f.inputs());
00112 
00113     jref.setZero();
00114     yref.setZero();
00115     f(x,&yref,&jref);
00116 //     std::cerr << y.transpose() << "\n\n";;
00117 //     std::cerr << j << "\n\n";;
00118 
00119     j.setZero();
00120     y.setZero();
00121     AutoDiffJacobian<Func> autoj(f);
00122     autoj(x, &y, &j);
00123 //     std::cerr << y.transpose() << "\n\n";;
00124 //     std::cerr << j << "\n\n";;
00125 
00126     VERIFY_IS_APPROX(y, yref);
00127     VERIFY_IS_APPROX(j, jref);
00128 }
00129 
00130 
00131 // TODO also check actual derivatives!
00132 void test_autodiff_scalar()
00133 {
00134   Vector2f p = Vector2f::Random();
00135   typedef AutoDiffScalar<Vector2f> AD;
00136   AD ax(p.x(),Vector2f::UnitX());
00137   AD ay(p.y(),Vector2f::UnitY());
00138   AD res = foo<AD>(ax,ay);
00139   VERIFY_IS_APPROX(res.value(), foo(p.x(),p.y()));
00140 }
00141 
00142 // TODO also check actual derivatives!
00143 void test_autodiff_vector()
00144 {
00145   Vector2f p = Vector2f::Random();
00146   typedef AutoDiffScalar<Vector2f> AD;
00147   typedef Matrix<AD,2,1> VectorAD;
00148   VectorAD ap = p.cast<AD>();
00149   ap.x().derivatives() = Vector2f::UnitX();
00150   ap.y().derivatives() = Vector2f::UnitY();
00151   
00152   AD res = foo<VectorAD>(ap);
00153   VERIFY_IS_APPROX(res.value(), foo(p));
00154 }
00155 
00156 void test_autodiff_jacobian()
00157 {
00158   CALL_SUBTEST(( forward_jacobian(TestFunc1<double,2,2>()) ));
00159   CALL_SUBTEST(( forward_jacobian(TestFunc1<double,2,3>()) ));
00160   CALL_SUBTEST(( forward_jacobian(TestFunc1<double,3,2>()) ));
00161   CALL_SUBTEST(( forward_jacobian(TestFunc1<double,3,3>()) ));
00162   CALL_SUBTEST(( forward_jacobian(TestFunc1<double>(3,3)) ));
00163 }
00164 
00165 void test_autodiff()
00166 {
00167   for(int i = 0; i < g_repeat; i++) {
00168     CALL_SUBTEST_1( test_autodiff_scalar() );
00169     CALL_SUBTEST_2( test_autodiff_vector() );
00170     CALL_SUBTEST_3( test_autodiff_jacobian() );
00171   }
00172 }
00173 


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:29:01