Eigen-Contrib  5.0.1-dev+master
 
Loading...
Searching...
No Matches
AutoDiffScalar.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.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_AUTODIFF_SCALAR_H
12#define EIGEN_AUTODIFF_SCALAR_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20
21template <typename DerivativeType, bool Enable>
22struct auto_diff_special_op;
23
24template <typename DerivativeType, typename OtherDerivativeType, typename EnableIf = void>
25struct maybe_coherent_pad_helper {
26 static constexpr int SizeAtCompileTime =
27 max_size_prefer_dynamic(DerivativeType::SizeAtCompileTime, OtherDerivativeType::SizeAtCompileTime);
28 using type = CoherentPadOp<DerivativeType, SizeAtCompileTime>;
29 static type pad(const DerivativeType& x, const OtherDerivativeType& y) {
30 // CoherentPadOp uses variable_if_dynamic<SizeAtCompileTime>. In this case, `SizeAtCompileTime` might
31 // be Dynamic, so we need to take the runtime maximum of x, y.
32 return CoherentPadOp<DerivativeType, SizeAtCompileTime>(x, numext::maxi(x.size(), y.size()));
33 }
34};
35
36// Both are fixed-sized and equal, don't need to pad.
37// Both are fixed-size and this is larger than other, don't need to pad.
38template <typename DerivativeType, typename OtherDerivativeType>
39struct maybe_coherent_pad_helper<
40 DerivativeType, OtherDerivativeType,
41 std::enable_if_t<enum_ge_not_dynamic(DerivativeType::SizeAtCompileTime, OtherDerivativeType::SizeAtCompileTime)>> {
42 using type = const DerivativeType&;
43 static const DerivativeType& pad(const DerivativeType& x, const OtherDerivativeType& /*y*/) { return x; }
44};
45
46template <typename DerivativeType, typename OtherDerivativeType>
47typename maybe_coherent_pad_helper<DerivativeType, OtherDerivativeType>::type MaybeCoherentPad(
48 const DerivativeType& x, const OtherDerivativeType& y) {
49 return maybe_coherent_pad_helper<DerivativeType, OtherDerivativeType>::pad(x, y);
50}
51
52template <typename Op, typename LhsDerivativeType, typename RhsDerivativeType>
53auto MakeCoherentCwiseBinaryOp(const LhsDerivativeType& x, const RhsDerivativeType& y, Op op = Op()) {
54 const auto& lhs = MaybeCoherentPad(x, y);
55 const auto& rhs = MaybeCoherentPad(y, x);
56 return CwiseBinaryOp<Op, remove_all_t<decltype(lhs)>, remove_all_t<decltype(rhs)>>(lhs, rhs, op);
57}
58
59} // namespace internal
60
61template <typename DerivativeType>
62class AutoDiffScalar;
63
64template <typename NewDerType>
65inline AutoDiffScalar<NewDerType> MakeAutoDiffScalar(const typename NewDerType::Scalar& value, const NewDerType& der) {
66 return AutoDiffScalar<NewDerType>(value, der);
67}
68
95
96template <typename DerivativeType>
98 : public internal::auto_diff_special_op<
99 DerivativeType, !std::is_same<typename internal::traits<internal::remove_all_t<DerivativeType>>::Scalar,
100 typename NumTraits<typename internal::traits<
101 internal::remove_all_t<DerivativeType>>::Scalar>::Real>::value> {
102 public:
103 using Base = internal::auto_diff_special_op<
104 DerivativeType,
105 !std::is_same<
106 typename internal::traits<internal::remove_all_t<DerivativeType>>::Scalar,
108 using DerType = internal::remove_all_t<DerivativeType>;
109 using Scalar = typename internal::traits<DerType>::Scalar;
110 using Real = typename NumTraits<Scalar>::Real;
111
112 using Base::operator+;
113 using Base::operator*;
114
117
120 AutoDiffScalar(const Scalar& value, int nbDer, int derNumber) : m_value(value), m_derivatives(DerType::Zero(nbDer)) {
121 m_derivatives.coeffRef(derNumber) = Scalar(1);
122 }
123
126 /*explicit*/ AutoDiffScalar(const Real& value) : m_value(value) {
127 if (m_derivatives.size() > 0) m_derivatives.setZero();
128 }
129
131 AutoDiffScalar(const Scalar& value, const DerType& der) : m_value(value), m_derivatives(der) {}
132
133#ifndef EIGEN_PARSED_BY_DOXYGEN
134 template <typename OtherDerType,
135 std::enable_if_t<
136 std::is_same<Scalar, typename internal::traits<internal::remove_all_t<OtherDerType>>::Scalar>::value &&
137 std::is_convertible<OtherDerType, DerType>::value,
138 int> = 0>
139#else
140 template <typename OtherDerType>
141#endif
143 : m_value(other.value()), m_derivatives(other.derivatives()) {
144 }
145
146#ifndef EIGEN_PARSED_BY_DOXYGEN
147 template <typename OtherDerType>
148 EIGEN_DEPRECATED_WITH_REASON("Omit the implementation-only second argument.")
150 const AutoDiffScalar<OtherDerType>& other,
151 std::enable_if_t<
152 std::is_same<Scalar, typename internal::traits<internal::remove_all_t<OtherDerType>>::Scalar>::value &&
153 std::is_convertible<OtherDerType, DerType>::value,
154 void*>)
155 : AutoDiffScalar(other) {}
156#endif
157
158 friend std::ostream& operator<<(std::ostream& s, const AutoDiffScalar& a) { return s << a.value(); }
159
160 AutoDiffScalar(const AutoDiffScalar& other) : m_value(other.value()), m_derivatives(other.derivatives()) {}
161
162 template <typename OtherDerType>
163 inline AutoDiffScalar& operator=(const AutoDiffScalar<OtherDerType>& other) {
164 m_value = other.value();
165 m_derivatives = other.derivatives();
166 return *this;
167 }
168
169 inline AutoDiffScalar& operator=(const AutoDiffScalar& other) {
170 m_value = other.value();
171 m_derivatives = other.derivatives();
172 return *this;
173 }
174
175 inline AutoDiffScalar& operator=(const Scalar& other) {
176 m_value = other;
177 if (m_derivatives.size() > 0) m_derivatives.setZero();
178 return *this;
179 }
180
181 // inline operator const Scalar& () const { return m_value; }
182 // inline operator Scalar& () { return m_value; }
183
184 inline const Scalar& value() const { return m_value; }
185 inline Scalar& value() { return m_value; }
186
187 inline const DerType& derivatives() const { return m_derivatives; }
188 inline DerType& derivatives() { return m_derivatives; }
189
190 inline bool operator<(const Scalar& other) const { return m_value < other; }
191 inline bool operator<=(const Scalar& other) const { return m_value <= other; }
192 inline bool operator>(const Scalar& other) const { return m_value > other; }
193 inline bool operator>=(const Scalar& other) const { return m_value >= other; }
194 inline bool operator==(const Scalar& other) const { return m_value == other; }
195 inline bool operator!=(const Scalar& other) const { return m_value != other; }
196
197 friend inline bool operator<(const Scalar& a, const AutoDiffScalar& b) { return a < b.value(); }
198 friend inline bool operator<=(const Scalar& a, const AutoDiffScalar& b) { return a <= b.value(); }
199 friend inline bool operator>(const Scalar& a, const AutoDiffScalar& b) { return a > b.value(); }
200 friend inline bool operator>=(const Scalar& a, const AutoDiffScalar& b) { return a >= b.value(); }
201 friend inline bool operator==(const Scalar& a, const AutoDiffScalar& b) { return a == b.value(); }
202 friend inline bool operator!=(const Scalar& a, const AutoDiffScalar& b) { return a != b.value(); }
203
204 template <typename OtherDerType>
205 inline bool operator<(const AutoDiffScalar<OtherDerType>& b) const {
206 return m_value < b.value();
207 }
208 template <typename OtherDerType>
209 inline bool operator<=(const AutoDiffScalar<OtherDerType>& b) const {
210 return m_value <= b.value();
211 }
212 template <typename OtherDerType>
213 inline bool operator>(const AutoDiffScalar<OtherDerType>& b) const {
214 return m_value > b.value();
215 }
216 template <typename OtherDerType>
217 inline bool operator>=(const AutoDiffScalar<OtherDerType>& b) const {
218 return m_value >= b.value();
219 }
220 template <typename OtherDerType>
221 inline bool operator==(const AutoDiffScalar<OtherDerType>& b) const {
222 return m_value == b.value();
223 }
224 template <typename OtherDerType>
225 inline bool operator!=(const AutoDiffScalar<OtherDerType>& b) const {
226 return m_value != b.value();
227 }
228
229 inline AutoDiffScalar<DerType&> operator+(const Scalar& other) const {
230 return AutoDiffScalar<DerType&>(m_value + other, m_derivatives);
231 }
232
233 friend inline AutoDiffScalar<DerType&> operator+(const Scalar& a, const AutoDiffScalar& b) {
234 return AutoDiffScalar<DerType&>(a + b.value(), b.derivatives());
235 }
236
237 inline AutoDiffScalar& operator+=(const Scalar& other) {
238 value() += other;
239 return *this;
240 }
241
242 template <typename OtherDerType>
243 inline auto operator+(const AutoDiffScalar<OtherDerType>& other) const {
244 return MakeAutoDiffScalar(
245 m_value + other.value(),
246 internal::MakeCoherentCwiseBinaryOp<internal::scalar_sum_op<Scalar>>(m_derivatives, other.derivatives()));
247 }
248
249 template <typename OtherDerType>
250 inline AutoDiffScalar& operator+=(const AutoDiffScalar<OtherDerType>& other) {
251 (*this) = (*this) + other;
252 return *this;
253 }
254
255 inline AutoDiffScalar<DerType&> operator-(const Scalar& b) const {
256 return AutoDiffScalar<DerType&>(m_value - b, m_derivatives);
257 }
258
259 friend inline AutoDiffScalar<CwiseUnaryOp<internal::scalar_opposite_op<Scalar>, const DerType>> operator-(
260 const Scalar& a, const AutoDiffScalar& b) {
261 return AutoDiffScalar<CwiseUnaryOp<internal::scalar_opposite_op<Scalar>, const DerType>>(a - b.value(),
262 -b.derivatives());
263 }
264
265 inline AutoDiffScalar& operator-=(const Scalar& other) {
266 value() -= other;
267 return *this;
268 }
269
270 template <typename OtherDerType>
271 inline auto operator-(const AutoDiffScalar<OtherDerType>& other) const {
272 return MakeAutoDiffScalar(m_value - other.value(),
273 internal::MakeCoherentCwiseBinaryOp<internal::scalar_difference_op<Scalar>>(
274 m_derivatives, other.derivatives()));
275 }
276
277 template <typename OtherDerType>
278 inline AutoDiffScalar& operator-=(const AutoDiffScalar<OtherDerType>& other) {
279 *this = *this - other;
280 return *this;
281 }
282
283 inline AutoDiffScalar<CwiseUnaryOp<internal::scalar_opposite_op<Scalar>, const DerType>> operator-() const {
284 return AutoDiffScalar<CwiseUnaryOp<internal::scalar_opposite_op<Scalar>, const DerType>>(-m_value, -m_derivatives);
285 }
286
287 inline auto operator*(const Scalar& other) const {
288 return MakeAutoDiffScalar(m_value * other, m_derivatives * other);
289 }
290
291 friend inline auto operator*(const Scalar& other, const AutoDiffScalar& a) {
292 return MakeAutoDiffScalar(a.value() * other, a.derivatives() * other);
293 }
294
295 inline auto operator/(const Scalar& other) const {
296 return MakeAutoDiffScalar(m_value / other, (m_derivatives * (Scalar(1) / other)));
297 }
298
299 friend inline auto operator/(const Scalar& other, const AutoDiffScalar& a) {
300 return MakeAutoDiffScalar(other / a.value(), a.derivatives() * (Scalar(-other) / (a.value() * a.value())));
301 }
302
303 template <typename OtherDerType>
304 inline auto operator/(const AutoDiffScalar<OtherDerType>& other) const {
305 return MakeAutoDiffScalar(m_value / other.value(),
306 internal::MakeCoherentCwiseBinaryOp<internal::scalar_difference_op<Scalar>>(
307 m_derivatives * other.value(), (other.derivatives() * m_value)) *
308 (Scalar(1) / (other.value() * other.value())));
309 }
310
311 template <typename OtherDerType>
312 inline auto operator*(const AutoDiffScalar<OtherDerType>& other) const {
313 return MakeAutoDiffScalar(m_value * other.value(),
314 internal::MakeCoherentCwiseBinaryOp<internal::scalar_sum_op<Scalar>>(
315 m_derivatives * other.value(), other.derivatives() * m_value));
316 }
317
318 inline AutoDiffScalar& operator*=(const Scalar& other) {
319 *this = *this * other;
320 return *this;
321 }
322
323 template <typename OtherDerType>
324 inline AutoDiffScalar& operator*=(const AutoDiffScalar<OtherDerType>& other) {
325 *this = *this * other;
326 return *this;
327 }
328
329 inline AutoDiffScalar& operator/=(const Scalar& other) {
330 *this = *this / other;
331 return *this;
332 }
333
334 template <typename OtherDerType>
335 inline AutoDiffScalar& operator/=(const AutoDiffScalar<OtherDerType>& other) {
336 *this = *this / other;
337 return *this;
338 }
339
340 protected:
341 Scalar m_value;
342 DerType m_derivatives;
343};
344
345namespace internal {
346
347template <typename DerivativeType>
348struct auto_diff_special_op<DerivativeType, true> {
349 using DerType = remove_all_t<DerivativeType>;
350 using Scalar = typename traits<DerType>::Scalar;
351 using Real = typename NumTraits<Scalar>::Real;
352
353 const AutoDiffScalar<DerivativeType>& derived() const {
354 return *static_cast<const AutoDiffScalar<DerivativeType>*>(this);
355 }
356 AutoDiffScalar<DerivativeType>& derived() { return *static_cast<AutoDiffScalar<DerivativeType>*>(this); }
357
358 inline AutoDiffScalar<DerType&> operator+(const Real& other) const {
359 return AutoDiffScalar<DerType&>(derived().value() + other, derived().derivatives());
360 }
361
362 friend inline AutoDiffScalar<DerType&> operator+(const Real& a, const AutoDiffScalar<DerivativeType>& b) {
363 return AutoDiffScalar<DerType&>(a + b.value(), b.derivatives());
364 }
365
366 inline AutoDiffScalar<DerivativeType>& operator+=(const Real& other) {
367 derived().value() += other;
368 return derived();
369 }
370
371 inline AutoDiffScalar<typename CwiseUnaryOp<bind2nd_op<scalar_product_op<Scalar, Real>>, DerType>::Type> operator*(
372 const Real& other) const {
373 return AutoDiffScalar<typename CwiseUnaryOp<bind2nd_op<scalar_product_op<Scalar, Real>>, DerType>::Type>(
374 derived().value() * other, derived().derivatives() * other);
375 }
376
377 friend inline AutoDiffScalar<typename CwiseUnaryOp<bind1st_op<scalar_product_op<Real, Scalar>>, DerType>::Type>
378 operator*(const Real& other, const AutoDiffScalar<DerivativeType>& a) {
379 return AutoDiffScalar<typename CwiseUnaryOp<bind1st_op<scalar_product_op<Real, Scalar>>, DerType>::Type>(
380 a.value() * other, a.derivatives() * other);
381 }
382
383 inline AutoDiffScalar<DerivativeType>& operator*=(const Scalar& other) {
384 *this = *this * other;
385 return derived();
386 }
387};
388
389template <typename DerivativeType>
390struct auto_diff_special_op<DerivativeType, false> {
391 void operator*() const;
392 void operator-() const;
393 void operator+() const;
394};
395
396} // end namespace internal
397
398template <typename DerType, typename BinOp>
399struct ScalarBinaryOpTraits<AutoDiffScalar<DerType>, typename DerType::Scalar, BinOp> {
400 using ReturnType = AutoDiffScalar<DerType>;
401};
402
403template <typename DerType, typename BinOp>
404struct ScalarBinaryOpTraits<typename DerType::Scalar, AutoDiffScalar<DerType>, BinOp> {
405 using ReturnType = AutoDiffScalar<DerType>;
406};
407
408#define EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(FUNC, CODE) \
409 template <typename DerType> \
410 inline auto FUNC(const Eigen::AutoDiffScalar<DerType>& x) { \
411 using namespace Eigen; \
412 using Scalar = typename Eigen::internal::traits<Eigen::internal::remove_all_t<DerType>>::Scalar; \
413 EIGEN_UNUSED_VARIABLE(sizeof(Scalar)); \
414 CODE; \
415 }
416
417template <typename DerType>
418struct CleanedUpDerType {
419 using type = AutoDiffScalar<typename Eigen::internal::remove_all_t<DerType>::PlainObject>;
420};
421
422template <typename DerType>
423inline const AutoDiffScalar<DerType>& conj(const AutoDiffScalar<DerType>& x) {
424 return x;
425}
426template <typename DerType>
427inline const AutoDiffScalar<DerType>& real(const AutoDiffScalar<DerType>& x) {
428 return x;
429}
430template <typename DerType>
431inline typename DerType::Scalar imag(const AutoDiffScalar<DerType>&) {
432 return 0.;
433}
434template <typename DerType>
435inline bool(isnan)(const AutoDiffScalar<DerType>& x) {
436 return (numext::isnan)(x.value());
437}
438template <typename DerType>
439inline bool(isinf)(const AutoDiffScalar<DerType>& x) {
440 return (numext::isinf)(x.value());
441}
442template <typename DerType>
443inline bool(isfinite)(const AutoDiffScalar<DerType>& x) {
444 return (numext::isfinite)(x.value());
445}
446template <typename DerType, typename T>
447inline typename CleanedUpDerType<DerType>::type(min)(const AutoDiffScalar<DerType>& x, const T& y) {
448 using ADS = typename CleanedUpDerType<DerType>::type;
449 return (x <= y ? ADS(x) : ADS(y));
450}
451template <typename DerType, typename T>
452inline typename CleanedUpDerType<DerType>::type(max)(const AutoDiffScalar<DerType>& x, const T& y) {
453 using ADS = typename CleanedUpDerType<DerType>::type;
454 return (x >= y ? ADS(x) : ADS(y));
455}
456template <typename DerType, typename T>
457inline typename CleanedUpDerType<DerType>::type(min)(const T& x, const AutoDiffScalar<DerType>& y) {
458 using ADS = typename CleanedUpDerType<DerType>::type;
459 return (x < y ? ADS(x) : ADS(y));
460}
461template <typename DerType, typename T>
462inline typename CleanedUpDerType<DerType>::type(max)(const T& x, const AutoDiffScalar<DerType>& y) {
463 using ADS = typename CleanedUpDerType<DerType>::type;
464 return (x > y ? ADS(x) : ADS(y));
465}
466template <typename DerType>
467inline
468 typename CleanedUpDerType<DerType>::type(min)(const AutoDiffScalar<DerType>& x, const AutoDiffScalar<DerType>& y) {
469 // Match std::min / max(ADS, ADS): on tie, return the first argument.
470 return (x.value() <= y.value() ? x : y);
471}
472template <typename DerType>
473inline
474 typename CleanedUpDerType<DerType>::type(max)(const AutoDiffScalar<DerType>& x, const AutoDiffScalar<DerType>& y) {
475 return (x.value() >= y.value() ? x : y);
476}
477
478EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(abs, using std::abs;
479 return Eigen::MakeAutoDiffScalar(abs(x.value()),
480 x.derivatives() * (x.value() < 0 ? -1 : 1));)
481
482EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(abs2, using numext::abs2;
483 return Eigen::MakeAutoDiffScalar(abs2(x.value()),
484 x.derivatives() * (Scalar(2) * x.value()));)
485
486EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(sqrt, using std::sqrt; Scalar sqrtx = sqrt(x.value());
487 return Eigen::MakeAutoDiffScalar(sqrtx, x.derivatives() * (Scalar(0.5) / sqrtx));)
488
489EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(cos, using std::cos; using std::sin;
490 return Eigen::MakeAutoDiffScalar(cos(x.value()),
491 x.derivatives() * (-sin(x.value())));)
492
493EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(sin, using std::sin; using std::cos;
494 return Eigen::MakeAutoDiffScalar(sin(x.value()), x.derivatives() * cos(x.value()));)
495
496EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(exp, using std::exp; Scalar expx = exp(x.value());
497 return Eigen::MakeAutoDiffScalar(expx, x.derivatives() * expx);)
498
499EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(log, using std::log;
500 return Eigen::MakeAutoDiffScalar(log(x.value()),
501 x.derivatives() * (Scalar(1) / x.value()));)
502
503template <typename DerType>
504inline auto pow(const Eigen::AutoDiffScalar<DerType>& x,
505 const typename internal::traits<internal::remove_all_t<DerType>>::Scalar& y) {
506 using namespace Eigen;
507 using std::pow;
508 return Eigen::MakeAutoDiffScalar(pow(x.value(), y), x.derivatives() * (y * pow(x.value(), y - 1)));
509}
510
511template <typename DerTypeA, typename DerTypeB>
514 using std::atan2;
515 using Scalar = typename internal::traits<internal::remove_all_t<DerTypeA>>::Scalar;
517 PlainADS ret;
518 ret.value() = atan2(a.value(), b.value());
519
520 Scalar squared_hypot = a.value() * a.value() + b.value() * b.value();
521
522 // if (squared_hypot==0) the derivation is undefined and the following results in a NaN:
523 ret.derivatives() = internal::MakeCoherentCwiseBinaryOp<internal::scalar_difference_op<Scalar>>(
524 a.derivatives() * b.value(), b.derivatives() * a.value()) /
525 squared_hypot;
526
527 return ret;
528}
529
530EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(tan, using std::tan; using std::cos; return Eigen::MakeAutoDiffScalar(
531 tan(x.value()), x.derivatives() * (Scalar(1) / numext::abs2(cos(x.value()))));)
532
533template <typename DerType>
534inline auto atan(const AutoDiffScalar<DerType>& x) {
535 using Scalar = typename AutoDiffScalar<DerType>::Scalar;
536 using RealScalar = typename NumTraits<Scalar>::Real;
537 const Scalar& value = x.value();
538 Scalar scale(1), denominator;
539 if (numext::abs(value) > RealScalar(1)) {
540 // dx / x / (x + 1/x) avoids overflowing x*x or underflowing its reciprocal before multiplying dx.
541 scale = value;
542 denominator = value + Scalar(1) / value;
543 } else {
544 denominator = Scalar(1) + value * value;
545 }
546 return MakeAutoDiffScalar(numext::atan(value), (x.derivatives() / scale) / denominator);
547}
548
549EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(asin, using std::sqrt; using std::asin; return Eigen::MakeAutoDiffScalar(
550 asin(x.value()),
551 x.derivatives() * (Scalar(1) / sqrt(1 - numext::abs2(x.value()))));)
552
553EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(acos, using std::sqrt; using std::acos; return Eigen::MakeAutoDiffScalar(
554 acos(x.value()),
555 x.derivatives() * (Scalar(-1) / sqrt(1 - numext::abs2(x.value()))));)
556
557EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(
558 tanh, using std::cosh; using std::tanh;
559 return Eigen::MakeAutoDiffScalar(tanh(x.value()), x.derivatives() * (Scalar(1) / numext::abs2(cosh(x.value()))));)
560
561EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(sinh, using std::sinh; using std::cosh;
562 return Eigen::MakeAutoDiffScalar(sinh(x.value()),
563 x.derivatives() * cosh(x.value()));)
564
565EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(cosh, using std::sinh; using std::cosh;
566 return Eigen::MakeAutoDiffScalar(cosh(x.value()),
567 x.derivatives() * sinh(x.value()));)
568
569EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(log1p, return MakeAutoDiffScalar(numext::log1p(x.value()),
570 x.derivatives() / (Scalar(1) + x.value()));)
571
572EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(expm1, return MakeAutoDiffScalar(numext::expm1(x.value()),
573 x.derivatives() * numext::exp(x.value()));)
574
575EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(log2,
576 return MakeAutoDiffScalar(numext::log2(x.value()),
577 (x.derivatives() / x.value()) / numext::log(Scalar(2)));)
578
579template <typename DerType>
580inline auto log10(const AutoDiffScalar<DerType>& x) {
581 using Scalar = typename AutoDiffScalar<DerType>::Scalar;
582 using RealScalar = typename NumTraits<Scalar>::Real;
583 const Scalar logarithm = numext::log(Scalar(10));
584 EIGEN_IF_CONSTEXPR (NumTraits<Scalar>::IsComplex) {
585 const bool below_one = numext::abs(x.value()) < RealScalar(1);
586 const Scalar first = below_one ? logarithm : x.value();
587 const Scalar second = below_one ? x.value() : logarithm;
588 return MakeAutoDiffScalar(numext::log10(x.value()), (x.derivatives() / first) / second);
589 }
590 const RealScalar magnitude = numext::abs(x.value());
591 const bool subnormal = magnitude < RealScalar((numext::numeric_limits<RealScalar>::min)());
592 const bool combine = magnitude < RealScalar(1) && !subnormal;
593 // Combine normal small x with log(10) before dividing a tiny seed. For subnormal x,
594 // (x/eps)*(log(10)*eps) keeps both factors normal without rounding away the seed.
595 const Scalar scale = subnormal ? Scalar(NumTraits<Scalar>::epsilon()) : Scalar(1);
596 const Scalar first = (x.value() * (combine ? logarithm : Scalar(1))) / scale;
597 const Scalar second = (combine ? Scalar(1) : logarithm) * scale;
598 return MakeAutoDiffScalar(numext::log10(x.value()), (x.derivatives() / first) / second);
599}
600
601EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(cbrt, const Scalar root = numext::cbrt(x.value());
602 return MakeAutoDiffScalar(root, x.derivatives() / (Scalar(3) * root * root));)
603
604EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(asinh,
605 return MakeAutoDiffScalar(numext::asinh(x.value()),
606 x.derivatives() / numext::hypot(Scalar(1), x.value()));)
607
608template <typename DerType>
609inline auto acosh(const AutoDiffScalar<DerType>& x) {
610 using Scalar = typename AutoDiffScalar<DerType>::Scalar;
611 // Separate square roots avoid overflowing x*x for large x.
612 const Scalar lower = numext::sqrt(Scalar(x.value() - Scalar(1)));
613 const Scalar upper = numext::sqrt(Scalar(x.value() + Scalar(1)));
614 return MakeAutoDiffScalar(numext::acosh(x.value()), (x.derivatives() / upper) / lower);
615}
616
617template <typename DerType>
618inline auto atanh(const AutoDiffScalar<DerType>& x) {
619 using Scalar = typename AutoDiffScalar<DerType>::Scalar;
620 using RealScalar = typename NumTraits<Scalar>::Real;
621 const Scalar lower = Scalar(1) - x.value();
622 const Scalar upper = Scalar(1) + x.value();
623 const bool lower_first = numext::abs(lower) > numext::abs(upper);
624 const Scalar larger = lower_first ? lower : upper;
625 const Scalar smaller = lower_first ? upper : lower;
626 EIGEN_IF_CONSTEXPR (NumTraits<Scalar>::IsComplex) {
627 return MakeAutoDiffScalar(numext::atanh(x.value()), (x.derivatives() / larger) / smaller);
628 }
629 // For real |x| <= 1 the product is in [0,1]: divide by it before rounding a tiny seed.
630 // Outside that interval, retain separate factors to avoid overflowing their product.
631 const bool combine = numext::abs(x.value()) <= RealScalar(1);
632 const Scalar first = larger * (combine ? smaller : Scalar(1));
633 const Scalar second = combine ? Scalar(1) : smaller;
634 return MakeAutoDiffScalar(numext::atanh(x.value()), (x.derivatives() / first) / second);
635}
636
637#undef EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY
638
639template <typename DerType>
640struct NumTraits<AutoDiffScalar<DerType>>
641 : NumTraits<typename NumTraits<typename internal::remove_all_t<DerType>::Scalar>::Real> {
642 using DerTypeCleaned = internal::remove_all_t<DerType>;
643 using Real = AutoDiffScalar<Matrix<typename NumTraits<typename DerTypeCleaned::Scalar>::Real,
644 DerTypeCleaned::RowsAtCompileTime, DerTypeCleaned::ColsAtCompileTime, 0,
645 DerTypeCleaned::MaxRowsAtCompileTime, DerTypeCleaned::MaxColsAtCompileTime>>;
646 using NonInteger = AutoDiffScalar<DerType>;
647 using Nested = AutoDiffScalar<DerType>;
648 using Literal = typename NumTraits<typename DerTypeCleaned::Scalar>::Literal;
649 enum { RequireInitialization = 1 };
650};
651
652namespace internal {
653template <typename LhsDerivatives, typename RhsDerivatives>
654struct functor_is_commutative<scalar_product_op<AutoDiffScalar<LhsDerivatives>, AutoDiffScalar<RhsDerivatives>>>
655 : functor_is_commutative<scalar_product_op<typename remove_all_t<LhsDerivatives>::Scalar,
656 typename remove_all_t<RhsDerivatives>::Scalar>> {};
657
658template <typename Derivatives>
659struct functor_is_commutative<
660 scalar_product_op<AutoDiffScalar<Derivatives>, typename remove_all_t<Derivatives>::Scalar>>
661 : functor_is_commutative<scalar_product_op<typename remove_all_t<Derivatives>::Scalar>> {};
662
663template <typename Derivatives>
664struct functor_is_commutative<
665 scalar_product_op<typename remove_all_t<Derivatives>::Scalar, AutoDiffScalar<Derivatives>>>
666 : functor_is_commutative<scalar_product_op<typename remove_all_t<Derivatives>::Scalar>> {};
667
668template <typename DerivativeType>
669struct is_identically_zero_impl<AutoDiffScalar<DerivativeType>> {
670 static inline bool run(const AutoDiffScalar<DerivativeType>& s) {
671 const DerivativeType& derivatives = s.derivatives();
672 for (int i = 0; i < derivatives.size(); ++i) {
673 if (!numext::is_exactly_zero(derivatives[i])) {
674 return false;
675 }
676 }
677 return numext::is_exactly_zero(s.value());
678 }
679};
680} // namespace internal
681} // namespace Eigen
682
683namespace std {
684
685template <typename T>
686class numeric_limits<Eigen::AutoDiffScalar<T>> : public numeric_limits<typename T::Scalar> {};
687
688template <typename T>
689class numeric_limits<Eigen::AutoDiffScalar<T&>> : public numeric_limits<typename T::Scalar> {};
690
691} // namespace std
692
693#endif // EIGEN_AUTODIFF_SCALAR_H
A scalar type replacement with automatic differentiation capability.
Definition AutoDiffScalar.h:101
AutoDiffScalar()
Definition AutoDiffScalar.h:116
AutoDiffScalar(const Scalar &value, int nbDer, int derNumber)
Definition AutoDiffScalar.h:120
AutoDiffScalar(const Real &value)
Definition AutoDiffScalar.h:126
AutoDiffScalar(const Scalar &value, const DerType &der)
Definition AutoDiffScalar.h:131
Namespace containing all symbols from the Eigen library.