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<internal::is_same<Scalar,
typename internal::traits<
typename internal::remove_all<OtherDerType>::type>::Scalar>::value,
void*>::type = 0
114 : m_value(other.value()), m_derivatives(other.derivatives())
117 friend std::ostream & operator << (std::ostream & s,
const AutoDiffScalar& a)
119 return s << a.value();
123 : m_value(other.value()), m_derivatives(other.derivatives())
126 template<
typename OtherDerType>
129 m_value = other.value();
130 m_derivatives = other.derivatives();
136 m_value = other.value();
137 m_derivatives = other.derivatives();
144 if(m_derivatives.size()>0)
145 m_derivatives.setZero();
152 inline const Scalar& value()
const {
return m_value; }
153 inline Scalar& value() {
return m_value; }
155 inline const DerType& derivatives()
const {
return m_derivatives; }
156 inline DerType& derivatives() {
return m_derivatives; }
158 inline bool operator< (
const Scalar& other)
const {
return m_value < other; }
159 inline bool operator<=(
const Scalar& other)
const {
return m_value <= other; }
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; }
165 friend inline bool operator< (
const Scalar& a,
const AutoDiffScalar& b) {
return a < b.value(); }
166 friend inline bool operator<=(
const Scalar& a,
const AutoDiffScalar& b) {
return a <= b.value(); }
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(); }
172 template<
typename OtherDerType>
inline bool operator< (const AutoDiffScalar<OtherDerType>& b)
const {
return m_value < b.value(); }
173 template<
typename OtherDerType>
inline bool operator<=(const AutoDiffScalar<OtherDerType>& b)
const {
return m_value <= b.value(); }
205 template<
typename OtherDerType>
209 internal::make_coherent(m_derivatives, other.derivatives());
211 m_value + other.value(),
212 m_derivatives + other.derivatives());
215 template<
typename OtherDerType>
219 (*this) = (*this) + other;
231 return AutoDiffScalar<CwiseUnaryOp<internal::scalar_opposite_op<Scalar>,
const DerType> >
232 (a - b.value(), -b.derivatives());
241 template<
typename OtherDerType>
245 internal::make_coherent(m_derivatives, other.derivatives());
247 m_value - other.value(),
248 m_derivatives - other.derivatives());
251 template<
typename OtherDerType>
255 *
this = *
this - other;
259 inline const AutoDiffScalar<CwiseUnaryOp<internal::scalar_opposite_op<Scalar>,
const DerType> >
262 return AutoDiffScalar<CwiseUnaryOp<internal::scalar_opposite_op<Scalar>,
const DerType> >(
268 operator*(
const Scalar& other)
const 270 return MakeAutoDiffScalar(m_value * other, m_derivatives * other);
276 return MakeAutoDiffScalar(a.value() * other, a.derivatives() * other);
296 operator/(
const Scalar& other)
const 298 return MakeAutoDiffScalar(m_value / other, (m_derivatives * (Scalar(1)/other)));
304 return MakeAutoDiffScalar(other / a.value(), a.derivatives() * (Scalar(-other) / (a.value()*a.value())));
323 template<
typename OtherDerType>
324 inline const AutoDiffScalar<EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(
325 CwiseBinaryOp<internal::scalar_difference_op<Scalar> EIGEN_COMMA
326 const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DerType,Scalar,product) EIGEN_COMMA
327 const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(
typename internal::remove_all<OtherDerType>::type,Scalar,product) >,Scalar,product) >
330 internal::make_coherent(m_derivatives, other.derivatives());
331 return MakeAutoDiffScalar(
332 m_value / other.value(),
333 ((m_derivatives * other.value()) - (other.derivatives() * m_value))
334 * (Scalar(1)/(other.value()*other.value())));
337 template<
typename OtherDerType>
339 const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DerType,Scalar,product),
340 const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(
typename internal::remove_all<OtherDerType>::type,Scalar,product) > >
343 internal::make_coherent(m_derivatives, other.derivatives());
344 return MakeAutoDiffScalar(
345 m_value * other.value(),
346 (m_derivatives * other.value()) + (other.derivatives() * m_value));
351 *
this = *
this * other;
355 template<
typename OtherDerType>
358 *
this = *
this * other;
364 *
this = *
this / other;
368 template<
typename OtherDerType>
371 *
this = *
this / other;
377 DerType m_derivatives;
383 template<
typename _DerType>
384 struct auto_diff_special_op<_DerType, true>
388 typedef typename remove_all<_DerType>::type DerType;
389 typedef typename traits<DerType>::Scalar Scalar;
390 typedef typename NumTraits<Scalar>::Real Real;
418 derived().value() += other;
424 operator*(
const Real& other)
const 427 derived().value() * other,
428 derived().derivatives() * other);
434 return AutoDiffScalar<typename CwiseUnaryOp<bind1st_op<scalar_product_op<Real,Scalar> >, DerType>::Type >(
436 a.derivatives() * other);
441 *
this = *
this * other;
446 template<
typename _DerType>
447 struct auto_diff_special_op<_DerType, false>
449 void operator*()
const;
450 void operator-()
const;
451 void operator+()
const;
454 template<
typename A_Scalar,
int A_Rows,
int A_Cols,
int A_Options,
int A_MaxRows,
int A_MaxCols,
typename B>
455 struct make_coherent_impl<Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols>, B> {
456 typedef Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols> A;
457 static void run(A& a, B& b) {
458 if((A_Rows==Dynamic || A_Cols==Dynamic) && (a.size()==0))
466 template<
typename A,
typename B_Scalar,
int B_Rows,
int B_Cols,
int B_Options,
int B_MaxRows,
int B_MaxCols>
467 struct make_coherent_impl<A, Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols> > {
468 typedef Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols> B;
469 static void run(A& a, B& b) {
470 if((B_Rows==Dynamic || B_Cols==Dynamic) && (b.size()==0))
478 template<
typename A_Scalar,
int A_Rows,
int A_Cols,
int A_Options,
int A_MaxRows,
int A_MaxCols,
479 typename B_Scalar,
int B_Rows,
int B_Cols,
int B_Options,
int B_MaxRows,
int B_MaxCols>
480 struct make_coherent_impl<Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols>,
481 Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols> > {
482 typedef Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols> A;
483 typedef Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols> B;
484 static void run(A& a, B& b) {
485 if((A_Rows==Dynamic || A_Cols==Dynamic) && (a.size()==0))
490 else if((B_Rows==Dynamic || B_Cols==Dynamic) && (b.size()==0))
500 template<
typename A_Scalar,
int A_Rows,
int A_Cols,
int A_Options,
int A_MaxRows,
int A_MaxCols,
typename BinOp>
501 struct ScalarBinaryOpTraits<Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols>,A_Scalar,BinOp>
503 enum { Defined = 1 };
504 typedef Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols> ReturnType;
507 template<
typename A_Scalar,
int A_Rows,
int A_Cols,
int A_Options,
int A_MaxRows,
int A_MaxCols,
typename BinOp>
508 struct ScalarBinaryOpTraits<A_Scalar, Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols>, BinOp>
510 enum { Defined = 1 };
511 typedef Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols> ReturnType;
514 template<
typename DerType,
typename BinOp>
515 struct ScalarBinaryOpTraits<AutoDiffScalar<DerType>,typename DerType::Scalar,BinOp>
517 enum { Defined = 1 };
521 template<
typename DerType,
typename BinOp>
522 struct ScalarBinaryOpTraits<typename DerType::Scalar,AutoDiffScalar<DerType>, BinOp>
524 enum { Defined = 1 };
545 #define EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(FUNC,CODE) \ 546 template<typename DerType> \ 547 inline const Eigen::AutoDiffScalar< \ 548 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) > \ 549 FUNC(const Eigen::AutoDiffScalar<DerType>& x) { \ 550 using namespace Eigen; \ 551 EIGEN_UNUSED typedef typename Eigen::internal::traits<typename Eigen::internal::remove_all<DerType>::type>::Scalar Scalar; \ 555 template<
typename DerType>
557 template<
typename DerType>
559 template<
typename DerType>
561 template<
typename DerType,
typename T>
564 return (x <= y ? ADS(x) : ADS(y));
566 template<
typename DerType,
typename T>
569 return (x >= y ? ADS(x) : ADS(y));
571 template<
typename DerType,
typename T>
574 return (x < y ? ADS(x) : ADS(y));
576 template<
typename DerType,
typename T>
579 return (x > y ? ADS(x) : ADS(y));
582 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(abs,
584 return Eigen::MakeAutoDiffScalar(abs(x.value()), x.derivatives() * (x.value()<0 ? -1 : 1) );)
586 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(abs2,
588 return Eigen::MakeAutoDiffScalar(abs2(x.value()), x.derivatives() * (Scalar(2)*x.value()));)
590 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(sqrt,
592 Scalar sqrtx = sqrt(x.value());
593 return Eigen::MakeAutoDiffScalar(sqrtx,x.derivatives() * (Scalar(0.5) / sqrtx));)
595 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(cos,
598 return Eigen::MakeAutoDiffScalar(cos(x.value()), x.derivatives() * (-sin(x.value())));)
600 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(sin,
603 return Eigen::MakeAutoDiffScalar(sin(x.value()),x.derivatives() * cos(x.value()));)
605 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(exp,
607 Scalar expx = exp(x.value());
608 return Eigen::MakeAutoDiffScalar(expx,x.derivatives() * expx);)
610 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(log,
612 return Eigen::MakeAutoDiffScalar(log(x.value()),x.derivatives() * (Scalar(1)/x.value()));)
614 template<typename DerType>
616 EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(
typename internal::remove_all<DerType>::type,
typename internal::traits<
typename internal::remove_all<DerType>::type>::Scalar,product) >
619 using namespace Eigen;
621 return Eigen::MakeAutoDiffScalar(pow(x.value(),y), x.derivatives() * (y * pow(x.value(),y-1)));
625 template<
typename DerTypeA,
typename DerTypeB>
630 typedef typename internal::traits<typename internal::remove_all<DerTypeA>::type>::Scalar Scalar;
633 ret.value() = atan2(a.value(), b.value());
635 Scalar squared_hypot = a.value() * a.value() + b.value() * b.value();
638 ret.derivatives() = (a.derivatives() * b.value() - a.value() * b.derivatives()) / squared_hypot;
643 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(tan,
646 return Eigen::MakeAutoDiffScalar(tan(x.value()),x.derivatives() * (Scalar(1)/numext::abs2(cos(x.value()))));)
648 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(asin,
651 return Eigen::MakeAutoDiffScalar(asin(x.value()),x.derivatives() * (Scalar(1)/sqrt(1-numext::abs2(x.value()))));)
653 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(acos,
656 return Eigen::MakeAutoDiffScalar(acos(x.value()),x.derivatives() * (Scalar(-1)/sqrt(1-numext::abs2(x.value()))));)
658 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(tanh,
661 return Eigen::MakeAutoDiffScalar(tanh(x.value()),x.derivatives() * (Scalar(1)/numext::abs2(cosh(x.value()))));)
663 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(sinh,
666 return Eigen::MakeAutoDiffScalar(sinh(x.value()),x.derivatives() * cosh(x.value()));)
668 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(cosh,
671 return Eigen::MakeAutoDiffScalar(cosh(x.value()),x.derivatives() * sinh(x.value()));)
673 #undef EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY
676 : NumTraits<
typename NumTraits<typename DerType::Scalar>::Real >
679 0, DerType::MaxRowsAtCompileTime, DerType::MaxColsAtCompileTime> > Real;
682 typedef typename NumTraits<typename DerType::Scalar>::Literal Literal;
684 RequireInitialization = 1
690 #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
AutoDiffScalar(const Real &value)
Definition: AutoDiffScalar.h:96
AutoDiffScalar(const Scalar &value, const DerType &der)
Definition: AutoDiffScalar.h:104