Eigen-Contrib  5.0.1
 
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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_ADOLC_FORWARD_MODULE_H
12#define EIGEN_ADOLC_FORWARD_MODULE_H
13
14//--------------------------------------------------------------------------------
15//
16// This file provides support for adolc's adouble type in forward mode.
17// ADOL-C is a C++ automatic differentiation library,
18// see https://projects.coin-or.org/ADOL-C for more information.
19//
20// Note that the maximal number of directions is controlled by
21// the preprocessor token NUMBER_DIRECTIONS. The default is 2.
22//
23//--------------------------------------------------------------------------------
24
25#define ADOLC_TAPELESS
26#ifndef NUMBER_DIRECTIONS
27#define NUMBER_DIRECTIONS 2
28#endif
29#include <adolc/adtl.h>
30
31// adolc defines some very stupid macros:
32#if defined(malloc)
33#undef malloc
34#endif
35
36#if defined(calloc)
37#undef calloc
38#endif
39
40#if defined(realloc)
41#undef realloc
42#endif
43
44#include "../../Eigen/Core"
45
46#include "../../Eigen/src/Core/util/DisableStupidWarnings.h"
47
48namespace Eigen {
49
67
68} // namespace Eigen
69
70// Eigen's require a few additional functions which must be defined in the same namespace
71// than the custom scalar type own namespace
72namespace adtl {
73
74inline const adouble& conj(const adouble& x) { return x; }
75inline const adouble& real(const adouble& x) { return x; }
76inline adouble imag(const adouble&) { return 0.; }
77inline adouble abs(const adouble& x) { return fabs(x); }
78inline adouble abs2(const adouble& x) { return x * x; }
79
80inline bool(isinf)(const adouble& x) { return (Eigen::numext::isinf)(x.getValue()); }
81inline bool(isnan)(const adouble& x) { return (Eigen::numext::isnan)(x.getValue()); }
82
83} // namespace adtl
84
85namespace Eigen {
86
87template <>
88struct NumTraits<adtl::adouble> : NumTraits<double> {
89 typedef adtl::adouble Real;
90 typedef adtl::adouble NonInteger;
91 typedef adtl::adouble Nested;
92 enum {
93 IsComplex = 0,
94 IsInteger = 0,
95 IsSigned = 1,
96 RequireInitialization = 1,
97 ReadCost = 1,
98 AddCost = 1,
99 MulCost = 1
100 };
101};
102
103template <typename Functor>
104class AdolcForwardJacobian : public Functor {
105 typedef adtl::adouble ActiveScalar;
106
107 public:
108 AdolcForwardJacobian() : Functor() {}
109 AdolcForwardJacobian(const Functor& f) : Functor(f) {}
110
111 // forward constructors
112 template <typename T0>
113 AdolcForwardJacobian(const T0& a0) : Functor(a0) {}
114 template <typename T0, typename T1>
115 AdolcForwardJacobian(const T0& a0, const T1& a1) : Functor(a0, a1) {}
116 template <typename T0, typename T1, typename T2>
117 AdolcForwardJacobian(const T0& a0, const T1& a1, const T2& a2) : Functor(a0, a1, a2) {}
118
119 typedef typename Functor::InputType InputType;
120 typedef typename Functor::ValueType ValueType;
121 typedef typename Functor::JacobianType JacobianType;
122
123 typedef Matrix<ActiveScalar, InputType::SizeAtCompileTime, 1> ActiveInput;
124 typedef Matrix<ActiveScalar, ValueType::SizeAtCompileTime, 1> ActiveValue;
125
126 void operator()(const InputType& x, ValueType* v, JacobianType* _jac) const {
127 eigen_assert(v != 0);
128 if (!_jac) {
129 Functor::operator()(x, v);
130 return;
131 }
132
133 JacobianType& jac = *_jac;
134
135 ActiveInput ax = x.template cast<ActiveScalar>();
136 ActiveValue av(jac.rows());
137
138 for (int j = 0; j < jac.cols(); j++)
139 for (int i = 0; i < jac.cols(); i++) ax[i].setADValue(j, i == j ? 1 : 0);
140
141 Functor::operator()(ax, &av);
142
143 for (int i = 0; i < jac.rows(); i++) {
144 (*v)[i] = av[i].getValue();
145 for (int j = 0; j < jac.cols(); j++) jac.coeffRef(i, j) = av[i].getADValue(j);
146 }
147 }
148
149 protected:
150};
151
153
154} // namespace Eigen
155
156#include "../../Eigen/src/Core/util/ReenableStupidWarnings.h"
157
158#endif // EIGEN_ADOLC_FORWARD_MODULE_H
Namespace containing all symbols from the Eigen library.