22 #ifndef SACADO_NO_NAMESPACE
27 #ifdef RAD_AUTO_AD_Const
28 ADvari *ADvari::First_ADvari, **ADvari::Last_ADvari = &ADvari::First_ADvari;
29 #undef RAD_DEBUG_BLOCKKEEP
31 #ifdef RAD_DEBUG_BLOCKKEEP
32 #if !(RAD_DEBUG_BLOCKKEEP > 0)
33 #undef RAD_DEBUG_BLOCKKEEP
35 extern "C" void _uninit_f2c(
void *
x,
int type,
long len);
37 static ADmemblock *rad_Oldcurmb;
38 static int rad_busy_blocks;
48 #ifdef RAD_DEBUG_BLOCKKEEP
49 static size_t rad_mleft_save;
73 #ifdef RAD_AUTO_AD_Const
85 #ifdef RAD_DEBUG_BLOCKKEEP
86 Mleft = rad_mleft_save;
91 if (!(mb0 = rad_Oldcurmb))
93 for(;; mb = mb->
next) {
101 if (rad_busy_blocks >= RAD_DEBUG_BLOCKKEEP) {
106 for(mb =
Busy; mb != mb0; mb = mb1) {
130 #ifdef RAD_AUTO_AD_Const
131 *ADvari::Last_ADvari = 0;
132 ADvari::Last_ADvari = &ADvari::First_ADvari;
133 if ((anext = ADvari::First_ADvari)) {
150 #ifdef RAD_DEBUG_BLOCKKEEP
187 #ifdef RAD_DEBUG_BLOCKKEEP
196 while((d = d->
next));
212 #ifdef RAD_DEBUG_BLOCKKEEP
218 for(i = 0; i < n; i++)
219 v[i]->cv->aval = w[i];
221 while((d = d->
next));
225 #ifdef RAD_AUTO_AD_Const
234 cv =
new ADvari(
this, (
double)d);
239 cv =
new ADvari(
this, (
double)d);
244 IndepADvar::IndepADvar(double d)
267 #ifdef RAD_AUTO_AD_Const
313 #ifdef RAD_AUTO_AD_Const
324 ADvari(
Hv_copy, y.cv->Val), d(&CADcontext::One, this, y.cv)
326 *ADvari::Last_ADvari =
this;
327 ADvari::Last_ADvari = &Next;
328 padv = (IndepADvar*)x;
332 ADvari(
Hv_copy, y.Val), d(&CADcontext::One, this, &y)
334 *ADvari::Last_ADvari =
this;
335 ADvari::Last_ADvari = &Next;
336 padv = (IndepADvar*)x;
352 double LR,
double R2,
const ADvari *Lcv,
const ADvari *Rcv):
354 pL(Lp), pR(Rp), pLR(LR), pR2(R2) { }
357 double L2,
double LR,
double R2,
const ADvari *Lcv,
const ADvari *Rcv):
359 pL(Lp), pR(Rp), pL2(L2), pLR(LR), pR2(R2) { }
364 #ifdef RAD_AUTO_AD_Const
377 #ifdef RAD_AUTO_AD_Const
402 #ifdef RAD_AUTO_AD_Const
417 #ifdef RAD_AUTO_AD_Const
437 #ifdef RAD_AUTO_AD_Const
452 #ifdef RAD_AUTO_AD_Const
472 #ifdef RAD_AUTO_AD_Const
487 #ifdef RAD_AUTO_AD_Const
501 double Lv = L.
Val, Rv = R.
Val, pL = 1. / Rv, q = Lv/Rv, qpL = q*pL;
502 return *(
new ADvar2q(q, pL, -qpL, -pL*pL, 2.*pL*qpL, &L, &R));
508 #ifdef RAD_AUTO_AD_Const
511 double Lv = Lcv->
Val, Rv = R.
Val, pL = 1. / Rv, q = Lv/Rv, qpL = q*pL;
512 cv =
new ADvar2q(q, pL, -qpL, -pL*pL, 2.*pL*qpL, Lcv, &R);
523 double recip = 1. / R.
Val;
524 double q = L * recip;
525 double d1 = -q*recip;
526 return *(
new ADvar1g(q, d1, -q*d1, &R));
532 #ifdef RAD_AUTO_AD_Const
542 double t1 = 1. - t*t;
543 double d1 = -1. /
sqrt(t1);
549 double d1, t, t1, t2;
551 t1 =
sqrt(t2 = t*t - 1.);
553 return *(
new ADvar1g(
log(t + t1), d1, -t*d1/t2, &v));
560 d1 = 1. /
sqrt(t1 = 1. - t*t);
566 double d1, t, t1, t2, td;
568 t1 =
sqrt(t2 = t*t + 1.);
573 return *(
new ADvar1g(td*
log(t*td + t1), d1, -(t/t2)*d1, &v));
581 return *(
new ADvar1g(
atan(t), d1, -(t+t)*d1*d1, &v));
587 double d1 = 1./(1. - t*t);
588 return *(
new ADvar1g(0.5*
log((1.+t)/(1.-t)), d1, (t+t)*d1*d1, &v));
634 double x2 = x*
x, y2 =
y*
y;
635 double t = 1./(x2 + y2);
637 double R2 = 2.*t2*x*
y;
638 return *(
new ADvar2g(
atan2(x,y), y*t, -x*t, -R2, t2*(x2 - y2), R2, &L, &R));
644 double x2 = x*
x, y2 = y*
y;
645 double t = 1./(x2 + y2);
646 return *(
new ADvar1g(
atan2(x,y), -x*t, 2.*t*t*x*y, &R));
652 double x2 = x*
x, y2 = y*
y;
653 double t = 1./(x2 + y2);
654 return *(
new ADvar1g(
atan2(x,y), y*t, -2.*t*t*x*y, &L));
672 return *(
new ADvar1g(t, t, t, &v));
679 return *(
new ADvar1g(
log(x), d1, -d1*d1, &v));
684 static double num = 1. /
log(10.);
685 double x = v.
Val, t = 1. /
x;
694 double xlog =
log(x);
696 double dy = t * xlog;
697 return *(
new ADvar2g(t, dx, dy, (
y-1.)*dx/x, xym1*(1. +
y*xlog), dy*xlog, &L, &R));
702 double y = R.
Val, t =
pow(x,y);
703 double xlog =
log(x);
704 double dy = t * xlog;
705 return *(
new ADvar1g(t, dy, dy*xlog, &R));
710 double x = L.
Val, t =
pow(x,y);
712 return *(
new ADvar1g(t, dx, (y-1.)*dx/x, &L));
731 return *(
new ADvar1g(t, d1, -0.5*d1/v.
Val, &v));
740 return *(
new ADvar1g(rv, d1, (rv+rv)*d1, &v));
749 return *(
new ADvar1g(rv, d1, -(rv+rv)*d1, &v));
757 if ((t = v.
Val) < 0) {
761 return *(
new ADvar1g(t, p, 0., &v));
766 return *(
new ADvar1g(f, g, h, &x));
770 ADf2(
double f,
double gx,
double gy,
double hxx,
double hxy,
double hyy,
772 return *(
new ADvar2g(f, gx, gy, hxx, hxy, hyy, &x, &y));
785 for(i = 0; i < n1; i++, d1++) {
794 nh = (n1*(n1+1)) >> 1;
795 a1 =
H = (
double*)
ADvari::adc.Memalloc(nh *
sizeof(*h));
796 for(i = 0; i < nh; i++)
801 ADfn(
double f,
int n,
const ADvar *
x,
const double *g,
const double *h) {
802 return *(
new ADvarn(f, n, x, g, h));
808 ADvari *
a, *aL, *aR, **ap, **ape;
811 double aO, adO, *
g, *h, *h0, t, tL, tR;
813 for(i = 0; i < n; i++) {
838 a->dO = -((
ADvar1*)a)->d.c->dO;
841 a->dO = ((
ADvar2*)a)->dL.c->dO + ((
ADvar2*)a)->dR.c->dO;
844 a->dO = ((
ADvar2*)a)->dL.c->dO - ((
ADvar2*)a)->dR.c->dO;
850 a->dO = ((
ADvar2*)a)->dR.c->Val * ((
ADvar2*)a)->dL.c->dO
864 for(i = 0; i < m; i++)
865 t += g[i] * d[i].
c->dO;
872 for(b = b0; b; b = b->
prev) {
934 aL->
aO += aO * (tL = aR->
Val) + adO*aR->
dO;
935 aR->
aO += aO * (tR = aL->
Val) + adO*aL->
dO;
959 for(i = 0; i < m; i++) {
961 aL->
adO += adO * (t = g[
i]);
964 for(h = h0, j = 0; j <=
i; j++)
965 d[j].
c->aO += t * *h++;
967 for(k = j; j < m; j++)
968 d[j].
c->
aO += t * *(h += k++);
973 for(i = 0; i < n; i++) {
980 #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 &)