Debian Codegraph @

packages/sacado/Fad/TinyFad/Specializations/tinyfadfour.h

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