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 assignDerivatives(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 // d(c/b) = -(c/b) db / b: dividing by b twice keeps every intermediate bounded by the result, whereas b*b can
301 // underflow or overflow.
302 return MakeAutoDiffScalar(other / a.value(), a.derivatives() * (-(other / a.value()) / a.value()));
303 }
304
305 template <typename OtherDerType>
306 inline auto operator/(const AutoDiffScalar<OtherDerType>& other) const {
307 // d(a/b) = (da b - db a) / b^2. When b^2 underflows below the smallest normal number or overflows, 1/b^2 is
308 // inexact, zero or infinite, so use d(a/b) = (da - (a/b) db) / b instead: its terms are bounded by the result when
309 // |b| < 1.
310 const Scalar& b = other.value();
311 const Scalar q = m_value / b;
312 const Real square = numext::abs2(b);
313 Scalar scale_der, scale_other, scale;
314 if (square < Real((numext::numeric_limits<Real>::min)()) || square > Real((numext::numeric_limits<Real>::max)())) {
315 scale_der = Scalar(1);
316 scale_other = q;
317 scale = Scalar(1) / b;
318 } else {
319 scale_der = b;
320 scale_other = m_value;
321 scale = Scalar(1) / (b * b);
322 }
323 return MakeAutoDiffScalar(q, internal::MakeCoherentCwiseBinaryOp<internal::scalar_difference_op<Scalar>>(
324 m_derivatives * scale_der, other.derivatives() * scale_other) *
325 scale);
326 }
327
328 template <typename OtherDerType>
329 inline auto operator*(const AutoDiffScalar<OtherDerType>& other) const {
330 return MakeAutoDiffScalar(m_value * other.value(),
331 internal::MakeCoherentCwiseBinaryOp<internal::scalar_sum_op<Scalar>>(
332 m_derivatives * other.value(), other.derivatives() * m_value));
333 }
334
335 inline AutoDiffScalar& operator*=(const Scalar& other) {
336 *this = *this * other;
337 return *this;
338 }
339
340 template <typename OtherDerType>
341 inline AutoDiffScalar& operator*=(const AutoDiffScalar<OtherDerType>& other) {
342 *this = *this * other;
343 return *this;
344 }
345
346 inline AutoDiffScalar& operator/=(const Scalar& other) {
347 *this = *this / other;
348 return *this;
349 }
350
351 template <typename OtherDerType>
352 inline AutoDiffScalar& operator/=(const AutoDiffScalar<OtherDerType>& other) {
353 *this = *this / other;
354 return *this;
355 }
356
357 protected:
358 // The right-hand side of `x = x + y` or `x += y` is an expression that reads x's derivatives. When the vectors differ
359 // in length, assigning it directly would resize (free) m_derivatives before the expression is read, so evaluate
360 // first. Same-size and fixed-size assignments resize nothing and are evaluated coefficient by coefficient as before.
361 template <typename OtherDerivatives>
362 void assignDerivatives(const OtherDerivatives& other) {
363 EIGEN_IF_CONSTEXPR (int(DerType::SizeAtCompileTime) == Dynamic &&
364 !std::is_same<internal::remove_all_t<OtherDerivatives>, typename DerType::PlainObject>::value) {
365 if (m_derivatives.size() != 0 && m_derivatives.size() != other.size()) {
366 const typename DerType::PlainObject evaluated = other;
367 m_derivatives = evaluated;
368 return;
369 }
370 }
371 m_derivatives = other;
372 }
373
374 Scalar m_value;
375 DerType m_derivatives;
376};
377
378namespace internal {
379
380template <typename DerivativeType>
381struct auto_diff_special_op<DerivativeType, true> {
382 using DerType = remove_all_t<DerivativeType>;
383 using Scalar = typename traits<DerType>::Scalar;
384 using Real = typename NumTraits<Scalar>::Real;
385
386 const AutoDiffScalar<DerivativeType>& derived() const {
387 return *static_cast<const AutoDiffScalar<DerivativeType>*>(this);
388 }
389 AutoDiffScalar<DerivativeType>& derived() { return *static_cast<AutoDiffScalar<DerivativeType>*>(this); }
390
391 inline AutoDiffScalar<DerType&> operator+(const Real& other) const {
392 return AutoDiffScalar<DerType&>(derived().value() + other, derived().derivatives());
393 }
394
395 friend inline AutoDiffScalar<DerType&> operator+(const Real& a, const AutoDiffScalar<DerivativeType>& b) {
396 return AutoDiffScalar<DerType&>(a + b.value(), b.derivatives());
397 }
398
399 inline AutoDiffScalar<DerivativeType>& operator+=(const Real& other) {
400 derived().value() += other;
401 return derived();
402 }
403
404 inline AutoDiffScalar<typename CwiseUnaryOp<bind2nd_op<scalar_product_op<Scalar, Real>>, DerType>::Type> operator*(
405 const Real& other) const {
406 return AutoDiffScalar<typename CwiseUnaryOp<bind2nd_op<scalar_product_op<Scalar, Real>>, DerType>::Type>(
407 derived().value() * other, derived().derivatives() * other);
408 }
409
410 friend inline AutoDiffScalar<typename CwiseUnaryOp<bind1st_op<scalar_product_op<Real, Scalar>>, DerType>::Type>
411 operator*(const Real& other, const AutoDiffScalar<DerivativeType>& a) {
412 return AutoDiffScalar<typename CwiseUnaryOp<bind1st_op<scalar_product_op<Real, Scalar>>, DerType>::Type>(
413 a.value() * other, a.derivatives() * other);
414 }
415
416 inline AutoDiffScalar<DerivativeType>& operator*=(const Scalar& other) {
417 *this = *this * other;
418 return derived();
419 }
420};
421
422template <typename DerivativeType>
423struct auto_diff_special_op<DerivativeType, false> {
424 void operator*() const;
425 void operator-() const;
426 void operator+() const;
427};
428
429} // end namespace internal
430
431template <typename DerType, typename BinOp>
432struct ScalarBinaryOpTraits<AutoDiffScalar<DerType>, typename DerType::Scalar, BinOp> {
433 using ReturnType = AutoDiffScalar<DerType>;
434};
435
436template <typename DerType, typename BinOp>
437struct ScalarBinaryOpTraits<typename DerType::Scalar, AutoDiffScalar<DerType>, BinOp> {
438 using ReturnType = AutoDiffScalar<DerType>;
439};
440
441#define EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(FUNC, CODE) \
442 template <typename DerType> \
443 inline auto FUNC(const Eigen::AutoDiffScalar<DerType>& x) { \
444 using namespace Eigen; \
445 using Scalar = typename Eigen::internal::traits<Eigen::internal::remove_all_t<DerType>>::Scalar; \
446 EIGEN_UNUSED_VARIABLE(sizeof(Scalar)); \
447 CODE; \
448 }
449
450template <typename DerType>
451struct CleanedUpDerType {
452 using type = AutoDiffScalar<typename Eigen::internal::remove_all_t<DerType>::PlainObject>;
453};
454
455template <typename DerType>
456inline const AutoDiffScalar<DerType>& conj(const AutoDiffScalar<DerType>& x) {
457 return x;
458}
459template <typename DerType>
460inline const AutoDiffScalar<DerType>& real(const AutoDiffScalar<DerType>& x) {
461 return x;
462}
463template <typename DerType>
464inline typename DerType::Scalar imag(const AutoDiffScalar<DerType>&) {
465 return 0.;
466}
467template <typename DerType>
468inline bool(isnan)(const AutoDiffScalar<DerType>& x) {
469 return (numext::isnan)(x.value());
470}
471template <typename DerType>
472inline bool(isinf)(const AutoDiffScalar<DerType>& x) {
473 return (numext::isinf)(x.value());
474}
475template <typename DerType>
476inline bool(isfinite)(const AutoDiffScalar<DerType>& x) {
477 return (numext::isfinite)(x.value());
478}
479template <typename DerType, typename T>
480inline typename CleanedUpDerType<DerType>::type(min)(const AutoDiffScalar<DerType>& x, const T& y) {
481 using ADS = typename CleanedUpDerType<DerType>::type;
482 return (x <= y ? ADS(x) : ADS(y));
483}
484template <typename DerType, typename T>
485inline typename CleanedUpDerType<DerType>::type(max)(const AutoDiffScalar<DerType>& x, const T& y) {
486 using ADS = typename CleanedUpDerType<DerType>::type;
487 return (x >= y ? ADS(x) : ADS(y));
488}
489template <typename DerType, typename T>
490inline typename CleanedUpDerType<DerType>::type(min)(const T& x, const AutoDiffScalar<DerType>& y) {
491 using ADS = typename CleanedUpDerType<DerType>::type;
492 return (x < y ? ADS(x) : ADS(y));
493}
494template <typename DerType, typename T>
495inline typename CleanedUpDerType<DerType>::type(max)(const T& x, const AutoDiffScalar<DerType>& y) {
496 using ADS = typename CleanedUpDerType<DerType>::type;
497 return (x > y ? ADS(x) : ADS(y));
498}
499template <typename DerType>
500inline
501 typename CleanedUpDerType<DerType>::type(min)(const AutoDiffScalar<DerType>& x, const AutoDiffScalar<DerType>& y) {
502 // Match std::min / max(ADS, ADS): on tie, return the first argument.
503 return (x.value() <= y.value() ? x : y);
504}
505template <typename DerType>
506inline
507 typename CleanedUpDerType<DerType>::type(max)(const AutoDiffScalar<DerType>& x, const AutoDiffScalar<DerType>& y) {
508 return (x.value() >= y.value() ? x : y);
509}
510
511EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(abs, using std::abs;
512 return Eigen::MakeAutoDiffScalar(abs(x.value()),
513 x.derivatives() * (x.value() < 0 ? -1 : 1));)
514
515EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(abs2, using numext::abs2;
516 return Eigen::MakeAutoDiffScalar(abs2(x.value()),
517 x.derivatives() * (Scalar(2) * x.value()));)
518
519EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(sqrt, using std::sqrt; Scalar sqrtx = sqrt(x.value());
520 return Eigen::MakeAutoDiffScalar(sqrtx, x.derivatives() * (Scalar(0.5) / sqrtx));)
521
522EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(cos, using std::cos; using std::sin;
523 return Eigen::MakeAutoDiffScalar(cos(x.value()),
524 x.derivatives() * (-sin(x.value())));)
525
526EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(sin, using std::sin; using std::cos;
527 return Eigen::MakeAutoDiffScalar(sin(x.value()), x.derivatives() * cos(x.value()));)
528
529EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(exp, using std::exp; Scalar expx = exp(x.value());
530 return Eigen::MakeAutoDiffScalar(expx, x.derivatives() * expx);)
531
532EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(log, using std::log;
533 return Eigen::MakeAutoDiffScalar(log(x.value()),
534 x.derivatives() * (Scalar(1) / x.value()));)
535
536template <typename DerType>
537inline auto pow(const Eigen::AutoDiffScalar<DerType>& x,
538 const typename internal::traits<internal::remove_all_t<DerType>>::Scalar& y) {
539 using namespace Eigen;
540 using Scalar = typename internal::traits<internal::remove_all_t<DerType>>::Scalar;
541 using std::pow;
542 // y * x^(y-1) is 0 * inf at x = 0, but x^0 is constant.
543 const Scalar slope = y == Scalar(0) ? Scalar(0) : y * pow(x.value(), y - 1);
544 return Eigen::MakeAutoDiffScalar(pow(x.value(), y), x.derivatives() * slope);
545}
546
547template <typename DerTypeA, typename DerTypeB>
550 using std::atan2;
551 using Scalar = typename internal::traits<internal::remove_all_t<DerTypeA>>::Scalar;
553 PlainADS ret;
554 ret.value() = atan2(a.value(), b.value());
555
556 Scalar squared_hypot = a.value() * a.value() + b.value() * b.value();
557
558 // if (squared_hypot==0) the derivation is undefined and the following results in a NaN:
559 ret.derivatives() = internal::MakeCoherentCwiseBinaryOp<internal::scalar_difference_op<Scalar>>(
560 a.derivatives() * b.value(), b.derivatives() * a.value()) /
561 squared_hypot;
562
563 return ret;
564}
565
566EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(tan, using std::tan; using std::cos; return Eigen::MakeAutoDiffScalar(
567 tan(x.value()), x.derivatives() * (Scalar(1) / numext::abs2(cos(x.value()))));)
568
569template <typename DerType>
570inline auto atan(const AutoDiffScalar<DerType>& x) {
571 using Scalar = typename AutoDiffScalar<DerType>::Scalar;
572 using RealScalar = typename NumTraits<Scalar>::Real;
573 const Scalar& value = x.value();
574 Scalar scale(1), denominator;
575 if (numext::abs(value) > RealScalar(1)) {
576 // dx / x / (x + 1/x) avoids overflowing x*x or underflowing its reciprocal before multiplying dx.
577 scale = value;
578 denominator = value + Scalar(1) / value;
579 } else {
580 denominator = Scalar(1) + value * value;
581 }
582 return MakeAutoDiffScalar(numext::atan(value), (x.derivatives() / scale) / denominator);
583}
584
585EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(asin, using std::sqrt; using std::asin; return Eigen::MakeAutoDiffScalar(
586 asin(x.value()),
587 x.derivatives() * (Scalar(1) / sqrt(1 - numext::abs2(x.value()))));)
588
589EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(acos, using std::sqrt; using std::acos; return Eigen::MakeAutoDiffScalar(
590 acos(x.value()),
591 x.derivatives() * (Scalar(-1) / sqrt(1 - numext::abs2(x.value()))));)
592
593EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(
594 tanh, using std::cosh; using std::tanh;
595 return Eigen::MakeAutoDiffScalar(tanh(x.value()), x.derivatives() * (Scalar(1) / numext::abs2(cosh(x.value()))));)
596
597EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(sinh, using std::sinh; using std::cosh;
598 return Eigen::MakeAutoDiffScalar(sinh(x.value()),
599 x.derivatives() * cosh(x.value()));)
600
601EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(cosh, using std::sinh; using std::cosh;
602 return Eigen::MakeAutoDiffScalar(cosh(x.value()),
603 x.derivatives() * sinh(x.value()));)
604
605EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(log1p, return MakeAutoDiffScalar(numext::log1p(x.value()),
606 x.derivatives() / (Scalar(1) + x.value()));)
607
608EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(expm1, return MakeAutoDiffScalar(numext::expm1(x.value()),
609 x.derivatives() * numext::exp(x.value()));)
610
611EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(log2,
612 return MakeAutoDiffScalar(numext::log2(x.value()),
613 (x.derivatives() / x.value()) / numext::log(Scalar(2)));)
614
615template <typename DerType>
616inline auto log10(const AutoDiffScalar<DerType>& x) {
617 using Scalar = typename AutoDiffScalar<DerType>::Scalar;
618 using RealScalar = typename NumTraits<Scalar>::Real;
619 const Scalar logarithm = numext::log(Scalar(10));
620 EIGEN_IF_CONSTEXPR (NumTraits<Scalar>::IsComplex) {
621 const bool below_one = numext::abs(x.value()) < RealScalar(1);
622 const Scalar first = below_one ? logarithm : x.value();
623 const Scalar second = below_one ? x.value() : logarithm;
624 return MakeAutoDiffScalar(numext::log10(x.value()), (x.derivatives() / first) / second);
625 }
626 const RealScalar magnitude = numext::abs(x.value());
627 const bool subnormal = magnitude < RealScalar((numext::numeric_limits<RealScalar>::min)());
628 const bool combine = magnitude < RealScalar(1) && !subnormal;
629 // Combine normal small x with log(10) before dividing a tiny seed. For subnormal x,
630 // (x/eps)*(log(10)*eps) keeps both factors normal without rounding away the seed.
631 const Scalar scale = subnormal ? Scalar(NumTraits<Scalar>::epsilon()) : Scalar(1);
632 const Scalar first = (x.value() * (combine ? logarithm : Scalar(1))) / scale;
633 const Scalar second = (combine ? Scalar(1) : logarithm) * scale;
634 return MakeAutoDiffScalar(numext::log10(x.value()), (x.derivatives() / first) / second);
635}
636
637EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(cbrt, const Scalar root = numext::cbrt(x.value());
638 return MakeAutoDiffScalar(root, x.derivatives() / (Scalar(3) * root * root));)
639
640EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(asinh,
641 return MakeAutoDiffScalar(numext::asinh(x.value()),
642 x.derivatives() / numext::hypot(Scalar(1), x.value()));)
643
644template <typename DerType>
645inline auto acosh(const AutoDiffScalar<DerType>& x) {
646 using Scalar = typename AutoDiffScalar<DerType>::Scalar;
647 // Separate square roots avoid overflowing x*x for large x.
648 const Scalar lower = numext::sqrt(Scalar(x.value() - Scalar(1)));
649 const Scalar upper = numext::sqrt(Scalar(x.value() + Scalar(1)));
650 return MakeAutoDiffScalar(numext::acosh(x.value()), (x.derivatives() / upper) / lower);
651}
652
653template <typename DerType>
654inline auto atanh(const AutoDiffScalar<DerType>& x) {
655 using Scalar = typename AutoDiffScalar<DerType>::Scalar;
656 using RealScalar = typename NumTraits<Scalar>::Real;
657 const Scalar lower = Scalar(1) - x.value();
658 const Scalar upper = Scalar(1) + x.value();
659 const bool lower_first = numext::abs(lower) > numext::abs(upper);
660 const Scalar larger = lower_first ? lower : upper;
661 const Scalar smaller = lower_first ? upper : lower;
662 EIGEN_IF_CONSTEXPR (NumTraits<Scalar>::IsComplex) {
663 return MakeAutoDiffScalar(numext::atanh(x.value()), (x.derivatives() / larger) / smaller);
664 }
665 // For real |x| <= 1 the product is in [0,1]: divide by it before rounding a tiny seed.
666 // Outside that interval, retain separate factors to avoid overflowing their product.
667 const bool combine = numext::abs(x.value()) <= RealScalar(1);
668 const Scalar first = larger * (combine ? smaller : Scalar(1));
669 const Scalar second = combine ? Scalar(1) : smaller;
670 return MakeAutoDiffScalar(numext::atanh(x.value()), (x.derivatives() / first) / second);
671}
672
673#undef EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY
674
675template <typename DerType>
676struct NumTraits<AutoDiffScalar<DerType>>
677 : NumTraits<typename NumTraits<typename internal::remove_all_t<DerType>::Scalar>::Real> {
678 using DerTypeCleaned = internal::remove_all_t<DerType>;
679 using Real = AutoDiffScalar<Matrix<typename NumTraits<typename DerTypeCleaned::Scalar>::Real,
680 DerTypeCleaned::RowsAtCompileTime, DerTypeCleaned::ColsAtCompileTime, 0,
681 DerTypeCleaned::MaxRowsAtCompileTime, DerTypeCleaned::MaxColsAtCompileTime>>;
682 using NonInteger = AutoDiffScalar<DerType>;
683 using Nested = AutoDiffScalar<DerType>;
684 using Literal = typename NumTraits<typename DerTypeCleaned::Scalar>::Literal;
685 enum { RequireInitialization = 1 };
686};
687
688namespace internal {
689template <typename LhsDerivatives, typename RhsDerivatives>
690struct functor_is_commutative<scalar_product_op<AutoDiffScalar<LhsDerivatives>, AutoDiffScalar<RhsDerivatives>>>
691 : functor_is_commutative<scalar_product_op<typename remove_all_t<LhsDerivatives>::Scalar,
692 typename remove_all_t<RhsDerivatives>::Scalar>> {};
693
694template <typename Derivatives>
695struct functor_is_commutative<
696 scalar_product_op<AutoDiffScalar<Derivatives>, typename remove_all_t<Derivatives>::Scalar>>
697 : functor_is_commutative<scalar_product_op<typename remove_all_t<Derivatives>::Scalar>> {};
698
699template <typename Derivatives>
700struct functor_is_commutative<
701 scalar_product_op<typename remove_all_t<Derivatives>::Scalar, AutoDiffScalar<Derivatives>>>
702 : functor_is_commutative<scalar_product_op<typename remove_all_t<Derivatives>::Scalar>> {};
703
704template <typename DerivativeType>
705struct is_identically_zero_impl<AutoDiffScalar<DerivativeType>> {
706 static inline bool run(const AutoDiffScalar<DerivativeType>& s) {
707 const DerivativeType& derivatives = s.derivatives();
708 for (int i = 0; i < derivatives.size(); ++i) {
709 if (!numext::is_exactly_zero(derivatives[i])) {
710 return false;
711 }
712 }
713 return numext::is_exactly_zero(s.value());
714 }
715};
716} // namespace internal
717} // namespace Eigen
718
719namespace std {
720
721template <typename T>
722class numeric_limits<Eigen::AutoDiffScalar<T>> : public numeric_limits<typename T::Scalar> {};
723
724template <typename T>
725class numeric_limits<Eigen::AutoDiffScalar<T&>> : public numeric_limits<typename T::Scalar> {};
726
727} // namespace std
728
729#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.