NeoPZ
tinyfadthree.h
Go to the documentation of this file.
1 #ifndef _tinyfadthree_h_
2 #define _tinyfadthree_h_
3 
4 //*********************************************************
5 // This file is generated by generate.cc.
6 // Use this file for any modification
7 //*********************************************************
8 
9 template <class T> class TinyFad<3,T> {
10 public:
11  typedef T value_type;
12  typedef T grad_type;
13 protected:
14 
15  int n;
16  T val_;
17 
18  T dx0_;
19  T dx1_;
20  T dx2_;
21 
22 
23 public:
24  void diff(const size_t ith, const size_t sz){
25  n = ith+1;
26  dx0_ = T(0.);
27  dx1_ = T(0.);
28  dx2_ = T(0.);
29 
30  switch(ith){
31  case 0 : dx0_ = T(1.);break;
32  case 1 : dx1_ = T(1.);break;
33  case 2 : dx2_ = T(1.);break;
34  default : cout << "ith = " << ith << " out of definition set" << endl;exit(1);
35  }
36  }
37 
38  TinyFad(const T& ind, const int ini) : n(ini+1), val_(ind) {
39  dx0_ = T(0.);
40  dx1_ = T(0.);
41  dx2_ = T(0.);
42 
43  switch(ini){
44  case 0 : dx0_ = T(1.);break;
45  case 1 : dx1_ = T(1.);break;
46  case 2 : dx2_ = T(1.);break;
47  default : cout << "ini = " << ini << " out of definition set" << endl;exit(1);
48  }
49  }
50  TinyFad() : n(0), val_(0.) {
51  dx0_ = T(0.);
52  dx1_ = T(0.);
53  dx2_ = T(0.);
54  }
55  TinyFad(const No_Initialization &): n(0) {}
56  TinyFad(const T& in) : n(0), val_(in) {
57  dx0_ = T(0.);
58  dx1_ = T(0.);
59  dx2_ = T(0.);
60  }
61  TinyFad(const TinyFad<3,T> & in) : n(0), val_(in.val_) {
62  dx0_ = in.dx0_;
63  dx1_ = in.dx1_;
64  dx2_ = in.dx2_;
65  }
66 
67  ~TinyFad() {}
68 
69  int N() const {return n-1;}
70 
71  const T& val() const { return val_;}
72  T& val() { return val_;}
73 
74  const T& d0() const { return dx0_;}
75  T& d0() { return dx0_;}
76 
77  const T& d1() const { return dx1_;}
78  T& d1() { return dx1_;}
79 
80  const T& d2() const { return dx2_;}
81  T& d2() { return dx2_;}
82 
83  T& dx(int i){
84  switch(i){
85  case 0 : return dx0_;
86  case 1 : return dx1_;
87  case 2 : return dx2_;
88  default : cout << "i out of bounds" << endl;exit(1);
89  }
90  }
91  const T& dx(int i) const {
92  switch(i){
93  case 0 : return dx0_;
94  case 1 : return dx1_;
95  case 2 : return dx2_;
96  default : cout << "i out of bounds" << endl;exit(1);
97  }
98  }
99  T& d(int i){
100  switch(i){
101  case 0 : return dx0_;
102  case 1 : return dx1_;
103  case 2 : return dx2_;
104  default : cout << "i out of bounds" << endl;exit(1);
105  }
106  }
107  const T& d(int i) const {
108  switch(i){
109  case 0 : return dx0_;
110  case 1 : return dx1_;
111  case 2 : return dx2_;
112  default : cout << "i out of bounds" << endl;exit(1);
113  }
114  }
115 
117  val_ = in.val_;
118 
119  dx0_ = in.dx0_;
120  dx1_ = in.dx1_;
121  dx2_ = in.dx2_;
122 
123  return *this;
124  }
125 
126  TinyFad<3,T> & operator = (const T & in){
127  val_ = in;
128 
129  dx0_ = T(0.);
130  dx1_ = T(0.);
131  dx2_ = T(0.);
132 
133  return *this;
134  }
135 
137  dx0_ += in.dx0_;
138  dx1_ += in.dx1_;
139  dx2_ += in.dx2_;
140  val_ += in.val_;
141 
142 
143  return *this;
144  }
146  dx0_ -= in.dx0_;
147  dx1_ -= in.dx1_;
148  dx2_ -= in.dx2_;
149  val_ -= in.val_;
150 
151  return *this;
152  }
154  dx0_ = dx0_ * in.val_ + val_ * in.dx0_;
155  dx1_ = dx1_ * in.val_ + val_ * in.dx1_;
156  dx2_ = dx2_ * in.val_ + val_ * in.dx2_;
157  val_ *= in.val_;
158 
159  return *this;
160  }
162  if (in.val_ == 0.) error("TinyFad & TinyFad::operator /= (const TinyFad & in), dividing by 0");
163  dx0_ = ( dx0_ * in.val_ - val_ * in.dx0_ ) / in.val_ / in.val_ ;
164  dx1_ = ( dx1_ * in.val_ - val_ * in.dx1_ ) / in.val_ / in.val_ ;
165  dx2_ = ( dx2_ * in.val_ - val_ * in.dx2_ ) / in.val_ / in.val_ ;
166  val_ /= in.val_;
167 
168  return *this;
169  }
170 
171  TinyFad<3,T> & operator += (const T & in){ val_ += in;
172 
173  return *this;
174  }
175  TinyFad<3,T> & operator -= (const T & in){ val_ -= in;
176 
177  return *this;
178  }
179  TinyFad<3,T> & operator *= (const T & in){
180  val_ *= in;
181 
182  dx0_ *= in;
183  dx1_ *= in;
184  dx2_ *= in;
185 
186  return *this;
187  }
188  TinyFad<3,T> & operator /= (const T & in){
189  if ( in == T(0.) ) error("TinyFad & TinyFad::operator /= (const T & in), dividing by 0");
190  val_ /= in;
191 
192  dx0_ /= in;
193  dx1_ /= in;
194  dx2_ /= in;
195 
196  return *this;
197  }
198 
200  TinyFad<3,T> tmp(*this);
201  tmp.val_++;
202  return tmp;
203  };
205  TinyFad<3,T> tmp(*this);
206  tmp.val_--;
207  return tmp;
208  };
210  return *this;
211  }
213  return *this;
214  }
215 };
216 
217 
218 template <class T> inline TinyFad<3,T> operator + (const TinyFad<3,T>& in)
219 {
220  return TinyFad<3,T>(in);
221 }
222 
223 template <class T> inline TinyFad<3,T> operator - (const TinyFad<3,T>& in)
224 {
226  tmp -= in;
227  return tmp;
228 }
229 
230 template <class L, class R> inline
232 operator +(const TinyFad<3,L>& un, const TinyFad<3,R>& deux) {
233 
234  typedef typename NumericalTraits<L,R>::promote value_type;
235 
236  No_Initialization nothing;
237  TinyFad<3,value_type> tmp( nothing );
238 
239  tmp.d0() = un.d0() + deux.d0();
240 
241  tmp.d1() = un.d1() + deux.d1();
242 
243  tmp.d2() = un.d2() + deux.d2();
244 
245  tmp.val() = un.val() + deux.val();
246 
247  return tmp;
248 }
249 
250 template <class L, class R> inline
252 operator +(const TinyFad<3,L>& un, const R& deux) {
253 
254  typedef typename NumericalTraits<L,R>::promote value_type;
255 
256  No_Initialization nothing;
257  TinyFad<3,value_type> tmp( nothing );
258 
259  tmp.d0() = un.d0();
260 
261  tmp.d1() = un.d1();
262 
263  tmp.d2() = un.d2();
264 
265  tmp.val() = un.val() + deux;
266 
267  return tmp;
268 }
269 
270 template <class L, class R> inline
272 operator +(const L& un, const TinyFad<3,R>& deux) {
273  return operator +(deux,un);
274 }
275 
276 template <class L, class R> inline
278 operator *(const TinyFad<3,L>& un, const TinyFad<3,R>& deux) {
279 
280  typedef typename NumericalTraits<L,R>::promote value_type;
281 
282  No_Initialization nothing;
283  TinyFad<3,value_type> tmp( nothing );
284 
285  tmp.d0() = un.d0()*deux.val() + un.val() * deux.d0();
286 
287  tmp.d1() = un.d1()*deux.val() + un.val() * deux.d1();
288 
289  tmp.d2() = un.d2()*deux.val() + un.val() * deux.d2();
290 
291  tmp.val() = un.val() * deux.val();
292 
293  return tmp;
294 }
295 
296 template <class L, class R> inline
298 operator *(const TinyFad<3,L>& un, const R& deux) {
299 
300  typedef typename NumericalTraits<L,R>::promote value_type;
301 
302  No_Initialization nothing;
303  TinyFad<3,value_type> tmp( nothing );
304 
305  tmp.d0() = un.d0()*deux;
306 
307  tmp.d1() = un.d1()*deux;
308 
309  tmp.d2() = un.d2()*deux;
310 
311  tmp.val() = un.val() * deux;
312 
313  return tmp;
314 }
315 
316 template <class L, class R> inline
318 operator *(const L& un, const TinyFad<3,R>& deux) {
319 
320  return operator *(deux,un);
321 }
322 
323 
324 template <class L, class R> inline
326 operator -(const TinyFad<3,L> & un, const TinyFad<3,R> & deux) {
327 
328  typedef typename NumericalTraits<L,R>::promote value_type;
329 
330  No_Initialization nothing;
331  TinyFad<3,value_type> tmp( nothing );
332 
333  tmp.d0() = un.d0() - deux.d0();
334 
335  tmp.d1() = un.d1() - deux.d1();
336 
337  tmp.d2() = un.d2() - deux.d2();
338 
339  tmp.val() = un.val() - deux.val();
340 
341  return tmp;
342 }
343 
344 template <class L, class R> inline
346 operator -(const L & un, const TinyFad<3,R> & deux) {
347 
348  typedef typename NumericalTraits<L,R>::promote value_type;
349 
350  No_Initialization nothing;
351  TinyFad<3,value_type> tmp( nothing );
352 
353  tmp.d0() -= deux.d0();
354 
355  tmp.d1() -= deux.d1();
356 
357  tmp.d2() -= deux.d2();
358 
359  tmp.val() = un - deux.val();
360 
361  return tmp;
362 }
363 
364 template <class L, class R> inline
366 operator -(const TinyFad<3,L> & un, const R & deux) {
367  typedef typename NumericalTraits<L,R>::promote value_type;
368 
369  No_Initialization nothing;
370  TinyFad<3,value_type> tmp( nothing );
371 
372  tmp.d0() = un.d0();
373 
374  tmp.d1() = un.d1();
375 
376  tmp.d2() = un.d2();
377 
378  tmp.val() = un.val() - deux;
379 
380  return tmp;
381 }
382 
383 template <class L, class R> inline
385 operator /(const TinyFad<3,L> & un, const TinyFad<3,R> & deux) {
386 
387  typedef typename NumericalTraits<L,R>::promote value_type;
388 
389  if (deux.val() == 0.) error("TinyFad & TinyFad::operator /(const TinyFad<3,L> & un, const TinyFad<3,R> & deux), dividing by 0");
390 
391  No_Initialization nothing;
392  TinyFad<3,value_type> tmp( nothing );
393  value_type dval = deux.val();
394 
395  tmp.d0() = ( un.d0()* deux.val() - un.val() * deux.d0() ) / dval / dval ;
396 
397  tmp.d1() = ( un.d1()* deux.val() - un.val() * deux.d1() ) / dval / dval ;
398 
399  tmp.d2() = ( un.d2()* deux.val() - un.val() * deux.d2() ) / dval / dval ;
400 
401  tmp.val() = un.val() / dval;
402 
403  return tmp;
404 }
405 
406 template <class L, class R> inline
408 operator /(const L & un, const TinyFad<3,R> & deux) {
409 
410  typedef typename NumericalTraits<L,R>::promote value_type;
411 
412  if (deux.val() == 0.) error("TinyFad & TinyFad::operator /(const L & un, const TinyFad<3,R> & deux), dividing by 0");
413 
414  No_Initialization nothing;
415  TinyFad<3,value_type> tmp( nothing );
416  value_type dval = deux.val();
417 
418  tmp.d0() = - un * deux.d0() / dval / dval ;
419 
420  tmp.d1() = - un * deux.d1() / dval / dval ;
421 
422  tmp.d2() = - un * deux.d2() / dval / dval ;
423 
424  tmp.val() = un / dval;
425 
426  return tmp;
427 }
428 
429 template <class L, class R> inline
431 operator /(const TinyFad<3,L> & un, const R & deux) {
432 
433  typedef typename NumericalTraits<L,R>::promote value_type;
434 
435  if (deux == 0.) error("TinyFad & TinyFad::operator /(const TinyFad<3,L> & un, const R & deux), dividing by 0");
436 
437  No_Initialization nothing;
438  TinyFad<3,value_type> tmp( nothing );
439 
440  tmp.d0() = un.d0() / deux;
441 
442  tmp.d1() = un.d1() / deux;
443 
444  tmp.d2() = un.d2() / deux;
445 
446  tmp.val() = un.val() / deux;
447 
448  return tmp;
449 }
450 
451 template <class T> TinyFad<3,T> exp (const TinyFad<3,T>& in)
452 {
453  TinyFad<3,T> tmp(exp(in.val()));
454 
455  tmp.d0() = in.d0()*exp(in.val());
456  tmp.d1() = in.d1()*exp(in.val());
457  tmp.d2() = in.d2()*exp(in.val());
458 
459  return tmp;
460 }
461 
462 template <class T> TinyFad<3,T> log (const TinyFad<3,T>& in)
463 {
464  if ( in.val() <= 0) error("TinyFad log (const TinyFad& in) : zero or negative value");
465  TinyFad<3,T> tmp(log(in.val()));
466 
467  tmp.d0() = in.d0() / in.val();
468  tmp.d1() = in.d1() / in.val();
469  tmp.d2() = in.d2() / in.val();
470 
471  return tmp;
472 }
473 
474 template <class T> TinyFad<3,T> sqrt (const TinyFad<3,T>& in)
475 {
476  if ( in.val() < 0. ) error("TinyFad<3,T> sqrt (const TinyFad& in) : negative value");
477  TinyFad<3,T> tmp(sqrt(in.val()));
478 
479  bool test=true;
480  if ( in.val() == T(0.) ){
481  if ( in.d0() != T(0.) ) test = false;
482 
483  if ( in.d1() != T(0.) ) test = false;
484 
485  if ( in.d2() != T(0.) ) test = false;
486 
487  if ( !test )
488  error("TinyFad<3,T> sqrt (const TinyFad& in) : null value");
489  }
490  else {
491  tmp.d0() = in.d0() / sqrt(in.val()) / 2.;
492  tmp.d1() = in.d1() / sqrt(in.val()) / 2.;
493  tmp.d2() = in.d2() / sqrt(in.val()) / 2.;
494  }
495 
496  return tmp;
497 }
498 
499 template <class T> TinyFad<3,T> sin (const TinyFad<3,T>& in)
500 {
501  TinyFad<3,T> tmp( sin(in.val()) );
502 
503  tmp.d0() = in.d0() * cos( in.val() );
504  tmp.d1() = in.d1() * cos( in.val() );
505  tmp.d2() = in.d2() * cos( in.val() );
506 
507  return tmp;
508 }
509 
510 template <class T> TinyFad<3,T> cos (const TinyFad<3,T>& in)
511 {
512  TinyFad<3,T> tmp(cos(in.val()));
513 
514  tmp.d0() = - in.d0() * sin( in.val() );
515  tmp.d1() = - in.d1() * sin( in.val() );
516  tmp.d2() = - in.d2() * sin( in.val() );
517 
518  return tmp;
519 }
520 
521 template <class T> TinyFad<3,T> tan (const TinyFad<3,T>& in)
522 {
523  if ( in.val() == 0) error("TinyFad tan (const TinyFad& in) undiefined in 0.");
524  TinyFad<3,T> tmp(tan(in.val()));
525 
526  T cosinus = cos(in.val());
527  tmp.d0() = in.d0() / cosinus / cosinus;
528  tmp.d1() = in.d1() / cosinus / cosinus;
529  tmp.d2() = in.d2() / cosinus / cosinus;
530 
531  return tmp;
532 }
533 
534 template <class T> TinyFad<3,T> pow (const TinyFad<3,T>& in, double e)
535 {
536  TinyFad<3,T> tmp(pow(in.val(), e));
537 
538  tmp.d0() = e*in.d0()*pow(in.val(), e-1);
539  tmp.d1() = e*in.d1()*pow(in.val(), e-1);
540  tmp.d2() = e*in.d2()*pow(in.val(), e-1);
541 
542  return tmp;
543 }
544 
545 template <class T> TinyFad<3,T> pow (const TinyFad<3,T>& un, const TinyFad<3,T>& deux)
546 {
547  if (un.val() == 0) error("TinyFad pow (const TinyFad& un, const TinyFad& deux), un = 0. ");
548  TinyFad<3,T> tmp(pow(un.val(), deux.val()));
549 
550  tmp.d0() = deux.d0() * log(un.val()) * pow(un.val(), deux.val())
551  + deux.val() * un.d0() * pow(un.val(), deux.val()-1);
552  tmp.d1() = deux.d1() * log(un.val()) * pow(un.val(), deux.val())
553  + deux.val() * un.d1() * pow(un.val(), deux.val()-1);
554  tmp.d2() = deux.d2() * log(un.val()) * pow(un.val(), deux.val())
555  + deux.val() * un.d2() * pow(un.val(), deux.val()-1);
556 
557  return tmp;
558 }
559 
560 template <class T> TinyFad<3,T> pow (const TinyFad<3,T>& in, const int e)
561 {
562  TinyFad<3,T> tmp( pow((double)in.val(), (double)e) );
563 
564  tmp.d0() = e*in.d0()*pow((double)in.val(), (double)e-1);
565  tmp.d1() = e*in.d1()*pow((double)in.val(), (double)e-1);
566  tmp.d2() = e*in.d2()*pow((double)in.val(), (double)e-1);
567 
568  return tmp;
569 }
570 
571 template <class T> TinyFad<3,T> abs (const TinyFad<3,T>& in)
572 {
573  int sign = in.val() > 0? 1:0;
574 
575  if (sign) return in;
576  else return (-in);
577 }
578 
579 template <class T> std::ostream& operator << (std::ostream& os, const TinyFad<3,T>& a)
580 {
581  os.setf(std::ios::fixed,std::ios::scientific);
582  os.width(12);
583  os << a.val() << " [";
584 
585  os.width(12);
586  os << a.d0();
587  os.width(12);
588  os << a.d1();
589  os.width(12);
590  os << a.d2();
591 
592  os << "]";
593 
594  return os;
595 }
596 
597 
598 #endif
TinyFad< 3, T > & operator--()
Definition: tinyfadthree.h:212
TinyFad< 3, T > log(const TinyFad< 3, T > &in)
Definition: tinyfadthree.h:462
const T & d(int i) const
Definition: tinyfadthree.h:107
TinyFad< 3, T > & operator++()
Definition: tinyfadthree.h:209
TinyFad(const T &ind, const int ini)
Definition: tinyfadthree.h:38
TinyFad< 3, typename NumericalTraits< L, R >::promote > operator*(const TinyFad< 3, L > &un, const TinyFad< 3, R > &deux)
Definition: tinyfadthree.h:278
T & d(int i)
Definition: tinyfadthree.h:99
TinyFad< Num, T > & operator-=(const TinyFad< Num, T > &in)
Definition: tinyfad.h:233
TinyFad< 3, T > operator++(int)
Definition: tinyfadthree.h:199
TinyFad< 3, T > pow(const TinyFad< 3, T > &in, double e)
Definition: tinyfadthree.h:534
Definition: test.py:1
TinyFad(const TinyFad< 3, T > &in)
Definition: tinyfadthree.h:61
TinyFad< 3, T > abs(const TinyFad< 3, T > &in)
Definition: tinyfadthree.h:571
TinyFad(const No_Initialization &)
Definition: tinyfadthree.h:55
TinyFad< 3, T > cos(const TinyFad< 3, T > &in)
Definition: tinyfadthree.h:510
AutoPointerMutexArrayInit tmp
T val_
Definition: tinyfad.h:47
TinyFad(const T &in)
Definition: tinyfadthree.h:56
void error(char *string)
Definition: testShape.cc:7
void diff(const size_t ith, const size_t sz)
Definition: tinyfadthree.h:24
const T & d2() const
Definition: tinyfadthree.h:80
TinyFad< 3, T > exp(const TinyFad< 3, T > &in)
Definition: tinyfadthree.h:451
const T & d1() const
Definition: tinyfadthree.h:77
TinyFad< 3, T > operator--(int)
Definition: tinyfadthree.h:204
TinyFad< Num, T > & operator=(const TinyFad< Num, T > &in)
Definition: tinyfad.h:189
TinyFad< 3, T > sqrt(const TinyFad< 3, T > &in)
Definition: tinyfadthree.h:474
const T & val() const
Definition: tinyfadthree.h:71
TinyFad< 3, T > sin(const TinyFad< 3, T > &in)
Definition: tinyfadthree.h:499
TinyFad< Num, T > & operator+=(const TinyFad< Num, T > &in)
Definition: tinyfad.h:219
T value_type
Definition: tinyfad.h:53
TinyFad< Num, T > & operator*=(const TinyFad< Num, T > &in)
Definition: tinyfad.h:247
T & dx(int i)
Definition: tinyfadthree.h:83
const T & d0() const
Definition: tinyfadthree.h:74
TinyFad< Num, T > & operator/=(const TinyFad< Num, T > &in)
Definition: tinyfad.h:261
int N() const
Definition: tinyfadthree.h:69
TinyFad< 3, T > operator+(const TinyFad< 3, T > &in)
Definition: tinyfadthree.h:218
TinyFad< 3, T > operator-(const TinyFad< 3, T > &in)
Definition: tinyfadthree.h:223
TinyFad< 3, T > tan(const TinyFad< 3, T > &in)
Definition: tinyfadthree.h:521
TinyFad< 3, typename NumericalTraits< L, R >::promote > operator/(const TinyFad< 3, L > &un, const TinyFad< 3, R > &deux)
Definition: tinyfadthree.h:385
const T & dx(int i) const
Definition: tinyfadthree.h:91
const T & val() const
Definition: tinyfad.h:72