13 #ifndef EIGEN_AUTODIFF_SCALAR_H_ 14 #define EIGEN_AUTODIFF_SCALAR_H_ 23 #include <Eigen/SparseCore> 29 template <
typename A,
typename B>
36 template <
typename A,
typename B>
42 template <
typename _DerType,
bool Enable>
47 template <
typename _DerType>
50 template <
typename NewDerType>
80 template <
typename _DerType>
82 :
public internal::auto_diff_special_op<_DerType, !internal::is_same<typename internal::traits<typename internal::remove_all<_DerType>::type>::Scalar,
83 typename NumTraits<typename internal::traits<typename internal::remove_all<_DerType>::type>::Scalar>::Real>::value>
87 typename NumTraits<typename internal::traits<typename internal::remove_all<_DerType>::type>::Scalar>::Real>::value>
89 typedef typename internal::remove_all<_DerType>::type
DerType;
90 typedef typename internal::traits<DerType>::Scalar
Scalar;
91 typedef typename NumTraits<Scalar>::Real
Real;
93 using Base::operator+;
94 using Base::operator*;
101 : m_value(value), m_derivatives(DerType::Zero(nbDer))
103 m_derivatives.coeffRef(derNumber) = Scalar(1);
111 if (m_derivatives.size() > 0)
112 m_derivatives.setZero();
117 : m_value(value), m_derivatives(der)
121 template <
typename OtherDerType>
123 #ifndef EIGEN_PARSED_BY_DOXYGEN
125 typename internal::enable_if<
126 internal::is_same<Scalar,
typename internal::traits<
typename internal::remove_all<OtherDerType>::type>::Scalar>::value && internal::is_convertible<OtherDerType, DerType>::value,
void*>::type = 0
129 : m_value(other.value()), m_derivatives(other.derivatives())
135 return s << a.
value();
139 : m_value(other.value()), m_derivatives(other.derivatives())
143 template <
typename OtherDerType>
146 m_value = other.
value();
153 m_value = other.
value();
161 if (m_derivatives.size() > 0)
162 m_derivatives.setZero();
169 inline const Scalar&
value()
const {
return m_value; }
170 inline Scalar&
value() {
return m_value; }
171 inline const DerType&
derivatives()
const {
return m_derivatives; }
173 inline bool operator<(
const Scalar& other)
const {
return m_value < other; }
174 inline bool operator<=(
const Scalar& other)
const {
return m_value <= other; }
175 inline bool operator>(
const Scalar& other)
const {
return m_value > other; }
176 inline bool operator>=(
const Scalar& other)
const {
return m_value >= other; }
177 inline bool operator==(
const Scalar& other)
const {
return m_value == other; }
178 inline bool operator!=(
const Scalar& other)
const {
return m_value != other; }
185 template <
typename OtherDerType>
186 inline bool operator<(const AutoDiffScalar<OtherDerType>& b)
const 188 return m_value < b.value();
190 template <
typename OtherDerType>
191 inline bool operator<=(const AutoDiffScalar<OtherDerType>& b)
const 193 return m_value <= b.value();
195 template <
typename OtherDerType>
198 return m_value > b.
value();
200 template <
typename OtherDerType>
203 return m_value >= b.
value();
205 template <
typename OtherDerType>
208 return m_value == b.
value();
210 template <
typename OtherDerType>
213 return m_value != b.
value();
242 template <
typename OtherDerType>
248 m_value + other.
value(),
252 template <
typename OtherDerType>
256 (*this) = (*this) + other;
268 return AutoDiffScalar<CwiseUnaryOp<internal::scalar_opposite_op<Scalar>,
const DerType>>(a - b.
value(), -b.
derivatives());
277 template <
typename OtherDerType>
283 m_value - other.
value(),
287 template <
typename OtherDerType>
291 this = *
this - other;
295 inline const AutoDiffScalar<CwiseUnaryOp<internal::scalar_opposite_op<Scalar>,
const DerType>>
298 return AutoDiffScalar<CwiseUnaryOp<internal::scalar_opposite_op<Scalar>,
const DerType>>(
359 template <
typename OtherDerType>
360 inline const AutoDiffScalar<EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(
361 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(
typename internal::remove_all<OtherDerType>::type, Scalar, product)>, Scalar, product)>
366 m_value / other.
value(),
370 template <
typename OtherDerType>
372 const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DerType, Scalar, product),
373 const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(
typename internal::remove_all<OtherDerType>::type, Scalar, product)>>
378 m_value * other.
value(),
384 this = *
this * other;
388 template <
typename OtherDerType>
391 this = *
this * other;
397 this = *
this / other;
401 template <
typename OtherDerType>
404 this = *
this / other;
415 template <
typename _DerType>
420 typedef typename remove_all<_DerType>::type
DerType;
421 typedef typename traits<DerType>::Scalar
Scalar;
422 typedef typename NumTraits<Scalar>::Real
Real;
448 derived().value() += other;
456 derived().value() * other,
457 derived().derivatives() * other);
463 return AutoDiffScalar<typename CwiseUnaryOp<bind1st_op<scalar_product_op<Real, Scalar>>, DerType>
::Type>(
470 this = *
this * other;
475 template <
typename _DerType>
478 void operator*()
const;
479 void operator-()
const;
480 void operator+()
const;
483 template <
typename A_Scalar,
int A_Rows,
int A_Cols,
int A_Options,
int A_MaxRows,
int A_MaxCols,
typename B>
486 typedef Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols>
A;
487 static void run(A& a, B& b)
489 if ((A_Rows == Dynamic || A_Cols == Dynamic) && (a.size() == 0))
494 eigen_assert(a.size() == b.size() &&
"Eigen::AutoDiffScalar: derivatives are not compatible.");
498 template <
typename A,
typename B_Scalar,
int B_Rows,
int B_Cols,
int B_Options,
int B_MaxRows,
int B_MaxCols>
501 typedef Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols>
B;
504 if ((B_Rows == Dynamic || B_Cols == Dynamic) && (b.size() == 0))
509 eigen_assert(a.size() == b.size() &&
"Eigen::AutoDiffScalar: derivatives are not compatible.");
513 template <
typename A_Scalar,
int A_Rows,
int A_Cols,
int A_Options,
int A_MaxRows,
int A_MaxCols,
514 typename B_Scalar,
int B_Rows,
int B_Cols,
int B_Options,
int B_MaxRows,
int B_MaxCols>
516 Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols>>
518 typedef Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols>
A;
519 typedef Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols>
B;
520 static void run(A& a, B& b)
522 if ((A_Rows == Dynamic || A_Cols == Dynamic) && (a.size() == 0))
527 else if ((B_Rows == Dynamic || B_Cols == Dynamic) && (b.size() == 0))
532 eigen_assert(a.size() == b.size() &&
"Eigen::AutoDiffScalar: derivatives are not compatible.");
536 template <
typename A_Scalar,
int A_Options,
typename A_Index,
typename B>
539 typedef SparseVector<A_Scalar, A_Options, A_Index>
A;
540 static void run(A& a, B& b)
546 eigen_assert(a.size() == b.size() &&
"Eigen::AutoDiffScalar: derivatives are not compatible.");
550 template <
typename A,
typename B_Scalar,
int B_Options,
typename B_Index>
553 typedef SparseVector<B_Scalar, B_Options, B_Index>
B;
560 eigen_assert(a.size() == b.size() &&
"Eigen::AutoDiffScalar: derivatives are not compatible.");
564 template <
typename A_Scalar,
int A_Options,
typename A_Index,
565 typename B_Scalar,
int B_Options,
typename B_Index>
567 SparseVector<B_Scalar, B_Options, B_Index>>
569 typedef SparseVector<A_Scalar, A_Options, A_Index>
A;
570 typedef SparseVector<B_Scalar, B_Options, B_Index>
B;
571 static void run(A& a, B& b)
577 else if (b.size() == 0)
581 eigen_assert(a.size() == b.size() &&
"Eigen::AutoDiffScalar: derivatives are not compatible.");
587 template <
typename DerType,
typename BinOp>
588 struct ScalarBinaryOpTraits<
AutoDiffScalar<DerType>, typename DerType::Scalar, BinOp>
593 template <
typename DerType,
typename BinOp>
594 struct ScalarBinaryOpTraits<typename DerType::Scalar,
AutoDiffScalar<DerType>, BinOp>
615 #define EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(FUNC, CODE) \ 616 template <typename DerType> \ 617 inline const Eigen::AutoDiffScalar< \ 618 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)> \ 619 FUNC(const Eigen::AutoDiffScalar<DerType>& x) \ 621 using namespace Eigen; \ 622 typedef typename Eigen::internal::traits<typename Eigen::internal::remove_all<DerType>::type>::Scalar Scalar; \ 623 EIGEN_UNUSED_VARIABLE(sizeof(Scalar)); \ 627 template <
typename DerType>
632 template <
typename DerType>
637 template <
typename DerType>
642 template <
typename DerType,
typename T>
646 return (x <= y ? ADS(x) : ADS(y));
648 template <
typename DerType,
typename T>
652 return (x >= y ? ADS(x) : ADS(y));
654 template <
typename DerType,
typename T>
658 return (x < y ? ADS(x) : ADS(y));
660 template <
typename DerType,
typename T>
664 return (x > y ? ADS(x) : ADS(y));
666 template <
typename DerType>
671 template <
typename DerType>
709 template <typename DerType>
711 EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(
typename internal::remove_all<DerType>::type,
typename internal::traits<
typename internal::remove_all<DerType>::type>::Scalar, product)>
714 using namespace Eigen;
719 template <
typename DerTypeA,
typename DerTypeB>
724 typedef typename internal::traits<typename internal::remove_all<DerTypeA>::type>::Scalar Scalar;
737 template <
typename DerTypeA,
typename DerTypeB>
742 typedef typename internal::traits<typename internal::remove_all<DerTypeA>::type>::Scalar Scalar;
787 template <typename DerType>
789 : NumTraits<
typename NumTraits<
typename internal::remove_all<DerType>::type::Scalar>::
Real>
793 0, DerTypeCleaned::MaxRowsAtCompileTime, DerTypeCleaned::MaxColsAtCompileTime>>
797 typedef typename NumTraits<typename DerTypeCleaned::Scalar>::Literal
Literal;
800 RequireInitialization = 1
804 template <
typename DerType_>
806 : NumTraits<typename NumTraits<typename internal::remove_all<SparseVector<DerType_>>::type::Scalar>::Real>
808 typedef typename internal::remove_all<SparseVector<DerType_>>::type
DerTypeCleaned;
812 typedef typename NumTraits<typename DerTypeCleaned::Scalar>::Literal
Literal;
815 RequireInitialization = 1
822 template <
typename T>
824 :
public numeric_limits<typename T::Scalar>
828 template <
typename T>
830 :
public numeric_limits<typename T::Scalar>
835 #endif // EIGEN_AUTODIFF_SCALAR_H_ bool operator==(const Scalar &other) const
static void run(A &a, B &b)
AutoDiffScalar(const Scalar &value, int nbDer, int derNumber)
static void run(A &a, B &b)
friend const AutoDiffScalar< CwiseUnaryOp< internal::scalar_opposite_op< Scalar >, const DerType > > operator-(const Scalar &a, const AutoDiffScalar &b)
const AutoDiffScalar< CwiseBinaryOp< internal::scalar_sum_op< Scalar >, const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DerType, Scalar, product), const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(typename internal::remove_all< OtherDerType >::type, Scalar, product)> > operator*(const AutoDiffScalar< OtherDerType > &other) const
AutoDiffScalar & operator*=(const AutoDiffScalar< OtherDerType > &other)
const AutoDiffScalar< DerType > & conj(const AutoDiffScalar< DerType > &x)
internal::remove_all< _DerType >::type DerType
internal::auto_diff_special_op< _DerType,!internal::is_same< typename internal::traits< typename internal::remove_all< _DerType >::type >::Scalar, typename NumTraits< typename internal::traits< typename internal::remove_all< _DerType >::type >::Scalar >::Real >::value > Base
AutoDiffScalar< DerType > ReturnType
AutoDiffScalar< SparseVector< DerType_ > > Nested
EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(abs, using std::abs;return Eigen::MakeAutoDiffScalar(abs(x.value()), x.derivatives()*(x.value()< 0?-1:1));) EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(abs2
Matrix< A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols > A
internal::remove_all< SparseVector< DerType_ > >::type DerTypeCleaned
const AutoDiffScalar< typename CwiseUnaryOp< bind2nd_op< scalar_product_op< Scalar, Real > >, DerType >::Type > operator*(const Real &other) const
friend bool operator<(const Scalar &a, const AutoDiffScalar &b)
INLINE Rall1d< T, V, S > log(const Rall1d< T, V, S > &arg)
AutoDiffScalar & operator+=(const AutoDiffScalar< OtherDerType > &other)
void make_coherent(const A &a, const B &b)
Eigen::internal::make_coherent_impl< Matrix< A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols >, Matrix< B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols > >::run static void run(A &a, B &b)
AutoDiffScalar(const AutoDiffScalar< OtherDerType > &other, typename internal::enable_if< internal::is_same< Scalar, typename internal::traits< typename internal::remove_all< OtherDerType >::type >::Scalar >::value &&internal::is_convertible< OtherDerType, DerType >::value, void * >::type=0)
INLINE Rall1d< T, V, S > cosh(const Rall1d< T, V, S > &arg)
DerType::Scalar imag(const AutoDiffScalar< DerType > &)
const AutoDiffScalar< CwiseBinaryOp< internal::scalar_difference_op< Scalar >, const DerType, const typename internal::remove_all< OtherDerType >::type > > operator-(const AutoDiffScalar< OtherDerType > &other) const
AutoDiffScalar< _DerType > & operator+=(const Real &other)
const AutoDiffScalar< DerType & > operator+(const Real &other) const
AutoDiffScalar< SparseVector< typename NumTraits< typename DerTypeCleaned::Scalar >::Real, DerTypeCleaned::Options, typename DerTypeCleaned::StorageIndex > > Real
const AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DerType, Scalar, product)> operator/(const Scalar &other) const
AutoDiffScalar & operator-=(const AutoDiffScalar< OtherDerType > &other)
INLINE Rall1d< T, V, S > sinh(const Rall1d< T, V, S > &arg)
friend bool operator!=(const Scalar &a, const AutoDiffScalar &b)
bool operator<(const Scalar &other) const
const AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DerType, Scalar, product)> operator*(const Scalar &other) const
const AutoDiffScalar< DerType & > operator+(const Scalar &other) const
traits< DerType >::Scalar Scalar
friend const AutoDiffScalar< DerType & > operator+(const Real &a, const AutoDiffScalar< _DerType > &b)
INLINE Rall1d< T, V, S > tanh(const Rall1d< T, V, S > &arg)
AutoDiffScalar & operator/=(const Scalar &other)
NumTraits< Scalar >::Real Real
AutoDiffScalar & operator=(const AutoDiffScalar &other)
Eigen::internal::make_coherent_impl< Matrix< A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols >, Matrix< B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols > >::A Matrix< A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols > A
SparseVector< A_Scalar, A_Options, A_Index > A
Eigen::internal::make_coherent_impl< Matrix< A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols >, Matrix< B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols > >::B Matrix< B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols > B
AutoDiffScalar< DerType > ReturnType
const AutoDiffScalar< DerType & > operator-(const Scalar &b) const
const 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(typename internal::remove_all< OtherDerType >::type, Scalar, product)>, Scalar, product)> operator/(const AutoDiffScalar< OtherDerType > &other) const
AutoDiffScalar< Matrix< typename NumTraits< typename DerTypeCleaned::Scalar >::Real, DerTypeCleaned::RowsAtCompileTime, DerTypeCleaned::ColsAtCompileTime, 0, DerTypeCleaned::MaxRowsAtCompileTime, DerTypeCleaned::MaxColsAtCompileTime > > Real
static void run(A &, B &)
remove_all< _DerType >::type DerType
bool operator<=(const Scalar &other) const
INLINE Rall1d< T, V, S > asin(const Rall1d< T, V, S > &x)
friend bool operator>(const Scalar &a, const AutoDiffScalar &b)
internal::traits< DerType >::Scalar Scalar
AutoDiffScalar(const Real &value)
AutoDiffScalar< _DerType > & operator*=(const Scalar &other)
bool operator!=(const AutoDiffScalar< OtherDerType > &b) const
static void run(A &a, B &b)
friend bool operator<=(const Scalar &a, const AutoDiffScalar &b)
AutoDiffScalar< NewDerType > MakeAutoDiffScalar(const typename NewDerType::Scalar &value, const NewDerType &der)
double min(double a, double b)
NumTraits< Scalar >::Real Real
INLINE Rall1d< T, V, S > sqrt(const Rall1d< T, V, S > &arg)
AutoDiffScalar & operator=(const Scalar &other)
NumTraits< typename DerTypeCleaned::Scalar >::Literal Literal
const AutoDiffScalar< Matrix< typename internal::traits< typename internal::remove_all< DerTypeA >::type >::Scalar, Dynamic, 1 > > atan2(const AutoDiffScalar< DerTypeA > &a, const AutoDiffScalar< DerTypeB > &b)
const AutoDiffScalar< _DerType > & derived() const
friend const AutoDiffScalar< DerType & > operator+(const Scalar &a, const AutoDiffScalar &b)
INLINE Rall1d< T, V, S > exp(const Rall1d< T, V, S > &arg)
const AutoDiffScalar< CwiseUnaryOp< internal::scalar_opposite_op< Scalar >, const DerType > > operator-() const
bool operator>=(const Scalar &other) const
INLINE Rall1d< T, V, S > pow(const Rall1d< T, V, S > &arg, double m)
AutoDiffScalar & operator=(const AutoDiffScalar< OtherDerType > &other)
friend bool operator==(const Scalar &a, const AutoDiffScalar &b)
INLINE Rall1d< T, V, S > acos(const Rall1d< T, V, S > &x)
INLINE Rall1d< T, V, S > abs(const Rall1d< T, V, S > &x)
const DerType & derivatives() const
bool operator>=(const AutoDiffScalar< OtherDerType > &b) const
friend const AutoDiffScalar< typename CwiseUnaryOp< bind1st_op< scalar_product_op< Real, Scalar > >, DerType >::Type > operator*(const Real &other, const AutoDiffScalar< _DerType > &a)
const AutoDiffScalar< CwiseBinaryOp< internal::scalar_sum_op< Scalar >, const DerType, const typename internal::remove_all< OtherDerType >::type > > operator+(const AutoDiffScalar< OtherDerType > &other) const
friend const AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DerType, Scalar, product)> operator/(const Scalar &other, const AutoDiffScalar &a)
AutoDiffScalar< DerType > Nested
friend std::ostream & operator<<(std::ostream &s, const AutoDiffScalar &a)
AutoDiffScalar & operator-=(const Scalar &other)
const Scalar & value() const
internal::remove_all< DerType >::type DerTypeCleaned
NumTraits< typename DerTypeCleaned::Scalar >::Literal Literal
SparseVector< B_Scalar, B_Options, B_Index > B
bool operator==(const AutoDiffScalar< OtherDerType > &b) const
Matrix< B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols > B
INLINE Rall1d< T, V, S > cos(const Rall1d< T, V, S > &arg)
INLINE Rall1d< T, V, S > tan(const Rall1d< T, V, S > &arg)
AutoDiffScalar(const Scalar &value, const DerType &der)
static void run(A &a, B &b)
AutoDiffScalar & operator+=(const Scalar &other)
bool operator>(const Scalar &other) const
AutoDiffScalar< _DerType > & derived()
AutoDiffScalar(const AutoDiffScalar &other)
bool operator!=(const Scalar &other) const
friend const AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DerType, Scalar, product)> operator*(const Scalar &other, const AutoDiffScalar &a)
const AutoDiffScalar< DerType > & real(const AutoDiffScalar< DerType > &x)
AutoDiffScalar & operator/=(const AutoDiffScalar< OtherDerType > &other)
double max(double a, double b)
const AutoDiffScalar< SparseVector< typename internal::traits< typename internal::remove_all< DerTypeA >::type >::Scalar > > atan2(const AutoDiffScalar< DerTypeA > &a, const AutoDiffScalar< DerTypeB > &b)
friend bool operator>=(const Scalar &a, const AutoDiffScalar &b)
static void run(A &a, B &b)
SparseVector< B_Scalar, B_Options, B_Index > B
INLINE Rall1d< T, V, S > sin(const Rall1d< T, V, S > &arg)
bool operator>(const AutoDiffScalar< OtherDerType > &b) const
AutoDiffScalar & operator*=(const Scalar &other)
SparseVector< A_Scalar, A_Options, A_Index > A
AutoDiffScalar< SparseVector< DerType_ > > NonInteger
AutoDiffScalar< DerType > NonInteger