10 #ifndef EIGEN_AUTODIFF_SCALAR_H 11 #define EIGEN_AUTODIFF_SCALAR_H 17 template<
typename A,
typename B>
19 static void run(
A&,
B&) {}
23 template<
typename A,
typename B>
24 void make_coherent(
const A& a,
const B&b)
35 template<
typename NewDerType>
66 template<
typename _DerType>
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>
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
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(
326 const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DerType,Scalar,product) EIGEN_COMMA
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),
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>
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>
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>
457 static void run(A& a,
B& b) {
466 template<
typename A,
typename B_Scalar,
int B_Rows,
int B_Cols,
int B_Options,
int B_MaxRows,
int B_MaxCols>
469 static void run(
A& a, B& b) {
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>
481 Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols> > {
484 static void run(A& a, B& b) {
500 template<
typename DerType,
typename BinOp>
506 template<
typename DerType,
typename BinOp>
529 #define EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(FUNC,CODE) \ 530 template<typename DerType> \ 531 inline const Eigen::AutoDiffScalar< \ 532 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) > \ 533 FUNC(const Eigen::AutoDiffScalar<DerType>& x) { \ 534 using namespace Eigen; \ 535 EIGEN_UNUSED typedef typename Eigen::internal::traits<typename Eigen::internal::remove_all<DerType>::type>::Scalar Scalar; \ 539 template<
typename DerType>
541 template<
typename DerType>
543 template<
typename DerType>
545 template<
typename DerType,
typename T>
548 return (x <= y ? ADS(x) : ADS(y));
550 template<
typename DerType,
typename T>
553 return (x >= y ? ADS(x) : ADS(y));
555 template<
typename DerType,
typename T>
558 return (x < y ? ADS(x) : ADS(y));
560 template<
typename DerType,
typename T>
563 return (x > y ? ADS(x) : ADS(y));
565 template<
typename DerType>
567 return (x.value() < y.value() ? x : y);
569 template<
typename DerType>
571 return (x.value() >= y.value() ? x : y);
575 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(abs,
577 return Eigen::MakeAutoDiffScalar(abs(x.value()), x.derivatives() * (x.value()<0 ? -1 : 1) );)
579 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(abs2,
581 return Eigen::MakeAutoDiffScalar(abs2(x.value()), x.derivatives() * (Scalar(2)*x.value()));)
583 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(sqrt,
585 Scalar sqrtx = sqrt(x.value());
586 return Eigen::MakeAutoDiffScalar(sqrtx,x.derivatives() * (Scalar(0.5) / sqrtx));)
588 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(cos,
591 return Eigen::MakeAutoDiffScalar(cos(x.value()), x.derivatives() * (-sin(x.value())));)
593 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(sin,
596 return Eigen::MakeAutoDiffScalar(sin(x.value()),x.derivatives() * cos(x.value()));)
598 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(exp,
600 Scalar expx = exp(x.value());
601 return Eigen::MakeAutoDiffScalar(expx,x.derivatives() * expx);)
603 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(log,
605 return Eigen::MakeAutoDiffScalar(log(x.value()),x.derivatives() * (Scalar(1)/x.value()));)
607 template<typename DerType>
612 using namespace Eigen;
614 return Eigen::MakeAutoDiffScalar(pow(x.value(),y), x.derivatives() * (y * pow(x.value(),y-1)));
618 template<
typename DerTypeA,
typename DerTypeB>
626 ret.value() = atan2(a.value(), b.value());
628 Scalar squared_hypot = a.value() * a.value() + b.value() * b.value();
631 ret.derivatives() = (a.derivatives() * b.value() - a.value() * b.derivatives()) / squared_hypot;
636 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(tan,
639 return Eigen::MakeAutoDiffScalar(tan(x.value()),x.derivatives() * (Scalar(1)/numext::abs2(cos(x.value()))));)
641 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(asin,
644 return Eigen::MakeAutoDiffScalar(asin(x.value()),x.derivatives() * (Scalar(1)/sqrt(1-numext::abs2(x.value()))));)
646 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(acos,
649 return Eigen::MakeAutoDiffScalar(acos(x.value()),x.derivatives() * (Scalar(-1)/sqrt(1-numext::abs2(x.value()))));)
651 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(tanh,
654 return Eigen::MakeAutoDiffScalar(tanh(x.value()),x.derivatives() * (Scalar(1)/numext::abs2(cosh(x.value()))));)
656 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(sinh,
659 return Eigen::MakeAutoDiffScalar(sinh(x.value()),x.derivatives() * cosh(x.value()));)
661 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(cosh,
664 return Eigen::MakeAutoDiffScalar(cosh(x.value()),x.derivatives() * sinh(x.value()));)
666 #undef EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY
673 0, DerTypeCleaned::MaxRowsAtCompileTime, DerTypeCleaned::MaxColsAtCompileTime> >
Real;
678 RequireInitialization = 1
684 #endif // EIGEN_AUTODIFF_SCALAR_H AutoDiffScalar(const Scalar &value, int nbDer, int derNumber)
Constructs an active scalar from its value, and initializes the nbDer derivatives such that it corres...
Definition: AutoDiffScalar.h:88
A scalar type replacement with automatic differentation capability.
Definition: AutoDiffScalar.h:33
AutoDiffScalar()
Default constructor without any initialization.
Definition: AutoDiffScalar.h:84
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:85
Holds information about the various numeric (i.e.
Definition: NumTraits.h:150
Definition: AutoDiffScalar.h:18
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:77
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void resize(Index rows, Index cols)
Resizes *this to a rows x cols matrix.
Definition: PlainObjectBase.h:273
Definition: AutoDiffScalar.h:29
AutoDiffScalar(const Real &value)
Conversion from a scalar constant to an active scalar.
Definition: AutoDiffScalar.h:96
EIGEN_DEVICE_FUNC Derived & setZero(Index size)
Resizes to the given size, and sets all coefficients in this expression to zero.
Definition: CwiseNullaryOp.h:515
Definition: BandTriangularSolver.h:13
Definition: BinaryFunctors.h:329
Determines whether the given binary operation of two numeric types is allowed and what the scalar ret...
Definition: XprHelper.h:757
const int Dynamic
This value means that a positive quantity (e.g., a size) is not known at compile-time, and that instead the value is stored in some runtime variable.
Definition: Constants.h:21
AutoDiffScalar(const Scalar &value, const DerType &der)
Constructs an active scalar from its value and derivatives der.
Definition: AutoDiffScalar.h:104
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:178
Definition: ForwardDeclarations.h:17