AutoDiffScalar.h
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 #ifndef EIGEN_AUTODIFF_SCALAR_H
00011 #define EIGEN_AUTODIFF_SCALAR_H
00012 
00013 namespace Eigen {
00014 
00015 namespace internal {
00016 
00017 template<typename A, typename B>
00018 struct make_coherent_impl {
00019   static void run(A&, B&) {}
00020 };
00021 
00022 // resize a to match b is a.size()==0, and conversely.
00023 template<typename A, typename B>
00024 void make_coherent(const A& a, const B&b)
00025 {
00026   make_coherent_impl<A,B>::run(a.const_cast_derived(), b.const_cast_derived());
00027 }
00028 
00029 template<typename _DerType, bool Enable> struct auto_diff_special_op;
00030 
00031 } // end namespace internal
00032 
00033 template<typename _DerType> class AutoDiffScalar;
00034 
00035 template<typename NewDerType>
00036 inline AutoDiffScalar<NewDerType> MakeAutoDiffScalar(const typename NewDerType::Scalar& value, const NewDerType &der) {
00037   return AutoDiffScalar<NewDerType>(value,der);
00038 }
00039 
00066 template<typename _DerType>
00067 class AutoDiffScalar
00068   : public internal::auto_diff_special_op
00069             <_DerType, !internal::is_same<typename internal::traits<typename internal::remove_all<_DerType>::type>::Scalar,
00070                                           typename NumTraits<typename internal::traits<typename internal::remove_all<_DerType>::type>::Scalar>::Real>::value>
00071 {
00072   public:
00073     typedef internal::auto_diff_special_op
00074             <_DerType, !internal::is_same<typename internal::traits<typename internal::remove_all<_DerType>::type>::Scalar,
00075                        typename NumTraits<typename internal::traits<typename internal::remove_all<_DerType>::type>::Scalar>::Real>::value> Base;
00076     typedef typename internal::remove_all<_DerType>::type DerType;
00077     typedef typename internal::traits<DerType>::Scalar Scalar;
00078     typedef typename NumTraits<Scalar>::Real Real;
00079 
00080     using Base::operator+;
00081     using Base::operator*;
00082 
00084     AutoDiffScalar() {}
00085 
00088     AutoDiffScalar(const Scalar& value, int nbDer, int derNumber)
00089       : m_value(value), m_derivatives(DerType::Zero(nbDer))
00090     {
00091       m_derivatives.coeffRef(derNumber) = Scalar(1);
00092     }
00093 
00096     /*explicit*/ AutoDiffScalar(const Real& value)
00097       : m_value(value)
00098     {
00099       if(m_derivatives.size()>0)
00100         m_derivatives.setZero();
00101     }
00102 
00104     AutoDiffScalar(const Scalar& value, const DerType& der)
00105       : m_value(value), m_derivatives(der)
00106     {}
00107 
00108     template<typename OtherDerType>
00109     AutoDiffScalar(const AutoDiffScalar<OtherDerType>& other
00110 #ifndef EIGEN_PARSED_BY_DOXYGEN
00111     , typename internal::enable_if<internal::is_same<Scalar, typename internal::traits<typename internal::remove_all<OtherDerType>::type>::Scalar>::value,void*>::type = 0
00112 #endif
00113     )
00114       : m_value(other.value()), m_derivatives(other.derivatives())
00115     {}
00116 
00117     friend  std::ostream & operator << (std::ostream & s, const AutoDiffScalar& a)
00118     {
00119       return s << a.value();
00120     }
00121 
00122     AutoDiffScalar(const AutoDiffScalar& other)
00123       : m_value(other.value()), m_derivatives(other.derivatives())
00124     {}
00125 
00126     template<typename OtherDerType>
00127     inline AutoDiffScalar& operator=(const AutoDiffScalar<OtherDerType>& other)
00128     {
00129       m_value = other.value();
00130       m_derivatives = other.derivatives();
00131       return *this;
00132     }
00133 
00134     inline AutoDiffScalar& operator=(const AutoDiffScalar& other)
00135     {
00136       m_value = other.value();
00137       m_derivatives = other.derivatives();
00138       return *this;
00139     }
00140 
00141     inline AutoDiffScalar& operator=(const Scalar& other)
00142     {
00143       m_value = other;
00144       if(m_derivatives.size()>0)
00145         m_derivatives.setZero();
00146       return *this;
00147     }
00148 
00149 //     inline operator const Scalar& () const { return m_value; }
00150 //     inline operator Scalar& () { return m_value; }
00151 
00152     inline const Scalar& value() const { return m_value; }
00153     inline Scalar& value() { return m_value; }
00154 
00155     inline const DerType& derivatives() const { return m_derivatives; }
00156     inline DerType& derivatives() { return m_derivatives; }
00157 
00158     inline bool operator< (const Scalar& other) const  { return m_value <  other; }
00159     inline bool operator<=(const Scalar& other) const  { return m_value <= other; }
00160     inline bool operator> (const Scalar& other) const  { return m_value >  other; }
00161     inline bool operator>=(const Scalar& other) const  { return m_value >= other; }
00162     inline bool operator==(const Scalar& other) const  { return m_value == other; }
00163     inline bool operator!=(const Scalar& other) const  { return m_value != other; }
00164 
00165     friend inline bool operator< (const Scalar& a, const AutoDiffScalar& b) { return a <  b.value(); }
00166     friend inline bool operator<=(const Scalar& a, const AutoDiffScalar& b) { return a <= b.value(); }
00167     friend inline bool operator> (const Scalar& a, const AutoDiffScalar& b) { return a >  b.value(); }
00168     friend inline bool operator>=(const Scalar& a, const AutoDiffScalar& b) { return a >= b.value(); }
00169     friend inline bool operator==(const Scalar& a, const AutoDiffScalar& b) { return a == b.value(); }
00170     friend inline bool operator!=(const Scalar& a, const AutoDiffScalar& b) { return a != b.value(); }
00171 
00172     template<typename OtherDerType> inline bool operator< (const AutoDiffScalar<OtherDerType>& b) const  { return m_value <  b.value(); }
00173     template<typename OtherDerType> inline bool operator<=(const AutoDiffScalar<OtherDerType>& b) const  { return m_value <= b.value(); }
00174     template<typename OtherDerType> inline bool operator> (const AutoDiffScalar<OtherDerType>& b) const  { return m_value >  b.value(); }
00175     template<typename OtherDerType> inline bool operator>=(const AutoDiffScalar<OtherDerType>& b) const  { return m_value >= b.value(); }
00176     template<typename OtherDerType> inline bool operator==(const AutoDiffScalar<OtherDerType>& b) const  { return m_value == b.value(); }
00177     template<typename OtherDerType> inline bool operator!=(const AutoDiffScalar<OtherDerType>& b) const  { return m_value != b.value(); }
00178 
00179     inline const AutoDiffScalar<DerType&> operator+(const Scalar& other) const
00180     {
00181       return AutoDiffScalar<DerType&>(m_value + other, m_derivatives);
00182     }
00183 
00184     friend inline const AutoDiffScalar<DerType&> operator+(const Scalar& a, const AutoDiffScalar& b)
00185     {
00186       return AutoDiffScalar<DerType&>(a + b.value(), b.derivatives());
00187     }
00188 
00189 //     inline const AutoDiffScalar<DerType&> operator+(const Real& other) const
00190 //     {
00191 //       return AutoDiffScalar<DerType&>(m_value + other, m_derivatives);
00192 //     }
00193 
00194 //     friend inline const AutoDiffScalar<DerType&> operator+(const Real& a, const AutoDiffScalar& b)
00195 //     {
00196 //       return AutoDiffScalar<DerType&>(a + b.value(), b.derivatives());
00197 //     }
00198 
00199     inline AutoDiffScalar& operator+=(const Scalar& other)
00200     {
00201       value() += other;
00202       return *this;
00203     }
00204 
00205     template<typename OtherDerType>
00206     inline const AutoDiffScalar<CwiseBinaryOp<internal::scalar_sum_op<Scalar>,const DerType,const typename internal::remove_all<OtherDerType>::type> >
00207     operator+(const AutoDiffScalar<OtherDerType>& other) const
00208     {
00209       internal::make_coherent(m_derivatives, other.derivatives());
00210       return AutoDiffScalar<CwiseBinaryOp<internal::scalar_sum_op<Scalar>,const DerType,const typename internal::remove_all<OtherDerType>::type> >(
00211         m_value + other.value(),
00212         m_derivatives + other.derivatives());
00213     }
00214 
00215     template<typename OtherDerType>
00216     inline AutoDiffScalar&
00217     operator+=(const AutoDiffScalar<OtherDerType>& other)
00218     {
00219       (*this) = (*this) + other;
00220       return *this;
00221     }
00222 
00223     inline const AutoDiffScalar<DerType&> operator-(const Scalar& b) const
00224     {
00225       return AutoDiffScalar<DerType&>(m_value - b, m_derivatives);
00226     }
00227 
00228     friend inline const AutoDiffScalar<CwiseUnaryOp<internal::scalar_opposite_op<Scalar>, const DerType> >
00229     operator-(const Scalar& a, const AutoDiffScalar& b)
00230     {
00231       return AutoDiffScalar<CwiseUnaryOp<internal::scalar_opposite_op<Scalar>, const DerType> >
00232             (a - b.value(), -b.derivatives());
00233     }
00234 
00235     inline AutoDiffScalar& operator-=(const Scalar& other)
00236     {
00237       value() -= other;
00238       return *this;
00239     }
00240 
00241     template<typename OtherDerType>
00242     inline const AutoDiffScalar<CwiseBinaryOp<internal::scalar_difference_op<Scalar>, const DerType,const typename internal::remove_all<OtherDerType>::type> >
00243     operator-(const AutoDiffScalar<OtherDerType>& other) const
00244     {
00245       internal::make_coherent(m_derivatives, other.derivatives());
00246       return AutoDiffScalar<CwiseBinaryOp<internal::scalar_difference_op<Scalar>, const DerType,const typename internal::remove_all<OtherDerType>::type> >(
00247         m_value - other.value(),
00248         m_derivatives - other.derivatives());
00249     }
00250 
00251     template<typename OtherDerType>
00252     inline AutoDiffScalar&
00253     operator-=(const AutoDiffScalar<OtherDerType>& other)
00254     {
00255       *this = *this - other;
00256       return *this;
00257     }
00258 
00259     inline const AutoDiffScalar<CwiseUnaryOp<internal::scalar_opposite_op<Scalar>, const DerType> >
00260     operator-() const
00261     {
00262       return AutoDiffScalar<CwiseUnaryOp<internal::scalar_opposite_op<Scalar>, const DerType> >(
00263         -m_value,
00264         -m_derivatives);
00265     }
00266 
00267     inline const AutoDiffScalar<EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DerType,Scalar,product) >
00268     operator*(const Scalar& other) const
00269     {
00270       return MakeAutoDiffScalar(m_value * other, m_derivatives * other);
00271     }
00272 
00273     friend inline const AutoDiffScalar<EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DerType,Scalar,product) >
00274     operator*(const Scalar& other, const AutoDiffScalar& a)
00275     {
00276       return MakeAutoDiffScalar(a.value() * other, a.derivatives() * other);
00277     }
00278 
00279 //     inline const AutoDiffScalar<typename CwiseUnaryOp<internal::scalar_multiple_op<Real>, DerType>::Type >
00280 //     operator*(const Real& other) const
00281 //     {
00282 //       return AutoDiffScalar<typename CwiseUnaryOp<internal::scalar_multiple_op<Real>, DerType>::Type >(
00283 //         m_value * other,
00284 //         (m_derivatives * other));
00285 //     }
00286 //
00287 //     friend inline const AutoDiffScalar<typename CwiseUnaryOp<internal::scalar_multiple_op<Real>, DerType>::Type >
00288 //     operator*(const Real& other, const AutoDiffScalar& a)
00289 //     {
00290 //       return AutoDiffScalar<typename CwiseUnaryOp<internal::scalar_multiple_op<Real>, DerType>::Type >(
00291 //         a.value() * other,
00292 //         a.derivatives() * other);
00293 //     }
00294 
00295     inline const AutoDiffScalar<EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DerType,Scalar,product) >
00296     operator/(const Scalar& other) const
00297     {
00298       return MakeAutoDiffScalar(m_value / other, (m_derivatives * (Scalar(1)/other)));
00299     }
00300 
00301     friend inline const AutoDiffScalar<EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DerType,Scalar,product) >
00302     operator/(const Scalar& other, const AutoDiffScalar& a)
00303     {
00304       return MakeAutoDiffScalar(other / a.value(), a.derivatives() * (Scalar(-other) / (a.value()*a.value())));
00305     }
00306 
00307 //     inline const AutoDiffScalar<typename CwiseUnaryOp<internal::scalar_multiple_op<Real>, DerType>::Type >
00308 //     operator/(const Real& other) const
00309 //     {
00310 //       return AutoDiffScalar<typename CwiseUnaryOp<internal::scalar_multiple_op<Real>, DerType>::Type >(
00311 //         m_value / other,
00312 //         (m_derivatives * (Real(1)/other)));
00313 //     }
00314 //
00315 //     friend inline const AutoDiffScalar<typename CwiseUnaryOp<internal::scalar_multiple_op<Real>, DerType>::Type >
00316 //     operator/(const Real& other, const AutoDiffScalar& a)
00317 //     {
00318 //       return AutoDiffScalar<typename CwiseUnaryOp<internal::scalar_multiple_op<Real>, DerType>::Type >(
00319 //         other / a.value(),
00320 //         a.derivatives() * (-Real(1)/other));
00321 //     }
00322 
00323     template<typename OtherDerType>
00324     inline const AutoDiffScalar<EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(
00325         CwiseBinaryOp<internal::scalar_difference_op<Scalar> EIGEN_COMMA
00326           const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DerType,Scalar,product) EIGEN_COMMA
00327           const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(typename internal::remove_all<OtherDerType>::type,Scalar,product) >,Scalar,product) >
00328     operator/(const AutoDiffScalar<OtherDerType>& other) const
00329     {
00330       internal::make_coherent(m_derivatives, other.derivatives());
00331       return MakeAutoDiffScalar(
00332         m_value / other.value(),
00333           ((m_derivatives * other.value()) - (other.derivatives() * m_value))
00334         * (Scalar(1)/(other.value()*other.value())));
00335     }
00336 
00337     template<typename OtherDerType>
00338     inline const AutoDiffScalar<CwiseBinaryOp<internal::scalar_sum_op<Scalar>,
00339         const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DerType,Scalar,product),
00340         const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(typename internal::remove_all<OtherDerType>::type,Scalar,product) > >
00341     operator*(const AutoDiffScalar<OtherDerType>& other) const
00342     {
00343       internal::make_coherent(m_derivatives, other.derivatives());
00344       return MakeAutoDiffScalar(
00345         m_value * other.value(),
00346         (m_derivatives * other.value()) + (other.derivatives() * m_value));
00347     }
00348 
00349     inline AutoDiffScalar& operator*=(const Scalar& other)
00350     {
00351       *this = *this * other;
00352       return *this;
00353     }
00354 
00355     template<typename OtherDerType>
00356     inline AutoDiffScalar& operator*=(const AutoDiffScalar<OtherDerType>& other)
00357     {
00358       *this = *this * other;
00359       return *this;
00360     }
00361 
00362     inline AutoDiffScalar& operator/=(const Scalar& other)
00363     {
00364       *this = *this / other;
00365       return *this;
00366     }
00367 
00368     template<typename OtherDerType>
00369     inline AutoDiffScalar& operator/=(const AutoDiffScalar<OtherDerType>& other)
00370     {
00371       *this = *this / other;
00372       return *this;
00373     }
00374 
00375   protected:
00376     Scalar m_value;
00377     DerType m_derivatives;
00378 
00379 };
00380 
00381 namespace internal {
00382 
00383 template<typename _DerType>
00384 struct auto_diff_special_op<_DerType, true>
00385 //   : auto_diff_scalar_op<_DerType, typename NumTraits<Scalar>::Real,
00386 //                            is_same<Scalar,typename NumTraits<Scalar>::Real>::value>
00387 {
00388   typedef typename remove_all<_DerType>::type DerType;
00389   typedef typename traits<DerType>::Scalar Scalar;
00390   typedef typename NumTraits<Scalar>::Real Real;
00391 
00392 //   typedef auto_diff_scalar_op<_DerType, typename NumTraits<Scalar>::Real,
00393 //                            is_same<Scalar,typename NumTraits<Scalar>::Real>::value> Base;
00394 
00395 //   using Base::operator+;
00396 //   using Base::operator+=;
00397 //   using Base::operator-;
00398 //   using Base::operator-=;
00399 //   using Base::operator*;
00400 //   using Base::operator*=;
00401 
00402   const AutoDiffScalar<_DerType>& derived() const { return *static_cast<const AutoDiffScalar<_DerType>*>(this); }
00403   AutoDiffScalar<_DerType>& derived() { return *static_cast<AutoDiffScalar<_DerType>*>(this); }
00404 
00405 
00406   inline const AutoDiffScalar<DerType&> operator+(const Real& other) const
00407   {
00408     return AutoDiffScalar<DerType&>(derived().value() + other, derived().derivatives());
00409   }
00410 
00411   friend inline const AutoDiffScalar<DerType&> operator+(const Real& a, const AutoDiffScalar<_DerType>& b)
00412   {
00413     return AutoDiffScalar<DerType&>(a + b.value(), b.derivatives());
00414   }
00415 
00416   inline AutoDiffScalar<_DerType>& operator+=(const Real& other)
00417   {
00418     derived().value() += other;
00419     return derived();
00420   }
00421 
00422 
00423   inline const AutoDiffScalar<typename CwiseUnaryOp<bind2nd_op<scalar_product_op<Scalar,Real> >, DerType>::Type >
00424   operator*(const Real& other) const
00425   {
00426     return AutoDiffScalar<typename CwiseUnaryOp<bind2nd_op<scalar_product_op<Scalar,Real> >, DerType>::Type >(
00427       derived().value() * other,
00428       derived().derivatives() * other);
00429   }
00430 
00431   friend inline const AutoDiffScalar<typename CwiseUnaryOp<bind1st_op<scalar_product_op<Real,Scalar> >, DerType>::Type >
00432   operator*(const Real& other, const AutoDiffScalar<_DerType>& a)
00433   {
00434     return AutoDiffScalar<typename CwiseUnaryOp<bind1st_op<scalar_product_op<Real,Scalar> >, DerType>::Type >(
00435       a.value() * other,
00436       a.derivatives() * other);
00437   }
00438 
00439   inline AutoDiffScalar<_DerType>& operator*=(const Scalar& other)
00440   {
00441     *this = *this * other;
00442     return derived();
00443   }
00444 };
00445 
00446 template<typename _DerType>
00447 struct auto_diff_special_op<_DerType, false>
00448 {
00449   void operator*() const;
00450   void operator-() const;
00451   void operator+() const;
00452 };
00453 
00454 template<typename A_Scalar, int A_Rows, int A_Cols, int A_Options, int A_MaxRows, int A_MaxCols, typename B>
00455 struct make_coherent_impl<Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols>, B> {
00456   typedef Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols> A;
00457   static void run(A& a, B& b) {
00458     if((A_Rows==Dynamic || A_Cols==Dynamic) && (a.size()==0))
00459     {
00460       a.resize(b.size());
00461       a.setZero();
00462     }
00463   }
00464 };
00465 
00466 template<typename A, typename B_Scalar, int B_Rows, int B_Cols, int B_Options, int B_MaxRows, int B_MaxCols>
00467 struct make_coherent_impl<A, Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols> > {
00468   typedef Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols> B;
00469   static void run(A& a, B& b) {
00470     if((B_Rows==Dynamic || B_Cols==Dynamic) && (b.size()==0))
00471     {
00472       b.resize(a.size());
00473       b.setZero();
00474     }
00475   }
00476 };
00477 
00478 template<typename A_Scalar, int A_Rows, int A_Cols, int A_Options, int A_MaxRows, int A_MaxCols,
00479          typename B_Scalar, int B_Rows, int B_Cols, int B_Options, int B_MaxRows, int B_MaxCols>
00480 struct make_coherent_impl<Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols>,
00481                              Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols> > {
00482   typedef Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols> A;
00483   typedef Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols> B;
00484   static void run(A& a, B& b) {
00485     if((A_Rows==Dynamic || A_Cols==Dynamic) && (a.size()==0))
00486     {
00487       a.resize(b.size());
00488       a.setZero();
00489     }
00490     else if((B_Rows==Dynamic || B_Cols==Dynamic) && (b.size()==0))
00491     {
00492       b.resize(a.size());
00493       b.setZero();
00494     }
00495   }
00496 };
00497 
00498 } // end namespace internal
00499 
00500 template<typename DerType, typename BinOp>
00501 struct ScalarBinaryOpTraits<AutoDiffScalar<DerType>,typename DerType::Scalar,BinOp>
00502 {
00503   typedef AutoDiffScalar<DerType> ReturnType;
00504 };
00505 
00506 template<typename DerType, typename BinOp>
00507 struct ScalarBinaryOpTraits<typename DerType::Scalar,AutoDiffScalar<DerType>, BinOp>
00508 {
00509   typedef AutoDiffScalar<DerType> ReturnType;
00510 };
00511 
00512 
00513 // The following is an attempt to let Eigen's known about expression template, but that's more tricky!
00514 
00515 // template<typename DerType, typename BinOp>
00516 // struct ScalarBinaryOpTraits<AutoDiffScalar<DerType>,AutoDiffScalar<DerType>, BinOp>
00517 // {
00518 //   enum { Defined = 1 };
00519 //   typedef AutoDiffScalar<typename DerType::PlainObject> ReturnType;
00520 // };
00521 //
00522 // template<typename DerType1,typename DerType2, typename BinOp>
00523 // struct ScalarBinaryOpTraits<AutoDiffScalar<DerType1>,AutoDiffScalar<DerType2>, BinOp>
00524 // {
00525 //   enum { Defined = 1 };//internal::is_same<typename DerType1::Scalar,typename DerType2::Scalar>::value };
00526 //   typedef AutoDiffScalar<typename DerType1::PlainObject> ReturnType;
00527 // };
00528 
00529 #define EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(FUNC,CODE) \
00530   template<typename DerType> \
00531   inline const Eigen::AutoDiffScalar< \
00532   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) > \
00533   FUNC(const Eigen::AutoDiffScalar<DerType>& x) { \
00534     using namespace Eigen; \
00535     EIGEN_UNUSED typedef typename Eigen::internal::traits<typename Eigen::internal::remove_all<DerType>::type>::Scalar Scalar; \
00536     CODE; \
00537   }
00538 
00539 template<typename DerType>
00540 inline const AutoDiffScalar<DerType>& conj(const AutoDiffScalar<DerType>& x)  { return x; }
00541 template<typename DerType>
00542 inline const AutoDiffScalar<DerType>& real(const AutoDiffScalar<DerType>& x)  { return x; }
00543 template<typename DerType>
00544 inline typename DerType::Scalar imag(const AutoDiffScalar<DerType>&)    { return 0.; }
00545 template<typename DerType, typename T>
00546 inline AutoDiffScalar<typename Eigen::internal::remove_all<DerType>::type::PlainObject> (min)(const AutoDiffScalar<DerType>& x, const T& y) {
00547   typedef AutoDiffScalar<typename Eigen::internal::remove_all<DerType>::type::PlainObject> ADS;
00548   return (x <= y ? ADS(x) : ADS(y));
00549 }
00550 template<typename DerType, typename T>
00551 inline AutoDiffScalar<typename Eigen::internal::remove_all<DerType>::type::PlainObject> (max)(const AutoDiffScalar<DerType>& x, const T& y) {
00552   typedef AutoDiffScalar<typename Eigen::internal::remove_all<DerType>::type::PlainObject> ADS;
00553   return (x >= y ? ADS(x) : ADS(y));
00554 }
00555 template<typename DerType, typename T>
00556 inline AutoDiffScalar<typename Eigen::internal::remove_all<DerType>::type::PlainObject> (min)(const T& x, const AutoDiffScalar<DerType>& y) {
00557   typedef AutoDiffScalar<typename Eigen::internal::remove_all<DerType>::type::PlainObject> ADS;
00558   return (x < y ? ADS(x) : ADS(y));
00559 }
00560 template<typename DerType, typename T>
00561 inline AutoDiffScalar<typename Eigen::internal::remove_all<DerType>::type::PlainObject> (max)(const T& x, const AutoDiffScalar<DerType>& y) {
00562   typedef AutoDiffScalar<typename Eigen::internal::remove_all<DerType>::type::PlainObject> ADS;
00563   return (x > y ? ADS(x) : ADS(y));
00564 }
00565 template<typename DerType>
00566 inline AutoDiffScalar<typename Eigen::internal::remove_all<DerType>::type::PlainObject> (min)(const AutoDiffScalar<DerType>& x, const AutoDiffScalar<DerType>& y) {
00567   return (x.value() < y.value() ? x : y);
00568 }
00569 template<typename DerType>
00570 inline AutoDiffScalar<typename Eigen::internal::remove_all<DerType>::type::PlainObject> (max)(const AutoDiffScalar<DerType>& x, const AutoDiffScalar<DerType>& y) {
00571   return (x.value() >= y.value() ? x : y);
00572 }
00573 
00574 
00575 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(abs,
00576   using std::abs;
00577   return Eigen::MakeAutoDiffScalar(abs(x.value()), x.derivatives() * (x.value()<0 ? -1 : 1) );)
00578 
00579 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(abs2,
00580   using numext::abs2;
00581   return Eigen::MakeAutoDiffScalar(abs2(x.value()), x.derivatives() * (Scalar(2)*x.value()));)
00582 
00583 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(sqrt,
00584   using std::sqrt;
00585   Scalar sqrtx = sqrt(x.value());
00586   return Eigen::MakeAutoDiffScalar(sqrtx,x.derivatives() * (Scalar(0.5) / sqrtx));)
00587 
00588 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(cos,
00589   using std::cos;
00590   using std::sin;
00591   return Eigen::MakeAutoDiffScalar(cos(x.value()), x.derivatives() * (-sin(x.value())));)
00592 
00593 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(sin,
00594   using std::sin;
00595   using std::cos;
00596   return Eigen::MakeAutoDiffScalar(sin(x.value()),x.derivatives() * cos(x.value()));)
00597 
00598 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(exp,
00599   using std::exp;
00600   Scalar expx = exp(x.value());
00601   return Eigen::MakeAutoDiffScalar(expx,x.derivatives() * expx);)
00602 
00603 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(log,
00604   using std::log;
00605   return Eigen::MakeAutoDiffScalar(log(x.value()),x.derivatives() * (Scalar(1)/x.value()));)
00606 
00607 template<typename DerType>
00608 inline const Eigen::AutoDiffScalar<
00609 EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(typename internal::remove_all<DerType>::type,typename internal::traits<typename internal::remove_all<DerType>::type>::Scalar,product) >
00610 pow(const Eigen::AutoDiffScalar<DerType> &x, const typename internal::traits<typename internal::remove_all<DerType>::type>::Scalar &y)
00611 {
00612   using namespace Eigen;
00613   using std::pow;
00614   return Eigen::MakeAutoDiffScalar(pow(x.value(),y), x.derivatives() * (y * pow(x.value(),y-1)));
00615 }
00616 
00617 
00618 template<typename DerTypeA,typename DerTypeB>
00619 inline const AutoDiffScalar<Matrix<typename internal::traits<typename internal::remove_all<DerTypeA>::type>::Scalar,Dynamic,1> >
00620 atan2(const AutoDiffScalar<DerTypeA>& a, const AutoDiffScalar<DerTypeB>& b)
00621 {
00622   using std::atan2;
00623   typedef typename internal::traits<typename internal::remove_all<DerTypeA>::type>::Scalar Scalar;
00624   typedef AutoDiffScalar<Matrix<Scalar,Dynamic,1> > PlainADS;
00625   PlainADS ret;
00626   ret.value() = atan2(a.value(), b.value());
00627   
00628   Scalar squared_hypot = a.value() * a.value() + b.value() * b.value();
00629   
00630   // if (squared_hypot==0) the derivation is undefined and the following results in a NaN:
00631   ret.derivatives() = (a.derivatives() * b.value() - a.value() * b.derivatives()) / squared_hypot;
00632 
00633   return ret;
00634 }
00635 
00636 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(tan,
00637   using std::tan;
00638   using std::cos;
00639   return Eigen::MakeAutoDiffScalar(tan(x.value()),x.derivatives() * (Scalar(1)/numext::abs2(cos(x.value()))));)
00640 
00641 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(asin,
00642   using std::sqrt;
00643   using std::asin;
00644   return Eigen::MakeAutoDiffScalar(asin(x.value()),x.derivatives() * (Scalar(1)/sqrt(1-numext::abs2(x.value()))));)
00645   
00646 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(acos,
00647   using std::sqrt;
00648   using std::acos;
00649   return Eigen::MakeAutoDiffScalar(acos(x.value()),x.derivatives() * (Scalar(-1)/sqrt(1-numext::abs2(x.value()))));)
00650 
00651 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(tanh,
00652   using std::cosh;
00653   using std::tanh;
00654   return Eigen::MakeAutoDiffScalar(tanh(x.value()),x.derivatives() * (Scalar(1)/numext::abs2(cosh(x.value()))));)
00655 
00656 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(sinh,
00657   using std::sinh;
00658   using std::cosh;
00659   return Eigen::MakeAutoDiffScalar(sinh(x.value()),x.derivatives() * cosh(x.value()));)
00660 
00661 EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY(cosh,
00662   using std::sinh;
00663   using std::cosh;
00664   return Eigen::MakeAutoDiffScalar(cosh(x.value()),x.derivatives() * sinh(x.value()));)
00665 
00666 #undef EIGEN_AUTODIFF_DECLARE_GLOBAL_UNARY
00667 
00668 template<typename DerType> struct NumTraits<AutoDiffScalar<DerType> >
00669   : NumTraits< typename NumTraits<typename internal::remove_all<DerType>::type::Scalar>::Real >
00670 {
00671   typedef typename internal::remove_all<DerType>::type DerTypeCleaned;
00672   typedef AutoDiffScalar<Matrix<typename NumTraits<typename DerTypeCleaned::Scalar>::Real,DerTypeCleaned::RowsAtCompileTime,DerTypeCleaned::ColsAtCompileTime,
00673                                 0, DerTypeCleaned::MaxRowsAtCompileTime, DerTypeCleaned::MaxColsAtCompileTime> > Real;
00674   typedef AutoDiffScalar<DerType> NonInteger;
00675   typedef AutoDiffScalar<DerType> Nested;
00676   typedef typename NumTraits<typename DerTypeCleaned::Scalar>::Literal Literal;
00677   enum{
00678     RequireInitialization = 1
00679   };
00680 };
00681 
00682 }
00683 
00684 #endif // EIGEN_AUTODIFF_SCALAR_H
 All Classes Functions Variables Typedefs Enumerator