NeoPZ
tinyfadfunc.h
Go to the documentation of this file.
1 // Emacs will be in -*- Mode: c++ -*-
2 //
3 // ********** DO NOT REMOVE THIS BANNER **********
4 //
5 // SUMMARY: Tools for Automatic Differentiaton (order 1)
6 // RELEASE: 0.1
7 // USAGE : You may copy freely these files and use it for
8 // teaching or research. These or part of these may
9 // not be sold or used for a commercial purpose with-
10 // out our consent : fax (33)1 44 27 72 00
11 //
12 // AUTHOR : Nicolas Di cesare
13 // ORG :
14 // E-MAIL : Nicolas.Dicesare@ann.jussieu.fr
15 //
16 // ORIG-DATE: September 97
17 // LAST-MOD : 28/07/98
18 // ********************************************************
19 // FILE : tinyfadfunc.h
20 // ********************************************************
21 #ifndef _tinyfadfunc_h_
22 #define _tinyfadfunc_h_
23 
24 template <int Num, class T> TinyFad<Num,T> exp (const TinyFad<Num,T>& in)
25 {
26  TinyFad<Num,T> tmp(exp(in.val()));
27 
28  for (int i=0; i< Num; i++)
29  tmp.dx(i) = in.dx(i)*exp(in.val());
30 
31  return tmp;
32 }
33 
34 template <int Num, class T> TinyFad<Num,T> log (const TinyFad<Num,T>& in)
35 {
36  if ( in.val() <= 0) error("TinyFad log (const TinyFad& in) : zero or negative value");
37  TinyFad<Num,T> tmp(log(in.val()));
38 
39  for (int i=0; i< Num; i++)
40  tmp.dx(i) = in.dx(i) / in.val();
41 
42  return tmp;
43 }
44 
45 template <int Num, class T> TinyFad<Num,T> sqrt (const TinyFad<Num,T>& in)
46 {
47  if ( in.val() < 0. ) error("TinyFad<Num,T> sqrt (const TinyFad& in) : negative value");
48  TinyFad<Num,T> tmp(sqrt(in.val()));
49 
50  bool test=true;
51  if ( in.val() == T(0.) ){
52  for (int i=0; i< Num; i++)
53  if ( in.dx(i) != T(0.) ) test = false;
54 
55  if ( !test )
56  error("TinyFad<Num,T> sqrt (const TinyFad& in) : null value");
57  }
58  else
59  for (int i=0; i< Num; i++)
60  tmp.dx(i) = in.dx(i) / sqrt(in.val()) / 2.;
61 
62  return tmp;
63 }
64 
65 template <int Num, class T> TinyFad<Num,T> sin (const TinyFad<Num,T>& in)
66 {
67  TinyFad<Num,T> tmp( sin(in.val()) );
68 
69  for (int i=0; i< Num; i++)
70  tmp.dx(i) = in.dx(i) * cos( in.val() );
71 
72 
73  return tmp;
74 }
75 
76 template <int Num, class T> TinyFad<Num,T> cos (const TinyFad<Num,T>& in)
77 {
78  TinyFad<Num,T> tmp(cos(in.val()));
79 
80  for (int i=0; i< Num; i++)
81  tmp.dx(i) = - in.dx(i) * sin( in.val() );
82 
83  return tmp;
84 }
85 
86 template <int Num, class T> TinyFad<Num,T> tan (const TinyFad<Num,T>& in)
87 {
88  if ( in.val() == 0) error("TinyFad tan (const TinyFad& in) undiefined in 0.");
89  TinyFad<Num,T> tmp(tan(in.val()));
90 
91  for (int i=0; i< Num; i++){
92  T cosinus = cos(in.val());
93  tmp.dx(i) = in.dx(i) / cosinus / cosinus;
94  }
95 
96  return tmp;
97 }
98 
99 template <int Num, class T> TinyFad<Num,T> pow (const TinyFad<Num,T>& in, double e)
100 {
101  TinyFad<Num,T> tmp(pow(in.val(), e));
102 
103  for (int i=0; i< Num; i++){
104  tmp.dx(i) = e*in.dx(i)*pow(in.val(), e-1);
105  }
106 
107  return tmp;
108 }
109 
110 template <int Num, class T> TinyFad<Num,T> pow (const TinyFad<Num,T>& un, const TinyFad<Num,T>& deux)
111 {
112  if (un.val() == 0) error("TinyFad pow (const TinyFad& un, const TinyFad& deux), un = 0. ");
113  TinyFad<Num,T> tmp(pow(un.val(), deux.val()));
114 
115  for (int i=0; i< Num; i++){
116  tmp.dx(i) = deux.dx(i) * log(un.val()) * pow(un.val(), deux.val())
117  + deux.val() * un.dx(i) * pow(un.val(), deux.val()-1);
118  }
119 
120  return tmp;
121 }
122 
123 template <int Num, class T> TinyFad<Num,T> pow (const TinyFad<Num,T>& in, const int e)
124 {
125  TinyFad<Num,T> tmp( pow((double)in.val(), (double)e) );
126 
127  for (int i=0; i< Num; i++){
128  tmp.dx(i) = e*in.dx(i)*pow((double)in.val(), (double)e-1);
129  }
130 
131  return tmp;
132 }
133 
134 template <int Num, class T> TinyFad<Num,T> abs (const TinyFad<Num,T>& in)
135 {
136  int sign = in.val() > 0? 1:0;
137 
138  if (sign) return in;
139  else return (-in);
140 }
141 
142 
143 #endif
Definition: test.py:1
TinyFad< Num, T > pow(const TinyFad< Num, T > &in, double e)
Definition: tinyfadfunc.h:99
TinyFad< Num, T > exp(const TinyFad< Num, T > &in)
Definition: tinyfadfunc.h:24
AutoPointerMutexArrayInit tmp
void error(char *string)
Definition: testShape.cc:7
const T & dx(int i) const
Definition: tinyfad.h:167
TinyFad< Num, T > log(const TinyFad< Num, T > &in)
Definition: tinyfadfunc.h:34
TinyFad< Num, T > cos(const TinyFad< Num, T > &in)
Definition: tinyfadfunc.h:76
TinyFad< Num, T > sin(const TinyFad< Num, T > &in)
Definition: tinyfadfunc.h:65
TinyFad< Num, T > tan(const TinyFad< Num, T > &in)
Definition: tinyfadfunc.h:86
TinyFad< Num, T > abs(const TinyFad< Num, T > &in)
Definition: tinyfadfunc.h:134
TinyFad< Num, T > sqrt(const TinyFad< Num, T > &in)
Definition: tinyfadfunc.h:45
const T & val() const
Definition: tinyfad.h:72