AutoDiffScalar.h
Go to the documentation of this file.
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 
10 #ifndef EIGEN_AUTODIFF_SCALAR_H
11 #define EIGEN_AUTODIFF_SCALAR_H
12 
13 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
17 namespace internal {
18 
19 template<typename A, typename B>
20 struct make_coherent_impl {
21  static void run(A&, B&) {}
22 };
23 
24 // resize a to match b is a.size()==0, and conversely.
25 template<typename A, typename B>
26 void make_coherent(const A& a, const B&b)
27 {
28  make_coherent_impl<A,B>::run(a.const_cast_derived(), b.const_cast_derived());
29 }
30 
31 template<typename DerivativeType, bool Enable> struct auto_diff_special_op;
32 
33 } // end namespace internal
34 
35 template<typename DerivativeType> class AutoDiffScalar;
36 
37 template<typename NewDerType>
38 inline AutoDiffScalar<NewDerType> MakeAutoDiffScalar(const typename NewDerType::Scalar& value, const NewDerType &der) {
39  return AutoDiffScalar<NewDerType>(value,der);
40 }
41 
68 template<typename DerivativeType>
70  : public internal::auto_diff_special_op
71  <DerivativeType, !internal::is_same<typename internal::traits<internal::remove_all_t<DerivativeType>>::Scalar,
72  typename NumTraits<typename internal::traits<internal::remove_all_t<DerivativeType>>::Scalar>::Real>::value>
73 {
74  public:
75  typedef internal::auto_diff_special_op
76  <DerivativeType, !internal::is_same<typename internal::traits<internal::remove_all_t<DerivativeType>>::Scalar,
79  typedef typename internal::traits<DerType>::Scalar Scalar;
80  typedef typename NumTraits<Scalar>::Real Real;
81 
82  using Base::operator+;
83  using Base::operator*;
84 
87 
90  AutoDiffScalar(const Scalar& value, int nbDer, int derNumber)
91  : m_value(value), m_derivatives(DerType::Zero(nbDer))
92  {
93  m_derivatives.coeffRef(derNumber) = Scalar(1);
94  }
95 
98  /*explicit*/ AutoDiffScalar(const Real& value)
99  : m_value(value)
100  {
101  if(m_derivatives.size()>0)
102  m_derivatives.setZero();
103  }
104 
106  AutoDiffScalar(const Scalar& value, const DerType& der)
107  : m_value(value), m_derivatives(der)
108  {}
109 
110  template<typename OtherDerType>
112 #ifndef EIGEN_PARSED_BY_DOXYGEN
113  , std::enable_if_t<
114  internal::is_same<Scalar, typename internal::traits<internal::remove_all_t<OtherDerType>>::Scalar>::value
115  && internal::is_convertible<OtherDerType,DerType>::value , void*> = 0
116 #endif
117  )
118  : m_value(other.value()), m_derivatives(other.derivatives())
119  {}
120 
121  friend std::ostream & operator << (std::ostream & s, const AutoDiffScalar& a)
122  {
123  return s << a.value();
124  }
125 
127  : m_value(other.value()), m_derivatives(other.derivatives())
128  {}
129 
130  template<typename OtherDerType>
132  {
133  m_value = other.value();
134  m_derivatives = other.derivatives();
135  return *this;
136  }
137 
139  {
140  m_value = other.value();
141  m_derivatives = other.derivatives();
142  return *this;
143  }
144 
145  inline AutoDiffScalar& operator=(const Scalar& other)
146  {
147  m_value = other;
148  if(m_derivatives.size()>0)
149  m_derivatives.setZero();
150  return *this;
151  }
152 
153 // inline operator const Scalar& () const { return m_value; }
154 // inline operator Scalar& () { return m_value; }
155 
156  inline const Scalar& value() const { return m_value; }
157  inline Scalar& value() { return m_value; }
158 
159  inline const DerType& derivatives() const { return m_derivatives; }
160  inline DerType& derivatives() { return m_derivatives; }
161 
162  inline bool operator< (const Scalar& other) const { return m_value < other; }
163  inline bool operator<=(const Scalar& other) const { return m_value <= other; }
164  inline bool operator> (const Scalar& other) const { return m_value > other; }
165  inline bool operator>=(const Scalar& other) const { return m_value >= other; }
166  inline bool operator==(const Scalar& other) const { return m_value == other; }
167  inline bool operator!=(const Scalar& other) const { return m_value != other; }
168 
169  friend inline bool operator< (const Scalar& a, const AutoDiffScalar& b) { return a < b.value(); }
170  friend inline bool operator<=(const Scalar& a, const AutoDiffScalar& b) { return a <= b.value(); }
171  friend inline bool operator> (const Scalar& a, const AutoDiffScalar& b) { return a > b.value(); }
172  friend inline bool operator>=(const Scalar& a, const AutoDiffScalar& b) { return a >= b.value(); }
173  friend inline bool operator==(const Scalar& a, const AutoDiffScalar& b) { return a == b.value(); }
174  friend inline bool operator!=(const Scalar& a, const AutoDiffScalar& b) { return a != b.value(); }
175 
176  template<typename OtherDerType> inline bool operator< (const AutoDiffScalar<OtherDerType>& b) const { return m_value < b.value(); }
177  template<typename OtherDerType> inline bool operator<=(const AutoDiffScalar<OtherDerType>& b) const { return m_value <= b.value(); }
178  template<typename OtherDerType> inline bool operator> (const AutoDiffScalar<OtherDerType>& b) const { return m_value > b.value(); }
179  template<typename OtherDerType> inline bool operator>=(const AutoDiffScalar<OtherDerType>& b) const { return m_value >= b.value(); }
180  template<typename OtherDerType> inline bool operator==(const AutoDiffScalar<OtherDerType>& b) const { return m_value == b.value(); }
181  template<typename OtherDerType> inline bool operator!=(const AutoDiffScalar<OtherDerType>& b) const { return m_value != b.value(); }
182 
183  inline AutoDiffScalar<DerType&> operator+(const Scalar& other) const
184  {
186  }
187 
188  friend inline AutoDiffScalar<DerType&> operator+(const Scalar& a, const AutoDiffScalar& b)
189  {
190  return AutoDiffScalar<DerType&>(a + b.value(), b.derivatives());
191  }
192 
193 // inline const AutoDiffScalar<DerType&> operator+(const Real& other) const
194 // {
195 // return AutoDiffScalar<DerType&>(m_value + other, m_derivatives);
196 // }
197 
198 // friend inline const AutoDiffScalar<DerType&> operator+(const Real& a, const AutoDiffScalar& b)
199 // {
200 // return AutoDiffScalar<DerType&>(a + b.value(), b.derivatives());
201 // }
202 
203  inline AutoDiffScalar& operator+=(const Scalar& other)
204  {
205  value() += other;
206  return *this;
207  }
208 
209  template<typename OtherDerType>
212  {
215  m_value + other.value(),
216  m_derivatives + other.derivatives());
217  }
218 
219  template<typename OtherDerType>
220  inline AutoDiffScalar&
222  {
223  (*this) = (*this) + other;
224  return *this;
225  }
226 
228  {
230  }
231 
233  operator-(const Scalar& a, const AutoDiffScalar& b)
234  {
236  (a - b.value(), -b.derivatives());
237  }
238 
239  inline AutoDiffScalar& operator-=(const Scalar& other)
240  {
241  value() -= other;
242  return *this;
243  }
244 
245  template<typename OtherDerType>
248  {
251  m_value - other.value(),
252  m_derivatives - other.derivatives());
253  }
254 
255  template<typename OtherDerType>
256  inline AutoDiffScalar&
258  {
259  *this = *this - other;
260  return *this;
261  }
262 
264  operator-() const
265  {
267  -m_value,
268  -m_derivatives);
269  }
270 
272  operator*(const Scalar& other) const
273  {
274  return MakeAutoDiffScalar(m_value * other, m_derivatives * other);
275  }
276 
278  operator*(const Scalar& other, const AutoDiffScalar& a)
279  {
280  return MakeAutoDiffScalar(a.value() * other, a.derivatives() * other);
281  }
282 
283 // inline const AutoDiffScalar<typename CwiseUnaryOp<internal::scalar_multiple_op<Real>, DerType>::Type >
284 // operator*(const Real& other) const
285 // {
286 // return AutoDiffScalar<typename CwiseUnaryOp<internal::scalar_multiple_op<Real>, DerType>::Type >(
287 // m_value * other,
288 // (m_derivatives * other));
289 // }
290 //
291 // friend inline const AutoDiffScalar<typename CwiseUnaryOp<internal::scalar_multiple_op<Real>, DerType>::Type >
292 // operator*(const Real& other, const AutoDiffScalar& a)
293 // {
294 // return AutoDiffScalar<typename CwiseUnaryOp<internal::scalar_multiple_op<Real>, DerType>::Type >(
295 // a.value() * other,
296 // a.derivatives() * other);
297 // }
298 
300  operator/(const Scalar& other) const
301  {
302  return MakeAutoDiffScalar(m_value / other, (m_derivatives * (Scalar(1)/other)));
303  }
304 
306  operator/(const Scalar& other, const AutoDiffScalar& a)
307  {
308  return MakeAutoDiffScalar(other / a.value(), a.derivatives() * (Scalar(-other) / (a.value()*a.value())));
309  }
310 
311 // inline const AutoDiffScalar<typename CwiseUnaryOp<internal::scalar_multiple_op<Real>, DerType>::Type >
312 // operator/(const Real& other) const
313 // {
314 // return AutoDiffScalar<typename CwiseUnaryOp<internal::scalar_multiple_op<Real>, DerType>::Type >(
315 // m_value / other,
316 // (m_derivatives * (Real(1)/other)));
317 // }
318 //
319 // friend inline const AutoDiffScalar<typename CwiseUnaryOp<internal::scalar_multiple_op<Real>, DerType>::Type >
320 // operator/(const Real& other, const AutoDiffScalar& a)
321 // {
322 // return AutoDiffScalar<typename CwiseUnaryOp<internal::scalar_multiple_op<Real>, DerType>::Type >(
323 // other / a.value(),
324 // a.derivatives() * (-Real(1)/other));
325 // }
326 
327  template<typename OtherDerType>
329  CwiseBinaryOp<internal::scalar_difference_op<Scalar> EIGEN_COMMA
330  const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DerType,Scalar,product) EIGEN_COMMA
333  {
335  return MakeAutoDiffScalar(
336  m_value / other.value(),
337  ((m_derivatives * other.value()) - (other.derivatives() * m_value))
338  * (Scalar(1)/(other.value()*other.value())));
339  }
340 
341  template<typename OtherDerType>
346  {
348  return MakeAutoDiffScalar(
349  m_value * other.value(),
350  (m_derivatives * other.value()) + (other.derivatives() * m_value));
351  }
352 
353  inline AutoDiffScalar& operator*=(const Scalar& other)
354  {
355  *this = *this * other;
356  return *this;
357  }
358 
359  template<typename OtherDerType>
361  {
362  *this = *this * other;
363  return *this;
364  }
365 
366  inline AutoDiffScalar& operator/=(const Scalar& other)
367  {
368  *this = *this / other;
369  return *this;
370  }
371 
372  template<typename OtherDerType>
374  {
375  *this = *this / other;
376  return *this;
377  }
378 
379  protected:
382 
383 };
384 
385 namespace internal {
386 
387 template<typename DerivativeType>
388 struct auto_diff_special_op<DerivativeType, true>
389 // : auto_diff_scalar_op<DerivativeType, typename NumTraits<Scalar>::Real,
390 // is_same<Scalar,typename NumTraits<Scalar>::Real>::value>
391 {
392  typedef remove_all_t<DerivativeType> DerType;
393  typedef typename traits<DerType>::Scalar Scalar;
394  typedef typename NumTraits<Scalar>::Real Real;
395 
396 // typedef auto_diff_scalar_op<DerivativeType, typename NumTraits<Scalar>::Real,
397 // is_same<Scalar,typename NumTraits<Scalar>::Real>::value> Base;
398 
399 // using Base::operator+;
400 // using Base::operator+=;
401 // using Base::operator-;
402 // using Base::operator-=;
403 // using Base::operator*;
404 // using Base::operator*=;
405 
406  const AutoDiffScalar<DerivativeType>& derived() const { return *static_cast<const AutoDiffScalar<DerivativeType>*>(this); }
407  AutoDiffScalar<DerivativeType>& derived() { return *static_cast<AutoDiffScalar<DerivativeType>*>(this); }
408 
409 
410  inline AutoDiffScalar<DerType&> operator+(const Real& other) const
411  {
412  return AutoDiffScalar<DerType&>(derived().value() + other, derived().derivatives());
413  }
414 
415  friend inline AutoDiffScalar<DerType&> operator+(const Real& a, const AutoDiffScalar<DerivativeType>& b)
416  {
417  return AutoDiffScalar<DerType&>(a + b.value(), b.derivatives());
418  }
419 
420  inline AutoDiffScalar<DerivativeType>& operator+=(const Real& other)
421  {
422  derived().value() += other;
423  return derived();
424  }
425 
426 
428  operator*(const Real& other) const
429  {
431  derived().value() * other,
432  derived().derivatives() * other);
433  }
434 
436  operator*(const Real& other, const AutoDiffScalar<DerivativeType>& a)
437  {
439  a.value() * other,
440  a.derivatives() * other);
441  }
442 
443  inline AutoDiffScalar<DerivativeType>& operator*=(const Scalar& other)
444  {
445  *this = *this * other;
446  return derived();
447  }
448 };
449 
450 template<typename DerivativeType>
451 struct auto_diff_special_op<DerivativeType, false>
452 {
453  void operator*() const;
454  void operator-() const;
455  void operator+() const;
456 };
457 
458 template<typename BinOp, typename A, typename B, typename RefType>
460 {
461  make_coherent(xpr.const_cast_derived().lhs(), ref);
462  make_coherent(xpr.const_cast_derived().rhs(), ref);
463 }
464 
465 template<typename UnaryOp, typename A, typename RefType>
466 void make_coherent_expression(const CwiseUnaryOp<UnaryOp,A> &xpr, const RefType &ref)
467 {
468  make_coherent(xpr.nestedExpression().const_cast_derived(), ref);
469 }
470 
471 // needed for compilation only
472 template<typename UnaryOp, typename A, typename RefType>
474 {}
475 
476 template<typename A_Scalar, int A_Rows, int A_Cols, int A_Options, int A_MaxRows, int A_MaxCols, typename B>
477 struct make_coherent_impl<Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols>, B> {
479  static void run(A& a, B& b) {
480  if((A_Rows==Dynamic || A_Cols==Dynamic) && (a.size()==0))
481  {
482  a.resize(b.size());
483  a.setZero();
484  }
485  else if (B::SizeAtCompileTime==Dynamic && a.size()!=0 && b.size()==0)
486  {
488  }
489  }
490 };
491 
492 template<typename A, typename B_Scalar, int B_Rows, int B_Cols, int B_Options, int B_MaxRows, int B_MaxCols>
493 struct make_coherent_impl<A, Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols> > {
494  typedef Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols> B;
495  static void run(A& a, B& b) {
496  if((B_Rows==Dynamic || B_Cols==Dynamic) && (b.size()==0))
497  {
498  b.resize(a.size());
499  b.setZero();
500  }
501  else if (A::SizeAtCompileTime==Dynamic && b.size()!=0 && a.size()==0)
502  {
504  }
505  }
506 };
507 
508 template<typename A_Scalar, int A_Rows, int A_Cols, int A_Options, int A_MaxRows, int A_MaxCols,
509  typename B_Scalar, int B_Rows, int B_Cols, int B_Options, int B_MaxRows, int B_MaxCols>
510 struct make_coherent_impl<Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols>,
511  Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols> > {
512  typedef Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols> A;
513  typedef Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols> B;
514  static void run(A& a, B& b) {
515  if((A_Rows==Dynamic || A_Cols==Dynamic) && (a.size()==0))
516  {
517  a.resize(b.size());
518  a.setZero();
519  }
520  else if((B_Rows==Dynamic || B_Cols==Dynamic) && (b.size()==0))
521  {
522  b.resize(a.size());
523  b.setZero();
524  }
525  }
526 };
527 
528 } // end namespace internal
529 
530 template<typename DerType, typename BinOp>
531 struct ScalarBinaryOpTraits<AutoDiffScalar<DerType>,typename DerType::Scalar,BinOp>
532 {
534 };
535 
536 template<typename DerType, typename BinOp>
537 struct ScalarBinaryOpTraits<typename DerType::Scalar,AutoDiffScalar<DerType>, BinOp>
538 {
540 };
541 
542 
543 // The following is an attempt to let Eigen's known about expression template, but that's more tricky!
544 
545 // template<typename DerType, typename BinOp>
546 // struct ScalarBinaryOpTraits<AutoDiffScalar<DerType>,AutoDiffScalar<DerType>, BinOp>
547 // {
548 // enum { Defined = 1 };
549 // typedef AutoDiffScalar<typename DerType::PlainObject> ReturnType;
550 // };
551 //
552 // template<typename DerType1,typename DerType2, typename BinOp>
553 // struct ScalarBinaryOpTraits<AutoDiffScalar<DerType1>,AutoDiffScalar<DerType2>, BinOp>
554 // {
555 // enum { Defined = 1 };//internal::is_same<typename DerType1::Scalar,typename DerType2::Scalar>::value };
556 // typedef AutoDiffScalar<typename DerType1::PlainObject> ReturnType;
557 // };
558 
559 #define EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(FUNC,CODE) \
560  template<typename DerType> \
561  inline Eigen::AutoDiffScalar< \
562  EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(Eigen::internal::remove_all_t<DerType>, typename Eigen::internal::traits<Eigen::internal::remove_all_t<DerType>>::Scalar, product) > \
563  FUNC(const Eigen::AutoDiffScalar<DerType>& x) { \
564  using namespace Eigen; \
565  typedef typename Eigen::internal::traits<Eigen::internal::remove_all_t<DerType>>::Scalar Scalar; \
566  EIGEN_UNUSED_VARIABLE(sizeof(Scalar)); \
567  CODE; \
568  }
569 
570 template<typename DerType>
573 };
574 
575 template<typename DerType>
576 inline const AutoDiffScalar<DerType>& conj(const AutoDiffScalar<DerType>& x) { return x; }
577 template<typename DerType>
578 inline const AutoDiffScalar<DerType>& real(const AutoDiffScalar<DerType>& x) { return x; }
579 template<typename DerType>
580 inline typename DerType::Scalar imag(const AutoDiffScalar<DerType>&) { return 0.; }
581 template<typename DerType, typename T>
582 inline typename CleanedUpDerType<DerType>::type (min)(const AutoDiffScalar<DerType>& x, const T& y) {
583  typedef typename CleanedUpDerType<DerType>::type ADS;
584  return (x <= y ? ADS(x) : ADS(y));
585 }
586 template<typename DerType, typename T>
587 inline typename CleanedUpDerType<DerType>::type (max)(const AutoDiffScalar<DerType>& x, const T& y) {
588  typedef typename CleanedUpDerType<DerType>::type ADS;
589  return (x >= y ? ADS(x) : ADS(y));
590 }
591 template<typename DerType, typename T>
592 inline typename CleanedUpDerType<DerType>::type (min)(const T& x, const AutoDiffScalar<DerType>& y) {
593  typedef typename CleanedUpDerType<DerType>::type ADS;
594  return (x < y ? ADS(x) : ADS(y));
595 }
596 template<typename DerType, typename T>
597 inline typename CleanedUpDerType<DerType>::type (max)(const T& x, const AutoDiffScalar<DerType>& y) {
598  typedef typename CleanedUpDerType<DerType>::type ADS;
599  return (x > y ? ADS(x) : ADS(y));
600 }
601 template<typename DerType>
603  return (x.value() < y.value() ? x : y);
604 }
605 template<typename DerType>
607  return (x.value() >= y.value() ? x : y);
608 }
609 
610 
612  using std::abs;
613  return Eigen::MakeAutoDiffScalar(abs(x.value()), x.derivatives() * (x.value()<0 ? -1 : 1) );)
614 
616  using numext::abs2;
617  return Eigen::MakeAutoDiffScalar(abs2(x.value()), x.derivatives() * (Scalar(2)*x.value()));)
618 
620  using std::sqrt;
621  Scalar sqrtx = sqrt(x.value());
622  return Eigen::MakeAutoDiffScalar(sqrtx,x.derivatives() * (Scalar(0.5) / sqrtx));)
623 
625  using std::cos;
626  using std::sin;
627  return Eigen::MakeAutoDiffScalar(cos(x.value()), x.derivatives() * (-sin(x.value())));)
628 
630  using std::sin;
631  using std::cos;
632  return Eigen::MakeAutoDiffScalar(sin(x.value()),x.derivatives() * cos(x.value()));)
633 
635  using std::exp;
636  Scalar expx = exp(x.value());
637  return Eigen::MakeAutoDiffScalar(expx,x.derivatives() * expx);)
638 
640  using std::log;
641  return Eigen::MakeAutoDiffScalar(log(x.value()),x.derivatives() * (Scalar(1)/x.value()));)
642 
643 template<typename DerType>
644 inline Eigen::AutoDiffScalar<
646 pow(const Eigen::AutoDiffScalar<DerType> &x, const typename internal::traits<internal::remove_all_t<DerType>>::Scalar &y)
647 {
648  using namespace Eigen;
649  using std::pow;
650  return Eigen::MakeAutoDiffScalar(pow(x.value(),y), x.derivatives() * (y * pow(x.value(),y-1)));
651 }
652 
653 
654 template<typename DerTypeA,typename DerTypeB>
657 {
658  using std::atan2;
659  typedef typename internal::traits<internal::remove_all_t<DerTypeA>>::Scalar Scalar;
660  typedef AutoDiffScalar<Matrix<Scalar,Dynamic,1> > PlainADS;
661  PlainADS ret;
662  ret.value() = atan2(a.value(), b.value());
663 
664  Scalar squared_hypot = a.value() * a.value() + b.value() * b.value();
665 
666  // if (squared_hypot==0) the derivation is undefined and the following results in a NaN:
667  ret.derivatives() = (a.derivatives() * b.value() - a.value() * b.derivatives()) / squared_hypot;
668 
669  return ret;
670 }
671 
673  using std::tan;
674  using std::cos;
675  return Eigen::MakeAutoDiffScalar(tan(x.value()),x.derivatives() * (Scalar(1)/numext::abs2(cos(x.value()))));)
676 
678  using std::sqrt;
679  using std::asin;
680  return Eigen::MakeAutoDiffScalar(asin(x.value()),x.derivatives() * (Scalar(1)/sqrt(1-numext::abs2(x.value()))));)
681 
683  using std::sqrt;
684  using std::acos;
685  return Eigen::MakeAutoDiffScalar(acos(x.value()),x.derivatives() * (Scalar(-1)/sqrt(1-numext::abs2(x.value()))));)
686 
688  using std::cosh;
689  using std::tanh;
690  return Eigen::MakeAutoDiffScalar(tanh(x.value()),x.derivatives() * (Scalar(1)/numext::abs2(cosh(x.value()))));)
691 
693  using std::sinh;
694  using std::cosh;
695  return Eigen::MakeAutoDiffScalar(sinh(x.value()),x.derivatives() * cosh(x.value()));)
696 
698  using std::sinh;
699  using std::cosh;
700  return Eigen::MakeAutoDiffScalar(cosh(x.value()),x.derivatives() * sinh(x.value()));)
701 
703 
704 template<typename DerType> struct NumTraits<AutoDiffScalar<DerType> >
706 {
708  typedef AutoDiffScalar<Matrix<typename NumTraits<typename DerTypeCleaned::Scalar>::Real,DerTypeCleaned::RowsAtCompileTime,DerTypeCleaned::ColsAtCompileTime,
709  0, DerTypeCleaned::MaxRowsAtCompileTime, DerTypeCleaned::MaxColsAtCompileTime> > Real;
713  enum{
714  RequireInitialization = 1
715  };
716 };
717 
718 namespace internal {
719 template<typename DerivativeType>
720 struct is_identically_zero_impl<AutoDiffScalar<DerivativeType>> {
721  static inline bool run(const AutoDiffScalar<DerivativeType>& s)
722  {
723  const DerivativeType& derivatives = s.derivatives();
724  for(int i=0; i<derivatives.size(); ++i)
725  {
726  if(!numext::is_exactly_zero(derivatives[i]))
727  {
728  return false;
729  }
730  }
731  return numext::is_exactly_zero(s.value());
732  }
733 };
734 }
735 }
736 
737 namespace std {
738 
739 template <typename T>
741  : public numeric_limits<typename T::Scalar> {};
742 
743 template <typename T>
745  : public numeric_limits<typename T::Scalar> {};
746 
747 } // namespace std
748 
749 #endif // EIGEN_AUTODIFF_SCALAR_H
ArrayXXi a
#define EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(FUNC, CODE)
SparseMatrix< double > A(n, n)
int i
Matrix3f y
MatrixXf B
#define EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(EXPR, SCALAR, OPNAME)
A scalar type replacement with automatic differentiation capability.
AutoDiffScalar & operator-=(const Scalar &other)
internal::remove_all_t< DerivativeType > DerType
bool operator>(const Scalar &other) const
AutoDiffScalar< CwiseBinaryOp< internal::scalar_sum_op< Scalar >, const DerType, const internal::remove_all_t< OtherDerType > > > operator+(const AutoDiffScalar< OtherDerType > &other) const
friend AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DerType, Scalar, product) > operator*(const Scalar &other, const AutoDiffScalar &a)
friend AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DerType, Scalar, product) > operator/(const Scalar &other, const AutoDiffScalar &a)
internal::traits< DerType >::Scalar Scalar
AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(CwiseBinaryOp< internal::scalar_difference_op< Scalar > EIGEN_COMMA const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DerType, Scalar, product) EIGEN_COMMA const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(internal::remove_all_t< OtherDerType >, Scalar, product) >, Scalar, product) > operator/(const AutoDiffScalar< OtherDerType > &other) const
AutoDiffScalar< DerType & > operator-(const Scalar &b) const
const Scalar & value() const
AutoDiffScalar & operator=(const AutoDiffScalar &other)
AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DerType, Scalar, product) > operator*(const Scalar &other) const
NumTraits< Scalar >::Real Real
friend AutoDiffScalar< CwiseUnaryOp< internal::scalar_opposite_op< Scalar >, const DerType > > operator-(const Scalar &a, const AutoDiffScalar &b)
AutoDiffScalar & operator*=(const AutoDiffScalar< OtherDerType > &other)
bool operator==(const AutoDiffScalar< OtherDerType > &b) const
internal::auto_diff_special_op< DerivativeType, !internal::is_same< typename internal::traits< internal::remove_all_t< DerivativeType > >::Scalar, typename NumTraits< typename internal::traits< internal::remove_all_t< DerivativeType > >::Scalar >::Real >::value > Base
AutoDiffScalar< CwiseBinaryOp< internal::scalar_sum_op< Scalar >, const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DerType, Scalar, product), const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(internal::remove_all_t< OtherDerType >, Scalar, product) > > operator*(const AutoDiffScalar< OtherDerType > &other) const
friend AutoDiffScalar< DerType & > operator+(const Scalar &a, const AutoDiffScalar &b)
bool operator!=(const AutoDiffScalar< OtherDerType > &b) const
bool operator<(const Scalar &other) const
AutoDiffScalar & operator/=(const AutoDiffScalar< OtherDerType > &other)
AutoDiffScalar(const AutoDiffScalar &other)
AutoDiffScalar & operator*=(const Scalar &other)
AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DerType, Scalar, product) > operator/(const Scalar &other) const
AutoDiffScalar & operator+=(const AutoDiffScalar< OtherDerType > &other)
bool operator<=(const Scalar &other) const
friend std::ostream & operator<<(std::ostream &s, const AutoDiffScalar &a)
AutoDiffScalar< CwiseBinaryOp< internal::scalar_difference_op< Scalar >, const DerType, const internal::remove_all_t< OtherDerType > > > operator-(const AutoDiffScalar< OtherDerType > &other) const
bool operator<=(const AutoDiffScalar< OtherDerType > &b) const
AutoDiffScalar(const AutoDiffScalar< OtherDerType > &other)
bool operator>=(const Scalar &other) const
AutoDiffScalar(const Scalar &value, int nbDer, int derNumber)
friend bool operator==(const Scalar &a, const AutoDiffScalar &b)
AutoDiffScalar & operator=(const Scalar &other)
bool operator==(const Scalar &other) const
AutoDiffScalar< DerType & > operator+(const Scalar &other) const
friend bool operator!=(const Scalar &a, const AutoDiffScalar &b)
bool operator!=(const Scalar &other) const
AutoDiffScalar & operator-=(const AutoDiffScalar< OtherDerType > &other)
friend bool operator<=(const Scalar &a, const AutoDiffScalar &b)
AutoDiffScalar(const Real &value)
const DerType & derivatives() const
AutoDiffScalar(const Scalar &value, const DerType &der)
AutoDiffScalar & operator/=(const Scalar &other)
AutoDiffScalar & operator=(const AutoDiffScalar< OtherDerType > &other)
AutoDiffScalar & operator+=(const Scalar &other)
friend bool operator>=(const Scalar &a, const AutoDiffScalar &b)
AutoDiffScalar< CwiseUnaryOp< internal::scalar_opposite_op< Scalar >, const DerType > > operator-() const
bool operator>=(const AutoDiffScalar< OtherDerType > &b) const
const LhsNested_ & lhs() const
const RhsNested_ & rhs() const
internal::remove_all_t< XprTypeNested > & nestedExpression()
CoeffReturnType value() const
Type
bfloat16 & operator*=(bfloat16 &a, const bfloat16 &b)
bfloat16 & operator+=(bfloat16 &a, const bfloat16 &b)
EIGEN_ALWAYS_INLINE TensorUInt128< uint64_t, uint64_t > operator-(const TensorUInt128< HL, LL > &lhs, const TensorUInt128< HR, LR > &rhs)
void make_coherent_expression(CwiseBinaryOp< BinOp, A, B > xpr, const RefType &ref)
EIGEN_ALWAYS_INLINE TensorUInt128< uint64_t, uint64_t > operator+(const TensorUInt128< HL, LL > &lhs, const TensorUInt128< HR, LR > &rhs)
static TensorUInt128< uint64_t, uint64_t > operator*(const TensorUInt128< HL, LL > &lhs, const TensorUInt128< HR, LR > &rhs)
typename remove_all< T >::type remove_all_t
void make_coherent(const A &a, const B &b)
bool abs2(bool x)
bool is_exactly_zero(const X &x)
: TensorContractionSycl.h, provides various tensor contraction kernel for SYCL backend
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_tanh_op< typename Derived::Scalar >, const Derived > tanh(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_imag_op< typename Derived::Scalar >, const Derived > imag(const Eigen::ArrayBase< Derived > &x)
Eigen::AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(Eigen::internal::remove_all_t< DerType >, typename Eigen::internal::traits< Eigen::internal::remove_all_t< DerType >>::Scalar, product) > sinh(const Eigen::AutoDiffScalar< DerType > &x)
Eigen::AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(Eigen::internal::remove_all_t< DerType >, typename Eigen::internal::traits< Eigen::internal::remove_all_t< DerType >>::Scalar, product) > log(const Eigen::AutoDiffScalar< DerType > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_cosh_op< typename Derived::Scalar >, const Derived > cosh(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_tan_op< typename Derived::Scalar >, const Derived > tan(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_acos_op< typename Derived::Scalar >, const Derived > acos(const Eigen::ArrayBase< Derived > &x)
Eigen::AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(Eigen::internal::remove_all_t< DerType >, typename Eigen::internal::traits< Eigen::internal::remove_all_t< DerType >>::Scalar, product) > cos(const Eigen::AutoDiffScalar< DerType > &x)
Eigen::AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(Eigen::internal::remove_all_t< DerType >, typename Eigen::internal::traits< Eigen::internal::remove_all_t< DerType >>::Scalar, product) > tan(const Eigen::AutoDiffScalar< DerType > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_abs2_op< typename Derived::Scalar >, const Derived > abs2(const Eigen::ArrayBase< Derived > &x)
Eigen::AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(Eigen::internal::remove_all_t< DerType >, typename Eigen::internal::traits< Eigen::internal::remove_all_t< DerType >>::Scalar, product) > sqrt(const Eigen::AutoDiffScalar< DerType > &x)
CleanedUpDerType< DerType >::type() min(const AutoDiffScalar< DerType > &x, const T &y)
CleanedUpDerType< DerType >::type() max(const AutoDiffScalar< DerType > &x, const T &y)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_asin_op< typename Derived::Scalar >, const Derived > asin(const Eigen::ArrayBase< Derived > &x)
Eigen::AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(Eigen::internal::remove_all_t< DerType >, typename Eigen::internal::traits< Eigen::internal::remove_all_t< DerType >>::Scalar, product) > acos(const Eigen::AutoDiffScalar< DerType > &x)
Eigen::AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(Eigen::internal::remove_all_t< DerType >, typename Eigen::internal::traits< Eigen::internal::remove_all_t< DerType >>::Scalar, product) > exp(const Eigen::AutoDiffScalar< DerType > &x)
Eigen::AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(internal::remove_all_t< DerType >, typename internal::traits< internal::remove_all_t< DerType >>::Scalar, product) > pow(const Eigen::AutoDiffScalar< DerType > &x, const typename internal::traits< internal::remove_all_t< DerType >>::Scalar &y)
Eigen::AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(Eigen::internal::remove_all_t< DerType >, typename Eigen::internal::traits< Eigen::internal::remove_all_t< DerType >>::Scalar, product) > asin(const Eigen::AutoDiffScalar< DerType > &x)
AutoDiffScalar< Matrix< typename internal::traits< internal::remove_all_t< DerTypeA > >::Scalar, Dynamic, 1 > > atan2(const AutoDiffScalar< DerTypeA > &a, const AutoDiffScalar< DerTypeB > &b)
Eigen::AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(Eigen::internal::remove_all_t< DerType >, typename Eigen::internal::traits< Eigen::internal::remove_all_t< DerType >>::Scalar, product) > abs(const Eigen::AutoDiffScalar< DerType > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_conjugate_op< typename Derived::Scalar >, const Derived > conj(const Eigen::ArrayBase< Derived > &x)
Eigen::AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(Eigen::internal::remove_all_t< DerType >, typename Eigen::internal::traits< Eigen::internal::remove_all_t< DerType >>::Scalar, product) > sin(const Eigen::AutoDiffScalar< DerType > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_real_op< typename Derived::Scalar >, const Derived > real(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_cos_op< typename Derived::Scalar >, const Derived > cos(const Eigen::ArrayBase< Derived > &x)
Eigen::AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(Eigen::internal::remove_all_t< DerType >, typename Eigen::internal::traits< Eigen::internal::remove_all_t< DerType >>::Scalar, product) > tanh(const Eigen::AutoDiffScalar< DerType > &x)
const int Dynamic
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_abs_op< typename Derived::Scalar >, const Derived > abs(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_exp_op< typename Derived::Scalar >, const Derived > exp(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sin_op< typename Derived::Scalar >, const Derived > sin(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_log_op< typename Derived::Scalar >, const Derived > log(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sinh_op< typename Derived::Scalar >, const Derived > sinh(const Eigen::ArrayBase< Derived > &x)
AutoDiffScalar< NewDerType > MakeAutoDiffScalar(const typename NewDerType::Scalar &value, const NewDerType &der)
Eigen::AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(Eigen::internal::remove_all_t< DerType >, typename Eigen::internal::traits< Eigen::internal::remove_all_t< DerType >>::Scalar, product) > cosh(const Eigen::AutoDiffScalar< DerType > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sqrt_op< typename Derived::Scalar >, const Derived > sqrt(const Eigen::ArrayBase< Derived > &x)
AutoDiffScalar< typename Eigen::internal::remove_all_t< DerType >::PlainObject > type
NumTraits< typename DerTypeCleaned::Scalar >::Literal Literal
internal::remove_all_t< DerType > DerTypeCleaned
AutoDiffScalar< Matrix< typename NumTraits< typename DerTypeCleaned::Scalar >::Real, DerTypeCleaned::RowsAtCompileTime, DerTypeCleaned::ColsAtCompileTime, 0, DerTypeCleaned::MaxRowsAtCompileTime, DerTypeCleaned::MaxColsAtCompileTime > > Real