42 #ifndef SACADO_NO_NAMESPACE
47 #ifdef RAD_AUTO_AD_Const
48 ADvari *ADvari::First_ADvari, **ADvari::Last_ADvari = &ADvari::First_ADvari;
49 #undef RAD_DEBUG_BLOCKKEEP
51 #ifdef RAD_DEBUG_BLOCKKEEP
52 #if !(RAD_DEBUG_BLOCKKEEP > 0)
53 #undef RAD_DEBUG_BLOCKKEEP
55 extern "C" void _uninit_f2c(
void *x,
int type,
long len);
57 static ADmemblock *rad_Oldcurmb;
58 static int rad_busy_blocks;
68 #ifdef RAD_DEBUG_BLOCKKEEP
69 static size_t rad_mleft_save;
93 #ifdef RAD_AUTO_AD_Const
105 #ifdef RAD_DEBUG_BLOCKKEEP
106 Mleft = rad_mleft_save;
111 if (!(mb0 = rad_Oldcurmb))
113 for(;; mb = mb->
next) {
121 if (rad_busy_blocks >= RAD_DEBUG_BLOCKKEEP) {
126 for(mb =
Busy; mb != mb0; mb = mb1) {
150 #ifdef RAD_AUTO_AD_Const
151 *ADvari::Last_ADvari = 0;
152 ADvari::Last_ADvari = &ADvari::First_ADvari;
153 if ((anext = ADvari::First_ADvari)) {
170 #ifdef RAD_DEBUG_BLOCKKEEP
207 #ifdef RAD_DEBUG_BLOCKKEEP
216 while((d = d->
next));
232 #ifdef RAD_DEBUG_BLOCKKEEP
238 for(i = 0; i < n; i++)
239 v[i]->cv->aval = w[i];
241 while((d = d->
next));
245 #ifdef RAD_AUTO_AD_Const
254 cv =
new ADvari(
this, (
double)d);
259 cv =
new ADvari(
this, (
double)d);
264 IndepADvar::IndepADvar(double d)
287 #ifdef RAD_AUTO_AD_Const
333 #ifdef RAD_AUTO_AD_Const
344 ADvari(
Hv_copy, y.cv->Val), d(&CADcontext::One, this, y.cv)
346 *ADvari::Last_ADvari =
this;
347 ADvari::Last_ADvari = &Next;
348 padv = (IndepADvar*)x;
352 ADvari(
Hv_copy, y.Val), d(&CADcontext::One, this, &y)
354 *ADvari::Last_ADvari =
this;
355 ADvari::Last_ADvari = &Next;
356 padv = (IndepADvar*)x;
372 double LR,
double R2,
const ADvari *Lcv,
const ADvari *Rcv):
374 pL(Lp), pR(Rp), pLR(LR), pR2(R2) { }
377 double L2,
double LR,
double R2,
const ADvari *Lcv,
const ADvari *Rcv):
379 pL(Lp), pR(Rp), pL2(L2), pLR(LR), pR2(R2) { }
384 #ifdef RAD_AUTO_AD_Const
397 #ifdef RAD_AUTO_AD_Const
422 #ifdef RAD_AUTO_AD_Const
437 #ifdef RAD_AUTO_AD_Const
457 #ifdef RAD_AUTO_AD_Const
472 #ifdef RAD_AUTO_AD_Const
492 #ifdef RAD_AUTO_AD_Const
507 #ifdef RAD_AUTO_AD_Const
521 double Lv = L.
Val, Rv = R.
Val, pL = 1. / Rv, q = Lv/Rv, qpL = q*pL;
522 return *(
new ADvar2q(q, pL, -qpL, -pL*pL, 2.*pL*qpL, &L, &R));
528 #ifdef RAD_AUTO_AD_Const
531 double Lv = Lcv->
Val, Rv = R.
Val, pL = 1. / Rv, q = Lv/Rv, qpL = q*pL;
532 cv =
new ADvar2q(q, pL, -qpL, -pL*pL, 2.*pL*qpL, Lcv, &R);
543 double recip = 1. / R.
Val;
544 double q = L * recip;
545 double d1 = -q*recip;
546 return *(
new ADvar1g(q, d1, -q*d1, &R));
552 #ifdef RAD_AUTO_AD_Const
562 double t1 = 1. - t*t;
563 double d1 = -1. /
sqrt(t1);
569 double d1, t, t1, t2;
571 t1 =
sqrt(t2 = t*t - 1.);
573 return *(
new ADvar1g(
log(t + t1), d1, -t*d1/t2, &v));
580 d1 = 1. /
sqrt(t1 = 1. - t*t);
586 double d1, t, t1, t2, td;
588 t1 =
sqrt(t2 = t*t + 1.);
593 return *(
new ADvar1g(td*
log(t*td + t1), d1, -(t/t2)*d1, &v));
601 return *(
new ADvar1g(
atan(t), d1, -(t+t)*d1*d1, &v));
607 double d1 = 1./(1. - t*t);
608 return *(
new ADvar1g(0.5*
log((1.+t)/(1.-t)), d1, (t+t)*d1*d1, &v));
653 double x = L.
Val, y = R.
Val;
654 double x2 = x*x, y2 = y*y;
655 double t = 1./(x2 + y2);
657 double R2 = 2.*t2*x*y;
658 return *(
new ADvar2g(
atan2(x,y), y*t, -x*t, -R2, t2*(x2 - y2), R2, &L, &R));
664 double x2 = x*x, y2 = y*y;
665 double t = 1./(x2 + y2);
666 return *(
new ADvar1g(
atan2(x,y), -x*t, 2.*t*t*x*y, &R));
672 double x2 = x*x, y2 = y*y;
673 double t = 1./(x2 + y2);
674 return *(
new ADvar1g(
atan2(x,y), y*t, -2.*t*t*x*y, &L));
692 return *(
new ADvar1g(t, t, t, &v));
699 return *(
new ADvar1g(
log(x), d1, -d1*d1, &v));
704 static double num = 1. /
log(10.);
705 double x = v.
Val, t = 1. / x;
712 double x = L.
Val, y = R.
Val, t =
pow(x,y);
714 double xlog =
log(x);
716 double dy = t * xlog;
717 return *(
new ADvar2g(t, dx, dy, (y-1.)*dx/x, xym1*(1. + y*xlog), dy*xlog, &L, &R));
722 double y = R.
Val, t =
pow(x,y);
723 double xlog =
log(x);
724 double dy = t * xlog;
725 return *(
new ADvar1g(t, dy, dy*xlog, &R));
730 double x = L.
Val, t =
pow(x,y);
732 return *(
new ADvar1g(t, dx, (y-1.)*dx/x, &L));
751 return *(
new ADvar1g(t, d1, -0.5*d1/v.
Val, &v));
760 return *(
new ADvar1g(rv, d1, (rv+rv)*d1, &v));
769 return *(
new ADvar1g(rv, d1, -(rv+rv)*d1, &v));
777 if ((t = v.
Val) < 0) {
781 return *(
new ADvar1g(t, p, 0., &v));
786 return *(
new ADvar1g(f, g, h, &x));
790 ADf2(
double f,
double gx,
double gy,
double hxx,
double hxy,
double hyy,
792 return *(
new ADvar2g(f, gx, gy, hxx, hxy, hyy, &x, &y));
805 for(i = 0; i < n1; i++, d1++) {
814 nh = (n1*(n1+1)) >> 1;
815 a1 =
H = (
double*)
ADvari::adc.Memalloc(nh *
sizeof(*h));
816 for(i = 0; i < nh; i++)
821 ADfn(
double f,
int n,
const ADvar *x,
const double *g,
const double *h) {
822 return *(
new ADvarn(f, n, x, g, h));
828 ADvari *
a, *aL, *aR, **ap, **ape;
831 double aO, adO, *
g, *h, *h0, t, tL, tR;
833 for(i = 0; i < n; i++) {
858 a->dO = -((
ADvar1*)a)->d.c->dO;
861 a->dO = ((
ADvar2*)a)->dL.c->dO + ((
ADvar2*)a)->dR.c->dO;
864 a->dO = ((
ADvar2*)a)->dL.c->dO - ((
ADvar2*)a)->dR.c->dO;
870 a->dO = ((
ADvar2*)a)->dR.c->Val * ((
ADvar2*)a)->dL.c->dO
884 for(i = 0; i < m; i++)
885 t += g[i] * d[i].
c->dO;
892 for(b = b0; b; b = b->
prev) {
954 aL->
aO += aO * (tL = aR->
Val) + adO*aR->
dO;
955 aR->
aO += aO * (tR = aL->
Val) + adO*aL->
dO;
979 for(i = 0; i < m; i++) {
981 aL->
adO += adO * (t = g[i]);
984 for(h = h0, j = 0; j <= i; j++)
985 d[j].
c->aO += t * *h++;
987 for(k = j; j < m; j++)
988 d[j].
c->
aO += t * *(h += k++);
993 for(i = 0; i < n; i++) {
1000 #ifndef SACADO_NO_NAMESPACE
ADvari & cos(const ADvari &v)
ADvar & operator/=(const ADvari &)
ADvari & operator-(const ADvari &T)
ADvar & operator-=(const ADvari &)
static void aval_reset(void)
ADvarn(double val1, int n1, const ADvar *x, const double *g, const double *h)
static const double negOne
ADvari & operator/(const ADvari &L, const ADvari &R)
ADvari & asinh(const ADvari &v)
ADvar2g(double val1, double Lp, double Rp, double L2, double LR, double R2, const ADvari *Lcv, const ADvari *Rcv)
ADvar2q(double val1, double Lp, double Rp, double LR, double R2, const ADvari *Lcv, const ADvari *Rcv)
IndepADvar & operator=(const IndepADvar &x)
static ConstADvari * lastcad
static void AD_Const(const IndepADvar &)
ADvari & operator*(const ADvari &L, const ADvari &R)
ADvari & acosh(const ADvari &v)
ADvari & operator+(ADvari &T)
ADvari & sinh(const ADvari &v)
ADvari & atanh(const ADvari &v)
Sacado::Rad::ADvar< double > R
ADvari & min(const ADvari &L, const ADvari &R)
ADvari & exp(const ADvari &v)
ADvari & fabs(const ADvari &v)
ADvari & log10(const ADvari &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
ADvari & asin(const ADvari &v)
ADvari & ADf2(double f, double gx, double gy, double hxx, double hxy, double hyy, const ADvari &x, const ADvari &y)
ADvari & atan(const ADvari &v)
ADvari & cosh(const ADvari &v)
ADvari & sin(const ADvari &v)
ADvari & tan(const ADvari &v)
static void Hvprod(int, ADvar **, double *, double *)
ADvari & sqrt(const ADvari &v)
void * new_ADmemblock(size_t)
static void Weighted_Gradcomp(int, ADvar **, double *)
ADvari & max(const ADvari &L, const ADvari &R)
static int rad_need_reinit
void ConstADvar_ctr(double)
ADvar & ADvar_operatoreq(ADvar *This, const ADvari &x)
ADvari & pow(const ADvari &L, const ADvari &R)
ADvar1(Advari_Opclass oc, double val1, const double *a1, const ADvari *c1)
ADvari & log(const ADvari &v)
ADvari & acos(const ADvari &v)
ADvar & operator+=(const ADvari &)
ADvar & operator=(const ADvari &x)
ADvari & atan2(const ADvari &L, const ADvari &R)
ADvari & ADf1(double f, double g, double h, const ADvari &x)
ADvari & ADfn(double f, int n, const ADvar *x, const double *g, const double *h)
ADvari & tanh(const ADvari &v)
ADvar & operator*=(const ADvari &)