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;
35 template<
typename NewDerType>
66 template<
typename _DerType>
68 :
public internal::auto_diff_special_op
69 <_DerType, !internal::is_same<typename internal::traits<typename internal::remove_all<_DerType>::type>::Scalar,
70 typename NumTraits<typename internal::traits<typename internal::remove_all<_DerType>::type>::Scalar>::Real>::value>
73 typedef internal::auto_diff_special_op
74 <_DerType, !internal::is_same<typename internal::traits<typename internal::remove_all<_DerType>::type>::Scalar,
75 typename NumTraits<typename internal::traits<typename internal::remove_all<_DerType>::type>::Scalar>::Real>::value> Base;
76 typedef typename internal::remove_all<_DerType>::type DerType;
77 typedef typename internal::traits<DerType>::Scalar Scalar;
78 typedef typename NumTraits<Scalar>::Real Real;
80 using Base::operator+;
81 using Base::operator*;
89 : m_value(value), m_derivatives(DerType::Zero(nbDer))
91 m_derivatives.coeffRef(derNumber) = Scalar(1);
99 if(m_derivatives.size()>0)
100 m_derivatives.setZero();
105 : m_value(value), m_derivatives(der)
108 template<
typename OtherDerType>
110 #ifndef EIGEN_PARSED_BY_DOXYGEN
111 ,
typename internal::enable_if<
112 internal::is_same<Scalar,
typename internal::traits<
typename internal::remove_all<OtherDerType>::type>::Scalar>::value
113 && internal::is_convertible<OtherDerType,DerType>::value ,
void*>::type = 0
116 : m_value(other.value()), m_derivatives(other.derivatives())
119 friend std::ostream & operator << (std::ostream & s,
const AutoDiffScalar& a)
121 return s << a.value();
125 : m_value(other.value()), m_derivatives(other.derivatives())
128 template<
typename OtherDerType>
131 m_value = other.value();
132 m_derivatives = other.derivatives();
138 m_value = other.value();
139 m_derivatives = other.derivatives();
146 if(m_derivatives.size()>0)
147 m_derivatives.setZero();
154 inline const Scalar& value()
const {
return m_value; }
155 inline Scalar& value() {
return m_value; }
157 inline const DerType& derivatives()
const {
return m_derivatives; }
158 inline DerType& derivatives() {
return m_derivatives; }
160 inline bool operator< (
const Scalar& other)
const {
return m_value < other; }
161 inline bool operator<=(
const Scalar& other)
const {
return m_value <= other; }
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; }
167 friend inline bool operator< (
const Scalar& a,
const AutoDiffScalar& b) {
return a < b.value(); }
168 friend inline bool operator<=(
const Scalar& a,
const AutoDiffScalar& b) {
return a <= b.value(); }
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(); }
174 template<
typename OtherDerType>
inline bool operator< (const AutoDiffScalar<OtherDerType>& b)
const {
return m_value < b.value(); }
175 template<
typename OtherDerType>
inline bool operator<=(const AutoDiffScalar<OtherDerType>& b)
const {
return m_value <= b.value(); }
207 template<
typename OtherDerType>
211 internal::make_coherent(m_derivatives, other.derivatives());
213 m_value + other.value(),
214 m_derivatives + other.derivatives());
217 template<
typename OtherDerType>
221 (*this) = (*this) + other;
233 return AutoDiffScalar<CwiseUnaryOp<internal::scalar_opposite_op<Scalar>,
const DerType> >
234 (a - b.value(), -b.derivatives());
243 template<
typename OtherDerType>
247 internal::make_coherent(m_derivatives, other.derivatives());
249 m_value - other.value(),
250 m_derivatives - other.derivatives());
253 template<
typename OtherDerType>
257 *
this = *
this - other;
261 inline const AutoDiffScalar<CwiseUnaryOp<internal::scalar_opposite_op<Scalar>,
const DerType> >
264 return AutoDiffScalar<CwiseUnaryOp<internal::scalar_opposite_op<Scalar>,
const DerType> >(
270 operator*(
const Scalar& other)
const 272 return MakeAutoDiffScalar(m_value * other, m_derivatives * other);
278 return MakeAutoDiffScalar(a.value() * other, a.derivatives() * other);
298 operator/(
const Scalar& other)
const 300 return MakeAutoDiffScalar(m_value / other, (m_derivatives * (Scalar(1)/other)));
306 return MakeAutoDiffScalar(other / a.value(), a.derivatives() * (Scalar(-other) / (a.value()*a.value())));
325 template<
typename OtherDerType>
326 inline const AutoDiffScalar<EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(
327 CwiseBinaryOp<internal::scalar_difference_op<Scalar> EIGEN_COMMA
328 const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DerType,Scalar,product) EIGEN_COMMA
329 const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(
typename internal::remove_all<OtherDerType>::type,Scalar,product) >,Scalar,product) >
332 internal::make_coherent(m_derivatives, other.derivatives());
333 return MakeAutoDiffScalar(
334 m_value / other.value(),
335 ((m_derivatives * other.value()) - (other.derivatives() * m_value))
336 * (Scalar(1)/(other.value()*other.value())));
339 template<
typename OtherDerType>
341 const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DerType,Scalar,product),
342 const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(
typename internal::remove_all<OtherDerType>::type,Scalar,product) > >
345 internal::make_coherent(m_derivatives, other.derivatives());
346 return MakeAutoDiffScalar(
347 m_value * other.value(),
348 (m_derivatives * other.value()) + (other.derivatives() * m_value));
353 *
this = *
this * other;
357 template<
typename OtherDerType>
360 *
this = *
this * other;
366 *
this = *
this / other;
370 template<
typename OtherDerType>
373 *
this = *
this / other;
379 DerType m_derivatives;
385 template<
typename _DerType>
386 struct auto_diff_special_op<_DerType, true>
390 typedef typename remove_all<_DerType>::type DerType;
391 typedef typename traits<DerType>::Scalar Scalar;
392 typedef typename NumTraits<Scalar>::Real Real;
420 derived().value() += other;
426 operator*(
const Real& other)
const 429 derived().value() * other,
430 derived().derivatives() * other);
436 return AutoDiffScalar<typename CwiseUnaryOp<bind1st_op<scalar_product_op<Real,Scalar> >, DerType>::Type >(
438 a.derivatives() * other);
443 *
this = *
this * other;
448 template<
typename _DerType>
449 struct auto_diff_special_op<_DerType, false>
451 void operator*()
const;
452 void operator-()
const;
453 void operator+()
const;
456 template<
typename A_Scalar,
int A_Rows,
int A_Cols,
int A_Options,
int A_MaxRows,
int A_MaxCols,
typename B>
457 struct make_coherent_impl<Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols>, B> {
458 typedef Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols> A;
459 static void run(A& a, B& b) {
460 if((A_Rows==Dynamic || A_Cols==Dynamic) && (a.size()==0))
468 template<
typename A,
typename B_Scalar,
int B_Rows,
int B_Cols,
int B_Options,
int B_MaxRows,
int B_MaxCols>
469 struct make_coherent_impl<A, Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols> > {
470 typedef Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols> B;
471 static void run(A& a, B& b) {
472 if((B_Rows==Dynamic || B_Cols==Dynamic) && (b.size()==0))
480 template<
typename A_Scalar,
int A_Rows,
int A_Cols,
int A_Options,
int A_MaxRows,
int A_MaxCols,
481 typename B_Scalar,
int B_Rows,
int B_Cols,
int B_Options,
int B_MaxRows,
int B_MaxCols>
482 struct make_coherent_impl<Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols>,
483 Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols> > {
484 typedef Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols> A;
485 typedef Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols> B;
486 static void run(A& a, B& b) {
487 if((A_Rows==Dynamic || A_Cols==Dynamic) && (a.size()==0))
492 else if((B_Rows==Dynamic || B_Cols==Dynamic) && (b.size()==0))
502 template<
typename DerType,
typename BinOp>
503 struct ScalarBinaryOpTraits<AutoDiffScalar<DerType>,typename DerType::Scalar,BinOp>
508 template<
typename DerType,
typename BinOp>
509 struct ScalarBinaryOpTraits<typename DerType::Scalar,AutoDiffScalar<DerType>, BinOp>
531 #define EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(FUNC,CODE) \ 532 template<typename DerType> \ 533 inline const Eigen::AutoDiffScalar< \ 534 EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(typename Eigen::internal::remove_all<DerType>::type, typename Eigen::internal::traits<typename Eigen::internal::remove_all<DerType>::type>::Scalar, product) > \ 535 FUNC(const Eigen::AutoDiffScalar<DerType>& x) { \ 536 using namespace Eigen; \ 537 EIGEN_UNUSED typedef typename Eigen::internal::traits<typename Eigen::internal::remove_all<DerType>::type>::Scalar Scalar; \ 541 template<
typename DerType>
543 template<
typename DerType>
545 template<
typename DerType>
547 template<
typename DerType,
typename T>
550 return (x <= y ? ADS(x) : ADS(y));
552 template<
typename DerType,
typename T>
555 return (x >= y ? ADS(x) : ADS(y));
557 template<
typename DerType,
typename T>
560 return (x < y ? ADS(x) : ADS(y));
562 template<
typename DerType,
typename T>
565 return (x > y ? ADS(x) : ADS(y));
567 template<
typename DerType>
569 return (x.value() < y.value() ? x : y);
571 template<
typename DerType>
573 return (x.value() >= y.value() ? x : y);
577 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(abs,
579 return Eigen::MakeAutoDiffScalar(abs(x.value()), x.derivatives() * (x.value()<0 ? -1 : 1) );)
581 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(abs2,
583 return Eigen::MakeAutoDiffScalar(abs2(x.value()), x.derivatives() * (Scalar(2)*x.value()));)
585 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(sqrt,
587 Scalar sqrtx = sqrt(x.value());
588 return Eigen::MakeAutoDiffScalar(sqrtx,x.derivatives() * (Scalar(0.5) / sqrtx));)
590 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(cos,
593 return Eigen::MakeAutoDiffScalar(cos(x.value()), x.derivatives() * (-sin(x.value())));)
595 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(sin,
598 return Eigen::MakeAutoDiffScalar(sin(x.value()),x.derivatives() * cos(x.value()));)
600 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(exp,
602 Scalar expx = exp(x.value());
603 return Eigen::MakeAutoDiffScalar(expx,x.derivatives() * expx);)
605 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(log,
607 return Eigen::MakeAutoDiffScalar(log(x.value()),x.derivatives() * (Scalar(1)/x.value()));)
609 template<typename DerType>
611 EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(
typename internal::remove_all<DerType>::type,
typename internal::traits<
typename internal::remove_all<DerType>::type>::Scalar,product) >
614 using namespace Eigen;
616 return Eigen::MakeAutoDiffScalar(pow(x.value(),y), x.derivatives() * (y * pow(x.value(),y-1)));
620 template<
typename DerTypeA,
typename DerTypeB>
625 typedef typename internal::traits<typename internal::remove_all<DerTypeA>::type>::Scalar Scalar;
628 ret.value() = atan2(a.value(), b.value());
630 Scalar squared_hypot = a.value() * a.value() + b.value() * b.value();
633 ret.derivatives() = (a.derivatives() * b.value() - a.value() * b.derivatives()) / squared_hypot;
638 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(tan,
641 return Eigen::MakeAutoDiffScalar(tan(x.value()),x.derivatives() * (Scalar(1)/numext::abs2(cos(x.value()))));)
643 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(asin,
646 return Eigen::MakeAutoDiffScalar(asin(x.value()),x.derivatives() * (Scalar(1)/sqrt(1-numext::abs2(x.value()))));)
648 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(acos,
651 return Eigen::MakeAutoDiffScalar(acos(x.value()),x.derivatives() * (Scalar(-1)/sqrt(1-numext::abs2(x.value()))));)
653 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(tanh,
656 return Eigen::MakeAutoDiffScalar(tanh(x.value()),x.derivatives() * (Scalar(1)/numext::abs2(cosh(x.value()))));)
658 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(sinh,
661 return Eigen::MakeAutoDiffScalar(sinh(x.value()),x.derivatives() * cosh(x.value()));)
663 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(cosh,
666 return Eigen::MakeAutoDiffScalar(cosh(x.value()),x.derivatives() * sinh(x.value()));)
668 #undef EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY
671 : NumTraits<
typename NumTraits<
typename internal::remove_all<DerType>::type::Scalar>::Real >
673 typedef typename internal::remove_all<DerType>::type DerTypeCleaned;
675 0, DerTypeCleaned::MaxRowsAtCompileTime, DerTypeCleaned::MaxColsAtCompileTime> > Real;
678 typedef typename NumTraits<typename DerTypeCleaned::Scalar>::Literal Literal;
680 RequireInitialization = 1
687 template <
typename T>
689 :
public numeric_limits<typename T::Scalar> {};
693 #endif // EIGEN_AUTODIFF_SCALAR_H AutoDiffScalar(const Scalar &value, int nbDer, int derNumber)
Definition: AutoDiffScalar.h:88
A scalar type replacement with automatic differentation capability.
Definition: AutoDiffScalar.h:33
AutoDiffScalar()
Definition: AutoDiffScalar.h:84
Namespace containing all symbols from the Eigen library.
Definition: AdolcForward:45
Definition: AutoDiffScalar.h:686
AutoDiffScalar(const Real &value)
Definition: AutoDiffScalar.h:96
AutoDiffScalar(const Scalar &value, const DerType &der)
Definition: AutoDiffScalar.h:104