Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Sacado_tradvec.hpp
Go to the documentation of this file.
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 // TRAD package (Templated Reverse Automatic Differentiation) --
11 // a package specialized for function and gradient evaluations.
12 // Written in 2004 and 2005 by David M. Gay at Sandia National Labs,
13 // Albuquerque, NM.
14 
15 #ifndef SACADO_TRADVEC_H
16 #define SACADO_TRADVEC_H
17 
18 #include "Sacado_ConfigDefs.h"
20 
21 #include <stddef.h>
22 #include <Sacado_cmath.hpp>
23 
24 #if defined(RAD_DEBUG_BLOCKKEEP) && !defined(HAVE_SACADO_UNINIT)
25 #undef RAD_DEBUG_BLOCKKEEP
26 #endif
27 
28 #ifdef RAD_Const_WARN //{ ==> RAD_AUTO_AD_Const and RAD_DEBUG
29 #ifndef RAD_AUTO_AD_Const
30 #define RAD_AUTO_AD_Const
31 #endif
32 #ifndef RAD_DEBUG
33 #define RAD_DEBUG
34 #endif
35 extern "C" int RAD_Const_Warn(const void*);// outside any namespace for
36  // ease in setting breakpoints
37 #endif //} RAD_Const_WARN
38 
39 #ifdef RAD_DEBUG
40 #include <cstdio>
41 #include <stdlib.h>
42 #endif
43 
44 #ifndef RAD_AUTO_AD_Const
45 #ifdef RAD_DEBUG_BLOCKKEEP
46 #include <complex> // must come before namespace Sacado
47 #endif
48 #endif
49 
50 namespace Sacado {
51 namespace RadVec {
52 
53 #ifdef RAD_AUTO_AD_Const
54 #undef RAD_DEBUG_BLOCKKEEP
55 #define Padvinit ,padv(0)
56 #else
57 #define Padvinit /*nothing*/
58 #ifdef RAD_DEBUG_BLOCKKEEP
59 #if !(RAD_DEBUG_BLOCKKEEP > 0)
60 #undef RAD_DEBUG_BLOCKKEEP
61 #else
62 extern "C" void _uninit_f2c(void *x, int type, long len);
63 
64 template <typename T>
65 struct UninitType {};
66 
67 template <>
68 struct UninitType<float> {
69  static const int utype = 4;
70 };
71 
72 template <>
73 struct UninitType<double> {
74  static const int utype = 5;
75 };
76 
77 template <typename T>
78 struct UninitType< std::complex<T> > {
79  static const int utype = UninitType<T>::utype + 2;
80 };
81 
82 #endif /*RAD_DEBUG_BLOCKKEEP > 0*/
83 #endif /*RAD_DEBUG_BLOCKKEEP*/
84 #endif /*RAD_AUTO_AD_Const*/
85 
86  class RAD_DoubleIgnore {};
87 
88  template<typename T> class
90  public:
91  typedef double dtype;
92  typedef T ttype;
93  };
94  template<> class
96  public:
99  };
100 
101 #define Dtype typename DoubleAvoid<Double>::dtype
102 #define Ttype typename DoubleAvoid<Double>::ttype
103 
104  template<typename Double> class IndepADvar;
105  template<typename Double> class ConstADvar;
106  template<typename Double> class ConstADvari;
107  template<typename Double> class ADvar;
108  template<typename Double> class ADvari;
109  template<typename Double> class ADvar1;
110  template<typename Double> class ADvar1s;
111  template<typename Double> class ADvar2;
112  template<typename Double> class ADvar2q;
113  template<typename Double> class ADvarn;
114  template<typename Double> class Derp;
115 
116  template<typename Double> struct
117 ADmemblock { // We get memory in ADmemblock chunks and never give it back,
118  // but reuse it once computations start anew after call(s) on
119  // ADcontext::Gradcomp() or ADcontext::Weighted_Gradcomp().
121  Double memblk[1000];
122  };
123 
124  template<typename Double> class
125 ADcontext {
130 
131  ADMemblock *Busy, *First, *Free, *Oldbusy;
132  char *Mbase;
133  size_t Mleft;
135  size_t ncur, nmax, rad_mleft_save;
136 #ifdef RAD_DEBUG_BLOCKKEEP
137  int rad_busy_blocks;
138  ADMemblock *rad_Oldcurmb;
139 #endif
140  void *new_ADmemblock(size_t);
141  void derp_init(size_t);
142  public:
143  static const Double One, negOne;
144  ADcontext();
145  void *Memalloc(size_t len);
146  static void Gradcomp();
147  static void aval_reset(void);
148  static void Weighted_Gradcomp(size_t, ADVar**, Double*);
149  static void Weighted_GradcompVec(size_t, size_t*, ADVar***, Double**);
150  static void Outvar_Gradcomp(ADVar&);
151  };
152 
153  template<typename Double> class
154 CADcontext: public ADcontext<Double> { // for possibly constant ADvar values
155  protected:
157  public:
158  friend class ADvar<Double>;
159  CADcontext(): ADcontext<Double>() { fpval_implies_const = false; }
160  };
161 
162  template<typename Double> class
163 Derp { // one derivative-propagation operation
164  public:
165  friend class ADvarn<Double>;
167  static Derp *LastDerp;
169  const Double *a;
170  const ADVari *b;
171  const ADVari *c;
172  Derp(){};
173  Derp(const ADVari *);
174  Derp(const Double *, const ADVari *);
175  Derp(const Double *, const ADVari *, const ADVari *);
176  inline void *operator new(size_t len) { return (Derp*)ADVari::adc.Memalloc(len); }
177  /* c->aval += a * b->aval; */
178  };
179 
180 // Now we use #define to overcome bad design in the C++ templating system
181 
182 #define Ai const ADvari<Double>&
183 #define AI const IndepADvar<Double>&
184 #define T template<typename Double>
185 #define D Double
186 #define T1(f) \
187 T F f (AI); \
188 T F f (Ai);
189 #define T2(r,f) \
190  T r f(Ai,Ai); \
191  T r f(Ai,D); \
192  T r f(Ai,Dtype); \
193  T r f(Ai,long); \
194  T r f(Ai,int); \
195  T r f(D,Ai); \
196  T r f(Dtype,Ai); \
197  T r f(long,Ai); \
198  T r f(int,Ai); \
199  T r f(AI,D); \
200  T r f(AI,Dtype); \
201  T r f(AI,long); \
202  T r f(AI,int); \
203  T r f(D,AI); \
204  T r f(Dtype,AI); \
205  T r f(long,AI); \
206  T r f(int,AI); \
207  T r f(Ai,AI);\
208  T r f(AI,Ai);\
209  T r f(AI,AI);
210 
211 #define F ADvari<Double>&
212 T2(F, operator+)
213 T2(F, operator-)
214 T2(F, operator*)
215 T2(F, operator/)
216 T2(F, atan2)
217 T2(F, pow)
218 T2(F, max)
219 T2(F, min)
220 T2(int, operator<)
221 T2(int, operator<=)
222 T2(int, operator==)
223 T2(int, operator!=)
224 T2(int, operator>=)
225 T2(int, operator>)
226 T1(operator+)
227 T1(operator-)
228 T1(abs)
229 T1(acos)
230 T1(acosh)
231 T1(asin)
232 T1(asinh)
233 T1(atan)
234 T1(atanh)
235 T1(cos)
236 T1(cosh)
237 T1(exp)
238 T1(log)
239 T1(log10)
240 T1(sin)
241 T1(sinh)
242 T1(sqrt)
243 T1(tan)
244 T1(tanh)
245 T1(fabs)
246 
247 T F copy(AI);
248 T F copy(Ai);
249 
250 #undef F
251 #undef T2
252 #undef T1
253 #undef D
254 #undef T
255 #undef AI
256 #undef Ai
257 
258  template<typename Double>ADvari<Double>& ADf1(Double f, Double g, const IndepADvar<Double> &x);
259  template<typename Double>ADvari<Double>& ADf2(Double f, Double gx, Double gy,
260  const IndepADvar<Double> &x, const IndepADvar<Double> &y);
261  template<typename Double>ADvari<Double>& ADfn(Double f, int n,
262  const IndepADvar<Double> *x, const Double *g);
263 
264  template<typename Double> IndepADvar<Double>& ADvar_operatoreq(IndepADvar<Double>*,
265  const ADvari<Double>&);
266  template<typename Double> ADvar<Double>& ADvar_operatoreq(ADvar<Double>*, const ADvari<Double>&);
267  template<typename Double> void AD_Const(const IndepADvar<Double>&);
268  template<typename Double> void AD_Const1(Double*, const IndepADvar<Double>&);
269  template<typename Double> ADvari<Double>& ADf1(Double, Double, const ADvari<Double>&);
270  template<typename Double> ADvari<Double>& ADf2(Double, Double, Double,
271  const ADvari<Double>&, const ADvari<Double>&);
272  template<typename Double> ADvari<Double>& ADf2(Double, Double, Double,
273  const IndepADvar<Double>&, const ADvari<Double>&);
274  template<typename Double> ADvari<Double>& ADf2(Double, Double, Double,
275  const ADvari<Double>&, const IndepADvar<Double>&);
276  template<typename Double> Double val(const ADvari<Double>&);
277 
278  template<typename Double> class
279 ADvari { // implementation of an ADvar
280  public:
284 #ifdef RAD_AUTO_AD_Const
285  friend class IndepADvar<Double>;
286 #ifdef RAD_Const_WARN
287  mutable const IndepADVar *padv;
288 #else
289  protected:
290  mutable const IndepADVar *padv;
291 #endif //RAD_Const_WARN
292  public:
293  inline void ADvari_padv(const IndepADVar *v) {
294  this->padv = v;
295  }
296 #endif //RAD_AUTO_AD_Const
297 #ifdef RAD_DEBUG
298  int gcgen;
299  int opno;
300  static int gcgen_cur, last_opno, zap_gcgen, zap_gcgen1, zap_opno;
301  static FILE *debug_file;
302 #endif
303  Double Val; // result of this operation
304  mutable Double *aval; // adjoint -- partial of final result w.r.t. this Val
305  static ADvari *First_ADvari, **Last_ADvari;
307  private:
308  inline void Linkup() {
309  *Last_ADvari = this;
310  Last_ADvari = &this->Next;
311  }
312  public:
313  void *operator new(size_t len) {
314 #ifdef RAD_DEBUG
315  ADvari *rv = (ADvari*)ADvari::adc.Memalloc(len);
316  rv->gcgen = gcgen_cur;
317  rv->opno = ++last_opno;
318  if (last_opno == zap_opno && gcgen_cur == zap_gcgen)
319  printf("Got to opno %d\n", last_opno);
320  return rv;
321 #else
322  return ADvari::adc.Memalloc(len);
323 #endif
324  }
325  void operator delete(void*) {} /*Should never be called.*/
326  inline ADvari(Double t): Val(t), aval(0) Padvinit { Linkup(); }
327  inline ADvari(): Val(0.), aval(0) Padvinit { Linkup(); }
328 #ifdef RAD_AUTO_AD_Const
329  friend class ADcontext<Double>;
330  friend class ADvar<Double>;
331  friend class ADvar1<Double>;
332  friend class ADvar1s<Double>;
333  friend class ADvar2<Double>;
334  friend class ADvar2q<Double>;
335  friend class ConstADvar<Double>;
336  ADvari(const IndepADVar *, Double);
337 #endif
338 #define F friend
339 #define R ADvari&
340 #define Ai const ADvari&
341 #define T1(r,f) F r f <>(Ai);
342 #define T2(r,f) \
343 F r f <>(Ai,Ai); \
344 F r f <>(Ttype,Ai); \
345 F r f <>(Ai,Ttype); \
346 F r f <>(double,Ai); \
347 F r f <>(Ai,double); \
348 F r f <>(long,Ai); \
349 F r f <>(Ai,long); \
350 F r f <>(int,Ai); \
351 F r f <>(Ai,int);
352  T1(R,operator+)
353  T2(R,operator+)
354  T1(R,operator-)
355  T2(R,operator-)
356  T2(R,operator*)
357  T2(R,operator/)
358  T1(R,abs)
359  T1(R,acos)
360  T1(R,acosh)
361  T1(R,asin)
362  T1(R,asinh)
363  T1(R,atan)
364  T1(R,atanh)
365  T2(R,atan2)
366  T2(R,max)
367  T2(R,min)
368  T1(R,cos)
369  T1(R,cosh)
370  T1(R,exp)
371  T1(R,log)
372  T1(R,log10)
373  T2(R,pow)
374  T1(R,sin)
375  T1(R,sinh)
376  T1(R,sqrt)
377  T1(R,tan)
378  T1(R,tanh)
379  T1(R,fabs)
380  T1(R,copy)
381  T2(int,operator<)
382  T2(int,operator<=)
383  T2(int,operator==)
384  T2(int,operator!=)
385  T2(int,operator>=)
386  T2(int,operator>)
387 #undef T2
388 #undef T1
389 #undef Ai
390 #undef R
391 #undef F
392 
393  friend ADvari& ADf1<>(Double f, Double g, const ADvari &x);
394  friend ADvari& ADf2<>(Double f, Double gx, Double gy, const ADvari &x, const ADvari &y);
395  friend ADvari& ADfn<>(Double f, int n, const IndepADVar *x, const Double *g);
396 
397  inline operator Double() { return this->Val; }
398  inline operator Double() const { return this->Val; }
399 
401  };
402 
403  template<typename Double> class
404 ADvar1: public ADvari<Double> { // simplest unary ops
405  public:
407  ADvar1(Double val1): ADVari(val1) {}
409  ADvar1(Double val1, const ADVari *c1): ADVari(val1), d(c1) {}
410  ADvar1(Double val1, const Double *a1, const ADVari *c1):
411  ADVari(val1), d(a1,this,c1) {}
412 #ifdef RAD_AUTO_AD_Const
413  typedef typename ADVari::IndepADVar IndepADVar;
414  typedef ADvar<Double> ADVar;
415  ADvar1(const IndepADVar*, const IndepADVar&);
416  ADvar1(const IndepADVar*, const ADVari&);
417  ADvar1(const Double val1, const Double *a1, const ADVari *c1, const ADVar *v):
418  ADVari(val1), d(a1,this,c1) {
419  c1->padv = 0;
420  this->ADvari_padv(v);
421  }
422 #endif
423  };
424 
425 
426  template<typename Double> class
427 ConstADvari: public ADvari<Double> {
428  private:
430  ConstADvari() {}; // prevent construction without value (?)
431  static ConstADvari *lastcad;
432  public:
433  friend class ADcontext<Double>;
437  inline void *operator new(size_t len) { return ConstADvari::cadc.Memalloc(len); }
438  inline ConstADvari(Double t): ADVari(t) { prevcad = lastcad; lastcad = this; }
439  static void aval_reset(void);
440  };
441 
442  template<typename Double> class
443 IndepADvar { // an independent ADvar
444  protected:
445  static void AD_Const(const IndepADvar&);
446  mutable ADvari<Double> *cv;
447  private:
449  /* private to prevent assignment */
450 #ifdef RAD_AUTO_AD_Const
451  if (cv)
452  cv->padv = 0;
453  cv = new ADvar1<Double>(this,x);
454  return *this;
455 #elif defined(RAD_EQ_ALIAS)
456  this->cv = x.cv;
457  return *this;
458 #else
459  return ADvar_operatoreq(this,*x.cv);
460 #endif //RAD_AUTO_AD_Const
461  }
462  public:
463  int Wantderiv(int);
465  friend class ADvar<Double>;
466  friend class ADcontext<Double>;
467  friend class ADvar1<Double>;
468  friend class ADvarn<Double>;
471  IndepADvar(Ttype);
472  IndepADvar(double);
473  IndepADvar(int);
474  IndepADvar(long);
475  IndepADvar& operator= (Double);
476  inline int Wantderiv() { return 1; }
477 #ifdef RAD_AUTO_AD_Const //{
478  inline IndepADvar(const IndepADvar &x) { cv = x.cv ? new ADvar1<Double>(this, x) : 0; };
479  inline IndepADvar() { cv = 0; }
480  inline ~IndepADvar() {
481  if (cv)
482  cv->padv = 0;
483  }
484 #else
485  inline IndepADvar() {
486 #ifndef RAD_EQ_ALIAS
487  cv = 0;
488 #endif
489  }
490  inline ~IndepADvar() {}
491  friend IndepADvar& ADvar_operatoreq<>(IndepADvar*, const ADVari&);
492 #endif //}
493 
494 #ifdef RAD_Const_WARN
495  inline operator ADVari&() const {
496  ADVari *tcv = this->cv;
497  if (tcv->opno < 0)
498  RAD_Const_Warn(tcv);
499  return *tcv;
500  }
501  inline operator ADVari*() const {
502  ADVari *tcv = this->cv;
503  if (tcv->opno < 0)
504  RAD_Const_Warn(tcv);
505  return tcv;
506  }
507 #else //RAD_Const_WARN
508  inline operator ADVari&() const { return *this->cv; }
509  inline operator ADVari*() const { return this->cv; }
510 #endif //RAD_Const_WARN
511 
512  inline Double val() const {
513  return cv->Val;
514  }
515  inline Double adj() const {
516  return *cv->aval;
517  }
518  inline Double adj(int n) const {
519  return cv->aval[n];
520  }
521 
522  friend void AD_Const1<>(Double*, const IndepADvar&);
523 
524  friend ADVari& ADf1<>(Double, Double, const IndepADvar&);
525  friend ADVari& ADf2<>(Double, Double, Double, const IndepADvar&, const IndepADvar&);
526  friend ADVari& ADf2<>(Double, Double, Double, const ADVari&, const IndepADvar&);
527  friend ADVari& ADf2<>(Double, Double, Double, const IndepADvar&, const ADVari&);
528 
529  static inline void Gradcomp() { ADcontext<Double>::Gradcomp(); }
530  static inline void aval_reset() { ConstADvari<Double>::aval_reset(); }
531  static inline void Weighted_Gradcomp(size_t n, ADVar **v, Double *w)
533  static inline void Weighted_GradcompVec(size_t n, size_t *np, ADVar ***v, Double **w)
535  static inline void Outvar_Gradcomp(ADVar &v)
537 
538  /* We use #define to deal with bizarre templating rules that apparently */
539  /* require us to spell the some conversion explicitly */
540 
541 
542 #define Ai const ADVari&
543 #define AI const IndepADvar&
544 #define D Double
545 #define T2(r,f) \
546  r f <>(AI,AI);\
547  r f <>(Ai,AI);\
548  r f <>(AI,Ai);\
549  r f <>(Ttype,AI);\
550  r f <>(double,AI);\
551  r f <>(long,AI);\
552  r f <>(int,AI);\
553  r f <>(AI,Ttype);\
554  r f <>(AI,double);\
555  r f <>(AI,long);\
556  r f <>(AI,int);
557 #define T1(f) friend ADVari& f<> (AI);
558 
559 #define F friend ADVari&
560 T2(F, operator+)
561 T2(F, operator-)
562 T2(F, operator*)
563 T2(F, operator/)
564 T2(F, atan2)
565 T2(F, max)
566 T2(F, min)
567 T2(F, pow)
568 #undef F
569 #define F friend int
570 T2(F, operator<)
571 T2(F, operator<=)
572 T2(F, operator==)
573 T2(F, operator!=)
574 T2(F, operator>=)
575 T2(F, operator>)
576 
577 T1(operator+)
578 T1(operator-)
579 T1(abs)
580 T1(acos)
581 T1(acosh)
582 T1(asin)
583 T1(asinh)
584 T1(atan)
585 T1(atanh)
586 T1(cos)
587 T1(cosh)
588 T1(exp)
589 T1(log)
590 T1(log10)
591 T1(sin)
592 T1(sinh)
593 T1(sqrt)
594 T1(tan)
595 T1(tanh)
596 T1(fabs)
597 T1(copy)
598 
599 #undef F
600 #undef T1
601 #undef T2
602 #undef D
603 #undef AI
604 #undef Ai
605 
606  };
607 
608  template<typename Double> class
609 ADvar: public IndepADvar<Double> { // an "active" variable
610  public:
612  template <typename U> struct apply { typedef ADvar<U> type; };
613 
615  typedef typename IndepADVar::ADVari ADVari;
617  private:
618  void ADvar_ctr(Double d) {
619  ADVari *x;
620  if (ConstADVari::cadc.fpval_implies_const)
621  x = new ConstADVari(d);
622  else {
623 #ifdef RAD_AUTO_AD_Const //{
624  x = new ADVari((IndepADVar*)this, d);
625  x->ADvari_padv(this);
626 #else
627  x = new ADVari(d);
628 #endif //}
629  }
630  this->cv = x;
631  }
632  public:
633  friend class ADvar1<Double>;
635  inline ADvar() { /* cv = 0; */ }
636  inline ADvar(Ttype d) { ADvar_ctr(d); }
637  inline ADvar(double i) { ADvar_ctr(Double(i)); }
638  inline ADvar(int i) { ADvar_ctr(Double(i)); }
639  inline ADvar(long i) { ADvar_ctr(Double(i)); }
640  inline ~ADvar() {}
641 #ifdef RAD_AUTO_AD_Const //{{
642  inline ADvar(IndepADVar &x) {
643  this->cv = x.cv ? new ADVar1(this, x) : 0;
644  }
645  inline ADvar(ADVari &x) { this->cv = &x; x.ADvari_padv(this); }
646  inline ADvar& operator=(IndepADVar &x) {
647  if (this->cv)
648  this->cv->padv = 0;
649  this->cv = new ADVar1(this,x);
650  return *this;
651  }
652  inline ADvar& operator=(ADVari &x) {
653  if (this->cv)
654  this->cv->padv = 0;
655  this->cv = new ADVar1(this, x);
656  return *this;
657  }
658 #else //}{
659  friend ADvar& ADvar_operatoreq<>(ADvar*, const ADVari&);
660 #ifdef RAD_EQ_ALIAS
661  /* allow aliasing v and w after "v = w;" */
662  inline ADvar(const IndepADVar &x) {
663  this->cv = (ADVari*)x.cv;
664  }
665  inline ADvar(const ADVari &x) { this->cv = (ADVari*)&x; }
666  inline ADvar& operator=(IndepADVar &x) {
667  this->cv = (ADVari*)x.cv; return *this;
668  }
669  inline ADvar& operator=(const ADVari &x) { this->cv = (ADVari*)&x; return *this; }
670 #else
671  ADvar(const IndepADVar &x) {
672  if (x.cv) {
673  this->cv = new ADVar1(x.cv->Val, &this->cv->adc.One, x.cv);
674  }
675  else
676  this->cv = 0;
677  }
678  ADvar(const ADvar&x) {
679  if (x.cv) {
680  this->cv = new ADVar1(x.cv->Val, &this->cv->adc.One, (ADVari*)x.cv);
681  }
682  else
683  this->cv = 0;
684  }
685  ADvar(const ADVari &x) {
686  this->cv = new ADVar1(x.Val, &this->cv->adc.One, &x);
687  }
688  inline ADvar& operator=(const ADVari &x) { return ADvar_operatoreq(this,x); };
689 #endif /* RAD_EQ_ALIAS */
690 #endif /* RAD_AUTO_AD_Const */ //}}
691  ADvar& operator=(Double);
692  ADvar& operator+=(const ADVari&);
693  ADvar& operator+=(Double);
694  ADvar& operator-=(const ADVari&);
695  ADvar& operator-=(Double);
696  ADvar& operator*=(const ADVari&);
697  ADvar& operator*=(Double);
698  ADvar& operator/=(const ADVari&);
699  ADvar& operator/=(Double);
700  inline static bool get_fpval_implies_const(void)
701  { return ConstADVari::cadc.fpval_implies_const; }
702  inline static void set_fpval_implies_const(bool newval)
703  { ConstADVari::cadc.fpval_implies_const = newval; }
704  inline static bool setget_fpval_implies_const(bool newval) {
705  bool oldval = ConstADVari::cadc.fpval_implies_const;
706  ConstADVari::cadc.fpval_implies_const = newval;
707  return oldval;
708  }
709  static inline void Gradcomp() { ADcontext<Double>::Gradcomp(); }
710  static inline void aval_reset() { ConstADVari::aval_reset(); }
711  static inline void Weighted_Gradcomp(size_t n, ADvar **v, Double *w)
713  static inline void Weighted_GradcompVec(size_t n, size_t *np, ADvar ***v, Double **w)
715  static inline void Outvar_Gradcomp(ADvar &v)
717  };
718 
719 template<typename Double>
720  inline void AD_Const1(Double *notused, const IndepADvar<Double>&v)
722 
723 template<typename Double>
724  inline void AD_Const(const IndepADvar<Double>&v) { AD_Const1((Double*)0, v); }
725 
726  template<typename Double> class
727 ConstADvar: public ADvar<Double> {
728  public:
730  typedef typename ADVar::ADVar1 ADVar1;
731  typedef typename ADVar::ADVari ADVari;
732  typedef typename ADVar::ConstADVari ConstADVari;
734  typedef typename ADVar::IndepADVar IndepADVar;
735  private: // disable op=
736  ConstADvar& operator+=(ADVari&);
737  ConstADvar& operator+=(Double);
738  ConstADvar& operator-=(ADVari&);
739  ConstADvar& operator-=(Double);
740  ConstADvar& operator*=(ADVari&);
741  ConstADvar& operator*=(Double);
742  ConstADvar& operator/=(ADVari&);
743  ConstADvar& operator/=(Double);
744  void ConstADvar_ctr(Double);
745  public:
746  ConstADvar(Ttype d) { ConstADvar_ctr(d); }
747  ConstADvar(double i) { ConstADvar_ctr(Double(i)); }
748  ConstADvar(int i) { ConstADvar_ctr(Double(i)); }
749  ConstADvar(long i) { ConstADvar_ctr(Double(i)); }
750  ConstADvar(const IndepADVar&);
751  ConstADvar(const ConstADvar&);
752  ConstADvar(const ADVari&);
753  inline ~ConstADvar() {}
754 #ifdef RAD_NO_CONST_UPDATE
755  private:
756 #endif
757  ConstADvar();
758  inline ConstADvar& operator=(Double d) {
759  this->cv->Val = d;
760  return *this;
761  }
762  inline ConstADvar& operator=(ADVari& d) {
763  this->cv->Val = d.Val;
764  return *this;
765  }
766  };
767 
768 #define ADvar_nd ADvar
769 
770  template<typename Double> class
771 ADvar1s: public ADvar1<Double> { // unary ops with partial "a"
772  public:
774  typedef typename ADVar1::ADVari ADVari;
775  Double a;
776  ADvar1s(Double val1, Double a1, const ADVari *c1): ADVar1(val1,&a,c1), a(a1) {}
777 #ifdef RAD_AUTO_AD_Const
778  typedef typename ADVar1::ADVar ADVar;
779  ADvar1s(Double val1, Double a1, const ADVari *c1, const ADVar *v):
780  ADVar1(val1,&a,c1,v), a(a1) {}
781 #endif
782  };
783 
784  template<typename Double> class
785 ADvar2: public ADvari<Double> { // basic binary ops
786  public:
789  ADvar2(Double val1): ADVari(val1) {}
790  DErp dL, dR;
791  ADvar2(Double val1, const ADVari *Lcv, const Double *Lc,
792  const ADVari *Rcv, const Double *Rc):
793  ADVari(val1) {
794  dR.next = DErp::LastDerp;
795  dL.next = &dR;
796  DErp::LastDerp = &dL;
797  dL.a = Lc;
798  dL.c = Lcv;
799  dR.a = Rc;
800  dR.c = Rcv;
801  dL.b = dR.b = this;
802  }
803 #ifdef RAD_AUTO_AD_Const
804  typedef ADvar<Double> ADVar;
805  ADvar2(Double val1, const ADVari *Lcv, const Double *Lc,
806  const ADVari *Rcv, const Double *Rc, ADVar *v):
807  ADVari(val1) {
808  dR.next = DErp::LastDerp;
809  dL.next = &dR;
810  DErp::LastDerp = &dL;
811  dL.a = Lc;
812  dL.c = Lcv;
813  dR.a = Rc;
814  dR.c = Rcv;
815  dL.b = dR.b = this;
816  Lcv->padv = 0;
817  this->ADvari_padv(v);
818  }
819 #endif
820  };
821 
822  template<typename Double> class
823 ADvar2q: public ADvar2<Double> { // binary ops with partials "a", "b"
824  public:
826  typedef typename ADVar2::ADVari ADVari;
827  typedef typename ADVar2::DErp DErp;
828  Double a, b;
829  ADvar2q(Double val1, Double Lp, Double Rp, const ADVari *Lcv, const ADVari *Rcv):
830  ADVar2(val1), a(Lp), b(Rp) {
831  this->dR.next = DErp::LastDerp;
832  this->dL.next = &this->dR;
833  DErp::LastDerp = &this->dL;
834  this->dL.a = &a;
835  this->dL.c = Lcv;
836  this->dR.a = &b;
837  this->dR.c = Rcv;
838  this->dL.b = this->dR.b = this;
839  }
840 #ifdef RAD_AUTO_AD_Const
841  typedef typename ADVar2::ADVar ADVar;
842  ADvar2q(Double val1, Double Lp, Double Rp, const ADVari *Lcv,
843  const ADVari *Rcv, const ADVar *v):
844  ADVar2(val1), a(Lp), b(Rp) {
845  this->dR.next = DErp::LastDerp;
846  this->dL.next = &this->dR;
847  DErp::LastDerp = &this->dL;
848  this->dL.a = &a;
849  this->dL.c = Lcv;
850  this->dR.a = &b;
851  this->dR.c = Rcv;
852  this->dL.b = this->dR.b = this;
853  Lcv->padv = 0;
854  this->ADvari_padv(v);
855  }
856 #endif
857  };
858 
859  template<typename Double> class
860 ADvarn: public ADvari<Double> { // n-ary ops with partials "a"
861  public:
863  typedef typename ADVari::IndepADVar IndepADVar;
865  int n;
866  Double *a;
867  DErp *Da;
868  ADvarn(Double val1, int n1, const IndepADVar *x, const Double *g):
869  ADVari(val1), n(n1) {
870  DErp *d1, *dlast;
871  Double *a1;
872  int i;
873 
874  a1 = a = (Double*)ADVari::adc.Memalloc(n*sizeof(*a));
875  d1 = Da = (DErp*)ADVari::adc.Memalloc(n*sizeof(DErp));
876  dlast = DErp::LastDerp;
877  for(i = 0; i < n1; i++, d1++) {
878  d1->next = dlast;
879  dlast = d1;
880  a1[i] = g[i];
881  d1->a = &a1[i];
882  d1->b = this;
883  d1->c = x[i].cv;
884  }
885  DErp::LastDerp = dlast;
886  }
887  };
888 
889 template<typename Double>
890  inline ADvari<Double>& operator+(const ADvari<Double> &T) { return *(ADvari<Double>*)&T; }
891 
892 template<typename Double>
893  inline int operator<(const ADvari<Double> &L, const ADvari<Double> &R) { return L.Val < R.Val; }
894 template<typename Double>
895  inline int operator<(const ADvari<Double> &L, Double R) { return L.Val < R; }
896 template<typename Double>
897  inline int operator<(Double L, const ADvari<Double> &R) { return L < R.Val; }
898 
899 template<typename Double>
900  inline int operator<=(const ADvari<Double> &L, const ADvari<Double> &R) { return L.Val <= R.Val; }
901 template<typename Double>
902  inline int operator<=(const ADvari<Double> &L, Double R) { return L.Val <= R; }
903 template<typename Double>
904  inline int operator<=(Double L, const ADvari<Double> &R) { return L <= R.Val; }
905 
906 template<typename Double>
907  inline int operator==(const ADvari<Double> &L, const ADvari<Double> &R) { return L.Val == R.Val; }
908 template<typename Double>
909  inline int operator==(const ADvari<Double> &L, Double R) { return L.Val == R; }
910 template<typename Double>
911  inline int operator==(Double L, const ADvari<Double> &R) { return L == R.Val; }
912 
913 template<typename Double>
914  inline int operator!=(const ADvari<Double> &L, const ADvari<Double> &R) { return L.Val != R.Val; }
915 template<typename Double>
916  inline int operator!=(const ADvari<Double> &L, Double R) { return L.Val != R; }
917 template<typename Double>
918  inline int operator!=(Double L, const ADvari<Double> &R) { return L != R.Val; }
919 
920 template<typename Double>
921  inline int operator>=(const ADvari<Double> &L, const ADvari<Double> &R) { return L.Val >= R.Val; }
922 template<typename Double>
923  inline int operator>=(const ADvari<Double> &L, Double R) { return L.Val >= R; }
924 template<typename Double>
925  inline int operator>=(Double L, const ADvari<Double> &R) { return L >= R.Val; }
926 
927 template<typename Double>
928  inline int operator>(const ADvari<Double> &L, const ADvari<Double> &R) { return L.Val > R.Val; }
929 template<typename Double>
930  inline int operator>(const ADvari<Double> &L, Double R) { return L.Val > R; }
931 template<typename Double>
932  inline int operator>(Double L, const ADvari<Double> &R) { return L > R.Val; }
933 
934 template<typename Double>
935  inline void *ADcontext<Double>::Memalloc(size_t len) {
936  if (Mleft >= len)
937  return Mbase + (Mleft -= len);
938  return new_ADmemblock(len);
939  }
940 
941 template<typename Double>
942  inline Derp<Double>::Derp(const ADVari *c1): c(c1) {
943  next = LastDerp;
944  LastDerp = this;
945  }
946 
947 template<typename Double>
948  inline Derp<Double>::Derp(const Double *a1, const ADVari *c1): a(a1), c(c1) {
949  next = LastDerp;
950  LastDerp = this;
951  }
952 
953 
954 template<typename Double>
955  inline Derp<Double>::Derp(const Double *a1, const ADVari *b1, const ADVari *c1): a(a1), b(b1), c(c1) {
956  next = LastDerp;
957  LastDerp = this;
958  }
959 
960 /**** radops ****/
961 
962 template<typename Double> Derp<Double> *Derp<Double>::LastDerp = 0;
963 template<typename Double> ADcontext<Double> ADvari<Double>::adc;
964 template<typename Double> const Double ADcontext<Double>::One = 1.;
965 template<typename Double> const Double ADcontext<Double>::negOne = -1.;
966 template<typename Double> CADcontext<Double> ConstADvari<Double>::cadc;
967 template<typename Double> ConstADvari<Double> *ConstADvari<Double>::lastcad;
968 
969 template<typename Double> ADvari<Double>* ADvari<Double>::First_ADvari;
971 
972 #ifdef RAD_DEBUG
973 #ifndef RAD_DEBUG_gcgen1
974 #define RAD_DEBUG_gcgen1 -1
975 #endif
976 template<typename Double> int ADvari<Double>::gcgen_cur;
977 template<typename Double> int ADvari<Double>::last_opno;
978 template<typename Double> int ADvari<Double>::zap_gcgen;
979 template<typename Double> int ADvari<Double>::zap_gcgen1 = RAD_DEBUG_gcgen1;
980 template<typename Double> int ADvari<Double>::zap_opno;
981 template<typename Double> FILE *ADvari<Double>::debug_file;
982 #endif
983 
984 
985 template<typename Double> ADcontext<Double>::ADcontext()
986 {
987  First = new ADMemblock;
988  First->next = 0;
989  Busy = First;
990  Free = Oldbusy = 0;
991  Mbase = (char*)First->memblk;
992  Mleft = sizeof(First->memblk);
993  ncur = nmax = rad_need_reinit = 0;
994 #ifdef RAD_DEBUG_BLOCKKEEP
995  rad_busy_blocks = 0;
996  rad_mleft_save = 0;
997  rad_Oldcurmb = 0;
998 #endif
999  }
1000 
1001 template<typename Double> void*
1003 {
1004  ADMemblock *mb, *mb0, *mb1, *mbf, *x;
1005 #ifdef RAD_AUTO_AD_Const
1006  ADVari *a, *anext;
1007  IndepADvar<Double> *v;
1008 #ifdef RAD_Const_WARN
1009  ADVari *cv;
1010  int i, j;
1011 #endif
1012 #endif /*RAD_AUTO_AD_Const*/
1013 
1014  if (rad_need_reinit && this == &ADVari::adc) {
1015  rad_need_reinit = 0;
1016  nmax = 0;
1017  DErp::LastDerp = 0;
1018  ADVari::Last_ADvari = &ADVari::First_ADvari;
1019 #ifdef RAD_DEBUG_BLOCKKEEP
1020  Mleft = rad_mleft_save;
1021  if (Mleft < sizeof(First->memblk))
1022  _uninit_f2c(Mbase + Mleft,
1023  UninitType<Double>::utype,
1024  (sizeof(First->memblk) - Mleft)
1025  /sizeof(typename Sacado::ValueType<Double>::type));
1026  if ((mb = Busy->next)) {
1027  mb0 = rad_Oldcurmb;
1028  for(;; mb = mb->next) {
1029  _uninit_f2c(mb->memblk,
1030  UninitType<Double>::utype,
1031  sizeof(First->memblk)
1032  /sizeof(typename Sacado::ValueType<Double>::type));
1033  if (mb == mb0)
1034  break;
1035  }
1036  }
1037  rad_Oldcurmb = Busy;
1038  if (rad_busy_blocks >= RAD_DEBUG_BLOCKKEEP) {
1039  rad_busy_blocks = 0;
1040  rad_Oldcurmb = 0;
1041  mb0 = 0;
1042  mbf = Free;
1043  for(mb = Busy; mb != mb0; mb = mb1) {
1044  mb1 = mb->next;
1045  mb->next = mbf;
1046  mbf = mb;
1047  }
1048  Free = mbf;
1049  Busy = mb;
1050  Mbase = (char*)First->memblk;
1051  Mleft = sizeof(First->memblk);
1052  }
1053 
1054 #else /* !RAD_DEBUG_BLOCKKEEP */
1055 
1056  mb0 = First;
1057  mbf = Free;
1058  for(mb = Busy; mb != mb0; mb = mb1) {
1059  mb1 = mb->next;
1060  mb->next = mbf;
1061  mbf = mb;
1062  }
1063  Free = mbf;
1064  Busy = mb;
1065  Mbase = (char*)First->memblk;
1066  Mleft = sizeof(First->memblk);
1067 #endif /*RAD_DEBUG_BLOCKKEEP*/
1068 #ifdef RAD_AUTO_AD_Const // {
1069  if ((a = ADVari::First_ADvari)) {
1070  do {
1071  anext = a->Next;
1072  if ((v = (IndepADvar<Double> *)a->padv)) {
1073 #ifdef RAD_Const_WARN
1074  if ((i = a->opno) > 0)
1075  i = -i;
1076  j = a->gcgen;
1077  v->cv = cv = new ADVari(v, a->Val);
1078  cv->opno = i;
1079  cv->gcgen = j;
1080 #else
1081  v->cv = new ADVari(v, a->Val);
1082 #endif
1083  }
1084  }
1085  while((a = anext));
1086  }
1087 #endif // } RAD_AUTO_AD_Const
1088  if (Mleft >= len)
1089  return Mbase + (Mleft -= len);
1090  }
1091 
1092  if ((x = Free))
1093  Free = x->next;
1094  else
1095  x = new ADMemblock;
1096 #ifdef RAD_DEBUG_BLOCKKEEP
1097  rad_busy_blocks++;
1098 #endif
1099  x->next = Busy;
1100  Busy = x;
1101  return (Mbase = (char*)x->memblk) +
1102  (Mleft = sizeof(First->memblk) - len);
1103  }
1104 
1105  template<typename Double> void
1107 {
1108  ADMemblock *mb, *mb0, *mb1, *mbf;
1109  ADVari *a;
1111  Double *d, *de;
1112  size_t len;
1113 
1114  if (!rad_need_reinit) {
1115  rad_mleft_save = Mleft;
1116  Oldbusy = Busy;
1117  }
1118  len = n * sizeof(Double);
1119  ncur = n;
1120  if (!nmax) {
1121  if (n > 0)
1122  goto aval_alloc;
1123  }
1124  else if (n > nmax) {
1125  mb0 = Oldbusy;
1126  mb = Busy;
1127  if (mb != mb0) {
1128  mbf = Free;
1129  do {
1130  mb1 = mb->next;
1131  mb->next = mbf;
1132  mbf = mb;
1133  mb = mb1;
1134  }
1135  while(mb != mb0);
1136  Free = mbf;
1137  }
1138  Mleft = rad_mleft_save;
1139  Busy = Oldbusy;
1140  aval_alloc:
1141  rad_need_reinit = 0;
1142  nmax = n;
1143  *ADVari::Last_ADvari = 0;
1144  for(a = ADVari::First_ADvari; a; a = a->Next) {
1145  d = a->aval = (Double*)Memalloc(len);
1146  de = d + n;
1147  do *d = 0.; while(++d < de);
1148  }
1149  for(c = ConstADvari<Double>::lastcad; c; c = c->prevcad) {
1150  d = c->aval = (Double*)Memalloc(len);
1151  de = d + n;
1152  do *d = 0.; while(++d < de);
1153  }
1154  }
1155  else if (!n) {
1156  nmax = 0;
1157  for(a = ADVari::First_ADvari; a; a = a->Next)
1158  a->aval = 0;
1159  }
1160  else for(a = ADVari::First_ADvari; a; a = a->Next) {
1161  d = a->aval;
1162  de = d + n;
1163  do *d = 0.; while(++d < de);
1164  }
1165  Mleft = 0;
1166  rad_need_reinit = 1;
1167 #ifdef RAD_DEBUG
1168  if (ADVari::gcgen_cur == ADVari::zap_gcgen1) {
1169  const char *fname;
1170  if (!(fname = getenv("RAD_DEBUG_FILE")))
1171  fname = "rad_debug.out";
1172  else if (!*fname)
1173  fname = 0;
1174  if (fname)
1175  ADVari::debug_file = fopen(fname, "w");
1176  ADVari::zap_gcgen1 = -1;
1177  }
1178 #endif
1179  }
1180 
1181  template<typename Double> void
1183 {
1184  DErp *d;
1185 #ifdef RAD_AUTO_AD_Const
1186  ADVari *a, *anext;
1187  IndepADvar<Double> *v;
1188 #ifdef RAD_Const_WARN
1189  ADVari *cv;
1190  int i, j;
1191 #endif
1192 #endif /*RAD_AUTO_AD_Const*/
1193 
1194  ADVari::adc.derp_init(1);
1195  if ((d = DErp::LastDerp) != 0) {
1196  *d->b->aval = 1;
1197 #ifdef RAD_DEBUG
1198  if (ADVari::debug_file)
1199  do {
1200  fprintf(ADVari::debug_file, "%d\t%d\t%g + %g * %g",
1201  d->c->opno, d->b->opno, &d->c->aval, *d->a, *d->b->aval);
1202  d->c->aval += *d->a * d->b->aval;
1203  fprintf(ADVari::debug_file, " = %g\n", *d->c->aval);
1204  } while((d = d->next));
1205  else
1206 #endif
1207  do *d->c->aval += *d->a * *d->b->aval;
1208  while((d = d->next));
1209  }
1210 #ifdef RAD_DEBUG
1211  if (ADVari::debug_file) {
1212  fclose(ADVari::debug_file);
1213  ADVari::debug_file = 0;
1214  }
1215  ADVari::gcgen_cur++;
1216  ADVari::last_opno = 0;
1217 #endif
1218  }
1219 
1220  template<typename Double> void
1221 ADcontext<Double>::Weighted_Gradcomp(size_t n, ADVar **V, Double *w)
1222 {
1223  size_t i;
1224  DErp *d;
1225 #ifdef RAD_AUTO_AD_Const
1226  ADVari *a, *anext;
1227  IndepADvar<Double> *v;
1228 #ifdef RAD_Const_WARN
1229  ADVari *cv;
1230  int j, k;
1231 #endif
1232 #endif /*RAD_AUTO_AD_Const*/
1233 
1234  ADVari::adc.derp_init(1);
1235 
1236  if ((d = DErp::LastDerp) != 0) {
1237  for(i = 0; i < n; i++)
1238  *V[i]->cv->aval = w[i];
1239 #ifdef RAD_DEBUG
1240  if (ADVari::debug_file)
1241  do {
1242  fprintf(ADVari::debug_file, "%d\t%d\t%g + %g * %g",
1243  d->c->opno, d->b->opno, *d->c->aval, *d->a, *d->b->aval);
1244  *d->c->aval += *d->a * *d->b->aval;
1245  fprintf(ADVari::debug_file, " = %g\n", *d->c->aval);
1246  } while((d = d->next));
1247  else
1248 #endif
1249  do *d->c->aval += *d->a * *d->b->aval;
1250  while((d = d->next));
1251  }
1252 #ifdef RAD_DEBUG
1253  if (ADVari::debug_file) {
1254  fclose(ADVari::debug_file);
1255  ADVari::debug_file = 0;
1256  }
1257  if (ADVari::debug_file)
1258  fflush(ADVari::debug_file);
1259  ADVari::gcgen_cur++;
1260  ADVari::last_opno = 0;
1261 #endif
1262  }
1263 
1264  template<typename Double> void
1265 ADcontext<Double>::Weighted_GradcompVec(size_t n, size_t *np, ADVar ***V, Double **w)
1266 {
1267  size_t i, ii, ni;
1268  ADVar **Vi;
1269  DErp *d;
1270  Double A, *D, *D1, *De, *wi;
1271 #ifdef RAD_AUTO_AD_Const
1272  ADVari *a, *anext;
1273  IndepADvar<Double> *v;
1274 #ifdef RAD_Const_WARN
1275  ADVari *cv;
1276  int j, k;
1277 #endif
1278 #endif /*RAD_AUTO_AD_Const*/
1279 
1280  ADVari::adc.derp_init(n);
1281  if (!n)
1282  return;
1283 
1284  if ((d = DErp::LastDerp) != 0) {
1285  for(i = 0; i < n; i++) {
1286  ni = np[i];
1287  wi = w[i];
1288  Vi = V[i];
1289  for(ii = 0; ii < ni; ++ii)
1290  Vi[ii]->cv->aval[i] = wi[ii];
1291  }
1292 #ifdef RAD_DEBUG
1293  if (ADVari::debug_file)
1294  do {
1295  D = d->c->aval;
1296  De = D + n;
1297  D1 = d->b->aval;
1298  A = *d->a;
1299  for(i = 0; i < n; ++i, ++D, ++D1) {
1300  fprintf(ADVari::debug_file, "%d:\t%d\t%d\t%g + %g * %g",
1301  i, d->c->opno, d->b->opno, *D, A, *D1);
1302  *D += A * *D1;
1303  fprintf(ADVari::debug_file, " = %g\n", *D);
1304  }
1305  } while((d = d->next));
1306  else
1307 #endif
1308  do {
1309  D = d->c->aval;
1310  De = D + n;
1311  D1 = d->b->aval;
1312  A = *d->a;
1313  do *D++ += A * *D1++; while(D < De);
1314  }
1315  while((d = d->next));
1316  }
1317 #ifdef RAD_DEBUG
1318  if (ADVari::debug_file) {
1319  fclose(ADVari::debug_file);
1320  ADVari::debug_file = 0;
1321  }
1322  if (ADVari::debug_file)
1323  fflush(ADVari::debug_file);
1324  ADVari::gcgen_cur++;
1325  ADVari::last_opno = 0;
1326 #endif
1327  }
1328 
1329  template<typename Double> void
1331 {
1332  Double w = 1;
1333  ADVar *v = &V;
1335  }
1336 
1337  template<typename Double>
1339 {
1340  cv = new ADVari(d);
1341  }
1342 
1343  template<typename Double>
1345 {
1346  cv = new ADVari(Double(i));
1347  }
1348 
1349  template<typename Double>
1351 {
1352  cv = new ADVari(Double(i));
1353  }
1354 
1355  template<typename Double>
1357 {
1358  cv = new ADVari(Double(i));
1359  }
1360 
1361  template<typename Double>
1363 {
1364  this->cv = new ConstADVari(0.);
1365  }
1366 
1367  template<typename Double> void
1369 {
1370  this->cv = new ConstADVari(d);
1371  }
1372 
1373  template<typename Double>
1375 {
1376  ConstADVari *y = new ConstADVari(x.cv->Val);
1377  DErp *d = new DErp(&x.adc.One, y, x.cv);
1378  this->cv = y;
1379  }
1380 
1381  template<typename Double>
1383 {
1384  ConstADVari *y = new ConstADVari(x.cv->Val);
1385  DErp *d = new DErp(&x.cv->adc.One, y, (ADVari*)x.cv);
1386  this->cv = y;
1387  }
1388 
1389  template<typename Double>
1391 {
1392  ConstADVari *y = new ConstADVari(x.Val);
1393  DErp *d = new DErp(&x.adc.One, y, &x);
1394  this->cv = y;
1395  }
1396 
1397  template<typename Double>
1398  void
1399 IndepADvar<Double>::AD_Const(const IndepADvar &v)
1400 {
1401  typedef ConstADvari<Double> ConstADVari;
1402 
1403  ConstADVari *ncv = new ConstADVari(v.val());
1404 #ifdef RAD_AUTO_AD_Const
1405  v.cv->padv = 0;
1406 #endif
1407  v.cv = ncv;
1408  }
1409 
1410  template<typename Double>
1411  int
1413 {
1414  return 1;
1415  }
1416 
1417  template<typename Double>
1418  void
1420 {
1422  while(x) {
1423  x->aval = 0;
1424  x = x->prevcad;
1425  }
1426  }
1427 
1428 #ifdef RAD_AUTO_AD_Const
1429 
1430  template<typename Double>
1431 ADvari<Double>::ADvari(const IndepADVar *x, Double d): Val(d), aval(0)
1432 {
1433  this->ADvari_padv(x);
1434  Linkup();
1435  }
1436 
1437  template<typename Double>
1438 ADvar1<Double>::ADvar1(const IndepADVar *x, const IndepADVar &y):
1439  ADVari(y.cv->Val), d((const Double*)&ADcontext<Double>::One, (ADVari*)this, y.cv)
1440 {
1441  this->ADvari_padv(x);
1442  }
1443 
1444  template<typename Double>
1445 ADvar1<Double>::ADvar1(const IndepADVar *x, const ADVari &y):
1446  ADVari(y.Val), d((const Double*)&ADcontext<Double>::One, this, &y)
1447 {
1448  this->ADvari_padv(x);
1449  }
1450 
1451 #else /* !RAD_AUTO_AD_Const */
1452 
1453  template<typename Double>
1454  IndepADvar<Double>&
1456 {
1457  This->cv = new ADvar1<Double>(x.Val, &x.adc.One, &x);
1458  return *(IndepADvar<Double>*) This;
1459  }
1460 
1461  template<typename Double>
1462  ADvar<Double>&
1464 {
1465  This->cv = new ADvar1<Double>(x.Val, &x.adc.One, &x);
1466  return *(ADvar<Double>*) This;
1467  }
1468 
1469 #endif /* RAD_AUTO_AD_Const */
1470 
1471 
1472  template<typename Double>
1473  IndepADvar<Double>&
1475 {
1476 #ifdef RAD_AUTO_AD_Const
1477  if (this->cv)
1478  this->cv->padv = 0;
1479  this->cv = new ADVari(this,d);
1480 #else
1481  this->cv = new ADVari(d);
1482 #endif
1483  return *this;
1484  }
1485 
1486  template<typename Double>
1487  ADvar<Double>&
1489 {
1490 #ifdef RAD_AUTO_AD_Const
1491  if (this->cv)
1492  this->cv->padv = 0;
1493  this->cv = new ADVari(this,d);
1494 #else
1495  this->cv = ConstADVari::cadc.fpval_implies_const
1496  ? new ConstADVari(d)
1497  : new ADVari(d);
1498 #endif
1499  return *this;
1500  }
1501 
1502  template<typename Double>
1505  return *(new ADvar1<Double>(-T.Val, &T.adc.negOne, &T));
1506  }
1507 
1508  template<typename Double>
1509  ADvari<Double>&
1511  return *(new ADvar2<Double>(L.Val + R.Val, &L, &L.adc.One, &R, &L.adc.One));
1512  }
1513 
1514 #ifdef RAD_AUTO_AD_Const
1515 #define RAD_ACA ,this
1516 #else
1517 #define RAD_ACA /*nothing*/
1518 #endif
1519 
1520  template<typename Double>
1521  ADvar<Double>&
1523  ADVari *Lcv = this->cv;
1524  this->cv = new ADvar2<Double>(Lcv->Val + R.Val, Lcv, &R.adc.One, &R, &R.adc.One RAD_ACA);
1525  return *this;
1526  }
1527 
1528  template<typename Double>
1530 operator+(const ADvari<Double> &L, Double R) {
1531  return *(new ADvar1<Double>(L.Val + R, &L.adc.One, &L));
1532  }
1533 
1534  template<typename Double>
1535  ADvar<Double>&
1537  ADVari *tcv = this->cv;
1538  this->cv = new ADVar1(tcv->Val + R, &tcv->adc.One, tcv RAD_ACA);
1539  return *this;
1540  }
1541 
1542  template<typename Double>
1544 operator+(Double L, const ADvari<Double> &R) {
1545  return *(new ADvar1<Double>(L + R.Val, &R.adc.One, &R));
1546  }
1547 
1548  template<typename Double>
1549  ADvari<Double>&
1551  return *(new ADvar2<Double>(L.Val - R.Val, &L, &L.adc.One, &R, &L.adc.negOne));
1552  }
1553 
1554  template<typename Double>
1555  ADvar<Double>&
1557  ADVari *Lcv = this->cv;
1558  this->cv = new ADvar2<Double>(Lcv->Val - R.Val, Lcv, &R.adc.One, &R, &R.adc.negOne RAD_ACA);
1559  return *this;
1560  }
1561 
1562  template<typename Double>
1564 operator-(const ADvari<Double> &L, Double R) {
1565  return *(new ADvar1<Double>(L.Val - R, &L.adc.One, &L));
1566  }
1567 
1568  template<typename Double>
1569  ADvar<Double>&
1571  ADVari *tcv = this->cv;
1572  this->cv = new ADVar1(tcv->Val - R, &tcv->adc.One, tcv RAD_ACA);
1573  return *this;
1574  }
1575 
1576  template<typename Double>
1578 operator-(Double L, const ADvari<Double> &R) {
1579  return *(new ADvar1<Double>(L - R.Val, &R.adc.negOne, &R));
1580  }
1581 
1582  template<typename Double>
1583  ADvari<Double>&
1585  return *(new ADvar2<Double>(L.Val * R.Val, &L, &R.Val, &R, &L.Val));
1586  }
1587 
1588  template<typename Double>
1589  ADvar<Double>&
1591  ADVari *Lcv = this->cv;
1592  this->cv = new ADvar2<Double>(Lcv->Val * R.Val, Lcv, &R.Val, &R, &Lcv->Val RAD_ACA);
1593  return *this;
1594  }
1595 
1596  template<typename Double>
1598 operator*(const ADvari<Double> &L, Double R) {
1599  return *(new ADvar1s<Double>(L.Val * R, R, &L));
1600  }
1601 
1602  template<typename Double>
1603  ADvar<Double>&
1605  ADVari *Lcv = this->cv;
1606  this->cv = new ADvar1s<Double>(Lcv->Val * R, R, Lcv RAD_ACA);
1607  return *this;
1608  }
1609 
1610  template<typename Double>
1612 operator*(Double L, const ADvari<Double> &R) {
1613  return *(new ADvar1s<Double>(L * R.Val, L, &R));
1614  }
1615 
1616  template<typename Double>
1617  ADvari<Double>&
1619  Double Lv = L.Val, Rv = R.Val, pL = 1. / Rv, q = Lv/Rv;
1620  return *(new ADvar2q<Double>(q, pL, -q*pL, &L, &R));
1621  }
1622 
1623  template<typename Double>
1624  ADvar<Double>&
1626  ADVari *Lcv = this->cv;
1627  Double Lv = Lcv->Val, Rv = R.Val, pL = 1. / Rv, q = Lv/Rv;
1628  this->cv = new ADvar2q<Double>(q, pL, -q*pL, Lcv, &R RAD_ACA);
1629  return *this;
1630  }
1631 
1632  template<typename Double>
1634 operator/(const ADvari<Double> &L, Double R) {
1635  return *(new ADvar1s<Double>(L.Val / R, 1./R, &L));
1636  }
1637 
1638  template<typename Double>
1639  ADvari<Double>&
1640 operator/(Double L, const ADvari<Double> &R) {
1641  Double recip = 1. / R.Val;
1642  Double q = L * recip;
1643  return *(new ADvar1s<Double>(q, -q*recip, &R));
1644  }
1645 
1646  template<typename Double>
1647  ADvar<Double>&
1649  ADVari *Lcv = this->cv;
1650  this->cv = new ADvar1s<Double>(Lcv->Val / R, 1./R, Lcv RAD_ACA);
1651  return *this;
1652  }
1653 
1654  template<typename Double>
1657  Double t = v.Val;
1658  return *(new ADvar1s<Double>(std::acos(t), -1./std::sqrt(1. - t*t), &v));
1659  }
1660 
1661  template<typename Double>
1662  ADvari<Double>&
1664  Double t = v.Val, t1 = std::sqrt(t*t - 1.);
1665  return *(new ADvar1s<Double>(std::log(t + t1), 1./t1, &v));
1666  }
1667 
1668  template<typename Double>
1669  ADvari<Double>&
1671  Double t = v.Val;
1672  return *(new ADvar1s<Double>(std::asin(t), 1./std::sqrt(1. - t*t), &v));
1673  }
1674 
1675  template<typename Double>
1676  ADvari<Double>&
1678  Double t = v.Val, td = 1., t1 = std::sqrt(t*t + 1.);
1679  if (t < 0.) {
1680  t = -t;
1681  td = -1.;
1682  }
1683  return *(new ADvar1s<Double>(td*std::log(t + t1), 1./t1, &v));
1684  }
1685 
1686  template<typename Double>
1687  ADvari<Double>&
1689  Double t = v.Val;
1690  return *(new ADvar1s<Double>(std::atan(t), 1./(1. + t*t), &v));
1691  }
1692 
1693  template<typename Double>
1694  ADvari<Double>&
1696  Double t = v.Val;
1697  return *(new ADvar1s<Double>(0.5*std::log((1.+t)/(1.-t)), 1./(1. - t*t), &v));
1698  }
1699 
1700  template<typename Double>
1701  ADvari<Double>&
1703  Double x = L.Val, y = R.Val, t = x*x + y*y;
1704  return *(new ADvar2q<Double>(std::atan2(x,y), y/t, -x/t, &L, &R));
1705  }
1706 
1707  template<typename Double>
1708  ADvari<Double>&
1709 atan2(Double x, const ADvari<Double> &R) {
1710  Double y = R.Val, t = x*x + y*y;
1711  return *(new ADvar1s<Double>(std::atan2(x,y), -x/t, &R));
1712  }
1713 
1714  template<typename Double>
1715  ADvari<Double>&
1716 atan2(const ADvari<Double> &L, Double y) {
1717  Double x = L.Val, t = x*x + y*y;
1718  return *(new ADvar1s<Double>(std::atan2(x,y), y/t, &L));
1719  }
1720 
1721  template<typename Double>
1722  ADvari<Double>&
1723 max(const ADvari<Double> &L, const ADvari<Double> &R) {
1724  const ADvari<Double> &x = L.Val >= R.Val ? L : R;
1725  return *(new ADvar1<Double>(x.Val, &x.adc.One, &x));
1726  }
1727 
1728  template<typename Double>
1729  ADvari<Double>&
1730 max(Double L, const ADvari<Double> &R) {
1731  if (L >= R.Val)
1732  return *(new ADvari<Double>(L));
1733  return *(new ADvar1<Double>(R.Val, &R.adc.One, &R));
1734  }
1735 
1736  template<typename Double>
1737  ADvari<Double>&
1738 max(const ADvari<Double> &L, Double R) {
1739  if (L.Val >= R)
1740  return *(new ADvar1<Double>(L.Val, &L.adc.One, &L));
1741  return *(new ADvari<Double>(R));
1742  }
1743 
1744  template<typename Double>
1745  ADvari<Double>&
1746 min(const ADvari<Double> &L, const ADvari<Double> &R) {
1747  const ADvari<Double> &x = L.Val <= R.Val ? L : R;
1748  return *(new ADvar1<Double>(x.Val, &x.adc.One, &x));
1749  }
1750 
1751  template<typename Double>
1752  ADvari<Double>&
1753 min(Double L, const ADvari<Double> &R) {
1754  if (L <= R.Val)
1755  return *(new ADvari<Double>(L));
1756  return *(new ADvar1<Double>(R.Val, &R.adc.One, &R));
1757  }
1758 
1759  template<typename Double>
1760  ADvari<Double>&
1761 min(const ADvari<Double> &L, Double R) {
1762  if (L.Val <= R)
1763  return *(new ADvar1<Double>(L.Val, &L.adc.One, &L));
1764  return *(new ADvari<Double>(R));
1765  }
1766 
1767  template<typename Double>
1768  ADvari<Double>&
1769 cos(const ADvari<Double> &v) {
1770  return *(new ADvar1s<Double>(std::cos(v.Val), -std::sin(v.Val), &v));
1771  }
1772 
1773  template<typename Double>
1774  ADvari<Double>&
1776  return *(new ADvar1s<Double>(std::cosh(v.Val), std::sinh(v.Val), &v));
1777  }
1778 
1779  template<typename Double>
1780  ADvari<Double>&
1781 exp(const ADvari<Double> &v) {
1782  ADvar1<Double>* rcv = new ADvar1<Double>(std::exp(v.Val), &v);
1783  rcv->d.a = &rcv->Val;
1784  rcv->d.b = rcv;
1785  return *rcv;
1786  }
1787 
1788  template<typename Double>
1789  ADvari<Double>&
1790 log(const ADvari<Double> &v) {
1791  Double x = v.Val;
1792  return *(new ADvar1s<Double>(std::log(x), 1. / x, &v));
1793  }
1794 
1795  template<typename Double>
1796  ADvari<Double>&
1798  static double num = 1. / std::log(10.);
1799  Double x = v.Val;
1800  return *(new ADvar1s<Double>(std::log10(x), num / x, &v));
1801  }
1802 
1803  template<typename Double>
1804  ADvari<Double>&
1805 pow(const ADvari<Double> &L, const ADvari<Double> &R) {
1806  Double x = L.Val, y = R.Val, t = std::pow(x,y);
1807  return *(new ADvar2q<Double>(t, y*t/x, t*std::log(x), &L, &R));
1808  }
1809 
1810  template<typename Double>
1811  ADvari<Double>&
1812 pow(Double x, const ADvari<Double> &R) {
1813  Double t = std::pow(x,R.Val);
1814  return *(new ADvar1s<Double>(t, t*std::log(x), &R));
1815  }
1816 
1817  template<typename Double>
1818  ADvari<Double>&
1819 pow(const ADvari<Double> &L, Double y) {
1820  Double x = L.Val, t = std::pow(x,y);
1821  return *(new ADvar1s<Double>(t, y*t/x, &L));
1822  }
1823 
1824  template<typename Double>
1825  ADvari<Double>&
1826 sin(const ADvari<Double> &v) {
1827  return *(new ADvar1s<Double>(std::sin(v.Val), std::cos(v.Val), &v));
1828  }
1829 
1830  template<typename Double>
1831  ADvari<Double>&
1833  return *(new ADvar1s<Double>(std::sinh(v.Val), std::cosh(v.Val), &v));
1834  }
1835 
1836  template<typename Double>
1837  ADvari<Double>&
1839  Double t = std::sqrt(v.Val);
1840  return *(new ADvar1s<Double>(t, 0.5/t, &v));
1841  }
1842 
1843  template<typename Double>
1844  ADvari<Double>&
1845 tan(const ADvari<Double> &v) {
1846  Double t = std::cos(v.Val);
1847  return *(new ADvar1s<Double>(std::tan(v.Val), 1./(t*t), &v));
1848  }
1849 
1850  template<typename Double>
1851  ADvari<Double>&
1853  Double t = 1. / std::cosh(v.Val);
1854  return *(new ADvar1s<Double>(std::tanh(v.Val), t*t, &v));
1855  }
1856 
1857  template<typename Double>
1858  ADvari<Double>&
1859 abs(const ADvari<Double> &v) {
1860  Double t, p;
1861  p = 1;
1862  if ((t = v.Val) < 0) {
1863  t = -t;
1864  p = -p;
1865  }
1866  return *(new ADvar1s<Double>(t, p, &v));
1867  }
1868 
1869  template<typename Double>
1870  ADvari<Double>&
1871 fabs(const ADvari<Double> &v) { // Synonym for "abs"
1872  // "fabs" is not the best choice of name,
1873  // but this name is used at Sandia.
1874  Double t, p;
1875  p = 1;
1876  if ((t = v.Val) < 0) {
1877  t = -t;
1878  p = -p;
1879  }
1880  return *(new ADvar1s<Double>(t, p, &v));
1881  }
1882 
1883  template<typename Double>
1884  ADvari<Double>&
1885 ADf1(Double f, Double g, const ADvari<Double> &x) {
1886  return *(new ADvar1s<Double>(f, g, &x));
1887  }
1888 
1889  template<typename Double>
1890  inline ADvari<Double>&
1891 ADf1(Double f, Double g, const IndepADvar<Double> &x) {
1892  return *(new ADvar1s<Double>(f, g, x.cv));
1893  }
1894 
1895  template<typename Double>
1896  ADvari<Double>&
1897 ADf2(Double f, Double gx, Double gy, const ADvari<Double> &x, const ADvari<Double> &y) {
1898  return *(new ADvar2q<Double>(f, gx, gy, &x, &y));
1899  }
1900 
1901  template<typename Double>
1902  ADvari<Double>&
1903 ADf2(Double f, Double gx, Double gy, const ADvari<Double> &x, const IndepADvar<Double> &y) {
1904  return *(new ADvar2q<Double>(f, gx, gy, &x, y.cv));
1905  }
1906 
1907  template<typename Double>
1908  ADvari<Double>&
1909 ADf2(Double f, Double gx, Double gy, const IndepADvar<Double> &x, const ADvari<Double> &y) {
1910  return *(new ADvar2q<Double>(f, gx, gy, x.cv, &y));
1911  }
1912 
1913  template<typename Double>
1914  ADvari<Double>&
1915 ADf2(Double f, Double gx, Double gy, const IndepADvar<Double> &x, const IndepADvar<Double> &y) {
1916  return *(new ADvar2q<Double>(f, gx, gy, x.cv, y.cv));
1917  }
1918 
1919  template<typename Double>
1920  ADvari<Double>&
1921 ADfn(Double f, int n, const IndepADvar<Double> *x, const Double *g) {
1922  return *(new ADvarn<Double>(f, n, x, g));
1923  }
1924 
1925  template<typename Double>
1926  inline ADvari<Double>&
1927 ADfn(Double f, int n, const ADvar<Double> *x, const Double *g) {
1928  return ADfn<Double>(f, n, (IndepADvar<Double>*)x, g);
1929  }
1930 
1931  template<typename Double>
1932  inline Double
1933 val(const ADvari<Double> &x) {
1934  return x.Val;
1935  }
1936 
1937 #undef RAD_ACA
1938 #define A (ADvari<Double>*)
1939 #ifdef RAD_Const_WARN
1940 #define C(x) (((x)->opno < 0) ? RAD_Const_Warn(x) : 0, *A x)
1941 #else
1942 #define C(x) *A x
1943 #endif
1944 #define T template<typename Double> inline
1945 #define F ADvari<Double>&
1946 #define Ai const ADvari<Double>&
1947 #define AI const IndepADvar<Double>&
1948 #define D Double
1949 #define T2(r,f) \
1950  T r f(Ai L, AI R) { return f(L, C(R.cv)); }\
1951  T r f(AI L, Ai R) { return f(C(L.cv), R); }\
1952  T r f(AI L, AI R) { return f(C(L.cv), C(R.cv)); }\
1953  T r f(AI L, D R) { return f(C(L.cv), R); }\
1954  T r f(Ai L, Dtype R) { return f(L, (D)R); }\
1955  T r f(AI L, Dtype R) { return f(C(L.cv), (D)R); }\
1956  T r f(Ai L, long R) { return f(L, (D)R); }\
1957  T r f(AI L, long R) { return f(C(L.cv), (D)R); }\
1958  T r f(Ai L, int R) { return f(L, (D)R); }\
1959  T r f(AI L, int R) { return f(C(L.cv), (D)R); }\
1960  T r f(D L, AI R) { return f(L, C(R.cv)); }\
1961  T r f(Dtype L, Ai R) { return f((D)L, R); }\
1962  T r f(Dtype L, AI R) { return f((D)L, C(R.cv)); }\
1963  T r f(long L, Ai R) { return f((D)L, R); }\
1964  T r f(long L, AI R) { return f((D)L, C(R.cv)); }\
1965  T r f(int L, Ai R) { return f((D)L, R); }\
1966  T r f(int L, AI R) { return f((D)L, C(R.cv)); }
1967 
1968 T2(F, operator+)
1969 T2(F, operator-)
1970 T2(F, operator*)
1971 T2(F, operator/)
1972 T2(F, atan2)
1973 T2(F, pow)
1974 T2(F, max)
1975 T2(F, min)
1976 T2(int, operator<)
1977 T2(int, operator<=)
1978 T2(int, operator==)
1979 T2(int, operator!=)
1980 T2(int, operator>=)
1981 T2(int, operator>)
1982 
1983 #undef T2
1984 #undef D
1985 
1986 #define T1(f)\
1987  T F f(AI x) { return f(C(x.cv)); }
1988 
1989 T1(operator+)
1990 T1(operator-)
1991 T1(abs)
1992 T1(acos)
1993 T1(acosh)
1994 T1(asin)
1995 T1(asinh)
1996 T1(atan)
1997 T1(atanh)
1998 T1(cos)
1999 T1(cosh)
2000 T1(exp)
2001 T1(log)
2002 T1(log10)
2003 T1(sin)
2004 T1(sinh)
2005 T1(sqrt)
2006 T1(tan)
2007 T1(tanh)
2008 T1(fabs)
2009 
2010 T F copy(AI x)
2011 {
2012  return *(new ADvar1<Double>(x.cv->Val, &ADcontext<Double>::One, (ADvari<Double>*)x.cv));
2013  }
2014 
2015 T F copy(Ai x)
2016 { return *(new ADvar1<Double>(x.Val, &ADcontext<Double>::One, (ADvari<Double>*)&x)); }
2017 
2018 #undef T1
2019 #undef AI
2020 #undef Ai
2021 #undef F
2022 #undef T
2023 #undef A
2024 #undef C
2025 #undef Ttype
2026 #undef Dtype
2027 #undef Padvinit
2028 
2029 } /* namespace RadVec */
2030 } /* namespace Sacado */
2031 
2032 #undef SNS
2033 
2034 #endif /* SACADO_TRADVEC_H */
const char * p
ADvar & operator+=(const ADVari &)
ADT_RAD ADvari< double > Ai
Double val(const ADvari< Double > &)
ADvari< Double > & operator-(const ADvari< Double > &T)
ADvari< Double > & max(const ADvari< Double > &L, const ADvari< Double > &R)
ADvari< Double > & operator/(const ADvari< Double > &L, const ADvari< Double > &R)
BigUInt< n > operator+(BigUInt< n > const &a, BigUInt< n > const &b)
#define RAD_ACA
void f()
asin(expr.val())
ADvari< Double > & atan(const ADvari< Double > &v)
ConstADvar & operator=(ADVari &d)
cosh(expr.val())
void AD_Const1(Double *, const IndepADvar< Double > &)
ADT_RAD IndepADvar< double > AI
ADvar1< Double > ADVar1
ADvarn(Double val1, int n1, const IndepADVar *x, const Double *g)
ADvari< Double > & ADf1(Double f, Double g, const IndepADvar< Double > &x)
Sacado::RadVec::ADvar< double > ADVar
ADvari< Double > ADVari
ADvari< Double > & log(const ADvari< Double > &v)
IndepADvar & operator=(IndepADvar &x)
static CADcontext< Double > cadc
ADVari::IndepADVar IndepADVar
IndepADvar< Double > IndepADVar
ADmemblock< Double > ADMemblock
ADvar(const ADVari &x)
static const Double One
static bool setget_fpval_implies_const(bool newval)
atan(expr.val())
ADvari< Double > & ADfn(Double f, int n, const IndepADvar< Double > *x, const Double *g)
void AD_Const1(Double *, const IndepADvar< Double > &)
#define R
Definition: Sacado_rad.hpp:161
void AD_Const(const IndepADvar< Double > &)
void ADvar_ctr(Double d)
static void Weighted_Gradcomp(size_t, ADVar **, Double *)
ADvari< Double > & ADf2(Double f, Double gx, Double gy, const IndepADvar< Double > &x, const IndepADvar< Double > &y)
bool operator!=(const Allocator< T > &a_t, const Allocator< U > &a_u)
ADvar & operator/=(const ADVari &)
ADvari< Double > & sinh(const ADvari< Double > &v)
ADvari< Double > & exp(const ADvari< Double > &v)
#define T
Definition: Sacado_rad.hpp:553
ADvari< Double > & acos(const ADvari< Double > &v)
ConstADvari< Double > ConstADVari
ADvar1(Double val1, const Double *a1, const ADVari *c1)
static void Outvar_Gradcomp(ADVar &v)
tanh(expr.val())
#define D
Definition: Sacado_rad.hpp:557
ADvari< Double > & cosh(const ADvari< Double > &v)
expr expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c *expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 c
ADVar::IndepADVar IndepADVar
FloatingPoint< double > Double
bool operator>(BigUInt< n > const &a, BigUInt< n > const &b)
#define A
Definition: Sacado_rad.hpp:552
#define T2(r, f)
Definition: Sacado_rad.hpp:558
ADvar & operator-=(const ADVari &)
IndepADvar< Double > & ADvar_operatoreq(IndepADvar< Double > *, const ADvari< Double > &)
static int rad_need_reinit
ADvar & operator=(const ADVari &x)
bool operator>=(BigUInt< n > const &a, BigUInt< n > const &b)
ADvar2(Double val1, const ADVari *Lcv, const Double *Lc, const ADVari *Rcv, const Double *Rc)
ADvar2q(Double val1, Double Lp, Double Rp, const ADVari *Lcv, const ADVari *Rcv)
static void Weighted_Gradcomp(size_t n, ADVar **v, Double *w)
static void Outvar_Gradcomp(ADvar &v)
int RAD_Const_Warn(void *v)
static void Weighted_Gradcomp(size_t n, ADvar **v, Double *w)
static void Weighted_GradcompVec(size_t, size_t *, ADVar ***, Double **)
ADvari< Double > & tan(const ADvari< Double > &v)
ADvari< Double > & fabs(const ADvari< Double > &v)
ADvari< Double > & asin(const ADvari< Double > &v)
sqrt(expr.val())
sinh(expr.val())
tan(expr.val())
static void AD_Const(const IndepADvar &)
ADvari< Double > & abs(const ADvari< Double > &v)
ADvari< Double > & atanh(const ADvari< Double > &v)
#define T1(r, f)
Definition: Sacado_rad.hpp:583
void g()
ADvari< Double > & pow(const ADvari< Double > &L, const ADvari< Double > &R)
IndepADvar< Double > IndepADVar
ADvari< Double > ADVari
IndepADVar::ADVari ADVari
atan2(expr1.val(), expr2.val())
ADvar & operator*=(const ADVari &)
static void set_fpval_implies_const(bool newval)
ADVar::ConstADVari ConstADVari
sin(expr.val())
ADvari< Double > & operator+(const ADvari< Double > &T)
bool operator==(const Handle< T > &h1, const Handle< T > &h2)
Compare two handles.
ADvari< Double > & log10(const ADvari< Double > &v)
ADvari< Double > & acosh(const ADvari< Double > &v)
log(expr.val())
ADvari< Double > & sqrt(const ADvari< Double > &v)
ADvari< Double > & atan2(const ADvari< Double > &L, const ADvari< Double > &R)
ConstADvar & operator=(Double d)
ADvari< Double > ADVari
ADvari< Double > & sin(const ADvari< Double > &v)
Turn ADvar into a meta-function class usable with mpl::apply.
acos(expr.val())
static ADcontext< Double > adc
static bool get_fpval_implies_const(void)
ADvari< Double > & tanh(const ADvari< Double > &v)
SACADO_INLINE_FUNCTION mpl::enable_if_c< ExprLevel< Expr< T1 > >::value==ExprLevel< Expr< T2 > >::value, Expr< PowerOp< Expr< T1 >, Expr< T2 > > > >::type pow(const Expr< T1 > &expr1, const Expr< T2 > &expr2)
ADvari< Double > & operator*(const ADvari< Double > &L, const ADvari< Double > &R)
static ConstADvari * lastcad
ADvari< Double > ADVari
ADvari< Double > & asinh(const ADvari< Double > &v)
ADvari< Double > & min(const ADvari< Double > &L, const ADvari< Double > &R)
ScalarType< value_type >::type scalar_type
static void Outvar_Gradcomp(ADVar &)
void AD_Const(const IndepADvar &v)
Definition: Sacado_rad.hpp:434
exp(expr.val())
ADvari< Double > & cos(const ADvari< Double > &v)
static void Weighted_GradcompVec(size_t n, size_t *np, ADvar ***v, Double **w)
#define Padvinit
int n
log10(expr.val())
ADvar1s(Double val1, Double a1, const ADVari *c1)
cos(expr.val())
ADvar1(Double val1, const ADVari *c1)
#define Ttype
static void Weighted_GradcompVec(size_t n, size_t *np, ADVar ***v, Double **w)
if(first)
Definition: uninit.c:110
const double y
Double adj(int n) const
static ADvari ** Last_ADvari