61 template <
class Expr>
class ADFuncCos
64 enum { nvar = Expr::nvar };
65 typedef typename Expr::value_type value_type;
72 ADFuncCos (
const Expr & expr ) : expr_( expr )
78 template<
int isFundamental,
typename Expr_>
82 typedef typename Expr_::value_type value_type;
83 static value_type signum( value_type a )
85 if ( abs( a ) < std::numeric_limits<value_type>::epsilon() )
86 return value_type( 0 );
90 static value_type value( Expr_
const& expr_ )
92 return cos( expr_.value() );
94 static value_type grad( Expr_
const& expr_,
int __i )
96 return -expr_.grad( __i )*sin( expr_.value() );
98 static value_type
hessian( Expr_
const& expr_,
int __i,
int __j )
100 return -expr_.hessian( __i,__j )*sin( expr_.value() ) - expr_.grad( __i )*expr_.grad( __j ) * cos( expr_.value() );
103 template<
typename Expr_>
104 struct Value<true, Expr_>
106 typedef typename Expr_::value_type value_type;
107 static value_type signum( value_type a )
109 if ( std::abs( a ) < std::numeric_limits<value_type>::epsilon() )
110 return value_type( 0 );
112 return a/std::abs( a );
114 static value_type value( Expr_
const& expr_ )
116 return std::cos( expr_.value() );
118 static value_type grad( Expr_
const& expr_,
int __i )
120 return -expr_.grad( __i )*std::sin( expr_.value() );
122 static value_type
hessian( Expr_
const& expr_,
int __i,
int __j )
124 return -expr_.hessian( __i,__j )*std::sin( expr_.value() ) - expr_.grad( __i )*expr_.grad( __j ) * std::cos( expr_.value() );
128 inline value_type value()
const
130 return Value<
true,Expr>::value( expr_ );
132 inline value_type grad(
int __i )
const
134 return Value<
true,Expr>::grad( expr_, __i );
136 inline value_type
hessian(
int __i,
int __j )
const
138 return Value<
true,Expr>
::hessian( expr_, __i, __j );
140 inline bool deps(
int __i )
const
142 return expr_.deps( __i ) ;
146 template <
class Expr>
inline ADExpr< ADFuncCos< ADExpr<Expr> > >
147 cos (
const ADExpr<Expr>& expr )
149 typedef ADFuncCos< ADExpr<Expr> > expr_t;
150 return ADExpr< expr_t >( expr_t( expr ) );
153 template <
class T,
int Nvar,
int Order,
int Var>
inline ADExpr< ADFuncCos< ADType<T, Nvar, Order, Var> > >
154 cos (
const ADType<T, Nvar, Order, Var>& x )
156 typedef ADFuncCos< ADType<T, Nvar, Order, Var> > expr_t;
157 return ADExpr< expr_t >( expr_t( x ) );
190 template <
class Expr>
class ADFuncSin
193 enum { nvar = Expr::nvar };
194 typedef typename Expr::value_type value_type;
201 ADFuncSin (
const Expr & expr ) : expr_( expr )
207 template<
int isFundamental,
typename Expr_>
211 typedef typename Expr_::value_type value_type;
212 static value_type signum( value_type a )
214 if ( abs( a ) < std::numeric_limits<value_type>::epsilon() )
215 return value_type( 0 );
219 static value_type value( Expr_
const& expr_ )
221 return sin( expr_.value() );
223 static value_type grad( Expr_
const& expr_,
int __i )
225 return expr_.grad( __i )*cos( expr_.value() );
227 static value_type
hessian( Expr_
const& expr_,
int __i,
int __j )
229 return expr_.hessian( __i,__j )*cos( expr_.value() ) - expr_.grad( __i )*expr_.grad( __j ) * sin( expr_.value() );
232 template<
typename Expr_>
233 struct Value<true, Expr_>
235 typedef typename Expr_::value_type value_type;
236 static value_type signum( value_type a )
238 if ( std::abs( a ) < std::numeric_limits<value_type>::epsilon() )
239 return value_type( 0 );
241 return a/std::abs( a );
243 static value_type value( Expr_
const& expr_ )
245 return std::sin( expr_.value() );
247 static value_type grad( Expr_
const& expr_,
int __i )
249 return expr_.grad( __i )*std::cos( expr_.value() );
251 static value_type
hessian( Expr_
const& expr_,
int __i,
int __j )
253 return expr_.hessian( __i,__j )*std::cos( expr_.value() ) - expr_.grad( __i )*expr_.grad( __j ) * std::sin( expr_.value() );
257 inline value_type value()
const
259 return Value<
true,Expr>::value( expr_ );
261 inline value_type grad(
int __i )
const
263 return Value<
true,Expr>::grad( expr_, __i );
265 inline value_type
hessian(
int __i,
int __j )
const
267 return Value<
true,Expr>
::hessian( expr_, __i, __j );
269 inline bool deps(
int __i )
const
271 return expr_.deps( __i ) ;
275 template <
class Expr>
inline ADExpr< ADFuncSin< ADExpr<Expr> > >
276 sin (
const ADExpr<Expr>& expr )
278 typedef ADFuncSin< ADExpr<Expr> > expr_t;
279 return ADExpr< expr_t >( expr_t( expr ) );
282 template <
class T,
int Nvar,
int Order,
int Var>
inline ADExpr< ADFuncSin< ADType<T, Nvar, Order, Var> > >
283 sin (
const ADType<T, Nvar, Order, Var>& x )
285 typedef ADFuncSin< ADType<T, Nvar, Order, Var> > expr_t;
286 return ADExpr< expr_t >( expr_t( x ) );
319 template <
class Expr>
class ADFuncTan
322 enum { nvar = Expr::nvar };
323 typedef typename Expr::value_type value_type;
330 ADFuncTan (
const Expr & expr ) : expr_( expr )
336 template<
int isFundamental,
typename Expr_>
340 typedef typename Expr_::value_type value_type;
341 static value_type signum( value_type a )
343 if ( abs( a ) < std::numeric_limits<value_type>::epsilon() )
344 return value_type( 0 );
348 static value_type value( Expr_
const& expr_ )
350 return tan( expr_.value() );
352 static value_type grad( Expr_
const& expr_,
int __i )
354 return expr_.grad( __i )*( 1.+tan( expr_.value() )*tan( expr_.value() ) );
356 static value_type
hessian( Expr_
const& expr_,
int __i,
int __j )
358 return 2*tan( expr_.value() )*( 1+tan( expr_.value() )*tan( expr_.value() ) )*expr_.grad( __i )*expr_.grad( __j ) + ( 1+tan( expr_.value() )*tan( expr_.value() ) )*expr_.hessian( __i,__j );
361 template<
typename Expr_>
362 struct Value<true, Expr_>
364 typedef typename Expr_::value_type value_type;
365 static value_type signum( value_type a )
367 if ( std::abs( a ) < std::numeric_limits<value_type>::epsilon() )
368 return value_type( 0 );
370 return a/std::abs( a );
372 static value_type value( Expr_
const& expr_ )
374 return std::tan( expr_.value() );
376 static value_type grad( Expr_
const& expr_,
int __i )
378 return expr_.grad( __i )*( 1.+std::tan( expr_.value() )*std::tan( expr_.value() ) );
380 static value_type
hessian( Expr_
const& expr_,
int __i,
int __j )
382 return 2*std::tan( expr_.value() )*( 1+std::tan( expr_.value() )*std::tan( expr_.value() ) )*expr_.grad( __i )*expr_.grad( __j ) + ( 1+std::tan( expr_.value() )*std::tan( expr_.value() ) )*expr_.hessian( __i,__j );
386 inline value_type value()
const
388 return Value<
true,Expr>::value( expr_ );
390 inline value_type grad(
int __i )
const
392 return Value<
true,Expr>::grad( expr_, __i );
394 inline value_type
hessian(
int __i,
int __j )
const
396 return Value<
true,Expr>
::hessian( expr_, __i, __j );
398 inline bool deps(
int __i )
const
400 return expr_.deps( __i ) ;
404 template <
class Expr>
inline ADExpr< ADFuncTan< ADExpr<Expr> > >
405 tan (
const ADExpr<Expr>& expr )
407 typedef ADFuncTan< ADExpr<Expr> > expr_t;
408 return ADExpr< expr_t >( expr_t( expr ) );
411 template <
class T,
int Nvar,
int Order,
int Var>
inline ADExpr< ADFuncTan< ADType<T, Nvar, Order, Var> > >
412 tan (
const ADType<T, Nvar, Order, Var>& x )
414 typedef ADFuncTan< ADType<T, Nvar, Order, Var> > expr_t;
415 return ADExpr< expr_t >( expr_t( x ) );
448 template <
class Expr>
class ADFuncSqrt
451 enum { nvar = Expr::nvar };
452 typedef typename Expr::value_type value_type;
459 ADFuncSqrt (
const Expr & expr ) : expr_( expr )
465 template<
int isFundamental,
typename Expr_>
469 typedef typename Expr_::value_type value_type;
470 static value_type signum( value_type a )
472 if ( abs( a ) < std::numeric_limits<value_type>::epsilon() )
473 return value_type( 0 );
477 static value_type value( Expr_
const& expr_ )
479 return sqrt( expr_.value() );
481 static value_type grad( Expr_
const& expr_,
int __i )
483 return expr_.grad( __i )/( 2.*sqrt( expr_.value() ) );
485 static value_type
hessian( Expr_
const& expr_,
int __i,
int __j )
487 return expr_.hessian( __i,__j )/( 2.*sqrt( expr_.value() ) )-expr_.grad( __i )*expr_.grad( __j )/( 4.*pow( expr_.value(),1.5 ) );
490 template<
typename Expr_>
491 struct Value<true, Expr_>
493 typedef typename Expr_::value_type value_type;
494 static value_type signum( value_type a )
496 if ( std::abs( a ) < std::numeric_limits<value_type>::epsilon() )
497 return value_type( 0 );
499 return a/std::abs( a );
501 static value_type value( Expr_
const& expr_ )
503 return std::sqrt( expr_.value() );
505 static value_type grad( Expr_
const& expr_,
int __i )
507 return expr_.grad( __i )/( 2.*std::sqrt( expr_.value() ) );
509 static value_type
hessian( Expr_
const& expr_,
int __i,
int __j )
511 return expr_.hessian( __i,__j )/( 2.*std::sqrt( expr_.value() ) )-expr_.grad( __i )*expr_.grad( __j )/( 4.*std::pow( expr_.value(),1.5 ) );
515 inline value_type value()
const
517 return Value<
true,Expr>::value( expr_ );
519 inline value_type grad(
int __i )
const
521 return Value<
true,Expr>::grad( expr_, __i );
523 inline value_type
hessian(
int __i,
int __j )
const
525 return Value<
true,Expr>
::hessian( expr_, __i, __j );
527 inline bool deps(
int __i )
const
529 return expr_.deps( __i ) ;
533 template <
class Expr>
inline ADExpr< ADFuncSqrt< ADExpr<Expr> > >
534 sqrt (
const ADExpr<Expr>& expr )
536 typedef ADFuncSqrt< ADExpr<Expr> > expr_t;
537 return ADExpr< expr_t >( expr_t( expr ) );
540 template <
class T,
int Nvar,
int Order,
int Var>
inline ADExpr< ADFuncSqrt< ADType<T, Nvar, Order, Var> > >
541 sqrt (
const ADType<T, Nvar, Order, Var>& x )
543 typedef ADFuncSqrt< ADType<T, Nvar, Order, Var> > expr_t;
544 return ADExpr< expr_t >( expr_t( x ) );
577 template <
class Expr>
class AdFuncExp
580 enum { nvar = Expr::nvar };
581 typedef typename Expr::value_type value_type;
588 AdFuncExp (
const Expr & expr ) : expr_( expr )
594 template<
int isFundamental,
typename Expr_>
598 typedef typename Expr_::value_type value_type;
599 static value_type signum( value_type a )
601 if ( abs( a ) < std::numeric_limits<value_type>::epsilon() )
602 return value_type( 0 );
606 static value_type value( Expr_
const& expr_ )
608 return exp( expr_.value() );
610 static value_type grad( Expr_
const& expr_,
int __i )
612 return expr_.grad( __i )*exp( expr_.value() );
614 static value_type
hessian( Expr_
const& expr_,
int __i,
int __j )
616 return expr_.hessian( __i,__j )*exp( expr_.value() ) + expr_.grad( __i )*expr_.grad( __j )*exp( expr_.value() );
619 template<
typename Expr_>
620 struct Value<true, Expr_>
622 typedef typename Expr_::value_type value_type;
623 static value_type signum( value_type a )
625 if ( std::abs( a ) < std::numeric_limits<value_type>::epsilon() )
626 return value_type( 0 );
628 return a/std::abs( a );
630 static value_type value( Expr_
const& expr_ )
632 return std::exp( expr_.value() );
634 static value_type grad( Expr_
const& expr_,
int __i )
636 return expr_.grad( __i )*std::exp( expr_.value() );
638 static value_type
hessian( Expr_
const& expr_,
int __i,
int __j )
640 return expr_.hessian( __i,__j )*std::exp( expr_.value() ) + expr_.grad( __i )*expr_.grad( __j )*std::exp( expr_.value() );
644 inline value_type value()
const
646 return Value<
true,Expr>::value( expr_ );
648 inline value_type grad(
int __i )
const
650 return Value<
true,Expr>::grad( expr_, __i );
652 inline value_type
hessian(
int __i,
int __j )
const
654 return Value<
true,Expr>
::hessian( expr_, __i, __j );
656 inline bool deps(
int __i )
const
658 return expr_.deps( __i ) ;
662 template <
class Expr>
inline ADExpr< AdFuncExp< ADExpr<Expr> > >
663 exp (
const ADExpr<Expr>& expr )
665 typedef AdFuncExp< ADExpr<Expr> > expr_t;
666 return ADExpr< expr_t >( expr_t( expr ) );
669 template <
class T,
int Nvar,
int Order,
int Var>
inline ADExpr< AdFuncExp< ADType<T, Nvar, Order, Var> > >
670 exp (
const ADType<T, Nvar, Order, Var>& x )
672 typedef AdFuncExp< ADType<T, Nvar, Order, Var> > expr_t;
673 return ADExpr< expr_t >( expr_t( x ) );
706 template <
class Expr>
class AdFuncLog
709 enum { nvar = Expr::nvar };
710 typedef typename Expr::value_type value_type;
717 AdFuncLog (
const Expr & expr ) : expr_( expr )
723 template<
int isFundamental,
typename Expr_>
727 typedef typename Expr_::value_type value_type;
728 static value_type signum( value_type a )
730 if ( abs( a ) < std::numeric_limits<value_type>::epsilon() )
731 return value_type( 0 );
735 static value_type value( Expr_
const& expr_ )
737 return log( expr_.value() );
739 static value_type grad( Expr_
const& expr_,
int __i )
741 return expr_.grad( __i )/expr_.value();
743 static value_type
hessian( Expr_
const& expr_,
int __i,
int __j )
745 return expr_.hessian( __i,__j )/expr_.value() - expr_.grad( __i )*expr_.grad( __j )/( pow( expr_.value(),2. ) );
748 template<
typename Expr_>
749 struct Value<true, Expr_>
751 typedef typename Expr_::value_type value_type;
752 static value_type signum( value_type a )
754 if ( std::abs( a ) < std::numeric_limits<value_type>::epsilon() )
755 return value_type( 0 );
757 return a/std::abs( a );
759 static value_type value( Expr_
const& expr_ )
761 return std::log( expr_.value() );
763 static value_type grad( Expr_
const& expr_,
int __i )
765 return expr_.grad( __i )/expr_.value();
767 static value_type
hessian( Expr_
const& expr_,
int __i,
int __j )
769 return expr_.hessian( __i,__j )/expr_.value() - expr_.grad( __i )*expr_.grad( __j )/( std::pow( expr_.value(),2. ) );
773 inline value_type value()
const
775 return Value<
true,Expr>::value( expr_ );
777 inline value_type grad(
int __i )
const
779 return Value<
true,Expr>::grad( expr_, __i );
781 inline value_type
hessian(
int __i,
int __j )
const
783 return Value<
true,Expr>
::hessian( expr_, __i, __j );
785 inline bool deps(
int __i )
const
787 return expr_.deps( __i ) ;
791 template <
class Expr>
inline ADExpr< AdFuncLog< ADExpr<Expr> > >
792 log (
const ADExpr<Expr>& expr )
794 typedef AdFuncLog< ADExpr<Expr> > expr_t;
795 return ADExpr< expr_t >( expr_t( expr ) );
798 template <
class T,
int Nvar,
int Order,
int Var>
inline ADExpr< AdFuncLog< ADType<T, Nvar, Order, Var> > >
799 log (
const ADType<T, Nvar, Order, Var>& x )
801 typedef AdFuncLog< ADType<T, Nvar, Order, Var> > expr_t;
802 return ADExpr< expr_t >( expr_t( x ) );
835 template <
class Expr>
class AdFuncLog10
838 enum { nvar = Expr::nvar };
839 typedef typename Expr::value_type value_type;
846 AdFuncLog10 (
const Expr & expr ) : expr_( expr )
852 template<
int isFundamental,
typename Expr_>
856 typedef typename Expr_::value_type value_type;
857 static value_type signum( value_type a )
859 if ( abs( a ) < std::numeric_limits<value_type>::epsilon() )
860 return value_type( 0 );
864 static value_type value( Expr_
const& expr_ )
866 return log10( expr_.value() );
868 static value_type grad( Expr_
const& expr_,
int __i )
870 return expr_.grad( __i )/( log( value_type( 10 ) )*expr_.value() );
872 static value_type
hessian( Expr_
const& expr_,
int __i,
int __j )
874 return expr_.hessian( __i,__j )/( log( value_type( 10 ) )*expr_.value() ) - expr_.grad( __i )*expr_.grad( __j )/( log( value_type( 10 ) )*pow( expr_.value(),2. ) );
877 template<
typename Expr_>
878 struct Value<true, Expr_>
880 typedef typename Expr_::value_type value_type;
881 static value_type signum( value_type a )
883 if ( std::abs( a ) < std::numeric_limits<value_type>::epsilon() )
884 return value_type( 0 );
886 return a/std::abs( a );
888 static value_type value( Expr_
const& expr_ )
890 return std::log10( expr_.value() );
892 static value_type grad( Expr_
const& expr_,
int __i )
894 return expr_.grad( __i )/( std::log( value_type( 10 ) )*expr_.value() );
896 static value_type
hessian( Expr_
const& expr_,
int __i,
int __j )
898 return expr_.hessian( __i,__j )/( std::log( value_type( 10 ) )*expr_.value() ) - expr_.grad( __i )*expr_.grad( __j )/( std::log( value_type( 10 ) )*std::pow( expr_.value(),2. ) );
902 inline value_type value()
const
904 return Value<
true,Expr>::value( expr_ );
906 inline value_type grad(
int __i )
const
908 return Value<
true,Expr>::grad( expr_, __i );
910 inline value_type
hessian(
int __i,
int __j )
const
912 return Value<
true,Expr>
::hessian( expr_, __i, __j );
914 inline bool deps(
int __i )
const
916 return expr_.deps( __i ) ;
920 template <
class Expr>
inline ADExpr< AdFuncLog10< ADExpr<Expr> > >
921 log10 (
const ADExpr<Expr>& expr )
923 typedef AdFuncLog10< ADExpr<Expr> > expr_t;
924 return ADExpr< expr_t >( expr_t( expr ) );
927 template <
class T,
int Nvar,
int Order,
int Var>
inline ADExpr< AdFuncLog10< ADType<T, Nvar, Order, Var> > >
928 log10 (
const ADType<T, Nvar, Order, Var>& x )
930 typedef AdFuncLog10< ADType<T, Nvar, Order, Var> > expr_t;
931 return ADExpr< expr_t >( expr_t( x ) );
964 template <
class Expr>
class AdFuncAbs
967 enum { nvar = Expr::nvar };
968 typedef typename Expr::value_type value_type;
975 AdFuncAbs (
const Expr & expr ) : expr_( expr )
981 template<
int isFundamental,
typename Expr_>
985 typedef typename Expr_::value_type value_type;
986 static value_type signum( value_type a )
988 if ( abs( a ) < std::numeric_limits<value_type>::epsilon() )
989 return value_type( 0 );
993 static value_type value( Expr_
const& expr_ )
995 return abs( expr_.value() );
997 static value_type grad( Expr_
const& expr_,
int __i )
999 return signum( expr_.value() ) * expr_.grad( __i );
1001 static value_type
hessian( Expr_
const& expr_,
int __i,
int __j )
1003 return signum( expr_.value() ) * expr_.hessian( __i,__j );
1006 template<
typename Expr_>
1007 struct Value<true, Expr_>
1009 typedef typename Expr_::value_type value_type;
1010 static value_type signum( value_type a )
1012 if ( std::abs( a ) < std::numeric_limits<value_type>::epsilon() )
1013 return value_type( 0 );
1015 return a/std::abs( a );
1017 static value_type value( Expr_
const& expr_ )
1019 return std::abs( expr_.value() );
1021 static value_type grad( Expr_
const& expr_,
int __i )
1023 return signum( expr_.value() ) * expr_.grad( __i );
1025 static value_type
hessian( Expr_
const& expr_,
int __i,
int __j )
1027 return signum( expr_.value() ) * expr_.hessian( __i,__j );
1031 inline value_type value()
const
1033 return Value<
true,Expr>::value( expr_ );
1035 inline value_type grad(
int __i )
const
1037 return Value<
true,Expr>::grad( expr_, __i );
1039 inline value_type
hessian(
int __i,
int __j )
const
1041 return Value<
true,Expr>
::hessian( expr_, __i, __j );
1043 inline bool deps(
int __i )
const
1045 return expr_.deps( __i ) ;
1049 template <
class Expr>
inline ADExpr< AdFuncAbs< ADExpr<Expr> > >
1050 abs (
const ADExpr<Expr>& expr )
1052 typedef AdFuncAbs< ADExpr<Expr> > expr_t;
1053 return ADExpr< expr_t >( expr_t( expr ) );
1056 template <
class T,
int Nvar,
int Order,
int Var>
inline ADExpr< AdFuncAbs< ADType<T, Nvar, Order, Var> > >
1057 abs (
const ADType<T, Nvar, Order, Var>& x )
1059 typedef AdFuncAbs< ADType<T, Nvar, Order, Var> > expr_t;
1060 return ADExpr< expr_t >( expr_t( x ) );