2009-02-03 16:34:09 +08:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
2009-05-23 02:25:33 +08:00
|
|
|
// for linear algebra.
|
2009-02-03 16:34:09 +08:00
|
|
|
//
|
|
|
|
// Copyright (C) 2008-2009 Gael Guennebaud <g.gael@free.fr>
|
|
|
|
//
|
2012-07-14 02:42:47 +08:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2009-02-03 16:34:09 +08:00
|
|
|
|
2009-02-04 17:44:44 +08:00
|
|
|
#ifndef EIGEN_ADLOC_FORWARD
|
|
|
|
#define EIGEN_ADLOC_FORWARD
|
2009-02-03 16:34:09 +08:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// This file provides support for adolc's adouble type in forward mode.
|
|
|
|
// ADOL-C is a C++ automatic differentiation library,
|
2009-09-27 07:56:50 +08:00
|
|
|
// see https://projects.coin-or.org/ADOL-C for more information.
|
2009-02-03 16:34:09 +08:00
|
|
|
//
|
|
|
|
// Note that the maximal number of directions is controlled by
|
|
|
|
// the preprocessor token NUMBER_DIRECTIONS. The default is 2.
|
|
|
|
//
|
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#define ADOLC_TAPELESS
|
|
|
|
#ifndef NUMBER_DIRECTIONS
|
|
|
|
# define NUMBER_DIRECTIONS 2
|
|
|
|
#endif
|
2015-11-30 23:00:22 +08:00
|
|
|
#include <adolc/adtl.h>
|
2009-02-03 16:34:09 +08:00
|
|
|
|
|
|
|
// adolc defines some very stupid macros:
|
|
|
|
#if defined(malloc)
|
|
|
|
# undef malloc
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(calloc)
|
|
|
|
# undef calloc
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(realloc)
|
|
|
|
# undef realloc
|
|
|
|
#endif
|
|
|
|
|
2018-09-17 15:54:29 +08:00
|
|
|
#include "../../Eigen/Core"
|
2009-02-03 16:34:09 +08:00
|
|
|
|
|
|
|
namespace Eigen {
|
|
|
|
|
2013-01-11 17:40:35 +08:00
|
|
|
/**
|
2009-02-04 17:44:44 +08:00
|
|
|
* \defgroup AdolcForward_Module Adolc forward module
|
|
|
|
* This module provides support for adolc's adouble type in forward mode.
|
|
|
|
* ADOL-C is a C++ automatic differentiation library,
|
2009-09-27 07:56:50 +08:00
|
|
|
* see https://projects.coin-or.org/ADOL-C for more information.
|
2009-02-04 17:44:44 +08:00
|
|
|
* It mainly consists in:
|
|
|
|
* - a struct Eigen::NumTraits<adtl::adouble> specialization
|
2010-10-25 22:15:22 +08:00
|
|
|
* - overloads of internal::* math function for adtl::adouble type.
|
2009-02-04 17:44:44 +08:00
|
|
|
*
|
|
|
|
* Note that the maximal number of directions is controlled by
|
|
|
|
* the preprocessor token NUMBER_DIRECTIONS. The default is 2.
|
|
|
|
*
|
|
|
|
* \code
|
|
|
|
* #include <unsupported/Eigen/AdolcSupport>
|
|
|
|
* \endcode
|
|
|
|
*/
|
|
|
|
//@{
|
|
|
|
|
|
|
|
} // namespace Eigen
|
2009-02-03 16:34:09 +08:00
|
|
|
|
2012-06-19 03:32:56 +08:00
|
|
|
// Eigen's require a few additional functions which must be defined in the same namespace
|
|
|
|
// than the custom scalar type own namespace
|
|
|
|
namespace adtl {
|
2010-10-25 22:15:22 +08:00
|
|
|
|
2012-06-19 03:32:56 +08:00
|
|
|
inline const adouble& conj(const adouble& x) { return x; }
|
|
|
|
inline const adouble& real(const adouble& x) { return x; }
|
|
|
|
inline adouble imag(const adouble&) { return 0.; }
|
|
|
|
inline adouble abs(const adouble& x) { return fabs(x); }
|
|
|
|
inline adouble abs2(const adouble& x) { return x*x; }
|
2009-02-03 16:34:09 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-02-28 00:19:13 +08:00
|
|
|
namespace Eigen {
|
2009-02-04 17:44:44 +08:00
|
|
|
|
2009-02-28 00:19:13 +08:00
|
|
|
template<> struct NumTraits<adtl::adouble>
|
2012-06-19 03:32:56 +08:00
|
|
|
: NumTraits<double>
|
2009-02-04 17:44:44 +08:00
|
|
|
{
|
|
|
|
typedef adtl::adouble Real;
|
2010-04-29 06:51:38 +08:00
|
|
|
typedef adtl::adouble NonInteger;
|
|
|
|
typedef adtl::adouble Nested;
|
2009-02-04 17:44:44 +08:00
|
|
|
enum {
|
|
|
|
IsComplex = 0,
|
2010-04-29 06:51:38 +08:00
|
|
|
IsInteger = 0,
|
|
|
|
IsSigned = 1,
|
2011-01-27 20:51:39 +08:00
|
|
|
RequireInitialization = 1,
|
2009-02-04 17:44:44 +08:00
|
|
|
ReadCost = 1,
|
|
|
|
AddCost = 1,
|
|
|
|
MulCost = 1
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2009-02-28 00:19:13 +08:00
|
|
|
template<typename Functor> class AdolcForwardJacobian : public Functor
|
|
|
|
{
|
|
|
|
typedef adtl::adouble ActiveScalar;
|
|
|
|
public:
|
|
|
|
|
|
|
|
AdolcForwardJacobian() : Functor() {}
|
|
|
|
AdolcForwardJacobian(const Functor& f) : Functor(f) {}
|
|
|
|
|
|
|
|
// forward constructors
|
|
|
|
template<typename T0>
|
|
|
|
AdolcForwardJacobian(const T0& a0) : Functor(a0) {}
|
|
|
|
template<typename T0, typename T1>
|
|
|
|
AdolcForwardJacobian(const T0& a0, const T1& a1) : Functor(a0, a1) {}
|
|
|
|
template<typename T0, typename T1, typename T2>
|
|
|
|
AdolcForwardJacobian(const T0& a0, const T1& a1, const T1& a2) : Functor(a0, a1, a2) {}
|
|
|
|
|
|
|
|
typedef typename Functor::InputType InputType;
|
|
|
|
typedef typename Functor::ValueType ValueType;
|
|
|
|
typedef typename Functor::JacobianType JacobianType;
|
|
|
|
|
|
|
|
typedef Matrix<ActiveScalar, InputType::SizeAtCompileTime, 1> ActiveInput;
|
|
|
|
typedef Matrix<ActiveScalar, ValueType::SizeAtCompileTime, 1> ActiveValue;
|
|
|
|
|
|
|
|
void operator() (const InputType& x, ValueType* v, JacobianType* _jac) const
|
|
|
|
{
|
2010-10-25 22:15:22 +08:00
|
|
|
eigen_assert(v!=0);
|
2009-02-28 00:19:13 +08:00
|
|
|
if (!_jac)
|
|
|
|
{
|
|
|
|
Functor::operator()(x, v);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JacobianType& jac = *_jac;
|
|
|
|
|
|
|
|
ActiveInput ax = x.template cast<ActiveScalar>();
|
|
|
|
ActiveValue av(jac.rows());
|
|
|
|
|
|
|
|
for (int j=0; j<jac.cols(); j++)
|
|
|
|
for (int i=0; i<jac.cols(); i++)
|
|
|
|
ax[i].setADValue(j, i==j ? 1 : 0);
|
|
|
|
|
|
|
|
Functor::operator()(ax, &av);
|
|
|
|
|
|
|
|
for (int i=0; i<jac.rows(); i++)
|
|
|
|
{
|
|
|
|
(*v)[i] = av[i].getValue();
|
|
|
|
for (int j=0; j<jac.cols(); j++)
|
|
|
|
jac.coeffRef(i,j) = av[i].getADValue(j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2009-03-19 04:06:06 +08:00
|
|
|
//@}
|
|
|
|
|
2009-02-28 00:19:13 +08:00
|
|
|
}
|
|
|
|
|
2009-02-04 17:44:44 +08:00
|
|
|
#endif // EIGEN_ADLOC_FORWARD
|