10 #ifndef EIGEN_AUTODIFF_SCALAR_H 11 #define EIGEN_AUTODIFF_SCALAR_H 17 template<
typename A,
typename B>
18 struct make_coherent_impl {
19 static void run(A&, B&) {}
23 template<
typename A,
typename B>
24 void make_coherent(
const A& a,
const B&b)
26 make_coherent_impl<A,B>::run(a.const_cast_derived(), b.const_cast_derived());
29 template<
typename _DerType,
bool Enable>
struct auto_diff_special_op;
59 template<
typename _DerType>
61 :
public internal::auto_diff_special_op
62 <_DerType, !internal::is_same<typename internal::traits<typename internal::remove_all<_DerType>::type>::Scalar,
63 typename NumTraits<typename internal::traits<typename internal::remove_all<_DerType>::type>::Scalar>::Real>::value>
66 typedef internal::auto_diff_special_op
67 <_DerType, !internal::is_same<typename internal::traits<typename internal::remove_all<_DerType>::type>::Scalar,
68 typename NumTraits<typename internal::traits<typename internal::remove_all<_DerType>::type>::Scalar>::Real>::value> Base;
69 typedef typename internal::remove_all<_DerType>::type DerType;
70 typedef typename internal::traits<DerType>::Scalar Scalar;
71 typedef typename NumTraits<Scalar>::Real Real;
73 using Base::operator+;
74 using Base::operator*;
82 : m_value(value), m_derivatives(DerType::Zero(nbDer))
84 m_derivatives.coeffRef(derNumber) = Scalar(1);
92 if(m_derivatives.size()>0)
93 m_derivatives.setZero();
98 : m_value(value), m_derivatives(der)
101 template<
typename OtherDerType>
103 #ifndef EIGEN_PARSED_BY_DOXYGEN
104 ,
typename internal::enable_if<internal::is_same<Scalar,typename OtherDerType::Scalar>::value,
void*>::type = 0
107 : m_value(other.value()), m_derivatives(other.derivatives())
110 friend std::ostream & operator << (std::ostream & s,
const AutoDiffScalar& a)
112 return s << a.value();
116 : m_value(other.value()), m_derivatives(other.derivatives())
119 template<
typename OtherDerType>
122 m_value = other.value();
123 m_derivatives = other.derivatives();
129 m_value = other.value();
130 m_derivatives = other.derivatives();
137 if(m_derivatives.size()>0)
138 m_derivatives.setZero();
145 inline const Scalar& value()
const {
return m_value; }
146 inline Scalar& value() {
return m_value; }
148 inline const DerType& derivatives()
const {
return m_derivatives; }
149 inline DerType& derivatives() {
return m_derivatives; }
151 inline bool operator< (
const Scalar& other)
const {
return m_value < other; }
152 inline bool operator<=(
const Scalar& other)
const {
return m_value <= other; }
153 inline bool operator> (
const Scalar& other)
const {
return m_value > other; }
154 inline bool operator>=(
const Scalar& other)
const {
return m_value >= other; }
155 inline bool operator==(
const Scalar& other)
const {
return m_value == other; }
156 inline bool operator!=(
const Scalar& other)
const {
return m_value != other; }
158 friend inline bool operator< (
const Scalar& a,
const AutoDiffScalar& b) {
return a < b.value(); }
159 friend inline bool operator<=(
const Scalar& a,
const AutoDiffScalar& b) {
return a <= b.value(); }
160 friend inline bool operator> (
const Scalar& a,
const AutoDiffScalar& b) {
return a > b.value(); }
161 friend inline bool operator>=(
const Scalar& a,
const AutoDiffScalar& b) {
return a >= b.value(); }
162 friend inline bool operator==(
const Scalar& a,
const AutoDiffScalar& b) {
return a == b.value(); }
163 friend inline bool operator!=(
const Scalar& a,
const AutoDiffScalar& b) {
return a != b.value(); }
165 template<
typename OtherDerType>
inline bool operator< (const AutoDiffScalar<OtherDerType>& b)
const {
return m_value < b.value(); }
166 template<
typename OtherDerType>
inline bool operator<=(const AutoDiffScalar<OtherDerType>& b)
const {
return m_value <= b.value(); }
198 template<
typename OtherDerType>
202 internal::make_coherent(m_derivatives, other.derivatives());
204 m_value + other.value(),
205 m_derivatives + other.derivatives());
208 template<
typename OtherDerType>
212 (*this) = (*this) + other;
224 return AutoDiffScalar<CwiseUnaryOp<internal::scalar_opposite_op<Scalar>,
const DerType> >
225 (a - b.value(), -b.derivatives());
234 template<
typename OtherDerType>
238 internal::make_coherent(m_derivatives, other.derivatives());
240 m_value - other.value(),
241 m_derivatives - other.derivatives());
244 template<
typename OtherDerType>
248 *
this = *
this - other;
252 inline const AutoDiffScalar<CwiseUnaryOp<internal::scalar_opposite_op<Scalar>,
const DerType> >
255 return AutoDiffScalar<CwiseUnaryOp<internal::scalar_opposite_op<Scalar>,
const DerType> >(
261 operator*(
const Scalar& other)
const 263 return AutoDiffScalar<CwiseUnaryOp<internal::scalar_multiple_op<Scalar>,
const DerType> >(
265 (m_derivatives * other));
268 friend inline const AutoDiffScalar<CwiseUnaryOp<internal::scalar_multiple_op<Scalar>,
const DerType> >
271 return AutoDiffScalar<CwiseUnaryOp<internal::scalar_multiple_op<Scalar>,
const DerType> >(
273 a.derivatives() * other);
292 inline const AutoDiffScalar<CwiseUnaryOp<internal::scalar_multiple_op<Scalar>,
const DerType> >
293 operator/(
const Scalar& other)
const 295 return AutoDiffScalar<CwiseUnaryOp<internal::scalar_multiple_op<Scalar>,
const DerType> >(
297 (m_derivatives * (Scalar(1)/other)));
300 friend inline const AutoDiffScalar<CwiseUnaryOp<internal::scalar_multiple_op<Scalar>,
const DerType> >
303 return AutoDiffScalar<CwiseUnaryOp<internal::scalar_multiple_op<Scalar>,
const DerType> >(
305 a.derivatives() * (Scalar(-other) / (a.value()*a.value())));
324 template<
typename OtherDerType>
325 inline const AutoDiffScalar<CwiseUnaryOp<internal::scalar_multiple_op<Scalar>,
326 const CwiseBinaryOp<internal::scalar_difference_op<Scalar>,
327 const CwiseUnaryOp<internal::scalar_multiple_op<Scalar>,
const DerType>,
328 const CwiseUnaryOp<internal::scalar_multiple_op<Scalar>,
const typename internal::remove_all<OtherDerType>::type > > > >
331 internal::make_coherent(m_derivatives, other.derivatives());
332 return AutoDiffScalar<CwiseUnaryOp<internal::scalar_multiple_op<Scalar>,
333 const CwiseBinaryOp<internal::scalar_difference_op<Scalar>,
334 const CwiseUnaryOp<internal::scalar_multiple_op<Scalar>,
const DerType>,
335 const CwiseUnaryOp<internal::scalar_multiple_op<Scalar>,
const typename internal::remove_all<OtherDerType>::type > > > >(
336 m_value / other.value(),
337 ((m_derivatives * other.value()) - (m_value * other.derivatives()))
338 * (Scalar(1)/(other.value()*other.value())));
341 template<
typename OtherDerType>
343 const CwiseUnaryOp<internal::scalar_multiple_op<Scalar>,
const DerType>,
344 const CwiseUnaryOp<internal::scalar_multiple_op<Scalar>,
const typename internal::remove_all<OtherDerType>::type> > >
347 internal::make_coherent(m_derivatives, other.derivatives());
349 const CwiseUnaryOp<internal::scalar_multiple_op<Scalar>,
const DerType>,
350 const CwiseUnaryOp<internal::scalar_multiple_op<Scalar>,
const typename internal::remove_all<OtherDerType>::type > > >(
351 m_value * other.value(),
352 (m_derivatives * other.value()) + (m_value * other.derivatives()));
357 *
this = *
this * other;
361 template<
typename OtherDerType>
364 *
this = *
this * other;
370 *
this = *
this / other;
374 template<
typename OtherDerType>
377 *
this = *
this / other;
383 DerType m_derivatives;
389 template<
typename _DerType>
390 struct auto_diff_special_op<_DerType, true>
394 typedef typename remove_all<_DerType>::type DerType;
395 typedef typename traits<DerType>::Scalar Scalar;
396 typedef typename NumTraits<Scalar>::Real Real;
424 derived().value() += other;
430 operator*(
const Real& other)
const 433 derived().value() * other,
434 derived().derivatives() * other);
440 return AutoDiffScalar<typename CwiseUnaryOp<scalar_multiple2_op<Scalar,Real>, DerType>::Type >(
442 a.derivatives() * other);
447 *
this = *
this * other;
452 template<
typename _DerType>
453 struct auto_diff_special_op<_DerType, false>
455 void operator*()
const;
456 void operator-()
const;
457 void operator+()
const;
460 template<
typename A_Scalar,
int A_Rows,
int A_Cols,
int A_Options,
int A_MaxRows,
int A_MaxCols,
typename B>
461 struct make_coherent_impl<Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols>, B> {
462 typedef Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols> A;
463 static void run(A& a, B& b) {
464 if((A_Rows==Dynamic || A_Cols==Dynamic) && (a.size()==0))
472 template<
typename A,
typename B_Scalar,
int B_Rows,
int B_Cols,
int B_Options,
int B_MaxRows,
int B_MaxCols>
473 struct make_coherent_impl<A, Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols> > {
474 typedef Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols> B;
475 static void run(A& a, B& b) {
476 if((B_Rows==Dynamic || B_Cols==Dynamic) && (b.size()==0))
484 template<
typename A_Scalar,
int A_Rows,
int A_Cols,
int A_Options,
int A_MaxRows,
int A_MaxCols,
485 typename B_Scalar,
int B_Rows,
int B_Cols,
int B_Options,
int B_MaxRows,
int B_MaxCols>
486 struct make_coherent_impl<Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols>,
487 Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols> > {
488 typedef Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols> A;
489 typedef Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols> B;
490 static void run(A& a, B& b) {
491 if((A_Rows==Dynamic || A_Cols==Dynamic) && (a.size()==0))
496 else if((B_Rows==Dynamic || B_Cols==Dynamic) && (b.size()==0))
504 template<
typename A_Scalar,
int A_Rows,
int A_Cols,
int A_Options,
int A_MaxRows,
int A_MaxCols>
505 struct scalar_product_traits<Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols>,A_Scalar>
507 enum { Defined = 1 };
508 typedef Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols> ReturnType;
511 template<
typename A_Scalar,
int A_Rows,
int A_Cols,
int A_Options,
int A_MaxRows,
int A_MaxCols>
512 struct scalar_product_traits<A_Scalar, Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols> >
514 enum { Defined = 1 };
515 typedef Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols> ReturnType;
518 template<
typename DerType>
519 struct scalar_product_traits<AutoDiffScalar<DerType>,typename DerType::Scalar>
521 enum { Defined = 1 };
525 template<
typename DerType>
526 struct scalar_product_traits<typename DerType::Scalar,AutoDiffScalar<DerType> >
528 enum { Defined = 1 };
534 #define EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(FUNC,CODE) \ 535 template<typename DerType> \ 536 inline const Eigen::AutoDiffScalar<Eigen::CwiseUnaryOp<Eigen::internal::scalar_multiple_op<typename Eigen::internal::traits<typename Eigen::internal::remove_all<DerType>::type>::Scalar>, const typename Eigen::internal::remove_all<DerType>::type> > \ 537 FUNC(const Eigen::AutoDiffScalar<DerType>& x) { \ 538 using namespace Eigen; \ 539 typedef typename Eigen::internal::traits<typename Eigen::internal::remove_all<DerType>::type>::Scalar Scalar; \ 540 typedef AutoDiffScalar<CwiseUnaryOp<Eigen::internal::scalar_multiple_op<Scalar>, const typename Eigen::internal::remove_all<DerType>::type> > ReturnType; \ 544 template<
typename DerType>
546 template<
typename DerType>
548 template<
typename DerType>
550 template<
typename DerType,
typename T>
552 template<
typename DerType,
typename T>
554 template<
typename DerType,
typename T>
556 template<
typename DerType,
typename T>
559 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(abs,
561 return ReturnType(abs(x.value()), x.derivatives() * (x.value()<0 ? -1 : 1) );)
563 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(abs2,
565 return ReturnType(abs2(x.value()), x.derivatives() * (Scalar(2)*x.value()));)
567 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(sqrt,
569 Scalar sqrtx = sqrt(x.value());
570 return ReturnType(sqrtx,x.derivatives() * (Scalar(0.5) / sqrtx));)
572 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(cos,
575 return ReturnType(cos(x.value()), x.derivatives() * (-sin(x.value())));)
577 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(sin,
580 return ReturnType(sin(x.value()),x.derivatives() * cos(x.value()));)
582 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(exp,
584 Scalar expx = exp(x.value());
585 return ReturnType(expx,x.derivatives() * expx);)
587 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(log,
589 return ReturnType(log(x.value()),x.derivatives() * (Scalar(1)/x.value()));)
591 template<typename DerType>
592 inline const Eigen::AutoDiffScalar<Eigen::CwiseUnaryOp<Eigen::internal::scalar_multiple_op<
typename Eigen::internal::traits<DerType>::Scalar>,
const DerType> >
595 using namespace Eigen;
596 typedef typename Eigen::internal::traits<DerType>::Scalar Scalar;
598 std::pow(x.value(),y),
599 x.derivatives() * (y * std::pow(x.value(),y-1)));
603 template<
typename DerTypeA,
typename DerTypeB>
608 typedef typename internal::traits<DerTypeA>::Scalar Scalar;
611 ret.value() = atan2(a.value(), b.value());
613 Scalar squared_hypot = a.value() * a.value() + b.value() * b.value();
616 ret.derivatives() = (a.derivatives() * b.value() - a.value() * b.derivatives()) / squared_hypot;
621 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(tan,
624 return ReturnType(tan(x.value()),x.derivatives() * (Scalar(1)/numext::abs2(cos(x.value()))));)
626 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(asin,
629 return ReturnType(asin(x.value()),x.derivatives() * (Scalar(1)/sqrt(1-numext::abs2(x.value()))));)
631 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(acos,
634 return ReturnType(acos(x.value()),x.derivatives() * (Scalar(-1)/sqrt(1-numext::abs2(x.value()))));)
636 #undef EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY
639 : NumTraits<
typename NumTraits<typename DerType::Scalar>::Real >
642 DerType::Options, DerType::MaxRowsAtCompileTime, DerType::MaxColsAtCompileTime> > Real;
646 RequireInitialization = 1
652 #endif // EIGEN_AUTODIFF_SCALAR_H AutoDiffScalar(const Scalar &value, int nbDer, int derNumber)
Definition: AutoDiffScalar.h:81
A scalar type replacement with automatic differentation capability.
Definition: AutoDiffScalar.h:60
AutoDiffScalar()
Definition: AutoDiffScalar.h:77
Namespace containing all symbols from the Eigen library.
Definition: CXX11Meta.h:13
AutoDiffScalar(const Real &value)
Definition: AutoDiffScalar.h:89
AutoDiffScalar(const Scalar &value, const DerType &der)
Definition: AutoDiffScalar.h:97