Debian Codegraph @

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