Eigen-unsupported  3.4.90 (git rev 9589cc4e7fd8e4538bedef80dd36c7738977a8be)
 
Loading...
Searching...
No Matches
AdolcForward
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008-2009 Gael Guennebaud <g.gael@free.fr>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10#ifndef EIGEN_ADLOC_FORWARD_MODULE_H
11#define EIGEN_ADLOC_FORWARD_MODULE_H
12
13//--------------------------------------------------------------------------------
14//
15// This file provides support for adolc's adouble type in forward mode.
16// ADOL-C is a C++ automatic differentiation library,
17// see https://projects.coin-or.org/ADOL-C for more information.
18//
19// Note that the maximal number of directions is controlled by
20// the preprocessor token NUMBER_DIRECTIONS. The default is 2.
21//
22//--------------------------------------------------------------------------------
23
24#define ADOLC_TAPELESS
25#ifndef NUMBER_DIRECTIONS
26#define NUMBER_DIRECTIONS 2
27#endif
28#include <adolc/adtl.h>
29
30// adolc defines some very stupid macros:
31#if defined(malloc)
32#undef malloc
33#endif
34
35#if defined(calloc)
36#undef calloc
37#endif
38
39#if defined(realloc)
40#undef realloc
41#endif
42
43#include "../../Eigen/Core"
44
45namespace Eigen {
46
64
65} // namespace Eigen
66
67// Eigen's require a few additional functions which must be defined in the same namespace
68// than the custom scalar type own namespace
69namespace adtl {
70
71inline const adouble& conj(const adouble& x) { return x; }
72inline const adouble& real(const adouble& x) { return x; }
73inline adouble imag(const adouble&) { return 0.; }
74inline adouble abs(const adouble& x) { return fabs(x); }
75inline adouble abs2(const adouble& x) { return x * x; }
76
77inline bool(isinf)(const adouble& x) { return (Eigen::numext::isinf)(x.getValue()); }
78inline bool(isnan)(const adouble& x) { return (Eigen::numext::isnan)(x.getValue()); }
79
80} // namespace adtl
81
82namespace Eigen {
83
84template <>
85struct NumTraits<adtl::adouble> : NumTraits<double> {
86 typedef adtl::adouble Real;
87 typedef adtl::adouble NonInteger;
88 typedef adtl::adouble Nested;
89 enum {
90 IsComplex = 0,
91 IsInteger = 0,
92 IsSigned = 1,
93 RequireInitialization = 1,
94 ReadCost = 1,
95 AddCost = 1,
96 MulCost = 1
97 };
98};
99
100template <typename Functor>
101class AdolcForwardJacobian : public Functor {
102 typedef adtl::adouble ActiveScalar;
103
104 public:
105 AdolcForwardJacobian() : Functor() {}
106 AdolcForwardJacobian(const Functor& f) : Functor(f) {}
107
108 // forward constructors
109 template <typename T0>
110 AdolcForwardJacobian(const T0& a0) : Functor(a0) {}
111 template <typename T0, typename T1>
112 AdolcForwardJacobian(const T0& a0, const T1& a1) : Functor(a0, a1) {}
113 template <typename T0, typename T1, typename T2>
114 AdolcForwardJacobian(const T0& a0, const T1& a1, const T1& a2) : Functor(a0, a1, a2) {}
115
116 typedef typename Functor::InputType InputType;
117 typedef typename Functor::ValueType ValueType;
118 typedef typename Functor::JacobianType JacobianType;
119
120 typedef Matrix<ActiveScalar, InputType::SizeAtCompileTime, 1> ActiveInput;
121 typedef Matrix<ActiveScalar, ValueType::SizeAtCompileTime, 1> ActiveValue;
122
123 void operator()(const InputType& x, ValueType* v, JacobianType* _jac) const {
124 eigen_assert(v != 0);
125 if (!_jac) {
126 Functor::operator()(x, v);
127 return;
128 }
129
130 JacobianType& jac = *_jac;
131
132 ActiveInput ax = x.template cast<ActiveScalar>();
133 ActiveValue av(jac.rows());
134
135 for (int j = 0; j < jac.cols(); j++)
136 for (int i = 0; i < jac.cols(); i++) ax[i].setADValue(j, i == j ? 1 : 0);
137
138 Functor::operator()(ax, &av);
139
140 for (int i = 0; i < jac.rows(); i++) {
141 (*v)[i] = av[i].getValue();
142 for (int j = 0; j < jac.cols(); j++) jac.coeffRef(i, j) = av[i].getADValue(j);
143 }
144 }
145
146 protected:
147};
148
150
151} // namespace Eigen
152
153#endif // EIGEN_ADLOC_FORWARD_MODULE_H
Namespace containing all symbols from the Eigen library.