11#ifndef EIGEN_AUTODIFF_SCALAR_H
12#define EIGEN_AUTODIFF_SCALAR_H
15#include "./InternalHeaderCheck.h"
21template <
typename DerivativeType,
bool Enable>
22struct auto_diff_special_op;
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) {
32 return CoherentPadOp<DerivativeType, SizeAtCompileTime>(x, numext::maxi(x.size(), y.size()));
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& ) {
return x; }
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);
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);
61template <
typename DerivativeType>
64template <
typename NewDerType>
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> {
103 using Base = internal::auto_diff_special_op<
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;
112 using Base::operator+;
113 using Base::operator*;
120 AutoDiffScalar(
const Scalar& value,
int nbDer,
int derNumber) : m_value(value), m_derivatives(DerType::Zero(nbDer)) {
121 m_derivatives.coeffRef(derNumber) = Scalar(1);
127 if (m_derivatives.size() > 0) m_derivatives.setZero();
131 AutoDiffScalar(
const Scalar& value,
const DerType& der) : m_value(value), m_derivatives(der) {}
133#ifndef EIGEN_PARSED_BY_DOXYGEN
134 template <
typename OtherDerType,
136 std::is_same<Scalar, typename internal::traits<internal::remove_all_t<OtherDerType>>::Scalar>::value &&
137 std::is_convertible<OtherDerType, DerType>::value,
140 template <
typename OtherDerType>
143 : m_value(other.value()), m_derivatives(other.derivatives()) {
146#ifndef EIGEN_PARSED_BY_DOXYGEN
147 template <
typename OtherDerType>
148 EIGEN_DEPRECATED_WITH_REASON(
"Omit the implementation-only second argument.")
152 std::is_same<Scalar, typename internal::traits<internal::remove_all_t<OtherDerType>>::Scalar>::value &&
153 std::is_convertible<OtherDerType, DerType>::value,
158 friend std::ostream& operator<<(std::ostream& s,
const AutoDiffScalar& a) {
return s << a.value(); }
162 template <
typename OtherDerType>
164 m_value = other.value();
165 assignDerivatives(other.derivatives());
170 m_value = other.value();
171 m_derivatives = other.derivatives();
177 if (m_derivatives.size() > 0) m_derivatives.setZero();
184 inline const Scalar& value()
const {
return m_value; }
185 inline Scalar& value() {
return m_value; }
187 inline const DerType& derivatives()
const {
return m_derivatives; }
188 inline DerType& derivatives() {
return m_derivatives; }
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; }
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(); }
204 template <
typename OtherDerType>
206 return m_value < b.value();
208 template <
typename OtherDerType>
210 return m_value <= b.value();
212 template <
typename OtherDerType>
214 return m_value > b.value();
216 template <
typename OtherDerType>
218 return m_value >= b.value();
220 template <
typename OtherDerType>
222 return m_value == b.value();
224 template <
typename OtherDerType>
226 return m_value != b.value();
242 template <
typename OtherDerType>
244 return MakeAutoDiffScalar(
245 m_value + other.value(),
246 internal::MakeCoherentCwiseBinaryOp<internal::scalar_sum_op<Scalar>>(m_derivatives, other.derivatives()));
249 template <
typename OtherDerType>
251 (*this) = (*this) + other;
270 template <
typename OtherDerType>
272 return MakeAutoDiffScalar(m_value - other.value(),
273 internal::MakeCoherentCwiseBinaryOp<internal::scalar_difference_op<Scalar>>(
274 m_derivatives, other.derivatives()));
277 template <
typename OtherDerType>
279 *
this = *
this - other;
287 inline auto operator*(
const Scalar& other)
const {
288 return MakeAutoDiffScalar(m_value * other, m_derivatives * other);
291 friend inline auto operator*(
const Scalar& other,
const AutoDiffScalar& a) {
292 return MakeAutoDiffScalar(a.value() * other, a.derivatives() * other);
295 inline auto operator/(
const Scalar& other)
const {
296 return MakeAutoDiffScalar(m_value / other, (m_derivatives * (Scalar(1) / other)));
299 friend inline auto operator/(
const Scalar& other,
const AutoDiffScalar& a) {
302 return MakeAutoDiffScalar(other / a.value(), a.derivatives() * (-(other / a.value()) / a.value()));
305 template <
typename OtherDerType>
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);
317 scale = Scalar(1) / b;
320 scale_other = m_value;
321 scale = Scalar(1) / (b * b);
323 return MakeAutoDiffScalar(q, internal::MakeCoherentCwiseBinaryOp<internal::scalar_difference_op<Scalar>>(
324 m_derivatives * scale_der, other.derivatives() * scale_other) *
328 template <
typename OtherDerType>
330 return MakeAutoDiffScalar(m_value * other.value(),
331 internal::MakeCoherentCwiseBinaryOp<internal::scalar_sum_op<Scalar>>(
332 m_derivatives * other.value(), other.derivatives() * m_value));
336 *
this = *
this * other;
340 template <
typename OtherDerType>
342 *
this = *
this * other;
347 *
this = *
this / other;
351 template <
typename OtherDerType>
353 *
this = *
this / other;
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;
371 m_derivatives = other;
375 DerType m_derivatives;
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;
386 const AutoDiffScalar<DerivativeType>& derived()
const {
387 return *
static_cast<const AutoDiffScalar<DerivativeType>*
>(
this);
389 AutoDiffScalar<DerivativeType>& derived() {
return *
static_cast<AutoDiffScalar<DerivativeType>*
>(
this); }
391 inline AutoDiffScalar<DerType&> operator+(
const Real& other)
const {
392 return AutoDiffScalar<DerType&>(derived().value() + other, derived().derivatives());
395 friend inline AutoDiffScalar<DerType&> operator+(
const Real& a,
const AutoDiffScalar<DerivativeType>& b) {
396 return AutoDiffScalar<DerType&>(a + b.value(), b.derivatives());
399 inline AutoDiffScalar<DerivativeType>& operator+=(
const Real& other) {
400 derived().value() += other;
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);
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);
416 inline AutoDiffScalar<DerivativeType>& operator*=(
const Scalar& other) {
417 *
this = *
this * other;
422template <
typename DerivativeType>
423struct auto_diff_special_op<DerivativeType, false> {
424 void operator*()
const;
425 void operator-()
const;
426 void operator+()
const;
431template <
typename DerType,
typename BinOp>
433 using ReturnType = AutoDiffScalar<DerType>;
436template <
typename DerType,
typename BinOp>
438 using ReturnType = AutoDiffScalar<DerType>;
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)); \
450template <
typename DerType>
451struct CleanedUpDerType {
452 using type = AutoDiffScalar<typename Eigen::internal::remove_all_t<DerType>::PlainObject>;
455template <
typename DerType>
459template <
typename DerType>
463template <
typename DerType>
467template <
typename DerType>
469 return (numext::isnan)(x.value());
471template <
typename DerType>
473 return (numext::isinf)(x.value());
475template <
typename DerType>
477 return (numext::isfinite)(x.value());
479template <
typename DerType,
typename T>
481 using ADS =
typename CleanedUpDerType<DerType>::type;
482 return (x <= y ? ADS(x) : ADS(y));
484template <
typename DerType,
typename T>
486 using ADS =
typename CleanedUpDerType<DerType>::type;
487 return (x >= y ? ADS(x) : ADS(y));
489template <
typename DerType,
typename T>
491 using ADS =
typename CleanedUpDerType<DerType>::type;
492 return (x < y ? ADS(x) : ADS(y));
494template <
typename DerType,
typename T>
496 using ADS =
typename CleanedUpDerType<DerType>::type;
497 return (x > y ? ADS(x) : ADS(y));
499template <
typename DerType>
503 return (x.value() <= y.value() ? x : y);
505template <
typename DerType>
508 return (x.value() >= y.value() ? x : y);
511EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(abs,
using std::abs;
512 return Eigen::MakeAutoDiffScalar(abs(x.value()),
513 x.derivatives() * (x.value() < 0 ? -1 : 1));)
515EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(abs2,
using numext::abs2;
516 return Eigen::MakeAutoDiffScalar(abs2(x.value()),
517 x.derivatives() * (Scalar(2) * x.value()));)
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));)
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())));)
526EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(sin,
using std::sin;
using std::cos;
527 return Eigen::MakeAutoDiffScalar(sin(x.value()), x.derivatives() * cos(x.value()));)
529EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(exp,
using std::exp; Scalar expx = exp(x.value());
530 return Eigen::MakeAutoDiffScalar(expx, x.derivatives() * expx);)
532EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(log,
using std::log;
533 return Eigen::MakeAutoDiffScalar(log(x.value()),
534 x.derivatives() * (Scalar(1) / x.value()));)
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;
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);
547template <
typename DerTypeA,
typename DerTypeB>
551 using Scalar =
typename internal::traits<internal::remove_all_t<DerTypeA>>::Scalar;
554 ret.value() = atan2(a.value(), b.value());
556 Scalar squared_hypot = a.value() * a.value() + b.value() * b.value();
559 ret.derivatives() = internal::MakeCoherentCwiseBinaryOp<internal::scalar_difference_op<Scalar>>(
560 a.derivatives() * b.value(), b.derivatives() * a.value()) /
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()))));)
569template <typename DerType>
571 using Scalar =
typename AutoDiffScalar<DerType>::Scalar;
573 const Scalar& value = x.value();
574 Scalar scale(1), denominator;
575 if (numext::abs(value) > RealScalar(1)) {
578 denominator = value + Scalar(1) / value;
580 denominator = Scalar(1) + value * value;
582 return MakeAutoDiffScalar(numext::atan(value), (x.derivatives() / scale) / denominator);
585EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(asin,
using std::sqrt;
using std::asin;
return Eigen::MakeAutoDiffScalar(
587 x.derivatives() * (Scalar(1) / sqrt(1 - numext::abs2(x.value()))));)
589EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(acos,
using std::sqrt;
using std::acos;
return Eigen::MakeAutoDiffScalar(
591 x.derivatives() * (Scalar(-1) / sqrt(1 - numext::abs2(x.value()))));)
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()))));)
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()));)
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()));)
605EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(log1p,
return MakeAutoDiffScalar(numext::log1p(x.value()),
606 x.derivatives() / (Scalar(1) + x.value()));)
608EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(expm1, return MakeAutoDiffScalar(numext::expm1(x.value()),
609 x.derivatives() * numext::exp(x.value()));)
611EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(log2,
612 return MakeAutoDiffScalar(numext::log2(x.value()),
613 (x.derivatives() / x.value()) / numext::log(Scalar(2)));)
615template <typename DerType>
617 using Scalar =
typename AutoDiffScalar<DerType>::Scalar;
619 const Scalar logarithm = numext::log(Scalar(10));
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);
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;
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);
637EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(cbrt,
const Scalar root = numext::cbrt(x.value());
638 return MakeAutoDiffScalar(root, x.derivatives() / (Scalar(3) * root * root));)
640EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(asinh,
641 return MakeAutoDiffScalar(numext::asinh(x.value()),
642 x.derivatives() / numext::hypot(Scalar(1), x.value()));)
644template <typename DerType>
646 using Scalar =
typename AutoDiffScalar<DerType>::Scalar;
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);
653template <
typename DerType>
655 using Scalar =
typename AutoDiffScalar<DerType>::Scalar;
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;
663 return MakeAutoDiffScalar(numext::atanh(x.value()), (x.derivatives() / larger) / smaller);
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);
673#undef EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY
675template <
typename 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 };
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>> {};
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>> {};
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>> {};
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])) {
713 return numext::is_exactly_zero(s.value());
722class numeric_limits<Eigen::AutoDiffScalar<T>> :
public numeric_limits<typename T::Scalar> {};
725class numeric_limits<Eigen::AutoDiffScalar<T&>> :
public numeric_limits<typename T::Scalar> {};
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.