15 #ifndef SACADO_TRAD2_H
16 #define SACADO_TRAD2_H
24 #if defined(RAD_DEBUG_BLOCKKEEP) && !defined(HAVE_SACADO_UNINIT)
25 #undef RAD_DEBUG_BLOCKKEEP
28 #ifdef RAD_Const_WARN // ==> RAD_AUTO_AD_Const and RAD_DEBUG
29 #ifndef RAD_AUTO_AD_Const
30 #define RAD_AUTO_AD_Const
37 #endif // RAD_Const_WARN
44 #ifndef RAD_AUTO_AD_Const
45 #ifdef RAD_DEBUG_BLOCKKEEP
54 #ifndef RAD_NO_USING_STDCC
75 #ifdef RAD_AUTO_AD_Const
76 #undef RAD_DEBUG_BLOCKKEEP
78 #ifdef RAD_DEBUG_BLOCKKEEP
79 #if !(RAD_DEBUG_BLOCKKEEP > 0)
80 #undef RAD_DEBUG_BLOCKKEEP
82 extern "C" void _uninit_f2c(
void *
x,
int type,
long len);
88 struct UninitType<float> {
89 static const int utype = 4;
93 struct UninitType<double> {
94 static const int utype = 5;
98 struct UninitType< std::complex<T> > {
99 static const int utype = UninitType<T>::utype + 2;
108 template<
typename T>
class
121 #define Dtype typename DoubleAvoid<Double>::dtype
122 #define Ttype typename DoubleAvoid<Double>::ttype
127 template<
typename Double>
class ADvar;
137 template<
typename Double>
class Derp;
139 template<
typename Double>
struct
147 template<
typename Double>
class
151 enum { Gulp = 1021 };
154 ADVari *pADvari[Gulp];
157 template<
typename Double>
class
176 double First0[(
sizeof(ADMemblock) +
sizeof(
double) - 1) /
sizeof(
double)];
177 double First1[(
sizeof(ADVari_block) +
sizeof(
double) - 1) /
sizeof(
double)];
178 void *new_ADmemblock(
size_t);
179 void new_ADvari_block();
184 #ifdef RAD_DEBUG_BLOCKKEEP
186 ADMemblock *rad_Oldcurmb;
191 void *Memalloc(
size_t len);
192 static void Gradcomp(
int wantgrad);
194 static void aval_reset(
void);
195 static void Weighted_Gradcomp(
int, ADVar**,
Double*);
198 if (Ainext >= Ailimit)
204 template<
typename Double>
class
213 template<
typename Double>
class
224 Derp(
const ADVari *);
226 Derp(
const Double *,
const ADVari *,
const ADVari *);
233 #define Ai const ADvari<Double>&
234 #define AI const IndepADvar<Double>&
235 #define T template<typename Double>
262 #define F ADvari<Double>&
311 #define SNS Sacado::Rad2
335 template<
typename Double>ADvari<Double>&
ADf1(
Double f,
Double g,
const IndepADvar<Double> &
x);
337 const IndepADvar<Double> &
x,
const IndepADvar<Double> &
y);
338 template<
typename Double>ADvari<Double>&
ADfn(
Double f,
int n,
339 const IndepADvar<Double> *
x,
const Double *g);
341 template<
typename Double> IndepADvar<Double>&
ADvar_operatoreq(IndepADvar<Double>*,
342 const ADvari<Double>&);
343 template<
typename Double> ADvar<Double>&
ADvar_operatoreq(ADvar<Double>*,
const ADvari<Double>&);
344 template<
typename Double>
void AD_Const(
const IndepADvar<Double>&);
345 template<
typename Double>
void AD_Const1(
Double*,
const IndepADvar<Double>&);
346 template<
typename Double> ADvari<Double>&
ADf1(
Double,
Double,
const ADvari<Double>&);
348 const ADvari<Double>&,
const ADvari<Double>&);
350 const IndepADvar<Double>&,
const ADvari<Double>&);
352 const ADvari<Double>&,
const IndepADvar<Double>&);
353 template<
typename Double>
Double val(
const ADvari<Double>&);
369 template<
typename Double> ADvari<Double>&
372 template<
typename Double> ADvari<Double>&
374 Double hxy,
Double hyy,
const ADvari<Double> &
x,
const ADvari<Double> &
y);
376 template<
typename Double> ADvari<Double>&
379 template<
typename Double>
class
385 #ifdef RAD_AUTO_AD_Const
387 #ifdef RAD_Const_WARN
388 mutable const IndepADVar *padv;
391 mutable const IndepADVar *padv;
392 #endif //RAD_Const_WARN
395 static ADvari *First_ADvari, **Last_ADvari;
397 inline void ADvari_padv(
const IndepADVar *v) {
399 Last_ADvari = &this->Next;
402 #endif //RAD_AUTO_AD_Const
406 static int gcgen_cur, last_opno, zap_gcgen, zap_gcgen1, zap_opno;
407 static FILE *debug_file;
416 void *
operator new(
size_t len) {
419 rv->gcgen = gcgen_cur;
420 rv->opno = ++last_opno;
421 if (last_opno == zap_opno && gcgen_cur == zap_gcgen)
428 void operator delete(
void*) {}
430 opclass(oc), Val(t), aval(0.), dO(0.)
433 opclass(oc), Val(t), aval(ta), dO(0.)
436 inline ADvari(): Val(0.), aval(0.), dO(0.) {}
439 #ifdef RAD_AUTO_AD_Const
453 #define Ai const ADvari&
454 #define T1(r,f) F r f <>(Ai);
457 F r f <>(Ttype,Ai); \
458 F r f <>(Ai,Ttype); \
459 F r f <>(double,Ai); \
460 F r f <>(Ai,double); \
509 friend ADvari& ADfn<>(
Double f,
int n,
const IndepADVar *
x,
512 inline operator Double() {
return this->Val; }
513 inline operator Double()
const {
return this->Val; }
516 template<
typename Double>
class
517 ADvar1:
public ADvari<Double> {
522 ADVari(oc,val1), d(a1,this,c1) {}
523 #ifdef RAD_AUTO_AD_Const
524 typedef typename ADVari::IndepADVar IndepADVar;
526 ADvar1(
const IndepADVar*,
const IndepADVar&);
527 ADvar1(
const IndepADVar*,
const ADVari&);
529 const ADVari *c1,
const ADVar *v):
530 ADVari(oc,val1), d(a1,this,c1) {
532 this->ADvari_padv(v);
538 template<
typename Double>
class
539 ConstADvari:
public ADvari<Double> {
550 static void aval_reset(
void);
554 template<
typename Double>
class
559 #ifdef RAD_AUTO_AD_Const
563 #elif defined(RAD_EQ_ALIAS)
568 #endif //RAD_AUTO_AD_Const
586 #ifdef RAD_AUTO_AD_Const
588 inline IndepADvar(
const IndepADvar &
x) { cv = x.
cv ?
new ADvar1<Double>(
this,
x) : 0; };
589 inline IndepADvar(ADVari *ncv) { cv = ncv; }
590 inline IndepADvar() { cv = 0; }
591 inline ~IndepADvar() {
602 friend IndepADvar& ADvar_operatoreq<>(IndepADvar*,
const ADVari&);
605 #ifdef RAD_Const_WARN
606 inline operator ADVari&()
const {
607 ADVari *tcv = this->cv;
612 inline operator ADVari*()
const {
613 ADVari *tcv = this->cv;
618 #else //RAD_Const_WARN
619 inline operator ADVari&()
const {
return *this->cv; }
620 inline operator ADVari*()
const {
return this->cv; }
621 #endif //RAD_Const_WARN
626 friend void AD_Const1<>(
Double*,
const IndepADvar&);
628 friend ADVari& ADf1<>(
Double,
Double,
const IndepADvar&);
637 static inline void Hvprod(
int n, ADVar **vp, Double *v, Double *hv)
647 #define Ai const ADVari&
648 #define AI const IndepADvar&
662 #define T1(f) friend ADVari& f<> (AI);
664 #define F friend ADVari&
713 template<
typename Double>
class
714 ADvar:
public IndepADvar<Double> {
725 if (ConstADVari::cadc.fpval_implies_const)
726 x =
new ConstADVari(d);
728 #ifdef RAD_AUTO_AD_Const
729 x =
new ADVari((IndepADVar*)
this, d);
730 x->ADvari_padv(
this);
746 #ifdef RAD_AUTO_AD_Const
748 this->cv = x.cv ?
new ADVar1(
this, x) : 0;
750 ADvar(ADVari &x) :IndepADVar(&x) { x.ADvari_padv((IndepADVar*)
this);}
751 inline ADvar& operator=(IndepADVar &x) {
754 this->cv =
new ADVar1(
this,x);
757 inline ADvar& operator=(ADVari &x) {
760 this->cv =
new ADVar1(
this, x);
764 friend ADvar& ADvar_operatoreq<>(ADvar*, const ADVari&);
767 inline ADvar(
const IndepADVar &x) { this->cv = (ADVari*)x.cv; }
768 inline ADvar(
const ADVari &x) { this->cv = (ADVari*)&x; }
769 inline ADvar& operator=(IndepADVar &x) { this->cv = (ADVari*)x.cv;
return *
this; }
770 inline ADvar& operator=(
const ADVari &x) { this->cv = (ADVari*)&x;
return *
this; }
772 friend ADvar& ADvar_operatoreq<>(ADvar*, const ADVari&);
773 ADvar(
const IndepADVar &x) { this->cv = x.
cv ?
new ADVar1(x.
cv->Val, &this->cv->adc.One, x.
cv) : 0; }
775 new ADVar1(
Hv_copy, x.
cv->Val, &this->cv->adc.One, (ADVari*)x.
cv) : 0; }
776 ADvar(
const ADVari &x) { this->cv =
new ADVar1(
Hv_copy, x.
Val, &this->cv->adc.One, &x); }
781 ADvar& operator=(Double);
782 ADvar& operator+=(
const ADVari&);
783 ADvar& operator+=(Double);
784 ADvar& operator-=(
const ADVari&);
785 ADvar& operator-=(Double);
786 ADvar& operator*=(
const ADVari&);
787 ADvar& operator*=(Double);
788 ADvar& operator/=(
const ADVari&);
789 ADvar& operator/=(Double);
791 {
return ConstADVari::cadc.fpval_implies_const; }
793 { ConstADVari::cadc.fpval_implies_const = newval; }
795 bool oldval = ConstADVari::cadc.fpval_implies_const;
796 ConstADVari::cadc.fpval_implies_const = newval;
803 static inline void aval_reset() { ConstADVari::aval_reset(); }
808 template<
typename Double>
812 template<
typename Double>
815 template<
typename Double>
class
816 ConstADvar:
public ADvar<Double> {
832 void ConstADvar_ctr(Double);
842 #ifdef RAD_NO_CONST_UPDATE
850 template<
typename Double>
class
851 ADvar1s:
public ADvar1<Double> {
856 ADvar1s(Double val1, Double a1,
const ADVari *c1):
858 #ifdef RAD_AUTO_AD_Const
861 ADvar1s(Double val1, Double a1,
const ADVari *c1,
const ADVar *v):
866 template<
typename Double>
class
867 ADvar1g:
public ADvar1<Double> {
873 ADvar1g(Double val1, Double d1, Double d2,
const ADVari *c1):
874 ADVar1(
Hv_unary,val1,&pL,c1), pL(d1), pL2(d2) {}
877 template<
typename Double>
class
878 ADvar2:
public ADvari<Double> {
884 const ADVari *Rcv,
const Double *Rc):
886 dR.
next = DErp::LastDerp;
888 DErp::LastDerp = &dL;
895 #ifdef RAD_AUTO_AD_Const
898 const ADVari *Rcv,
const Double *Rc, ADVar *v):
900 dR.
next = DErp::LastDerp;
902 DErp::LastDerp = &dL;
909 this->ADvari_padv(v);
914 template<
typename Double>
class
915 ADvar2q:
public ADvar2<Double> {
924 ADvar2q(Double val1, Double Lp, Double Rp, Double LR, Double R2,
925 const ADVari *Lcv,
const ADVari *Rcv):
927 pL(Lp), pR(Rp), pLR(LR), pR2(R2) {}
928 #ifdef RAD_AUTO_AD_Const
930 ADvar2q(Double val1, Double Lp, Double Rp, Double LR, Double R2,
931 const ADVari *Lcv,
const ADVari *Rcv,
const ADVar *v):
932 ADVar2(
Hv_quotLR,val1,Lcv,&pL,Rcv,&pR,v),
933 pL(Lp), pR(Rp), pLR(LR), pR2(R2) {}
937 template<
typename Double>
class
938 ADvar2g:
public ADvar2<Double> {
947 ADvar2g(Double val1, Double Lp, Double Rp, Double L2, Double LR, Double R2,
948 const ADVari *Lcv,
const ADVari *Rcv):
950 pL(Lp), pR(Rp), pL2(L2), pLR(LR), pR2(R2) { }
953 template<
typename Double>
class
954 ADvarn:
public ADvari<Double> {
964 ADvarn(Double val1,
int n1,
const IndepADVar *x,
const Double *g,
const Double *h):
970 a1 = G = (Double*)ADVari::adc.Memalloc(n1*
sizeof(*g));
971 d1 = D = (DErp*)ADVari::adc.Memalloc(n1*
sizeof(DErp));
972 dlast = DErp::LastDerp;
973 for(i = 0; i < n1; i++, d1++) {
981 DErp::LastDerp = dlast;
982 nh = (n1*(n1+1)) >> 1;
983 a1 = H = (
double*)ADVari::adc.Memalloc(nh *
sizeof(*h));
984 for(i = 0; i < nh; i++)
989 template<
typename Double>
992 template<
typename Double>
993 inline int operator<(const ADvari<Double> &L,
const ADvari<Double> &
R) {
return L.Val <
R.Val; }
994 template<
typename Double>
995 inline int operator<(const ADvari<Double> &L, Double
R) {
return L.Val <
R; }
996 template<
typename Double>
997 inline int operator<(Double L, const ADvari<Double> &
R) {
return L <
R.Val; }
999 template<
typename Double>
1000 inline int operator<=(const ADvari<Double> &L,
const ADvari<Double> &
R) {
return L.Val <=
R.Val; }
1001 template<
typename Double>
1002 inline int operator<=(const ADvari<Double> &L, Double
R) {
return L.Val <=
R; }
1003 template<
typename Double>
1004 inline int operator<=(Double L, const ADvari<Double> &
R) {
return L <=
R.Val; }
1006 template<
typename Double>
1008 template<
typename Double>
1010 template<
typename Double>
1013 template<
typename Double>
1015 template<
typename Double>
1017 template<
typename Double>
1020 template<
typename Double>
1022 template<
typename Double>
1024 template<
typename Double>
1027 template<
typename Double>
1029 template<
typename Double>
1031 template<
typename Double>
1034 template<
typename Double>
1037 return Mbase + (Mleft -= len);
1038 return new_ADmemblock(len);
1041 template<
typename Double>
1047 template<
typename Double>
1053 template<
typename Double>
1055 a(a1), b(b1),
c((ADVari*)c1) {
1069 #ifdef RAD_AUTO_AD_Const
1075 #ifndef RAD_DEBUG_gcgen1
1076 #define RAD_DEBUG_gcgen1 -1
1087 template<
typename Double>
1096 Mbase = (
char*)First->memblk;
1097 Mleft =
sizeof(First->memblk);
1102 fb->
limit = Ailimit = fb->
pADvari + ADVari_block::Gulp;
1104 #ifdef RAD_DEBUG_BLOCKKEEP
1105 rad_busy_blocks = 0;
1111 template<
typename Double>
void*
1116 #ifdef RAD_AUTO_AD_Const
1119 #ifdef RAD_Const_WARN
1128 Aibusy = b = AiFirst;
1131 Ailimit = b->
limit = (Ainext = b->
pADvari) + ADVari_block::Gulp;
1132 #ifdef RAD_DEBUG_BLOCKKEEP
1133 Mleft = rad_mleft_save;
1134 if (Mleft <
sizeof(First->memblk))
1135 _uninit_f2c(Mbase + Mleft,
1136 UninitType<Double>::utype,
1137 (
sizeof(First->memblk) - Mleft)
1139 if ((mb = Busy->
next)) {
1140 if (!(mb0 = rad_Oldcurmb))
1142 for(;; mb = mb->
next) {
1144 UninitType<Double>::utype,
1145 sizeof(First->memblk)
1151 rad_Oldcurmb = Busy;
1152 if (rad_busy_blocks >= RAD_DEBUG_BLOCKKEEP) {
1153 rad_busy_blocks = 0;
1157 for(mb = Busy; mb != mb0; mb = mb1) {
1164 Mbase = (
char*)First->
memblk;
1165 Mleft =
sizeof(First->memblk);
1172 for(mb = Busy; mb != mb0; mb = mb1) {
1179 Mbase = (
char*)First->
memblk;
1180 Mleft =
sizeof(First->memblk);
1181 #ifdef RAD_AUTO_AD_Const
1183 ADVari::adc.rad_need_reinit &= ~2;
1184 *ADVari::Last_ADvari = 0;
1185 ADVari::Last_ADvari = &ADVari::First_ADvari;
1186 anext = ADVari::First_ADvari;
1188 while((a = anext)) {
1191 #ifdef RAD_Const_WARN
1192 if ((i = a->opno) > 0)
1195 v->
cv = cv =
new ADVari(v, a->
Val, a->
aval);
1208 return Mbase + (Mleft -= len);
1215 #ifdef RAD_DEBUG_BLOCKKEEP
1220 return (Mbase = (
char*)x->
memblk) +
1221 (Mleft =
sizeof(First->memblk) - len);
1224 template<
typename Double>
void
1229 ob->
limit = Ailimit;
1234 Aibusy = Aibusy->
next = nb;
1235 nb->
limit = Ailimit = (Ainext = nb->
pADvari) + ADVari_block::Gulp;
1241 template<
typename Double>
void
1247 for(d = DErp::LastDerp; d; d = d->
next)
1250 if (!(ADVari::adc.rad_need_reinit & 1)) {
1251 ADVari::adc.rad_need_reinit = 1;
1252 ADVari::adc.rad_mleft_save = ADVari::adc.Mleft;
1253 ADVari::adc.Mleft = 0;
1256 if (ADVari::gcgen_cur == ADVari::zap_gcgen1) {
1258 if (!(fname = getenv(
"RAD_DEBUG_FILE")))
1259 fname =
"rad_debug.out";
1263 ADVari::debug_file = fopen(fname,
"w");
1264 ADVari::zap_gcgen1 = -1;
1267 if ((d = DErp::LastDerp) != 0) {
1268 #ifdef RAD_AUTO_AD_Const
1269 ADVari::adc.rad_need_reinit |= 2;
1274 if (ADVari::debug_file)
1276 fprintf(ADVari::debug_file,
"%d\t%d\t%g + %g * %g",
1277 d->
c->opno, d->
b->opno, d->
c->
aval,
1280 fprintf(ADVari::debug_file,
" = %g\n", d->
c->
aval);
1281 fflush(ADVari::debug_file);
1282 }
while((d = d->
next));
1286 while((d = d->
next));
1291 if (ADVari::debug_file) {
1292 fclose(ADVari::debug_file);
1293 ADVari::debug_file = 0;
1297 ADVari::gcgen_cur++;
1298 ADVari::last_opno = 0;
1302 template<
typename Double>
void
1307 #ifdef RAD_Const_WARN
1311 #ifdef RAD_AUTO_AD_Const
1317 for(d = DErp::LastDerp; d; d = d->
next)
1320 if (!(ADVari::adc.rad_need_reinit & 1)) {
1321 ADVari::adc.rad_need_reinit = 1;
1322 ADVari::adc.rad_mleft_save = ADVari::adc.Mleft;
1323 ADVari::adc.Mleft = 0;
1326 if (ADVari::gcgen_cur == ADVari::zap_gcgen1) {
1328 if (!(fname = getenv(
"RAD_DEBUG_FILE")))
1329 fname =
"rad_debug.out";
1333 ADVari::debug_file = fopen(fname,
"w");
1334 ADVari::zap_gcgen1 = -1;
1337 if ((d = DErp::LastDerp) != 0) {
1338 for(i = 0; i < n; i++)
1339 V[i]->cv->
aval = w[i];
1341 if (ADVari::debug_file)
1343 fprintf(ADVari::debug_file,
"%d\t%d\t%g + %g * %g",
1346 fprintf(ADVari::debug_file,
" = %g\n", d->
c->
aval);
1347 fflush(ADVari::debug_file);
1348 }
while((d = d->
next));
1352 while((d = d->
next));
1355 if (ADVari::debug_file) {
1356 fclose(ADVari::debug_file);
1357 ADVari::debug_file = 0;
1360 #ifdef RAD_AUTO_AD_Const
1361 *ADVari::Last_ADvari = 0;
1362 ADVari::Last_ADvari = &ADVari::First_ADvari;
1363 if ((anext = ADVari::First_ADvari) && !(ADVari::adc.rad_need_reinit & 2)) {
1364 ADVari::adc.rad_need_reinit = 3;
1365 while((a = anext)) {
1368 #ifdef RAD_Const_WARN
1369 if ((i = a->opno) > 0)
1372 v->
cv = cv =
new ADVari(v, a->
Val, a->
aval);
1384 ADVari::gcgen_cur++;
1385 ADVari::last_opno = 0;
1389 template<
typename Double>
1393 ADVari *x =
new ADVari(
Hv_const, d);
1397 template<
typename Double>
1405 template<
typename Double>
1413 template<
typename Double>
1421 template<
typename Double>
1424 ConstADVari *x =
new ConstADVari(0.);
1428 template<
typename Double>
void
1431 ConstADVari *x =
new ConstADVari(d);
1435 template<
typename Double>
1438 ConstADVari *
y =
new ConstADVari(x.
cv->Val);
1443 template<
typename Double>
1446 ConstADVari *
y =
new ConstADVari(x.
cv->Val);
1451 template<
typename Double>
1454 ConstADVari *
y =
new ConstADVari(x.
Val);
1459 template<
typename Double>
1465 ConstADVari *ncv =
new ConstADVari(v.
val());
1466 #ifdef RAD_AUTO_AD_Const
1473 template<
typename Double>
1484 #ifdef RAD_AUTO_AD_Const
1486 template<
typename Double>
1490 this->ADvari_padv(x);
1493 template<
typename Double>
1497 this->ADvari_padv(x);
1500 template<
typename Double>
1502 ADVari(
Hv_copy, y.cv->Val), d((const Double*)&ADcontext<Double>::One, (ADVari*)this, y.cv)
1504 this->ADvari_padv(x);
1507 template<
typename Double>
1509 ADVari(
Hv_copy, y.Val), d((const Double*)&ADcontext<Double>::One, this, &y)
1511 this->ADvari_padv(x);
1516 template<
typename Double>
1522 template<
typename Double>
1527 template<
typename Double>
1531 #ifdef RAD_AUTO_AD_Const
1532 ADVari *ncv =
new ADVari(
this, d);
1537 this->cv =
new ADVari(
Hv_const, d);
1542 template<
typename Double>
1546 #ifdef RAD_AUTO_AD_Const
1547 ADVari *nv =
new ADVari(
this, d);
1552 this->cv = ConstADVari::cadc.fpval_implies_const
1553 ?
new ConstADVari(d)
1559 template<
typename Double>
1565 template<
typename Double>
1571 #ifdef RAD_AUTO_AD_Const
1572 #define RAD_ACA ,this
1577 template<
typename Double>
1580 ADVari *Lcv = this->cv;
1586 template<
typename Double>
1592 template<
typename Double>
1595 ADVari *tcv = this->cv;
1600 template<
typename Double>
1606 template<
typename Double>
1612 template<
typename Double>
1615 ADVari *Lcv = this->cv;
1621 template<
typename Double>
1627 template<
typename Double>
1630 ADVari *tcv = this->cv;
1635 template<
typename Double>
1641 template<
typename Double>
1647 template<
typename Double>
1650 ADVari *Lcv = this->cv;
1656 template<
typename Double>
1662 template<
typename Double>
1665 ADVari *Lcv = this->cv;
1670 template<
typename Double>
1676 template<
typename Double>
1679 Double Lv = L.
Val, Rv = R.
Val, pL = 1. / Rv, q = Lv/Rv, qpL = q*pL;
1683 template<
typename Double>
1686 ADVari *Lcv = this->cv;
1687 Double Lv = Lcv->
Val, Rv = R.
Val, pL = 1. / Rv, q = Lv/Rv, qpL = q*pL;
1692 template<
typename Double>
1698 template<
typename Double>
1701 Double recip = 1. / R.
Val;
1702 Double q = L * recip;
1703 Double d1 = -q*recip;
1707 template<
typename Double>
1710 ADVari *Lcv = this->cv;
1715 template<
typename Double>
1718 Double t = v.
Val, t1 = 1. - t*t, d1 = -1./
std::sqrt(t1);
1722 template<
typename Double>
1725 Double d1, t, t1, t2;
1732 template<
typename Double>
1741 template<
typename Double>
1744 Double d1, t, t1, t2, td;
1754 template<
typename Double>
1757 Double t = v.
Val, d1 = 1./(1. + t*t);
1761 template<
typename Double>
1764 Double t = v.
Val, d1 = 1./(1. - t*t);
1768 template<
typename Double>
1771 Double R2, t, t2,
x, x2,
y, y2;
1782 template<
typename Double>
1785 Double t, x2,
y, y2;
1793 template<
typename Double>
1796 Double t,
x, x2, y2;
1804 template<
typename Double>
1811 template<
typename Double>
1819 template<
typename Double>
1827 template<
typename Double>
1834 template<
typename Double>
1842 template<
typename Double>
1850 template<
typename Double>
1857 template<
typename Double>
1864 template<
typename Double>
1871 template<
typename Double>
1874 Double x = v.
Val, d1 = 1. /
x;
1878 template<
typename Double>
1881 static double num = 1. /
std::log(10.);
1889 template<
typename Double>
1892 Double
dx, dy, t,
x, xlog, xym1,
y;
1900 return *(
new ADvar2g<Double>(t,
dx, dy, (y-1.)*dx/
x, xym1*(1. + y*xlog), dy*xlog, &L, &R));
1903 template<
typename Double>
1906 Double dy, t, xlog,
y;
1914 template<
typename Double>
1924 template<
typename Double>
1931 template<
typename Double>
1938 template<
typename Double>
1942 Double d1 = 0.5 / t;
1946 template<
typename Double>
1956 template<
typename Double>
1966 template<
typename Double>
1971 if ((t = v.
Val) < 0) {
1978 template<
typename Double>
1985 if ((t = v.
Val) < 0) {
1992 template<
typename Double>
1998 template<
typename Double>
1999 inline ADvari<Double>&
2004 template<
typename Double>
2006 ADf2(Double f, Double gx, Double gy, Double hxx, Double hxy, Double hyy,
2011 template<
typename Double>
2013 ADf2(Double f, Double gx, Double gy, Double hxx, Double hxy, Double hyy,
2018 template<
typename Double>
2020 ADf2(Double f, Double gx, Double gy, Double hxx, Double hxy, Double hyy,
2025 template<
typename Double>
2027 ADf2(Double f, Double gx, Double gy, Double hxx, Double hxy, Double hyy,
2032 template<
typename Double>
2038 template<
typename Double>
2039 inline ADvari<Double>&
2044 template<
typename Double>
2048 ADVari *
a, *aL, *aR, **ap, **ape;
2051 Double aO, adO, *
g, *h, *h0, t, tL, tR;
2053 for(i = 0; i < n; i++) {
2056 a->
aO = a->
adO = 0.;
2058 ADVari::adc.Aibusy->limit = ADVari::adc.Ainext;
2060 for(b0 = 0, b = ADVari::adc.AiFirst; b; b0 = b, b = b->
next) {
2065 a->
aO = a->
adO = 0.;
2068 a->
dO = ((ADVar1*)a)->d.c->dO;
2078 a->dO = -((ADVar1*)a)->d.c->dO;
2081 a->dO = ((
ADVar2*)a)->dL.c->dO + ((
ADVar2*)a)->dR.c->dO;
2084 a->dO = ((
ADVar2*)a)->dL.c->dO - ((
ADVar2*)a)->dR.c->dO;
2090 a->dO = ((
ADVar2*)a)->dR.c->Val * ((
ADVar2*)a)->dL.c->dO
2102 for(i = 0; i < m; i++)
2103 t += g[i] * d[i].
c->dO;
2113 for(b = b0; b; b = b->
prev) {
2122 aL = ((ADVar1*)a)->d.c;
2147 aL = ((ADVar1*)a)->d.c;
2169 aL->
aO += aO * (tL = ((
ADVar1s*)a)->pL);
2170 aL->
adO += adO * tL;
2175 aL->
aO += aO * (tL = aR->
Val) + adO*aR->
dO;
2176 aR->
aO += aO * (tR = aL->
Val) + adO*aL->
dO;
2177 aL->
adO += adO * tL;
2178 aR->
adO += adO * tR;
2198 for(i = 0; i < m; i++) {
2200 aL->
adO += adO * (t = g[
i]);
2203 for(h = h0, j = 0; j <=
i; j++)
2204 d[j].
c->aO += t * *h++;
2206 for(k = j; j < m; j++)
2207 d[j].
c->
aO += t * *(h += k++);
2214 for(i = 0; i < n; i++) {
2221 template<
typename Double>
2228 #define A (ADvari<Double>*)
2229 #ifdef RAD_Const_WARN
2230 #define C(x) (((x)->opno < 0) ? RAD_Const_Warn(x) : 0, *A x)
2234 #define T template<typename Double> inline
2235 #define F ADvari<Double>&
2236 #define Ai const ADvari<Double>&
2237 #define AI const IndepADvar<Double>&
2240 T r f(Ai L, AI R) { return f(L, C(R.cv)); }\
2241 T r f(AI L, Ai R) { return f(C(L.cv), R); }\
2242 T r f(AI L, AI R) { return f(C(L.cv), C(R.cv)); }\
2243 T r f(AI L, D R) { return f(C(L.cv), R); }\
2244 T r f(Ai L, Dtype R) { return f(L, (D)R); }\
2245 T r f(AI L, Dtype R) { return f(C(L.cv), (D)R); }\
2246 T r f(Ai L, long R) { return f(L, (D)R); }\
2247 T r f(AI L, long R) { return f(C(L.cv), (D)R); }\
2248 T r f(Ai L, int R) { return f(L, (D)R); }\
2249 T r f(AI L, int R) { return f(C(L.cv), (D)R); }\
2250 T r f(D L, AI R) { return f(L, C(R.cv)); }\
2251 T r f(Dtype L, Ai R) { return f((D)L, R); }\
2252 T r f(Dtype L, AI R) { return f((D)L, C(R.cv)); }\
2253 T r f(long L, Ai R) { return f((D)L, R); }\
2254 T r f(long L, AI R) { return f((D)L, C(R.cv)); }\
2255 T r f(int L, Ai R) { return f((D)L, R); }\
2256 T r f(int L, AI R) { return f((D)L, C(R.cv)); }
2277 T F f(AI x) { return f(C(x.cv)); }
ADT_RAD ADvari< double > Ai
ADvari< Double > & abs(const ADvari< Double > &v)
ADvar(const IndepADVar &x)
static ADcontext< Double > adc
ADvari< Double > & acos(const ADvari< Double > &v)
static bool get_fpval_implies_const(void)
ADvar2q< Double > ADVar2q
void ADvari_record(ADVari *x)
ADvari< Double > & asinh(const ADvari< Double > &v)
ADT_RAD IndepADvar< double > AI
ADvari< Double > & atan(const ADvari< Double > &v)
ADvari< Double > & sqrt(const ADvari< Double > &v)
static void Weighted_Gradcomp(int n, ADvar **v, Double *w)
ADvari< Double > & fabs(const ADvari< Double > &v)
Sacado::RadVec::ADvar< double > ADVar
ADvar1s< Double > ADVar1s
ADvar2g< Double > ADVar2g
Double val(const ADvari< Double > &)
static ConstADvari * lastcad
ADvari(Advari_Opclass oc, Double t, Double ta)
ConstADvari< Double > ConstADVari
ADvari< Double > & operator-(const ADvari< Double > &T)
void AD_Const(const IndepADvar< Double > &)
ADvar2q(Double val1, Double Lp, Double Rp, Double LR, Double R2, const ADVari *Lcv, const ADVari *Rcv)
ADvari< Double > & cos(const ADvari< Double > &v)
static void Weighted_Gradcomp(int, ADVar **, Double *)
ADVar::IndepADVar IndepADVar
ADvari< Double > & asin(const ADvari< Double > &v)
ADvari< Double > & sin(const ADvari< Double > &v)
static void Gradcomp(int wantgrad)
ADvarn(Double val1, int n1, const IndepADVar *x, const Double *g, const Double *h)
ADvari< Double > & max(const ADvari< Double > &L, const ADvari< Double > &R)
ADvar & operator+=(const ADVari &)
IndepADvar< Double > & ADvar_operatoreq(IndepADvar< Double > *, const ADvari< Double > &)
static void Hvprod(int n, ADVar **vp, Double *v, Double *hv)
ADvari< Double > & log(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
FloatingPoint< double > Double
ADvar2(Advari_Opclass oc, Double val1, const ADVari *Lcv, const Double *Lc, const ADVari *Rcv, const Double *Rc)
ADVar::ConstADVari ConstADVari
int operator>=(const ADvari< Double > &L, const ADvari< Double > &R)
static int rad_need_reinit
void AD_Const1(Double *, const IndepADvar< Double > &)
ADVari::IndepADVar IndepADVar
int RAD_Const_Warn(void *v)
static void AD_Const(const IndepADvar &)
ConstADvar & operator=(Double d)
ADmemblock< Double > ADMemblock
ADvari< Double > & sinh(const ADvari< Double > &v)
ADvari< Double > & atanh(const ADvari< Double > &v)
ADvar1g< Double > ADVar1g
void * new_ADmemblock(size_t)
ADvari< Double > & operator+(const ADvari< Double > &T)
int operator==(const ADvari< Double > &L, const ADvari< Double > &R)
static void Hvprod(int, ADVar **, Double *, Double *)
ADvari< Double > & pow(const ADvari< Double > &L, const ADvari< Double > &R)
int operator>(const ADvari< Double > &L, const ADvari< Double > &R)
atan2(expr1.val(), expr2.val())
ADvar & operator*=(const ADVari &)
IndepADvar< Double > IndepADVar
static CADcontext< Double > cadc
void ConstADvar_ctr(Double)
IndepADvar & operator=(IndepADvar &x)
IndepADvar< Double > IndepADVar
ADvari< Double > & ADf2(Double f, Double gx, Double gy, const IndepADvar< Double > &x, const IndepADvar< Double > &y)
ADvar1(Advari_Opclass oc, Double val1, const Double *a1, const ADVari *c1)
ADvari< Double > & atan2(const ADvari< Double > &L, const ADvari< Double > &R)
static void Gradcomp(int wantgrad)
static void Weighted_Gradcomp(int n, ADVar **v, Double *w)
static void set_fpval_implies_const(bool newval)
ADvar & operator=(const ADVari &x)
static void aval_reset(void)
ADvari_block< Double > ADVari_block
ADvari< Double > & tan(const ADvari< Double > &v)
ADvari< Double > & operator*(const ADvari< Double > &L, const ADvari< Double > &R)
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 > & log10(const ADvari< Double > &v)
ADvari< Double > & operator/(const ADvari< Double > &L, const ADvari< Double > &R)
ADvari< Double > & cosh(const ADvari< Double > &v)
ADvari< Double > & acosh(const ADvari< Double > &v)
ConstADvar & operator=(ADVari &d)
ADvari< Double > & min(const ADvari< Double > &L, const ADvari< Double > &R)
ScalarType< value_type >::type scalar_type
static bool setget_fpval_implies_const(bool newval)
ADvari(Advari_Opclass oc, Double t)
ADvar1s(Double val1, Double a1, const ADVari *c1)
ADvari< Double > & exp(const ADvari< Double > &v)
ADvari< Double > & ADf1(Double f, Double g, const IndepADvar< Double > &x)
Turn ADvar into a meta-function class usable with mpl::apply.
ADvar2g(Double val1, Double Lp, Double Rp, Double L2, Double LR, Double R2, const ADVari *Lcv, const ADVari *Rcv)
ADvari< Double > & tanh(const ADvari< Double > &v)
void * Memalloc(size_t len)
int operator!=(const ADvari< Double > &L, const ADvari< Double > &R)
ADvar & operator=(IndepADVar &x)
ADvar & operator-=(const ADVari &)
ADvar & operator/=(const ADVari &)
ADvari< Double > & ADfn(Double f, int n, const IndepADvar< Double > *x, const Double *g)
ADvar1g(Double val1, Double d1, Double d2, const ADVari *c1)