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