NeoPZ
tinyfad.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 : tinyfad.h
20 // ********************************************************
21 #ifndef _tinyfad_h_
22 #define _tinyfad_h_
23 
24 // C include
25 #include <cmath>
26 
27 // ANSI C++ include
28 #include <iostream>
29 
30 // include to manage error
31 #include <utils/error.h>
32 
33 template <class A, class B, class Enable> class NumericalTraits;
34 
36 public:
39 };
40 
41 
42 
43 template <int Num, class T = float> class TinyFad {
44 protected:
45 
46  int n; // number of the control or zero for independant variables
47  T val_; // value
48 
49  T df_[Num]; // vector of partial derivatives
50 
51 
52 public:
53  typedef T value_type;
54 
55  // controls constructor
56  TinyFad(const T& ind, const int ini);
57 
58  // expressions constructors
59  TinyFad();
60  TinyFad(const No_Initialization &): n(0) {;}
61  TinyFad(const T& in);
62  TinyFad(const TinyFad& in);
63 
64  // destructor
65  ~TinyFad() ;
66 
67  void diff(const int ith, const int n);
68 
69  // acces functions
70  int N() const {return n-1;}
71 
72  const T& val() const { return val_;}
73  T& val() { return val_;}
74 
75 
76  const T& d(int i) const { return df_[i];}
77 
78  const T& dx(int i) const;
79  T& dx(int i);
80 
81 
82  // operators
83  TinyFad<Num,T> & operator = (const TinyFad<Num,T> & in);
84  TinyFad<Num,T> & operator = (const T & in);
85 
86  TinyFad<Num,T> & operator += (const TinyFad<Num,T> & in);
88  TinyFad<Num,T> & operator *= (const TinyFad<Num,T> & in);
89  TinyFad<Num,T> & operator /= (const TinyFad<Num,T> & in);
90 
91  TinyFad<Num,T> & operator += (const T & in);
92  TinyFad<Num,T> & operator -= (const T & in);
93  TinyFad<Num,T> & operator *= (const T & in);
94  TinyFad<Num,T> & operator /= (const T & in);
95 
96  TinyFad<Num,T> operator++(int);
97  TinyFad<Num,T> operator--(int);
98  TinyFad<Num,T> & operator++();
99  TinyFad<Num,T> & operator--();
100 
101 };
102 
103 
104 template <int Num, class T> inline
105 TinyFad<Num,T>::TinyFad(const T& ind, const int ini) : n(ini+1), val_(ind)
106 {
107 #ifdef PZDEBUG_TINYFAD
108  std::cout << "TinyFad::TinyFad(const T& ind, const int ini), ind = " << ind << ", ini = " << ini << std::endl;
109 #endif
110  if ( ini >= 2 ) error("control number ini, out of bound ");
111 
112  for (int i=0; i<Num; ++i)
113  df_[i] = 0.;
114 
115  df_[ini] = 1.;
116 }
117 
118 template <int Num, class T> inline
120 {
121 #ifdef PZDEBUG_TINYFAD
122  std::cout << "TinyFad::TinyFad()" << std::endl;
123 #endif
124  for (int i=0; i<Num; ++i)
125  df_[i] = 0.;
126 }
127 
128 template <int Num, class T> inline
129 TinyFad<Num,T>::TinyFad(const T& in) : n(0), val_(in)
130 {
131 #ifdef PZDEBUG_TINYFAD
132  std::cout << "TinyFad::TinyFad(const T& in)" << std::endl;
133 #endif
134  for (int i=0; i<Num; ++i)
135  df_[i] = 0.;
136 }
137 
138 template <int Num, class T> inline
140 {
141 #ifdef PZDEBUG_TINYFAD
142  std::cout << "TinyFad::TinyFad(const TinyFad& in)" << std::endl;
143 #endif
144 
145  for (int i=0; i<Num; ++i)
146  df_[i] = in.df_[i];
147 }
148 
149 template <int Num, class T> inline
151 {
152 #ifdef PZDEBUG_TINYFAD
153  std::cout << "TinyFad::~TinyFad()" << std::endl;
154 #endif
155 }
156 
157 template <int Num, class T> inline
158 void TinyFad<Num,T>::diff(const int ith, const int n)
159 {
160 
161  df_ = T(0.);
162  df_[ith] = T(1.);
163 
164 }
165 
166 template <int Num, class T>
167 const T& TinyFad<Num,T>::dx(int i) const {
168  if ( (i<0) || (i>=Num ) ) {
169  std::cerr << "i = " << i << std::endl;
170  error("df_, partial derivative undefined");
171  }
172 
173  return df_[i];
174 }
175 
176 template <int Num, class T>
177 T& TinyFad<Num,T>::dx(int i) {
178  if ( (i<0) || (i>=Num ) ) {
179  std::cerr << "i = " << i << std::endl;
180  error("df_, partial derivative undefined");
181  }
182 
183  return df_[i];
184 }
185 
186 
187 
188 template <int Num, class T> inline
190 {
191 #ifdef CHECK_VAR
192  if (n) error("TinyFad & TinyFad::operator = (const TinyFad & in), you do not change the value of control");
193 #endif
194 
195  val_ = in.val_;
196 
197  for (int i=0; i<Num; ++i)
198  df_[i] = in.df_[i];
199 
200  return *this;
201 }
202 
203 
204 
205 template <int Num, class T> TinyFad<Num,T> & TinyFad<Num,T>::operator = (const T & in)
206 {
207 #ifdef CHECK_VAR
208  if (n) error("TinyFad & TinyFad::operator = (const T & in), you do not change the value of control");
209 #endif
210  val_ = in;
211 
212  for (int i=0; i<Num; i++)
213  df_[i] = 0.;
214 
215  return *this;
216 }
217 
218 
219 template <int Num, class T> TinyFad<Num,T> & TinyFad<Num,T>::operator += (const TinyFad<Num,T> & in)
220 {
221 #ifdef CHECK_VAR
222  if (n) error("TinyFad & TinyFad::operator += (const TinyFad & in), you do not change the value of control");
223 #endif
224 
225  for (int i=0; i< Num; i++)
226  df_[i] += in.df_[i] ;
227 
228  val_ += in.val_;
229 
230  return *this;
231 }
232 
233 template <int Num, class T> TinyFad<Num,T> & TinyFad<Num,T>::operator -= (const TinyFad<Num,T> & in)
234 {
235 #ifdef CHECK_VAR
236  if (n) error("TinyFad & TinyFad::operator -= (const TinyFad & in), you do not change the value of control");
237 #endif
238 
239  for (int i=0; i< Num; i++)
240  df_[i] -= in.df_[i] ;
241 
242  val_ -= in.val_;
243 
244  return *this;
245 }
246 
247 template <int Num, class T> TinyFad<Num,T> & TinyFad<Num,T>::operator *= (const TinyFad<Num,T> & in)
248 {
249 #ifdef CHECK_VAR
250  if (n) error("TinyFad & TinyFad::operator *= (const TinyFad & in), you do not change the value of control");
251 #endif
252 
253  for (int i=0; i< Num; i++)
254  df_[i] = df_[i] * in.val_ + val_ * in.df_[i];
255 
256  val_ *= in.val_;
257 
258  return *this;
259 }
260 
261 template <int Num, class T> TinyFad<Num,T> & TinyFad<Num,T>::operator /= (const TinyFad<Num,T> & in)
262 {
263 #ifdef CHECK_VAR
264  if (n) error("TinyFad & TinyFad::operator /= (const TinyFad & in), you do not change the value of control");
265 #endif
266 
267  if (in.val_ == 0.) error("TinyFad & TinyFad::operator /= (const TinyFad & in), dividing by 0");
268 
269  for (int i=0; i< Num; i++)
270  df_[i] = ( df_[i]* in.val_ - val_ * in.df_[i] ) / in.val_ / in.val_ ;
271 
272  val_ /= in.val_;
273 
274  return *this;
275 }
276 
277 template <int Num, class T> TinyFad<Num,T> & TinyFad<Num,T>::operator += (const T & in)
278 {
279 #ifdef CHECK_VAR
280  if (n) error("TinyFad<Num,T> & TinyFad<Num,T>::operator += (const T & in), you do not change the value of control");
281 #endif
282 
283  val_ += in;
284 
285  return *this;
286 }
287 
288 template <int Num, class T> TinyFad<Num,T> & TinyFad<Num,T>::operator -= (const T & in)
289 {
290 #ifdef CHECK_VAR
291  if (n) error("TinyFad & TinyFad::operator -= (const T & in), you do not change the value of control");
292 #endif
293 
294  val_ -= in;
295 
296  return *this;
297 }
298 
299 template <int Num, class T> TinyFad<Num,T> & TinyFad<Num,T>::operator *= (const T & in)
300 {
301 #ifdef CHECK_VAR
302  if (n) error("TinyFad & TinyFad::operator *= (const T & in), you do not change the value of control");
303 #endif
304 
305  val_ *= in;
306 
307  for (int i=0; i< Num; i++)
308  df_[i] *= in;
309 
310  return *this;
311 }
312 
313 template <int Num, class T> TinyFad<Num,T> & TinyFad<Num,T>::operator /= (const T & in)
314 {
315 #ifdef CHECK_VAR
316  if (n) error("TinyFad & TinyFad::operator /= (const T & in), you do not change the value of control");
317 #endif
318 
319  if (in == 0.) error("TinyFad & TinyFad::operator /= (const T & in), dividing by 0");
320 
321  val_ /= in;
322 
323  for (int i=0; i< Num; i++)
324  df_[i] /= in;
325 
326 
327  return *this;
328 }
329 
330 
331 template <int Num, class T> inline
333 {
334  TinyFad<Num,T> tmp(*this);
335  tmp.val_++;
336  return tmp;
337 }
338 
339 template <int Num, class T> inline
341 {
342  TinyFad<Num,T> tmp(*this);
343  tmp.val_--;
344  return tmp;
345 }
346 
347 template <int Num, class T> inline
349 {
350  ++val_;
351  return *this;
352 }
353 
354 template <int Num, class T> inline
356 {
357  --val_;
358  return *this;
359 }
360 
361 
362 //----------------------------------------------------------------------------------------------//
363 // unary operators
364 template <int Num, class T> inline TinyFad<Num,T> operator + (const TinyFad<Num,T>& in)
365 {
366  return TinyFad<Num,T>(in);
367 }
368 
369 template <int Num, class T> inline TinyFad<Num,T> operator - (const TinyFad<Num,T>& in)
370 {
372  tmp -= in;
373 
374  return tmp;
375 }
376 
377 
378 template <int Num, class T> std::ostream& operator << (std::ostream& os, const TinyFad<Num,T>& a)
379 {
380  os.setf(std::ios::fixed,std::ios::floatfield);
381  os.width(12);
382  os << a.val() << " [";
383 
384  for (int i=0; i< Num; i++) {
385  os.width(12);
386  os << a.dx(i);
387  }
388 
389  os << "]\n";
390  return os;
391 }
392 
393 
394 
395 // logical operators
396 #include <TinyFad/tinyfadlog.h>
397 // binary operators
398 #include <TinyFad/tinyfadbin.h>
399 // math functions
400 #include <TinyFad/tinyfadfunc.h>
401 
402 // specializations
423 
424 
425 #endif
int N() const
Definition: tinyfad.h:70
TinyFad< Num, T > operator+(const TinyFad< Num, T > &in)
Definition: tinyfad.h:364
int n
Definition: tinyfad.h:46
TinyFad()
Definition: tinyfad.h:119
TinyFad< Num, T > & operator-=(const TinyFad< Num, T > &in)
Definition: tinyfad.h:233
TinyFad(const No_Initialization &)
Definition: tinyfad.h:60
T df_[Num]
Definition: tinyfad.h:49
TPZVec< T > & operator-=(TPZVec< T > &a, const TPZVec< T > &b)
substracts two vectors
Definition: pzvec_extras.h:80
const T & d(int i) const
Definition: tinyfad.h:76
AutoPointerMutexArrayInit tmp
T val_
Definition: tinyfad.h:47
void error(char *string)
Definition: testShape.cc:7
~TinyFad()
Definition: tinyfad.h:150
const T & dx(int i) const
Definition: tinyfad.h:167
void diff(const int ith, const int n)
Definition: tinyfad.h:158
expr_ dx(i) *cos(expr_.val())
TinyFad< Num, T > operator-(const TinyFad< Num, T > &in)
Definition: tinyfad.h:369
TinyFad< Num, T > & operator=(const TinyFad< Num, T > &in)
Definition: tinyfad.h:189
TinyFad< Num, T > & operator+=(const TinyFad< Num, T > &in)
Definition: tinyfad.h:219
TinyFad< Num, T > & operator--()
Definition: tinyfad.h:355
T value_type
Definition: tinyfad.h:53
TinyFad< Num, T > & operator*=(const TinyFad< Num, T > &in)
Definition: tinyfad.h:247
TinyFad< Num, T > & operator++()
Definition: tinyfad.h:348
TinyFad< Num, T > & operator/=(const TinyFad< Num, T > &in)
Definition: tinyfad.h:261
T & val()
Definition: tinyfad.h:73
const T & val() const
Definition: tinyfad.h:72