NeoPZ
fadfunc.h
Go to the documentation of this file.
1 // Emacs will be in -*- Mode: c++ -*-
2 //
3 // ************ DO NOT REMOVE THIS BANNER ****************
4 //
5 // Nicolas Di Cesare <Nicolas.Dicesare@ann.jussieu.fr>
6 // http://www.ann.jussieu.fr/~dicesare
7 //
8 // CEMRACS 98 : C++ courses,
9 // templates : new C++ techniques
10 // for scientific computing
11 //
12 //********************************************************
13 //
14 // A short implementation ( not all operators and
15 // functions are overloaded ) of 1st order Automatic
16 // Differentiation in forward mode (FAD) using
17 // EXPRESSION TEMPLATES.
18 //
19 //********************************************************
20 #ifndef _fadfunc_h_
21 #define _fadfunc_h_
22 
23 
24 #define FAD_FUNC_MACRO(NAME,FCT,FCTSTD,GRD,FASTGRD) \
25 template <class Expr> class NAME \
26 { \
27 public: \
28  typedef typename Expr::value_type value_type; \
29 protected: \
30  NAME () {} \
31  \
32  Expr expr_; \
33 public: \
34  NAME (const Expr & expr) : expr_(expr) {;} \
35  inline value_type val() const { return FCTSTD(expr_.val());} \
36  inline value_type dx(int i) const {return GRD ; } \
37  inline int size() const { return expr_.size();} \
38  \
39  bool hasFastAccess() const { return expr_.hasFastAccess();} \
40  value_type fastAccessDx(int i) const { return FASTGRD;} \
41 }; \
42  \
43 template <class Expr> inline FadExpr< NAME< FadExpr<Expr> > > \
44 FCT (const FadExpr<Expr>& expr) \
45 { \
46  typedef NAME< FadExpr<Expr> > expr_t; \
47  return FadExpr< expr_t >( expr_t(expr) ); \
48 } \
49  \
50 template <class T> inline FadExpr< NAME< Fad<T> > > \
51 FCT (const Fad<T>& x) \
52 { \
53  typedef NAME< Fad<T> > expr_t; \
54  return FadExpr< expr_t >( expr_t(x) ); \
55 }
56 
57 FAD_FUNC_MACRO(FadFuncCos,
58  cos,
59  std::cos,
60  -expr_.dx(i)*std::sin( expr_.val() ),
61  -expr_.fastAccessDx(i)*std::sin( expr_.val() ) )
62 FAD_FUNC_MACRO(FadFuncSin,
63  sin,
64  std::sin,
65  expr_.dx(i)*std::cos(expr_.val()),
66  expr_.fastAccessDx(i)*std::cos(expr_.val()) )
67 FAD_FUNC_MACRO(FadFuncTan,
68  tan,
69  std::tan,
70  expr_.dx(i)*(1.+std::tan(expr_.val())*std::tan(expr_.val())),
71  expr_.fastAccessDx(i)*(1.+std::tan(expr_.val())*std::tan(expr_.val())))
72 FAD_FUNC_MACRO(FadFuncAcos,
73  acos,
74  std::acos,
75  -expr_.dx(i)/std::sqrt(1.-expr_.val()*expr_.val()),
76  -expr_.fastAccessDx(i)/std::sqrt(1.-expr_.val()*expr_.val()))
77 FAD_FUNC_MACRO(FadFuncAsin,
78  asin,
79  std::asin,
80  expr_.dx(i)/std::sqrt(1.-expr_.val()*expr_.val()),
81  expr_.fastAccessDx(i)/std::sqrt(1.-expr_.val()*expr_.val()))
82 FAD_FUNC_MACRO(FadFuncAtan,
83  atan,
84  std::atan,
85  expr_.dx(i)/(1.+expr_.val()*expr_.val()),
86  expr_.fastAccessDx(i)/(1.+expr_.val()*expr_.val()))
87 FAD_FUNC_MACRO(FadFuncCosh,
88  cosh,
89  cosh,
90  expr_.dx(i)*std::sinh(expr_.val()),
91  expr_.fastAccessDx(i)*std::sinh(expr_.val()))
92 FAD_FUNC_MACRO(FadFuncSinh,
93  sinh,
94  sinh,
95  expr_.dx(i)*std::cosh(expr_.val()),
96  expr_.fastAccessDx(i)*std::cosh(expr_.val()))
97 FAD_FUNC_MACRO(FadFuncTanh,
98  tanh,
99  tanh,
100  expr_.dx(i)/(std::cosh(expr_.val())*std::cosh(expr_.val())),
101  expr_.fastAccessDx(i)/(std::cosh(expr_.val())*std::cosh(expr_.val())))
102 FAD_FUNC_MACRO(FadFuncAcosh,
103  acosh,
104  acosh,
105  expr_.dx(i)/std::sqrt((expr_.val()-1.)/(expr_.val()+1.)),
106  expr_.fastAccessDx(i)/std::sqrt((expr_.val()-1.)/(expr_.val()+1.)))
107 FAD_FUNC_MACRO(FadFuncAsinh,
108  asinh,
109  asinh,
110  expr_.dx(i)/std::sqrt(1.+expr_.val()*expr_.val()),
111  expr_.fastAccessDx(i)/std::sqrt(1.+expr_.val()*expr_.val()))
112 FAD_FUNC_MACRO(FadFuncAtanh,
113  atanh,
114  atanh,
115  expr_.dx(i)/(1.-expr_.val()*expr_.val()),
116  expr_.fastAccessDx(i)/(1.-expr_.val()*expr_.val()))
117 FAD_FUNC_MACRO(FadFuncSqrt,
118  sqrt,
119  std::sqrt,
120  expr_.dx(i)/(2.*std::sqrt(expr_.val())),
121  expr_.fastAccessDx(i)/(2.*std::sqrt(expr_.val())))
122 FAD_FUNC_MACRO(FadFuncExp,
123  exp,
124  std::exp,
125  expr_.dx(i)*std::exp(expr_.val()),
126  expr_.fastAccessDx(i)*std::exp(expr_.val()))
127 FAD_FUNC_MACRO(FadFuncLog,
128  log,
129  log,
130  expr_.dx(i)/expr_.val(),
131  expr_.fastAccessDx(i)/expr_.val())
132 FAD_FUNC_MACRO(FadFuncLog10,
133  log10,
134  std::log10,
135  expr_.dx(i)/(std::log(value_type(10))*expr_.val()),
136  expr_.fastAccessDx(i)/(std::log(value_type(10))*expr_.val()))
137 FAD_FUNC_MACRO(FadFuncAbs,
138  abs,
139  std::abs,
140  std::abs(expr_.dx(i)),
141  std::abs(expr_.fastAccessDx(i)))
142 
143 
144 #undef FAD_FUNC_MACRO
145 
146 
147 
148 #endif
expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ tanh
Definition: tfadfunc.h:100
TinyFad< 8, T > abs(const TinyFad< 8, T > &in)
Definition: tinyfadeight.h:846
REAL val(STATE &number)
Returns value of the variable.
Definition: pzartdiff.h:23
#define FAD_FUNC_MACRO(NAME, FCT, FCTSTD, GRD, FASTGRD)
Definition: fadfunc.h:24
expr_ expr_ expr_ expr_ acos
Definition: tfadfunc.h:75
expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ acosh
Definition: tfadfunc.h:105
sin
Definition: fadfunc.h:63
expr_ dx(i) *cos(expr_.val())
expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ cosh
Definition: tfadfunc.h:90
expr_ expr_ tan
Definition: tfadfunc.h:70
expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ sqrt
Definition: tfadfunc.h:120
expr_ expr_ fastAccessDx(i) *cos(expr_.val())) FAD_FUNC_MACRO(TFadFuncTan
expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ atanh
Definition: tfadfunc.h:115
expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ log
Definition: tfadfunc.h:130
expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ sinh
Definition: tfadfunc.h:95
expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ atan
Definition: tfadfunc.h:85
expr_ expr_ expr_ expr_ expr_ expr_ asin
Definition: tfadfunc.h:80
expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ log10
Definition: tfadfunc.h:135
expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ exp
Definition: tfadfunc.h:125
expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ asinh
Definition: tfadfunc.h:110
TPZFlopCounter cos(const TPZFlopCounter &orig)
Returns the cosine in radians and increments the counter of the Cosine.
Definition: pzreal.h:514