42 #if defined(RAD_DEBUG_BLOCKKEEP) && !defined(HAVE_SACADO_UNINIT)
43 #undef RAD_DEBUG_BLOCKKEEP
54 #undef RAD_ALLOW_WANTDERIV
55 #ifndef RAD_DISALLOW_WANTDERIV
56 #define RAD_ALLOW_WANTDERIV
62 #define RAD_REINIT_0(x)
63 #define RAD_REINIT_1(x)
64 #define RAD_REINIT_2(x)
67 #if RAD_REINIT == 1 //{{
69 #define RAD_REINIT_1(x) x
70 #ifdef RAD_ALLOW_WANTDERIV
71 #define ADvar ADvar_1n
72 #define IndepADvar IndepADvar_1n
75 #define IndepADvar IndepADvar_1
76 #endif //RAD_ALLOW_WANTDERIV
77 #elif RAD_REINIT == 2 //}{
79 #define RAD_REINIT_2(x) x
81 #define RAD_cvchk(x) if (x.gen != x.IndepADvar_root.gen) { x.cv = new ADvari<Double>(x.Val);\
82 x.gen = x.IndepADvar_root.gen; }
83 #ifdef RAD_ALLOW_WANTDERIV
84 #define IndepADvar IndepADvar_2n
85 #define ADvar ADvar_2n
87 #define IndepADvar IndepADvar_2
89 #endif //RAD_ALLOW_WANTDERIV
90 #elif RAD_REINIT != 0 //}{
91 Botch! Expected
"RAD_REINIT" to be 0, 1, or 2.
94 #undef RAD_ALLOW_WANTDERIV
96 #define RAD_REINIT_0(x) x
99 #ifdef RAD_ALLOW_WANTDERIV
100 #define Allow_noderiv(x) x
102 #define Allow_noderiv(x)
106 #undef RAD_Const_WARN
107 #undef RAD_AUTO_AD_Const
108 #undef RAD_DEBUG_BLOCKKEEP
114 #ifdef RAD_Const_WARN // ==> RAD_AUTO_AD_Const and RAD_DEBUG
115 #ifndef RAD_AUTO_AD_Const
116 #define RAD_AUTO_AD_Const
123 #endif // RAD_Const_WARN
130 #ifndef RAD_AUTO_AD_Const
131 #ifdef RAD_DEBUG_BLOCKKEEP
139 #ifdef RAD_AUTO_AD_Const
140 #undef RAD_DEBUG_BLOCKKEEP
142 #ifdef RAD_DEBUG_BLOCKKEEP
143 #if !(RAD_DEBUG_BLOCKKEEP > 0)
144 #undef RAD_DEBUG_BLOCKKEEP
146 extern "C" void _uninit_f2c(
void *
x,
int type,
long len);
148 template <
typename T>
149 struct UninitType {};
152 struct UninitType<float> {
153 static const int utype = 4;
157 struct UninitType<double> {
158 static const int utype = 5;
161 template <
typename T>
162 struct UninitType< std::complex<T> > {
163 static const int utype = UninitType<T>::utype + 2;
172 template<
typename T>
class
205 #define Dtype typename DoubleAvoid<Double>::dtype
206 #define Ltype typename DoubleAvoid<Double>::ltype
207 #define Itype typename DoubleAvoid<Double>::itype
208 #define Ttype typename DoubleAvoid<Double>::ttype
213 template<
typename Double>
class ADvar;
220 template<
typename Double>
class Derp;
222 template<
typename Double>
struct
227 char memblk[1000*
sizeof(double)];
230 template<
typename Double>
class
237 ADMemblock *Busy, *First, *
Free;
242 ADMemblock *DBusy, *DFree;
243 size_t DMleft, nderps;
245 #ifdef RAD_DEBUG_BLOCKKEEP
247 ADMemblock *rad_Oldcurmb;
249 void *new_ADmemblock(
size_t);
254 void *Memalloc(
size_t len);
255 static void Gradcomp(
int wantgrad);
257 static void aval_reset();
258 static void free_all();
259 static void re_init();
260 static void zero_out();
261 static void Weighted_Gradcomp(
size_t, ADVar**,
Double*);
262 static void Outvar_Gradcomp(ADVar&);
264 DErp *new_Derp(
const Double *,
const ADVari*,
const ADVari*);
270 template<
typename Double>
class
280 template<
typename Double>
class
287 inline void *
operator new(size_t,
Derp *
x) {
return x; }
288 inline void operator delete(
void*,
Derp *) {}
297 Derp(
const ADVari *);
299 Derp(
const Double *,
const ADVari *,
const ADVari *);
300 inline void *
operator new(
size_t len) {
return (
Derp*)ADVari::adc.Memalloc(len); }
304 #if RAD_REINIT > 0 //{
305 template<
typename Double> Derp<Double>*
306 ADcontext<Double>::new_Derp(
const Double *
a,
const ADvari<Double> *b,
const ADvari<Double> *
c)
311 if (this->DMleft == 0) {
318 this->DMleft = nderps;
320 rv = &((DErp*)DBusy->
memblk)[--this->DMleft];
328 #define Ai const Base< ADvari<Double> >&
329 #define AI const Base< IndepADvar<Double> >&
330 #define T template<typename Double>
357 #define F ADvari<Double>&
405 template<
typename Double>ADvari<Double>&
ADf1(
Double f,
Double g,
const IndepADvar<Double> &x);
407 const IndepADvar<Double> &x,
const IndepADvar<Double> &
y);
408 template<
typename Double>ADvari<Double>&
ADfn(
Double f,
int n,
409 const IndepADvar<Double> *x,
const Double *g);
411 template<
typename Double> IndepADvar<Double>&
ADvar_operatoreq(IndepADvar<Double>*,
412 const ADvari<Double>&);
413 template<
typename Double> ADvar<Double>&
ADvar_operatoreq(ADvar<Double>*,
const ADvari<Double>&);
414 template<
typename Double>
void AD_Const(
const IndepADvar<Double>&);
415 template<
typename Double>
void AD_Const1(
Double*,
const IndepADvar<Double>&);
416 template<
typename Double> ADvari<Double>&
ADf1(
Double,
Double,
const ADvari<Double>&);
418 const ADvari<Double>&,
const ADvari<Double>&);
420 const IndepADvar<Double>&,
const ADvari<Double>&);
422 const ADvari<Double>&,
const IndepADvar<Double>&);
423 template<
typename Double>
Double val(
const ADvari<Double>&);
425 template<
typename Double>
class
426 ADvari :
public Base< ADvari<Double> > {
431 #ifdef RAD_AUTO_AD_Const
433 #ifdef RAD_Const_WARN
434 mutable const IndepADVar *padv;
437 mutable const IndepADVar *padv;
438 #endif //RAD_Const_WARN
441 static ADvari *First_ADvari, **Last_ADvari;
443 inline void ADvari_padv(
const IndepADVar *v) {
445 Last_ADvari = &this->Next;
448 #endif //RAD_AUTO_AD_Const
452 static int gcgen_cur, last_opno, zap_gcgen, zap_gcgen1, zap_opno;
453 static FILE *debug_file;
458 void *
operator new(
size_t len) {
461 rv->gcgen = gcgen_cur;
462 rv->opno = ++last_opno;
463 if (last_opno == zap_opno && gcgen_cur == zap_gcgen)
464 printf(
"Got to opno %d\n", last_opno);
470 void operator delete(
void*) {}
471 #ifdef RAD_ALLOW_WANTDERIV
472 inline ADvari(
Double t): Val(t), aval(0.), wantderiv(1) {}
473 inline ADvari(): Val(0.), aval(0.), wantderiv(1) {}
474 inline void no_deriv() { wantderiv = 0; }
479 #ifdef RAD_AUTO_AD_Const
492 #define Ai const Base< ADvari >&
494 #define T1(r,f) F r f <>(Ai);
544 friend ADvari& ADfn<>(
Double f,
int n,
const IndepADVar *
x,
const Double *g);
546 inline operator Double() {
return this->Val; }
547 inline operator Double()
const {
return this->Val; }
552 template<
typename Double>
class
559 #ifdef RAD_ALLOW_WANTDERIV
560 if (!ADVari::adc.new_Derp(a1,
this,c1))
563 ADVari::adc.new_Derp(a1,
this,c1);
566 #else // RAD_REINIT == 0
570 ADVari(val1), d(a1,this,c1) {}
571 #ifdef RAD_AUTO_AD_Const
572 typedef typename ADVari::IndepADVar IndepADVar;
574 ADvar1(
const IndepADVar*,
const IndepADVar&);
575 ADvar1(
const IndepADVar*,
const ADVari&);
577 ADVari(val1), d(a1,this,c1) {
579 this->ADvari_padv(v);
582 #endif // RAD_REININT > 0
586 template<
typename Double>
class
587 ConstADvari:
public ADvari<Double> {
601 inline void *
operator new(
size_t len) {
return ADVari::adc.Memalloc(len); }
602 inline ConstADvari(
Double t): ADVari(t) {}
604 static void aval_reset(
void);
607 template<
typename Double>
class
613 #elif RAD_REINIT == 2
614 typedef unsigned long ADGenType;
615 mutable ADGenType gen;
621 template<
typename Double>
class
631 IndepADvar_root.ADvprev = (this->ADvprev = IndepADvar_root.ADvprev)->ADvnext =
this;
632 this->ADvnext = &IndepADvar_root;
643 (this->ADvnext->ADvprev = this->ADvprev)->ADvnext = this->ADvnext;
656 #if RAD_REINIT > 0 //{
657 template<
typename Double> IndepADvar_base0<Double>
658 IndepADvar_base<Double>::IndepADvar_root(0.);
660 template<
typename Double>
void
661 IndepADvar_base<Double>::Move_to_end() {
662 if (
this != IndepADvar_root.ADvprev) {
663 (this->ADvnext->ADvprev = this->ADvprev)->ADvnext = this->ADvnext;
664 IndepADvar_root.ADvprev = (this->ADvprev = IndepADvar_root.ADvprev)->ADvnext =
this;
665 this->ADvnext = &IndepADvar_root;
668 #elif RAD_REINIT == 2
669 template<
typename Double>
typename IndepADvar_base0<Double>::ADGenType
670 IndepADvar_base<Double>::gen0(1);
674 template<
typename Double>
class
675 IndepADvar:
protected IndepADvar_base<Double>,
public Base< IndepADvar<Double> > {
677 static void AD_Const(
const IndepADvar&);
683 #ifdef RAD_AUTO_AD_Const
688 #elif defined(RAD_EQ_ALIAS)
693 #endif //RAD_AUTO_AD_Const
710 #ifdef RAD_ALLOW_WANTDERIV
711 inline int Wantderiv() {
return this->wantderiv; }
720 #ifdef RAD_AUTO_AD_Const
722 inline IndepADvar() { cv = 0; }
723 inline ~IndepADvar() {
737 #ifdef RAD_Const_WARN
738 inline operator ADVari&()
const {
739 ADVari *tcv = this->cv;
744 inline operator ADVari*()
const {
745 ADVari *tcv = this->cv;
750 #else //RAD_Const_WARN
751 inline operator ADVari&()
const {
RAD_cvchk((*
this))
return *this->cv; }
752 inline operator ADVari*()
const {
RAD_cvchk((*
this))
return this->cv; }
753 #endif //RAD_Const_WARN
768 friend void AD_Const1<>(
Double*,
const IndepADvar&);
770 friend ADVari& ADf1<>(
Double,
Double,
const IndepADvar&);
789 #define Ai const Base< ADVari >&
790 #define AI const Base< IndepADvar >&
798 #define T1(f) friend ADVari& f<> (AI);
800 #define F friend ADVari&
850 template<
typename Double>
class
851 ADvar:
public IndepADvar<Double> {
861 #if RAD_REINIT == 0 //{
863 if (ConstADVari::cadc.fpval_implies_const)
864 x =
new ConstADVari(d);
866 #ifdef RAD_AUTO_AD_Const //{
867 x =
new ADVari((IndepADVar*)
this, d);
868 x->ADvari_padv(
this);
875 ADVari *x =
new ADVari(d);
891 #ifdef RAD_AUTO_AD_Const
892 inline ADvar(IndepADVar &x) {
893 this->cv = x.cv ?
new ADVar1(
this, x) : 0;
896 inline ADvar(ADVari &x) { this->cv = &
x; x.ADvari_padv(
this); }
897 inline ADvar& operator=(IndepADVar &x) {
900 this->cv =
new ADVar1(
this,x);
903 inline ADvar& operator=(ADVari &x) {
906 this->cv =
new ADVar1(
this, x);
910 friend ADvar& ADvar_operatoreq<>(ADvar*, const ADVari&);
913 inline ADvar(
const IndepADVar &x) {
915 this->cv = (ADVari*)x.cv;
917 inline ADvar(const ADVari &x) { this->cv = (ADVari*)&x; }
918 inline ADvar& operator=(IndepADVar &x) {
920 this->cv = (ADVari*)x.cv; return *this;
922 inline ADvar& operator=(const ADVari &x) { this->cv = (ADVari*)&x;
return *
this; }
924 ADvar(const IndepADVar &x) {
928 this->cv =
new ADVar1(x.cv->Val, &this->cv->adc.One, x.cv);
938 this->cv =
new ADVar1(x.
cv->Val, &this->cv->adc.One, (ADVari*)x.
cv);
946 this->cv =
new ADVar1(x.
Val, &this->cv->adc.One, &x);
952 ADvar& operator=(Double);
953 ADvar& operator+=(
const ADVari&);
954 ADvar& operator+=(Double);
955 ADvar& operator-=(
const ADVari&);
956 ADvar& operator-=(Double);
957 ADvar& operator*=(
const ADVari&);
958 ADvar& operator*=(Double);
959 ADvar& operator/=(
const ADVari&);
960 ADvar& operator/=(Double);
963 {
return ConstADVari::cadc.fpval_implies_const; }
965 { ConstADVari::cadc.fpval_implies_const = newval; }
967 bool oldval = ConstADVari::cadc.fpval_implies_const;
968 ConstADVari::cadc.fpval_implies_const = newval;
972 inline static bool get_fpval_implies_const(
void) {
return true; }
973 inline static void set_fpval_implies_const(
bool newval) {}
974 inline static bool setget_fpval_implies_const(
bool newval) {
return newval; }
980 static inline void aval_reset() { ConstADVari::aval_reset(); }
988 template<
typename Double>
992 template<
typename Double>
995 template<
typename Double>
996 inline void AD_Const(
const IndepADvar<Double>&v) {}
997 #endif //RAD_REINIT == 0
999 template<
typename Double>
class
1000 ConstADvar:
public ADvar<Double> {
1017 void ConstADvar_ctr(Double);
1027 #ifdef RAD_NO_CONST_UPDATE
1033 this->cv =
new ADVari(d);
1042 this->cv =
new ADVar1(
this,d);
1051 #ifdef RAD_ALLOW_WANTDERIV
1052 template<
typename Double>
class
1056 typedef ADvar<Double>
ADVar;
1057 typedef IndepADvar<Double> IndepADVar;
1058 typedef typename IndepADVar::ADVari ADVari;
1059 typedef ADvar1<Double> ADVar1;
1061 void ADvar_ndctr(Double d) {
1062 ADVari *x =
new ADVari(d);
1073 inline ADvar_nd(Double d): ADVar((
void*)0,0) { ADvar_ndctr(d); }
1077 this->cv =
new ADVar1(x.cv->Val, &this->cv->adc.One, x.cv);
1084 this->cv =
new ADVar1(x.Val, &this->cv->adc.One, &x);
1090 #define ADvar_nd ADvar
1091 #endif //RAD_ALLOW_WANTDERIV
1093 template<
typename Double>
class
1094 ADvar1s:
public ADvar1<Double> {
1098 #if RAD_REINIT > 0 //{{
1099 inline ADvar1s(Double val1, Double a1,
const ADVari *c1): ADVar1(val1,&a1,c1) {}
1102 ADvar1s(Double val1, Double a1,
const ADVari *c1): ADVar1(val1,&a,c1), a(a1) {}
1103 #ifdef RAD_AUTO_AD_Const
1105 ADvar1s(Double val1, Double a1,
const ADVari *c1,
const ADVar *v):
1106 ADVar1(val1,&a,c1,v), a(a1) {}
1108 #endif // }} RAD_REINIT > 0
1111 template<
typename Double>
class
1112 ADvar2:
public ADvari<Double> {
1117 #if RAD_REINIT > 0 //{{
1118 ADvar2(Double val1,
const ADVari *Lcv,
const Double *Lc,
1119 const ADVari *Rcv,
const Double *Rc): ADVari(val1) {
1120 #ifdef RAD_ALLOW_WANTDERIV
1122 Ld = ADVari::adc.new_Derp(Lc,
this, Lcv);
1123 Rd = ADVari::adc.new_Derp(Rc,
this, Rcv);
1127 ADVari::adc.new_Derp(Lc, this, Lcv);
1128 ADVari::adc.new_Derp(Rc,
this, Rcv);
1129 #endif //RAD_ALLOW_WANTDERIV
1131 #else //}{ RAD_REINIT == 0
1133 ADvar2(Double val1,
const ADVari *Lcv,
const Double *Lc,
1134 const ADVari *Rcv,
const Double *Rc):
1136 dR.
next = DErp::LastDerp;
1138 DErp::LastDerp = &dL;
1145 #ifdef RAD_AUTO_AD_Const
1147 ADvar2(Double val1,
const ADVari *Lcv,
const Double *Lc,
1148 const ADVari *Rcv,
const Double *Rc, ADVar *v):
1150 dR.
next = DErp::LastDerp;
1152 DErp::LastDerp = &dL;
1159 this->ADvari_padv(v);
1162 #endif // }} RAD_REINIT
1165 template<
typename Double>
class
1166 ADvar2q:
public ADvar2<Double> {
1171 #if RAD_REINIT > 0 //{{
1172 inline ADvar2q(Double val1, Double Lp, Double Rp,
const ADVari *Lcv,
const ADVari *Rcv):
1174 #ifdef RAD_ALLOW_WANTDERIV
1176 Ld = ADVari::adc.new_Derp(&Lp,
this, Lcv);
1177 Rd = ADVari::adc.new_Derp(&Rp,
this, Rcv);
1181 ADVari::adc.new_Derp(&Lp, this, Lcv);
1182 ADVari::adc.new_Derp(&Rp,
this, Rcv);
1183 #endif //RAD_ALLOW_WANTDERIV
1185 #else //}{ RADINIT == 0
1187 ADvar2q(Double val1, Double Lp, Double Rp,
const ADVari *Lcv,
const ADVari *Rcv):
1188 ADVar2(val1), a(Lp), b(Rp) {
1189 this->dR.next = DErp::LastDerp;
1190 this->dL.next = &this->dR;
1191 DErp::LastDerp = &this->dL;
1196 this->dL.b = this->dR.b =
this;
1198 #ifdef RAD_AUTO_AD_Const
1200 ADvar2q(Double val1, Double Lp, Double Rp,
const ADVari *Lcv,
1201 const ADVari *Rcv,
const ADVar *v):
1202 ADVar2(val1), a(Lp), b(Rp) {
1203 this->dR.next = DErp::LastDerp;
1204 this->dL.next = &this->dR;
1205 DErp::LastDerp = &this->dL;
1210 this->dL.b = this->dR.b =
this;
1212 this->ADvari_padv(v);
1215 #endif //}} RAD_REINIT > 0
1218 template<
typename Double>
class
1219 ADvarn:
public ADvari<Double> {
1224 #if RAD_REINIT > 0 // {{
1225 ADvarn(Double val1,
int n1,
const IndepADVar *x,
const Double *g): ADVari(val1) {
1226 #ifdef RAD_ALLOW_WANTDERIV
1228 for(i = nd = 0; i < n1; i++)
1229 if (ADVari::adc.new_Derp(g+i,
this, x[i].cv))
1234 for(
int i = 0; i < n1; i++)
1235 ADVari::adc.new_Derp(g+i,
this, x[i].cv);
1236 #endif // RAD_ALLOW_WANTDERIV
1242 ADvarn(Double val1,
int n1,
const IndepADVar *x,
const Double *g):
1243 ADVari(val1), n(n1) {
1248 a1 = a = (Double*)ADVari::adc.Memalloc(n*
sizeof(*a));
1249 d1 = Da = (DErp*)ADVari::adc.Memalloc(n*
sizeof(DErp));
1250 dlast = DErp::LastDerp;
1251 for(i = 0; i < n1; i++, d1++) {
1259 DErp::LastDerp = dlast;
1261 #endif //}} RAD_REINIT > 0
1264 template<
typename Double>
1269 template<
typename Double>
1275 template<
typename Double>
1276 inline int operator<(const Base< ADvari<Double> > &LL, Double
R) {
1280 template<
typename Double>
1281 inline int operator<(Double L, const Base< ADvari<Double> > &RR) {
1286 template<
typename Double>
1292 template<
typename Double>
1293 inline int operator<=(const Base< ADvari<Double> > &LL, Double
R) {
1297 template<
typename Double>
1298 inline int operator<=(Double L, const Base< ADvari<Double> > &RR) {
1303 template<
typename Double>
1309 template<
typename Double>
1314 template<
typename Double>
1320 template<
typename Double>
1326 template<
typename Double>
1331 template<
typename Double>
1337 template<
typename Double>
1343 template<
typename Double>
1348 template<
typename Double>
1354 template<
typename Double>
1360 template<
typename Double>
1365 template<
typename Double>
1371 template<
typename Double>
1374 return Mbase + (Mleft -= len);
1375 return new_ADmemblock(len);
1377 #if RAD_REINIT > 0 //{{
1379 template<
typename Double>
1382 template<
typename Double>
1386 template<
typename Double>
1388 a(*a1), b(b1), c(c1) {}
1389 #else //}{ RAD_REINIT == 0
1391 template<
typename Double>
1397 template<
typename Double>
1404 template<
typename Double>
1409 #endif //}} RAD_REINIT
1423 #ifdef RAD_AUTO_AD_Const
1429 #ifndef RAD_DEBUG_gcgen1
1430 #define RAD_DEBUG_gcgen1 -1
1442 First =
new ADMemblock;
1446 Mbase = (
char*)First->
memblk;
1447 Mleft =
sizeof(First->
memblk);
1448 rad_need_reinit = 0;
1449 #ifdef RAD_DEBUG_BLOCKKEEP
1450 rad_busy_blocks = 0;
1455 DBusy =
new ADMemblock;
1458 DMleft = nderps =
sizeof(DBusy->
memblk)/
sizeof(DErp);
1465 ADMemblock *mb, *mb1;
1467 for(mb = ADVari::adc.Busy; mb; mb = mb1) {
1471 for(mb = ADVari::adc.Free; mb; mb = mb1) {
1475 for(mb = ConstADVari::cadc.Busy; mb; mb = mb1) {
1479 ConstADVari::cadc.Busy = ADVari::adc.Busy = ADVari::adc.Free = 0;
1480 ConstADVari::cadc.Mleft = ADVari::adc.Mleft = 0;
1481 ConstADVari::cadc.Mbase = ADVari::adc.Mbase = 0;
1483 for(mb = ADVari::adc.DBusy; mb; mb = mb1) {
1487 for(mb = ADVari::adc.DFree; mb; mb = mb1) {
1491 ADVari::adc.DBusy = ADVari::adc.DFree = 0;
1492 ADVari::adc.DMleft = 0;
1493 ConstADVari::cadc.Mbase = ADVari::adc.Mbase = 0;
1495 ConstADVari::lastcad = 0;
1496 Derp<Double>::LastDerp = 0;
1504 if (ConstADVari::cadc.Busy || ADVari::adc.Busy || ADVari::adc.Free
1506 || ADVari::adc.DBusy || ADVari::adc.DFree
1509 ADVari::adc.do_init();
1510 ConstADVari::cadc.do_init();
1513 template<
typename Double>
void*
1516 ADMemblock *mb, *mb0, *mb1, *mbf, *
x;
1517 #ifdef RAD_AUTO_AD_Const
1520 #ifdef RAD_Const_WARN
1532 if ((rad_need_reinit & 1) &&
this == &ADVari::adc) {
1533 rad_need_reinit &= ~1;
1535 #ifdef RAD_DEBUG_BLOCKKEEP
1536 Mleft = rad_mleft_save;
1537 if (Mleft <
sizeof(First->
memblk))
1538 _uninit_f2c(Mbase + Mleft,
1539 UninitType<Double>::utype,
1540 (
sizeof(First->
memblk) - Mleft)
1542 if ((mb = Busy->
next)) {
1544 for(;; mb = mb->
next) {
1546 UninitType<Double>::utype,
1553 rad_Oldcurmb = Busy;
1554 if (rad_busy_blocks >= RAD_DEBUG_BLOCKKEEP) {
1555 rad_busy_blocks = 0;
1559 for(mb = Busy; mb != mb0; mb = mb1) {
1566 Mbase = (
char*)First->
memblk;
1567 Mleft =
sizeof(First->
memblk);
1574 for(mb = Busy; mb != mb0; mb = mb1) {
1581 Mbase = (
char*)First->
memblk;
1582 Mleft =
sizeof(First->
memblk);
1584 #ifdef RAD_AUTO_AD_Const // {
1585 *ADVari::Last_ADvari = 0;
1586 ADVari::Last_ADvari = &ADVari::First_ADvari;
1587 a = ADVari::First_ADvari;
1592 #ifdef RAD_Const_WARN
1593 if ((i = a->opno) > 0)
1596 v->
cv = cv =
new ADVari(v, a->
Val);
1600 v->
cv =
new ADVari(v, a->
Val);
1606 #endif // } RAD_AUTO_AD_Const
1607 #if RAD_REINIT > 0 //{
1609 while((mb1 = mb->
next)) {
1620 while((vb = vb->ADvnext) != vb0)
1621 if ((tcv = ((IADv*)vb)->cv))
1622 ((IADv*)vb)->cv =
new ADVari(tcv->
Val);
1623 #elif RAD_REINIT == 2
1628 return Mbase + (Mleft -= len);
1635 #ifdef RAD_DEBUG_BLOCKKEEP
1640 return (Mbase = (
char*)x->
memblk) +
1641 (Mleft =
sizeof(First->
memblk) - len);
1644 template<
typename Double>
void
1647 #if RAD_REINIT > 0 //{{
1651 if (ADVari::adc.rad_need_reinit && wantgrad) {
1652 mb = ADVari::adc.DBusy;
1653 d = ((DErp*)mb->
memblk) + ADVari::adc.DMleft;
1654 de = ((DErp*)mb->
memblk) + ADVari::adc.nderps;
1661 de = d + ADVari::adc.nderps;
1664 #else //}{ RAD_REINIT == 0
1667 if (ADVari::adc.rad_need_reinit && wantgrad) {
1668 for(d = DErp::LastDerp; d; d = d->
next)
1671 #endif //}} RAD_REINIT
1673 if (!(ADVari::adc.rad_need_reinit & 1)) {
1674 ADVari::adc.rad_need_reinit = 1;
1675 ADVari::adc.rad_mleft_save = ADVari::adc.Mleft;
1676 ADVari::adc.Mleft = 0;
1680 if (ADVari::gcgen_cur == ADVari::zap_gcgen1 && wantgrad) {
1682 if (!(fname = getenv(
"RAD_DEBUG_FILE")))
1683 fname =
"rad_debug.out";
1687 ADVari::debug_file = fopen(fname,
"w");
1688 ADVari::zap_gcgen1 = -1;
1691 #if RAD_REINIT > 0 //{{
1692 if (ADVari::adc.DMleft < ADVari::adc.nderps && wantgrad) {
1693 mb = ADVari::adc.DBusy;
1694 d = ((DErp*)mb->
memblk) + ADVari::adc.DMleft;
1695 de = ((DErp*)mb->
memblk) + ADVari::adc.nderps;
1699 if (ADVari::debug_file) {
1700 for(; d < de; d++) {
1701 fprintf(ADVari::debug_file,
"%d\t%d\t%g + %g * %g",
1704 fprintf(ADVari::debug_file,
" = %g\n", d->
c->
aval);
1714 de = d + ADVari::adc.nderps;
1717 #else //}{ RAD_REINIT == 0
1718 if ((d = DErp::LastDerp) && wantgrad) {
1721 if (ADVari::debug_file)
1723 fprintf(ADVari::debug_file,
"%d\t%d\t%g + %g * %g",
1726 fprintf(ADVari::debug_file,
" = %g\n", d->
c->
aval);
1727 }
while((d = d->
next));
1731 while((d = d->
next));
1734 if (ADVari::debug_file) {
1735 fclose(ADVari::debug_file);
1736 ADVari::debug_file = 0;
1739 #endif // }} RAD_REINIT
1741 if (ADVari::debug_file)
1742 fflush(ADVari::debug_file);
1743 ADVari::gcgen_cur++;
1744 ADVari::last_opno = 0;
1748 template<
typename Double>
void
1752 #if RAD_REINIT > 0 //{{
1756 if (ADVari::adc.rad_need_reinit) {
1757 mb = ADVari::adc.DBusy;
1758 d = ((DErp*)mb->
memblk) + ADVari::adc.DMleft;
1759 de = ((DErp*)mb->
memblk) + ADVari::adc.nderps;
1766 de = d + ADVari::adc.nderps;
1769 #else //}{ RAD_REINIT == 0
1772 if (ADVari::adc.rad_need_reinit) {
1773 for(d = DErp::LastDerp; d; d = d->
next)
1776 #endif //}} RAD_REINIT
1778 if (!(ADVari::adc.rad_need_reinit & 1)) {
1779 ADVari::adc.rad_need_reinit = 1;
1780 ADVari::adc.rad_mleft_save = ADVari::adc.Mleft;
1781 ADVari::adc.Mleft = 0;
1785 if (ADVari::gcgen_cur == ADVari::zap_gcgen1) {
1787 if (!(fname = getenv(
"RAD_DEBUG_FILE")))
1788 fname =
"rad_debug.out";
1792 ADVari::debug_file = fopen(fname,
"w");
1793 ADVari::zap_gcgen1 = -1;
1796 #if RAD_REINIT > 0 //{{
1797 if (ADVari::adc.DMleft < ADVari::adc.nderps) {
1798 for(i = 0; i < n; i++)
1799 V[i]->cv->
aval = w[i];
1800 mb = ADVari::adc.DBusy;
1801 d = ((DErp*)mb->
memblk) + ADVari::adc.DMleft;
1802 de = ((DErp*)mb->
memblk) + ADVari::adc.nderps;
1806 if (ADVari::debug_file) {
1807 for(; d < de; d++) {
1808 fprintf(ADVari::debug_file,
"%d\t%d\t%g + %g * %g",
1811 fprintf(ADVari::debug_file,
" = %g\n", d->
c->
aval);
1821 de = d + ADVari::adc.nderps;
1824 #else //}{ RAD_REINIT == 0
1825 if ((d = DErp::LastDerp) != 0) {
1826 for(i = 0; i < n; i++)
1827 V[i]->cv->
aval = w[i];
1829 if (ADVari::debug_file)
1831 fprintf(ADVari::debug_file,
"%d\t%d\t%g + %g * %g",
1834 fprintf(ADVari::debug_file,
" = %g\n", d->
c->
aval);
1835 }
while((d = d->
next));
1839 while((d = d->
next));
1842 if (ADVari::debug_file) {
1843 fclose(ADVari::debug_file);
1844 ADVari::debug_file = 0;
1847 #endif // }} RAD_REINIT
1849 if (ADVari::debug_file)
1850 fflush(ADVari::debug_file);
1851 ADVari::gcgen_cur++;
1852 ADVari::last_opno = 0;
1856 template<
typename Double>
void
1864 template<
typename Double>
void
1867 for(DErp *d = DErp::LastDerp; d; d = d->
next) {
1869 *(
const_cast<Double*
>(d->
a)) =
Double(0.0);
1881 template<
typename Double>
1886 RAD_REINIT_2(Val = d; this->gen = this->IndepADvar_root.gen;)
1889 template<typename Double>
1893 cv =
new ADVari(
Double(i));
1894 RAD_REINIT_2(Val = i; this->gen = this->IndepADvar_root.gen;)
1897 template<typename Double>
1901 cv =
new ADVari(
Double(i));
1902 RAD_REINIT_2(Val = i; this->gen = this->IndepADvar_root.gen;)
1905 template<typename Double>
1909 cv =
new ADVari(
Double(i));
1910 RAD_REINIT_2(Val = i; this->gen = this->IndepADvar_root.gen;)
1913 template<typename Double>
1917 this->cv =
new ConstADVari(0.);
1918 RAD_REINIT_2(this->Val = 0.; this->gen = this->IndepADvar_root.gen;)
1921 template<typename Double>
void
1925 this->cv =
new ConstADVari(d);
1926 RAD_REINIT_2(this->Val = d; this->gen = this->IndepADvar_root.gen;)
1929 template<typename Double>
1934 ConstADVari *
y =
new ConstADVari(x.
cv->Val);
1936 x.
cv->adc.new_Derp(&x.adc.One,
y, x.
cv);
1938 DErp *d =
new DErp(&x.adc.One,
y, x.
cv);
1941 RAD_REINIT_2(this->Val =
y->Val; this->gen = this->IndepADvar_root.gen;)
1944 template<
typename Double>
1949 ConstADVari *
y =
new ConstADVari(x.
cv->Val);
1951 x.
cv->adc.new_Derp(&x.
cv->adc.One,
y, x.
cv);
1953 DErp *d =
new DErp(&x.
cv->adc.One,
y, (ADVari*)x.
cv);
1956 RAD_REINIT_2(this->Val =
y->Val; this->gen = this->IndepADvar_root.gen;)
1959 template<
typename Double>
1963 ConstADVari *
y =
new ConstADVari(x.
Val);
1965 x.
adc.new_Derp(&x.
adc.One,
y, &x);
1967 DErp *d =
new DErp(&x.
adc.One,
y, &x);
1970 RAD_REINIT_2(this->Val =
y->Val; this->gen = this->IndepADvar_root.gen;)
1973 template<
typename Double>
1979 ConstADVari *ncv =
new ConstADVari(v.
val());
1980 #ifdef RAD_AUTO_AD_Const
1987 template<
typename Double>
1991 #ifdef RAD_ALLOW_WANTDERIV
1993 if (this->gen != this->IndepADvar_root.gen) {
1994 cv =
new ADVari(Val);
1995 this->gen = this->IndepADvar_root.gen;
1998 return this->wantderiv = cv->wantderiv = n;
2001 #endif // RAD_ALLOW_WANTDERIV
2004 template<
typename Double>
2014 #elif RAD_REINIT == 1
2019 #ifdef RAD_AUTO_AD_Const
2021 template<
typename Double>
2024 this->ADvari_padv(x);
2028 template<typename Double>
2031 this->ADvari_padv(x);
2035 template<typename Double>
2037 ADVari(y.cv->Val), d((const Double*)&
ADcontext<Double>::One, (ADVari*)this, y.cv)
2039 this->ADvari_padv(x);
2042 template<
typename Double>
2044 ADVari(y.Val), d((const Double*)&ADcontext<Double>::One, this, &y)
2046 this->ADvari_padv(x);
2051 template<
typename Double>
2057 RAD_REINIT_2(This->Val = x.
Val; This->gen = This->IndepADvar_root.gen;)
2061 template<
typename Double>
2067 RAD_REINIT_2(This->Val = x.
Val; This->gen = This->IndepADvar_root.gen;)
2074 template<
typename Double>
2078 #ifdef RAD_AUTO_AD_Const
2081 this->cv =
new ADVari(
this,d);
2083 this->cv =
new ADVari(d);
2090 template<
typename Double>
2094 #ifdef RAD_AUTO_AD_Const
2097 this->cv =
new ADVari(
this,d);
2099 this->cv =
RAD_REINIT_0(ConstADVari::cadc.fpval_implies_const
2100 ?
new ConstADVari(d)
2103 RAD_REINIT_2(this->Val = d; this->gen = this->IndepADvar_root.gen;)
2108 template<
typename Double>
2115 template<
typename Double>
2123 #ifdef RAD_AUTO_AD_Const
2124 #define RAD_ACA ,this
2129 template<
typename Double>
2132 ADVari *Lcv = this->cv;
2140 template<
typename Double>
2147 template<
typename Double>
2150 ADVari *tcv = this->cv;
2151 this->cv =
new ADVar1(tcv->
Val + R, &tcv->
adc.One, tcv
RAD_ACA);
2158 template<
typename Double>
2165 template<
typename Double>
2173 template<
typename Double>
2176 ADVari *Lcv = this->cv;
2184 template<
typename Double>
2191 template<
typename Double>
2194 ADVari *tcv = this->cv;
2195 this->cv =
new ADVar1(tcv->
Val - R, &tcv->
adc.One, tcv
RAD_ACA);
2202 template<
typename Double>
2209 template<
typename Double>
2217 template<
typename Double>
2220 ADVari *Lcv = this->cv;
2228 template<
typename Double>
2235 template<
typename Double>
2238 ADVari *Lcv = this->cv;
2246 template<
typename Double>
2253 template<
typename Double>
2258 Double Lv = L.
Val, Rv = R.
Val, pL = 1. / Rv, q = Lv/Rv;
2262 template<
typename Double>
2265 ADVari *Lcv = this->cv;
2266 Double Lv = Lcv->
Val, Rv = R.
Val, pL = 1. / Rv, q = Lv/Rv;
2274 template<
typename Double>
2281 template<
typename Double>
2285 Double recip = 1. / R.
Val;
2286 Double q = L * recip;
2290 template<
typename Double>
2293 ADVari *Lcv = this->cv;
2301 template<
typename Double>
2309 template<
typename Double>
2317 template<
typename Double>
2325 template<
typename Double>
2337 template<
typename Double>
2345 template<
typename Double>
2353 template<
typename Double>
2358 Double x = L.
Val,
y = R.
Val, t = x*x +
y*
y;
2362 template<
typename Double>
2366 Double
y = R.
Val, t = x*x + y*
y;
2370 template<
typename Double>
2374 Double x = L.
Val, t = x*x + y*
y;
2378 template<
typename Double>
2387 template<
typename Double>
2396 template<
typename Double>
2405 template<
typename Double>
2414 template<
typename Double>
2423 template<
typename Double>
2432 template<
typename Double>
2439 template<
typename Double>
2446 template<
typename Double>
2455 rcv->
d.a = &rcv->
Val;
2461 template<
typename Double>
2469 template<
typename Double>
2473 static double num = 1. /
std::log(10.);
2478 template<
typename Double>
2487 template<
typename Double>
2495 template<
typename Double>
2503 template<
typename Double>
2510 template<
typename Double>
2517 template<
typename Double>
2525 template<
typename Double>
2533 template<
typename Double>
2541 template<
typename Double>
2547 if ((t = v.
Val) < 0) {
2554 template<
typename Double>
2563 if ((t = v.
Val) < 0) {
2570 template<
typename Double>
2578 template<
typename Double>
2584 template<
typename Double>
2585 inline ADvari<Double>&
2590 template<
typename Double>
2596 template<
typename Double>
2602 template<
typename Double>
2608 template<
typename Double>
2614 template<
typename Double>
2620 template<
typename Double>
2621 inline ADvari<Double>&
2626 template<
typename Double>
2633 #define A (ADvari<Double>*)
2634 #ifdef RAD_Const_WARN
2635 #define C(x) (((x)->opno < 0) ? RAD_Const_Warn(x) : 0, *A x)
2639 #define T template<typename Double> inline
2640 #define F ADvari<Double>&
2641 #define Ai const Base< ADvari<Double> >&
2642 #define AI const Base< IndepADvar<Double> >&
2644 #define CAI(x,y) const IndepADvar<Double> & x = y.derived()
2645 #define CAi(x,y) const ADvari<Double> & x = y.derived()
2647 T r f(Ai LL, AI RR) { CAi(L,LL); CAI(R,RR); RAD_cvchk(R) return f(L, C(R.cv)); } \
2648 T r f(AI LL, Ai RR) { CAI(L,LL); CAi(R,RR); RAD_cvchk(L) return f(C(L.cv), R); }\
2649 T r f(AI LL, AI RR) { CAI(L,LL); CAI(R,RR); RAD_cvchk(L) RAD_cvchk(R) return f(C(L.cv), C(R.cv)); }\
2650 T r f(AI LL, D R) { CAI(L,LL); RAD_cvchk(L) return f(C(L.cv), R); } \
2651 T r f(D L, AI RR) { CAI(R,RR); RAD_cvchk(R) return f(L, C(R.cv)); } \
2652 T r f(Ai L, Dtype R) { return f(L, (D)R); }\
2653 T r f(AI L, Dtype R) { return f(L, (D)R); }\
2654 T r f(Ai L, Ltype R) { return f(L, (D)R); }\
2655 T r f(AI L, Ltype R) { return f(L, (D)R); }\
2656 T r f(Ai L, Itype R) { return f(L, (D)R); }\
2657 T r f(AI L, Itype R) { return f(L, (D)R); }\
2658 T r f(Dtype L, Ai R) { return f((D)L, R); }\
2659 T r f(Dtype L, AI R) {return f((D)L, R); }\
2660 T r f(Ltype L, Ai R) { return f((D)L, R); }\
2661 T r f(Ltype L, AI R) { return f((D)L, R); }\
2662 T r f(Itype L, Ai R) { return f((D)L, R); }\
2663 T r f(Itype L, AI R) { return f((D)L, R); }
2684 T F f(AI xx) { CAI(x,xx); RAD_cvchk(x) return f(C(x.cv)); }
2739 #undef Allow_noderiv
static void Outvar_Gradcomp(ADVar &v)
ADvari< Double > & asinh(const Base< ADvari< Double > > &vv)
ADT_RAD ADvari< double > Ai
ADvari< Double > & pow(const Base< ADvari< Double > > &LL, const Base< ADvari< Double > > &RR)
static ADcontext< Double > adc
ADvari< Double > & operator/(const Base< ADvari< Double > > &LL, const Base< ADvari< Double > > &RR)
static void aval_reset(void)
ADvari< Double > & exp(const Base< ADvari< Double > > &vv)
ADvar2q(Double val1, Double Lp, Double Rp, const ADVari *Lcv, const ADVari *Rcv)
void AD_Const1(Double *, const IndepADvar< Double > &)
ADT_RAD IndepADvar< double > AI
static CADcontext< Double > cadc
ADvar & operator+=(const ADVari &)
Sacado::RadVec::ADvar< double > ADVar
ADvari< Double > & abs(const Base< ADvari< Double > > &vv)
ADvari< Double > & ADf2(Double f, Double gx, Double gy, const IndepADvar< Double > &x, const IndepADvar< Double > &y)
void AD_Const(const IndepADvar< Double > &)
ADvar & operator*=(const ADVari &)
int operator!=(const Base< ADvari< Double > > &LL, const Base< ADvari< Double > > &RR)
static bool setget_fpval_implies_const(bool newval)
ADvari< Double > & log(const Base< ADvari< Double > > &vv)
Double val(const ADvari< Double > &)
IndepADvar & operator=(IndepADvar &x)
ADvari< Double > & cbrt(const Base< ADvari< Double > > &vv)
Turn ADvar into a meta-function class usable with mpl::apply.
ADvari< Double > & atan2(const Base< ADvari< Double > > &LL, const Base< ADvari< Double > > &RR)
ADvar1(Double val1, const ADVari *c1)
ConstADvar & operator=(ADVari &d)
Base class for Sacado types to control overload resolution.
IndepADvar< Double > & ADvar_operatoreq(IndepADvar< Double > *, const ADvari< Double > &)
ADvari< Double > & cosh(const Base< ADvari< Double > > &vv)
ADvari< Double > & sqrt(const Base< ADvari< Double > > &vv)
ADvari< Double > & max(const Base< ADvari< Double > > &LL, const Base< ADvari< Double > > &RR)
ADvar1s(Double val1, Double a1, const ADVari *c1)
static void AD_Const(const IndepADvar &)
IndepADvar() Allow_noderiv(
IndepADVar::ADVari ADVari
static void Gradcomp(int wantgrad)
ADvari< Double > & asin(const Base< ADvari< Double > > &vv)
static void Weighted_Gradcomp(size_t, ADVar **, Double *)
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
FloatingPoint< double > Double
ADvar2(Double val1, const ADVari *Lcv, const Double *Lc, const ADVari *Rcv, const Double *Rc)
int operator>(const Base< ADvari< Double > > &LL, const Base< ADvari< Double > > &RR)
static void set_fpval_implies_const(bool newval)
ADvari< Double > & atanh(const Base< ADvari< Double > > &vv)
ConstADvar & operator=(Double d)
int RAD_Const_Warn(void *v)
char memblk[1000 *sizeof(double)]
ADvar & operator-=(const ADVari &)
ADvar & operator=(const ADVari &x)
ADvar1(Double val1, const Double *a1, const ADVari *c1)
static void Weighted_Gradcomp(size_t n, ADVar **v, Double *w)
ScalarType< value_type >::type scalar_type
ADvari< Double > & acosh(const Base< ADvari< Double > > &vv)
static void Gradcomp(int wantgrad)
ADvari< Double > & min(const Base< ADvari< Double > > &LL, const Base< ADvari< Double > > &RR)
ADvar & operator/=(const ADVari &)
atan2(expr1.val(), expr2.val())
ADvari< Double > & tan(const Base< ADvari< Double > > &vv)
ADvari< Double > & log10(const Base< ADvari< Double > > &vv)
ADvari< Double > & acos(const Base< ADvari< Double > > &vv)
ADvari< Double > & operator-(const Base< ADvari< Double > > &TT)
ADvari< Double > & tanh(const Base< ADvari< Double > > &vv)
ADvari< Double > & fabs(const Base< ADvari< Double > > &vv)
ADmemblock< Double > ADMemblock
static void Outvar_Gradcomp(ADvar &v)
ADvari< Double > & operator*(const Base< ADvari< Double > > &LL, const Base< ADvari< Double > > &RR)
IndepADvar_base(Allow_noderiv(int wd))
ADvari< Double > & operator+(const Base< ADvari< Double > > &TT)
ADvarn(Double val1, int n1, const IndepADVar *x, const Double *g)
static void Outvar_Gradcomp(ADVar &)
int operator>=(const Base< ADvari< Double > > &LL, const Base< ADvari< Double > > &RR)
int operator==(const Base< ADvari< Double > > &LL, const Base< ADvari< Double > > &RR)
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 > & sinh(const Base< ADvari< Double > > &vv)
const derived_type & derived() const
IndepADvar< Double > IndepADVar
ADVari::IndepADVar IndepADVar
ADvari< Double > & sin(const Base< ADvari< Double > > &vv)
void * new_ADmemblock(size_t)
ADvari< Double > & cos(const Base< ADvari< Double > > &vv)
ADvari< Double > & ADf1(Double f, Double g, const IndepADvar< Double > &x)
void * Memalloc(size_t len)
static void Weighted_Gradcomp(size_t n, ADvar **v, Double *w)
IndepADvar< Double > IndepADVar
ADvari< Double > & ADfn(Double f, int n, const IndepADvar< Double > *x, const Double *g)
static bool get_fpval_implies_const(void)
ConstADvari< Double > ConstADVari
ADVar::ConstADVari ConstADVari
ADvari< Double > & atan(const Base< ADvari< Double > > &vv)
ADVar::IndepADVar IndepADVar