NeoPZ
tfadfunc.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 _tfadfunc_h_
21 #define _tfadfunc_h_
22 
23 // C include
24 #include <cmath>
25 
26 #define FAD_FUNC_MACRO(NAME,FCT,FCTSTD,GRD,FASTGRD) \
27 template <class Expr> class NAME \
28 { \
29 public: \
30  typedef typename Expr::value_type value_type; \
31 protected: \
32  NAME () {} \
33  \
34  Expr expr_; \
35 public: \
36  NAME (const Expr & expr) : expr_(expr) {;} \
37  inline value_type val() const { return FCTSTD(expr_.val());} \
38  inline value_type dx(int i) const {return GRD ; } \
39  inline int size() const { return expr_.size();} \
40  \
41  bool hasFastAccess() const { return expr_.hasFastAccess();} \
42  value_type fastAccessDx(int i) const { return FASTGRD;} \
43 }; \
44  \
45 template <class Expr> inline TFadExpr< NAME< TFadExpr<Expr> > > \
46 FCT (const TFadExpr<Expr>& expr) \
47 { \
48  typedef NAME< TFadExpr<Expr> > expr_t; \
49  return TFadExpr< expr_t >( expr_t(expr) ); \
50 } \
51  \
52 template <class T,int Num> inline TFadExpr< NAME< TFad<Num,T> > > \
53 FCT (const TFad<Num,T>& x) \
54 { \
55  typedef NAME< TFad<Num,T> > expr_t; \
56  return TFadExpr< expr_t >( expr_t(x) ); \
57 }
58 
59 FAD_FUNC_MACRO(TFadFuncCos,
60  cos,
61  cos,
62  -expr_.dx(i)*/*std::*/sin( expr_.val() ),
63  -expr_.fastAccessDx(i)*/*std::*/sin( expr_.val() ) )
64 FAD_FUNC_MACRO(TFadFuncSin,
65  sin,
66  sin,
67  expr_.dx(i)*/*std::*/cos(expr_.val()),
68  expr_.fastAccessDx(i)*/*std::*/cos(expr_.val()) )
69 FAD_FUNC_MACRO(TFadFuncTan,
70  tan,
71  tan,
72  expr_.dx(i)*(1.+/*std::*/tan(expr_.val())*/*std::*/tan(expr_.val())),
73  expr_.fastAccessDx(i)*(1.+/*std::*/tan(expr_.val())*/*std::*/tan(expr_.val())))
74 FAD_FUNC_MACRO(TFadFuncAcos,
76  acos,
77  -expr_.dx(i)/ /*std::*/sqrt(1.-expr_.val()*expr_.val()),
78  -expr_.fastAccessDx(i)/ /*std::*/sqrt(1.-expr_.val()*expr_.val()))
79 FAD_FUNC_MACRO(TFadFuncAsin,
81  asin,
82  expr_.dx(i)/ /*std::*/sqrt(1.-expr_.val()*expr_.val()),
83  expr_.fastAccessDx(i)/ /*std::*/sqrt(1.-expr_.val()*expr_.val()))
84 FAD_FUNC_MACRO(TFadFuncAtan,
86  atan,
87  expr_.dx(i)/(1.+expr_.val()*expr_.val()),
88  expr_.fastAccessDx(i)/(1.+expr_.val()*expr_.val()))
89 FAD_FUNC_MACRO(TFadFuncCosh,
91  cosh,
92  expr_.dx(i)* /*std::*/sinh(expr_.val()),
93  expr_.fastAccessDx(i)* /*std::*/sinh(expr_.val()))
94 FAD_FUNC_MACRO(TFadFuncSinh,
96  sinh,
97  expr_.dx(i)* /*std::*/cosh(expr_.val()),
98  expr_.fastAccessDx(i)* /*std::*/cosh(expr_.val()))
99 FAD_FUNC_MACRO(TFadFuncTanh,
101  tanh,
102  expr_.dx(i)/(/*std::*/cosh(expr_.val())*/*std::*/cosh(expr_.val())),
103  expr_.fastAccessDx(i)/(/*std::cosh*/(expr_.val())*/*std::*/cosh(expr_.val())))
104 FAD_FUNC_MACRO(TFadFuncAcosh,
106  acosh,
107  expr_.dx(i)/ /*std::*/sqrt((expr_.val()-1.)/(expr_.val()+1.)),
108  expr_.fastAccessDx(i)/ /*std::*/sqrt((expr_.val()-1.)/(expr_.val()+1.)))
109 FAD_FUNC_MACRO(TFadFuncAsinh,
111  asinh,
112  expr_.dx(i)/ /*std::*/sqrt(1.+expr_.val()*expr_.val()),
113  expr_.fastAccessDx(i)/ /*std::*/sqrt(1.+expr_.val()*expr_.val()))
114 FAD_FUNC_MACRO(TFadFuncAtanh,
116  atanh,
117  expr_.dx(i)/(1.-expr_.val()*expr_.val()),
118  expr_.fastAccessDx(i)/(1.-expr_.val()*expr_.val()))
119 FAD_FUNC_MACRO(TFadFuncSqrt,
121  sqrt,
122  expr_.dx(i)/(2.*/*std::*/sqrt(expr_.val())),
123  expr_.fastAccessDx(i)/(2.*/*std::*/sqrt(expr_.val())))
124 FAD_FUNC_MACRO(TFadFuncExp,
126  exp,
127  expr_.dx(i)*/*std::*/exp(expr_.val()),
128  expr_.fastAccessDx(i)*/*std::*/exp(expr_.val()))
129 FAD_FUNC_MACRO(TFadFuncLog,
131  log,
132  expr_.dx(i)/expr_.val(),
133  expr_.fastAccessDx(i)/expr_.val())
134 FAD_FUNC_MACRO(TFadFuncLog10,
136  log10,
137  expr_.dx(i)/(/*std::*/log(value_type(10))*expr_.val()),
138  expr_.fastAccessDx(i)/(/*std::*/log(value_type(10))*expr_.val()))
139 FAD_FUNC_MACRO(TFadFuncAbs,
141  fabs,
142  expr_.val() >= 0. ?
143  expr_.dx(i) : -expr_.dx(i),
144  expr_.val() >= 0. ?
145  expr_.fastAccessDx(i) :
146  -expr_.fastAccessDx(i)
147  )
148 
149 
150 
151 #undef FAD_FUNC_MACRO
152 
153 
154 
155 #endif
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_ expr_ expr_ fabs
Definition: tfadfunc.h:140
expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ tanh
Definition: tfadfunc.h:100
#define FAD_FUNC_MACRO(NAME, FCT, FCTSTD, GRD, FASTGRD)
Definition: tfadfunc.h:26
REAL val(STATE &number)
Returns value of the variable.
Definition: pzartdiff.h:23
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: tfadfunc.h:65
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