Debian Codegraph @

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