Main MRPT website > C++ reference
MRPT logo
AdolcForward
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) 2008-2009 Gael Guennebaud <g.gael@free.fr>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #ifndef EIGEN_ADLOC_FORWARD
00026 #define EIGEN_ADLOC_FORWARD
00027 
00028 //--------------------------------------------------------------------------------
00029 //
00030 // This file provides support for adolc's adouble type in forward mode.
00031 // ADOL-C is a C++ automatic differentiation library,
00032 // see https://projects.coin-or.org/ADOL-C for more information.
00033 //
00034 // Note that the maximal number of directions is controlled by
00035 // the preprocessor token NUMBER_DIRECTIONS. The default is 2.
00036 //
00037 //--------------------------------------------------------------------------------
00038 
00039 #define ADOLC_TAPELESS
00040 #ifndef NUMBER_DIRECTIONS
00041 # define NUMBER_DIRECTIONS 2
00042 #endif
00043 #include <adolc/adouble.h>
00044 
00045 // adolc defines some very stupid macros:
00046 #if defined(malloc)
00047 # undef malloc
00048 #endif
00049 
00050 #if defined(calloc)
00051 # undef calloc
00052 #endif
00053 
00054 #if defined(realloc)
00055 # undef realloc
00056 #endif
00057 
00058 #include <Eigen/Core>
00059 
00060 namespace Eigen {
00061 
00062 /** \ingroup Unsupported_modules
00063   * \defgroup AdolcForward_Module Adolc forward module  
00064  * \ingroup eigen_grp
00065  * \ingroup eigen_grp
00066   * This module provides support for adolc's adouble type in forward mode.
00067   * ADOL-C is a C++ automatic differentiation library,
00068   * see https://projects.coin-or.org/ADOL-C for more information.
00069   * It mainly consists in:
00070   *  - a struct Eigen::NumTraits<adtl::adouble> specialization
00071   *  - overloads of internal::* math function for adtl::adouble type.
00072   *
00073   * Note that the maximal number of directions is controlled by
00074   * the preprocessor token NUMBER_DIRECTIONS. The default is 2.
00075   *
00076   * \code
00077   * #include <unsupported/Eigen/AdolcSupport>
00078   * \endcode
00079   */
00080   //@{
00081 
00082 } // namespace Eigen
00083 
00084 // the Adolc's type adouble is defined in the adtl namespace
00085 // therefore, the following internal::* functions *must* be defined
00086 // in the same namespace
00087 namespace Eigen {
00088 
00089   namespace internal {
00090 
00091     inline const adtl::adouble& conj(const adtl::adouble& x)  { return x; }
00092     inline const adtl::adouble& real(const adtl::adouble& x)  { return x; }
00093     inline adtl::adouble imag(const adtl::adouble&)    { return 0.; }
00094     inline adtl::adouble abs(const adtl::adouble&  x)  { return adtl::fabs(x); }
00095     inline adtl::adouble abs2(const adtl::adouble& x)  { return x*x; }
00096     
00097     using adtl::sqrt;
00098     using adtl::exp;
00099     using adtl::log;
00100     using adtl::sin;
00101     using adtl::cos;
00102     using adtl::pow;
00103 
00104   }
00105 
00106 }
00107 
00108 namespace Eigen {
00109 
00110 template<> struct NumTraits<adtl::adouble>
00111 {
00112   typedef adtl::adouble Real;
00113   typedef adtl::adouble NonInteger;
00114   typedef adtl::adouble Nested;
00115   enum {
00116     IsComplex = 0,
00117     IsInteger = 0,
00118     IsSigned = 1,
00119     RequireInitialization = 1,
00120     ReadCost = 1,
00121     AddCost = 1,
00122     MulCost = 1
00123   };
00124 };
00125 
00126 template<typename Functor> class AdolcForwardJacobian : public Functor
00127 {
00128   typedef adtl::adouble ActiveScalar;
00129 public:
00130 
00131   AdolcForwardJacobian() : Functor() {}
00132   AdolcForwardJacobian(const Functor& f) : Functor(f) {}
00133 
00134   // forward constructors
00135   template<typename T0>
00136   AdolcForwardJacobian(const T0& a0) : Functor(a0) {}
00137   template<typename T0, typename T1>
00138   AdolcForwardJacobian(const T0& a0, const T1& a1) : Functor(a0, a1) {}
00139   template<typename T0, typename T1, typename T2>
00140   AdolcForwardJacobian(const T0& a0, const T1& a1, const T1& a2) : Functor(a0, a1, a2) {}
00141 
00142   typedef typename Functor::InputType InputType;
00143   typedef typename Functor::ValueType ValueType;
00144   typedef typename Functor::JacobianType JacobianType;
00145 
00146   typedef Matrix<ActiveScalar, InputType::SizeAtCompileTime, 1> ActiveInput;
00147   typedef Matrix<ActiveScalar, ValueType::SizeAtCompileTime, 1> ActiveValue;
00148 
00149   void operator() (const InputType& x, ValueType* v, JacobianType* _jac) const
00150   {
00151     eigen_assert(v!=0);
00152     if (!_jac)
00153     {
00154       Functor::operator()(x, v);
00155       return;
00156     }
00157 
00158     JacobianType& jac = *_jac;
00159 
00160     ActiveInput ax = x.template cast<ActiveScalar>();
00161     ActiveValue av(jac.rows());
00162 
00163     for (int j=0; j<jac.cols(); j++)
00164       for (int i=0; i<jac.cols(); i++)
00165         ax[i].setADValue(j, i==j ? 1 : 0);
00166 
00167     Functor::operator()(ax, &av);
00168 
00169     for (int i=0; i<jac.rows(); i++)
00170     {
00171       (*v)[i] = av[i].getValue();
00172       for (int j=0; j<jac.cols(); j++)
00173         jac.coeffRef(i,j) = av[i].getADValue(j);
00174     }
00175   }
00176 protected:
00177 
00178 };
00179 
00180 //@}
00181 
00182 }
00183 
00184 #endif // EIGEN_ADLOC_FORWARD



Page generated by Doxygen 1.7.4 for MRPT 0.9.5 SVN:2717 at Sun Oct 16 16:08:03 PDT 2011 Hosted on:
SourceForge.net Logo