Debian Codegraph @

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