1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
/*
--------------------------------------------------------------
File scalexam.C of ADOL-C version 1.8.0 as of Dec/01/98
--------------------------------------------------------------
This program can be used to verify the consistency and
correctness of derivatives computed by ADOL-C in its forward
and reverse mode.
The use is required to selct one integer input id.
For positive n = id the monomial x^n is evaluated recursively
at x=0.5 and all its nonzero Taylor coeffcients at this point
are evaluated in the forward and reverse mode.
A negative choice of id >= -9 leads to one of nine
identities, whose derivatives should be trivial. These identities
may be used to check the correctness of particular code segments
in the ADOL-C sources uni5_.c and *o_rev.c. No timings are
performed in this example program.
Last changes:
981201 olvo: new headers
980821 olvo: this is the old version 1.7
--------------------------------------------------------------
*/
/****************************************************************************/
/* INCLUDES */
#include "adouble.h" // These includes provide the compiler with
#include "interfaces.h" // definitions and utilities for `adoubles'.
#include <math.h>
/****************************************************************************/
/* POWER */
/* The monomial evaluation routine which has been obtained from
the original version by retyping all `doubles' as `adoubles' */
adouble power( adouble x, int n )
{ adouble z = 1;
if (n > 0)
{ int nh =n/2;
z = power(x,nh);
z *= z;
if (2*nh != n)
z *= x;
return z;
}
else
if (n == 0)
return z;
else
return 1.0/power(x,-n);
}
/****************************************************************************/
/* MAIN */
int main()
{ int n, i, id;
int tag = 0;
/*--------------------------------------------------------------------------*/
fprintf(stdout,"SCALEXAM (ADOL-C Example)\n\n");
fprintf(stdout,"problem number(-1 .. -10) / degree of monomial =? \n");
scanf("%d",&id);
n = id >0 ? id : 3;
double *xp,*yp;
xp = new double[n+4];
yp = new double[n+4];
yp[0] = 0;
xp[0] = 0.5;
xp[1] = 1.0;
/*--------------------------------------------------------------------------*/
int dum = 1;
trace_on(tag,dum); // Begin taping all calculations with 'adoubles'
adouble y,x;
x <<= xp[0];
if (id >= 0)
{ fprintf(stdout,"Evaluate and differentiate recursive power routine \n");
y = power(x,n);
}
else
{ fprintf(stdout,
"Check Operations and Functions by Algebraic Identities \n");
double pi = 2*asin(1.0);
switch (id) {
case -1 :
fprintf(stdout,
"Addition/Subtraction: y = x + x - (2.0/3)*x - x/3 \n");
y = x + x - (2.0/3)*x - x/3 ;
break;
case -2 :
fprintf(stdout,"Multiplication/divison: y = x*x/x \n");
y = x*x/x;
break;
case -3 :
fprintf(stdout,"Square root and power: y = sqrt(pow(x,2)) \n");
y = sqrt(pow(x,2));
break;
case -4 :
fprintf(stdout,"Exponential and log: y = exp(log(log(exp(x)))) \n");
y = exp(log(log(exp(x))));
break;
case -5 :
fprintf(stdout,"Trig identity: y = x + sin(2*x)-2*cos(x)*sin(x) \n");
y = x + sin(2.0*x)-2.0*cos(x)*sin(x);
break;
case -6 :
fprintf(stdout,"Check out quadrature macro \n");
y = exp(myquad(myquad(exp(x))));
break;
case -7 :
fprintf(stdout,"Arcsin: y = sin(asin(acos(cos(x)))) \n");
y = sin(asin(acos(cos(x))));
break;
case -8 :
fprintf(stdout,
"Hyperbolic tangent: y = x + tanh(x)-sinh(x)/cosh(x) \n");
y = x + tanh(x)-sinh(x)/cosh(x) ;
break;
case -9 :
fprintf(stdout,"Absolute value: y = x + fabs(x) - fabs(-x) \n");
y = x + fabs(-x) - fabs(x);
break;
case -10 :
fprintf(stdout,"atan2: y = atan2(sin(x-0.5+pi),cos(x-0.5+pi)) \n");
y = atan2(sin(x),cos(x));
break;
default : fprintf(stdout," Please select problem number >= -10 \n");
exit(-1);
}
}
y >>= yp[0];
trace_off(); // The (partial) execution trace is completed.
/*--------------------------------------------------------------------------*/
if( id < 0 )
fprintf(stdout,"Round-off error: %14.6le\n",value(y-x));
/*--------------------------------------------------------------------------*/
int tape_stats[11];
tapestats(tag,tape_stats); /* Reading of tape statistics */
fprintf(stdout,"\n independents %d\n",tape_stats[0]);
fprintf(stdout," dependents %d\n",tape_stats[1]);
fprintf(stdout," operations %d\n",tape_stats[5]);
fprintf(stdout," buffer size %d\n",tape_stats[4]);
fprintf(stdout," maxlive %d\n",tape_stats[2]);
fprintf(stdout," valstack size %d\n\n",tape_stats[3]);
/*--------------------------------------------------------------------------*/
double *res;
res = new double[n+2];
double u[1];
u[0] = 1;
fprintf(stdout,
"\nThe two Taylor coefficients in each row should agree\n\n");
double ***V = (double***)new double**[1];
V[0] = new double*[1];
V[0][0] = new double[n+2];
double **U = new double*[1];
U[0] = new double[1];
U[0][0] = 1;
double** xpoint = &xp;
double** ypoint = &yp;
double** respoint = &res;
// tape_doc(tag,depen,indep,*xpoint,*respoint);
fprintf(stdout," \n \t forward \t reverse \n");
for (i=0; i < n+2; i++)
{ xp[i+2]=0;
forward(tag,1,1,i,i+1,xpoint,respoint);
fprintf(stdout,"%d\t%14.6le\t\t%14.6le\n",i,res[i],yp[i]);
reverse(tag,1,1,i,u,ypoint); // call higher order scalar reverse
reverse(tag,1,1,i,1,U,V);
yp[i+1] = yp[i]/(i+1);
if (V[0][0][i] != yp[i])
fprintf(stdout,"%d-th component in error %14.6le\n",i,V[0][0][i]-yp[i]);
}
cout << "\nWhen n<0 all rows except the first two should vanish \n";
return 1;
}
|