Debian Codegraph @

packages/sacado/Fad/TinyFad/tinyfad.h

raw
1 // Emacs will be in -*- Mode: c++ -*-
2 // @HEADER
3 // *****************************************************************************
4 // Sacado Package
5 //
6 // Copyright 2006 NTESS and the Sacado contributors.
7 // SPDX-License-Identifier: LGPL-2.1-or-later
8 // *****************************************************************************
9 // @HEADER
10
11 // ********** DO NOT REMOVE THIS BANNER **********
12 //
13 // SUMMARY: Tools for Automatic Differentiaton (order 1)
14 // RELEASE: 0.1
15 // USAGE : You may copy freely these files and use it for
16 // teaching or research. These or part of these may
17 // not be sold or used for a commercial purpose with-
18 // out our consent : fax (33)1 44 27 72 00
19 //
20 // AUTHOR : Nicolas Di cesare
21 // ORG :
22 // E-MAIL : [email protected]
23 //
24 // ORIG-DATE: September 97
25 // LAST-MOD : 28/07/98
26 // ********************************************************
27 // FILE : tinyfad.h
28 // ********************************************************
29 #ifndef _tinyfad_h_
30 #define _tinyfad_h_
31
32 // C include
33 #include <cmath>
34
35 // ANSI C++ include
36 #include <iostream>
37
38 // include to manage error
39 #include "utils/error.h"
40
41 namespace FAD {
42
43 template <class A, class B> class NumericalTraits;
44
45 class No_Initialization {
46 public:
47 No_Initialization() {;}
48 ~No_Initialization() {;}
49 };
50
51
52
53 template <int Num, class T = float> class TinyFad {
54 protected:
55
56 int n; // number of the control or zero for independant variables
57 T val_; // value
58
59 T df_[Num]; // vector of partial derivatives
60
61
62 public:
63 typedef T value_type;
64
65 // controls constructor
66 TinyFad(const T& ind, const int ini);
67
68 // expressions constructors
69 TinyFad();
70 TinyFad(const No_Initialization &): n(0) {;}
71 TinyFad(const T& in);
72 TinyFad(const TinyFad& in);
73
74 // destructor
75 ~TinyFad() ;
76
77 void diff(const int ith, const int n = Num);
78
79 // acces functions
80 int N() const {return n-1;}
81
82 const T& val() const { return val_;}
83 T& val() { return val_;}
84
85
86 const T& d(int i) const { return df_[i];}
87
88 const T& dx(int i) const;
89 T& dx(int i);
90
91
92 // operators
93 TinyFad<Num,T> & operator = (const TinyFad<Num,T> & in);
94 TinyFad<Num,T> & operator = (const T & in);
95
96 TinyFad<Num,T> & operator += (const TinyFad<Num,T> & in);
97 TinyFad<Num,T> & operator -= (const TinyFad<Num,T> & in);
98 TinyFad<Num,T> & operator *= (const TinyFad<Num,T> & in);
99 TinyFad<Num,T> & operator /= (const TinyFad<Num,T> & in);
100
101 TinyFad<Num,T> & operator += (const T & in);
102 TinyFad<Num,T> & operator -= (const T & in);
103 TinyFad<Num,T> & operator *= (const T & in);
104 TinyFad<Num,T> & operator /= (const T & in);
105
106 TinyFad<Num,T> operator++(int);
107 TinyFad<Num,T> operator--(int);
108 TinyFad<Num,T> & operator++();
109 TinyFad<Num,T> & operator--();
110
111 };
112
113
114 template <int Num, class T> inline
115 TinyFad<Num,T>::TinyFad(const T& ind, const int ini) : n(ini+1), val_(ind)
116 {
117 #ifdef DEBUG_TINYFAD
118 cout << "TinyFad::TinyFad(const T& ind, const int ini), ind = " << ind << ", ini = " << ini << endl;
119 #endif
120 if ( ini >= 2 ) error("control number ini, out of bound ");
121
122 for (int i=0; i<Num; ++i)
123 df_[i] = 0.;
124
125 df_[ini] = 1.;
126 }
127
128 template <int Num, class T> inline
129 TinyFad<Num,T>::TinyFad() : n(0), val_(0.)
130 {
131 #ifdef DEBUG_TINYFAD
132 cout << "TinyFad::TinyFad()" << endl;
133 #endif
134 for (int i=0; i<Num; ++i)
135 df_[i] = 0.;
136 }
137
138 template <int Num, class T> inline
139 TinyFad<Num,T>::TinyFad(const T& in) : n(0), val_(in)
140 {
141 #ifdef DEBUG_TINYFAD
142 cout << "TinyFad::TinyFad(const T& in)" << endl;
143 #endif
144 for (int i=0; i<Num; ++i)
145 df_[i] = 0.;
146 }
147
148 template <int Num, class T> inline
149 TinyFad<Num,T>::TinyFad(const TinyFad<Num,T>& in) : n(0), val_(in.val_)
150 {
151 #ifdef DEBUG_TINYFAD
152 cout << "TinyFad::TinyFad(const TinyFad& in)" << endl;
153 #endif
154
155 for (int i=0; i<Num; ++i)
156 df_[i] = in.df_[i];
157 }
158
159 template <int Num, class T> inline
160 TinyFad<Num,T>::~TinyFad()
161 {
162 #ifdef DEBUG_TINYFAD
163 cout << "TinyFad::~TinyFad()" << endl;
164 #endif
165 }
166
167 template <int Num, class T> inline
168 void TinyFad<Num,T>::diff(const int ith, const int n_)
169 {
170
171 df_ = T(0.);
172 df_[ith] = T(1.);
173
174 }
175
176 template <int Num, class T>
177 const T& TinyFad<Num,T>::dx(int i) const {
178 if ( (i<0) || (i>=Num ) ) {
179 std::cerr << "i = " << i << std::endl;
180 error("df_, partial derivative undefined");
181 }
182
183 return df_[i];
184 }
185
186 template <int Num, class T>
187 T& TinyFad<Num,T>::dx(int i) {
188 if ( (i<0) || (i>=Num ) ) {
189 std::cerr << "i = " << i << std::endl;
190 error("df_, partial derivative undefined");
191 }
192
193 return df_[i];
194 }
195
196
197
198 template <int Num, class T> inline
199 TinyFad<Num,T> & TinyFad<Num,T>::operator = (const TinyFad<Num,T> & in)
200 {
201 #ifdef CHECK_VAR
202 if (n) error("TinyFad & TinyFad::operator = (const TinyFad & in), you do not change the value of control");
203 #endif
204
205 val_ = in.val_;
206
207 for (int i=0; i<Num; ++i)
208 df_[i] = in.df_[i];
209
210 return *this;
211 }
212
213
214
215 template <int Num, class T> TinyFad<Num,T> & TinyFad<Num,T>::operator = (const T & in)
216 {
217 #ifdef CHECK_VAR
218 if (n) error("TinyFad & TinyFad::operator = (const T & in), you do not change the value of control");
219 #endif
220 val_ = in;
221
222 for (int i=0; i<Num; i++)
223 df_[i] = 0.;
224
225 return *this;
226 }
227
228
229 template <int Num, class T> TinyFad<Num,T> & TinyFad<Num,T>::operator += (const TinyFad<Num,T> & in)
230 {
231 #ifdef CHECK_VAR
232 if (n) error("TinyFad & TinyFad::operator += (const TinyFad & in), you do not change the value of control");
233 #endif
234
235 for (int i=0; i< Num; i++)
236 df_[i] += in.df_[i] ;
237
238 val_ += in.val_;
239
240 return *this;
241 }
242
243 template <int Num, class T> TinyFad<Num,T> & TinyFad<Num,T>::operator -= (const TinyFad<Num,T> & in)
244 {
245 #ifdef CHECK_VAR
246 if (n) error("TinyFad & TinyFad::operator -= (const TinyFad & in), you do not change the value of control");
247 #endif
248
249 for (int i=0; i< Num; i++)
250 df_[i] -= in.df_[i] ;
251
252 val_ -= in.val_;
253
254 return *this;
255 }
256
257 template <int Num, class T> TinyFad<Num,T> & TinyFad<Num,T>::operator *= (const TinyFad<Num,T> & in)
258 {
259 #ifdef CHECK_VAR
260 if (n) error("TinyFad & TinyFad::operator *= (const TinyFad & in), you do not change the value of control");
261 #endif
262
263 for (int i=0; i< Num; i++)
264 df_[i] = df_[i] * in.val_ + val_ * in.df_[i];
265
266 val_ *= in.val_;
267
268 return *this;
269 }
270
271 template <int Num, class T> TinyFad<Num,T> & TinyFad<Num,T>::operator /= (const TinyFad<Num,T> & in)
272 {
273 #ifdef CHECK_VAR
274 if (n) error("TinyFad & TinyFad::operator /= (const TinyFad & in), you do not change the value of control");
275 #endif
276
277 if (in.val_ == 0.) error("TinyFad & TinyFad::operator /= (const TinyFad & in), dividing by 0");
278
279 for (int i=0; i< Num; i++)
280 df_[i] = ( df_[i]* in.val_ - val_ * in.df_[i] ) / in.val_ / in.val_ ;
281
282 val_ /= in.val_;
283
284 return *this;
285 }
286
287 template <int Num, class T> TinyFad<Num,T> & TinyFad<Num,T>::operator += (const T & in)
288 {
289 #ifdef CHECK_VAR
290 if (n) error("TinyFad<Num,T> & TinyFad<Num,T>::operator += (const T & in), you do not change the value of control");
291 #endif
292
293 val_ += in;
294
295 return *this;
296 }
297
298 template <int Num, class T> TinyFad<Num,T> & TinyFad<Num,T>::operator -= (const T & in)
299 {
300 #ifdef CHECK_VAR
301 if (n) error("TinyFad & TinyFad::operator -= (const T & in), you do not change the value of control");
302 #endif
303
304 val_ -= in;
305
306 return *this;
307 }
308
309 template <int Num, class T> TinyFad<Num,T> & TinyFad<Num,T>::operator *= (const T & in)
310 {
311 #ifdef CHECK_VAR
312 if (n) error("TinyFad & TinyFad::operator *= (const T & in), you do not change the value of control");
313 #endif
314
315 val_ *= in;
316
317 for (int i=0; i< Num; i++)
318 df_[i] *= in;
319
320 return *this;
321 }
322
323 template <int Num, class T> TinyFad<Num,T> & TinyFad<Num,T>::operator /= (const T & in)
324 {
325 #ifdef CHECK_VAR
326 if (n) error("TinyFad & TinyFad::operator /= (const T & in), you do not change the value of control");
327 #endif
328
329 if (in == 0.) error("TinyFad & TinyFad::operator /= (const T & in), dividing by 0");
330
331 val_ /= in;
332
333 for (int i=0; i< Num; i++)
334 df_[i] /= in;
335
336
337 return *this;
338 }
339
340
341 template <int Num, class T> inline
342 TinyFad<Num,T> TinyFad<Num,T>::operator++(int)
343 {
344 TinyFad<Num,T> tmp(*this);
345 tmp.val_++;
346 return tmp;
347 }
348
349 template <int Num, class T> inline
350 TinyFad<Num,T> TinyFad<Num,T>::operator--(int)
351 {
352 TinyFad<Num,T> tmp(*this);
353 tmp.val_--;
354 return tmp;
355 }
356
357 template <int Num, class T> inline
358 TinyFad<Num,T> & TinyFad<Num,T>::operator++()
359 {
360 ++val_;
361 return *this;
362 }
363
364 template <int Num, class T> inline
365 TinyFad<Num,T> & TinyFad<Num,T>::operator--()
366 {
367 --val_;
368 return *this;
369 }
370
371
372 //----------------------------------------------------------------------------------------------//
373 // unary operators
374 template <int Num, class T> inline TinyFad<Num,T> operator + (const TinyFad<Num,T>& in)
375 {
376 return TinyFad<Num,T>(in);
377 }
378
379 template <int Num, class T> inline TinyFad<Num,T> operator - (const TinyFad<Num,T>& in)
380 {
381 TinyFad<Num,T> tmp;
382 tmp -= in;
383
384 return tmp;
385 }
386
387
388 template <int Num, class T> std::ostream& operator << (std::ostream& os, const TinyFad<Num,T>& a)
389 {
390 os.setf(std::ios::fixed,std::ios::floatfield);
391 os.width(12);
392 os << a.val() << " [";
393
394 for (int i=0; i< Num; i++) {
395 os.width(12);
396 os << a.dx(i);
397 }
398
399 os << "]\n";
400 return os;
401 }
402
403
404
405 // logical operators
406 #include "TinyFad/tinyfadlog.h"
407 // binary operators
408 #include "TinyFad/tinyfadbin.h"
409 // math functions
410 #include "TinyFad/tinyfadfunc.h"
411
412 // specializations
413 #include "TinyFad/Specializations/tinyfadone.h"
414 #include "TinyFad/Specializations/tinyfadtwo.h"
415 #include "TinyFad/Specializations/tinyfadthree.h"
416 #include "TinyFad/Specializations/tinyfadfour.h"
417 #include "TinyFad/Specializations/tinyfadfive.h"
418 #include "TinyFad/Specializations/tinyfadsix.h"
419 #include "TinyFad/Specializations/tinyfadseven.h"
420 #include "TinyFad/Specializations/tinyfadeight.h"
421 #include "TinyFad/Specializations/tinyfadnine.h"
422 #include "TinyFad/Specializations/tinyfadten.h"
423 #include "TinyFad/Specializations/tinyfadeleven.h"
424 #include "TinyFad/Specializations/tinyfadtwelve.h"
425 #include "TinyFad/Specializations/tinyfadthirteen.h"
426 #include "TinyFad/Specializations/tinyfadfourteen.h"
427 #include "TinyFad/Specializations/tinyfadfifteen.h"
428 #include "TinyFad/Specializations/tinyfadsixteen.h"
429 #include "TinyFad/Specializations/tinyfadseventeen.h"
430 #include "TinyFad/Specializations/tinyfadeighteen.h"
431 #include "TinyFad/Specializations/tinyfadnineteen.h"
432 #include "TinyFad/Specializations/tinyfadtwenty.h"
433
434 } // namespace FAD
435
436 #endif