49 #define UF(T,f,fn) T fn(T &x) { return f(x); }
51 #define UF4(f) UF(ADVar,f,f ## _ADV) UF(DADVar,f,f ## _DADV) UF(FDReal,f,f ## _FDR) UF(AFReal,f,f ## _AFR)
73 #define UF(T,f,fn) T fn(T &x, T &y) { return f(x,y); }
104 #define U4f(f,d) { #f, d, f ## _ADV, f ## _DADV, f ## _FDR, f ## _AFR }
158 return d > 5e-15 * (a + b);
166 fprintf(rc ? stderr : stdout,
"Usage: %s [-v]\n\
167 to compare consistency among several schemes for Hessian-vector computations.\n\
168 -v ==> verbose (show results)\n\
169 No -v ==> just print OK if all goes well; otherwise complain.\n", progname);
179 double a, *ap,
c, h[4], h1[4], v[3];
180 int b0, b1, b2, k, verbose;
181 static double unopargs[] = { .7, -.9, 2.5, -4.2, .1, .3, 1.2, -2.4, 0. };
182 static double binargs[] = { .1,.2, -.3,.4, -.5,-.6, .7,-.8,
183 1.1,2.2, -2.3,3.4, -2.5,-3.6, 2.7,-3.8, 0.};
185 #define Dcl(T,T1) T x ## T1, y ## T1, z ## T1
200 if (argc > 2 || *(s = argv[1]) !=
'-')
205 return usage(s[2] != 0);
209 return usage(strcmp(s,
"--help") ? 1 : 0);
222 #define In(T) p ## T[0] = &x ## T; p ## T[1] = &z ## T
227 fe = UT +
sizeof(
UT)/
sizeof(UT[0]);
228 for(ap = unopargs; (a = *ap++) != 0.; )
229 for(f = UT; f < fe; f++) {
230 if (a < f->dom[0] || a > f->
dom[1])
235 ADVar::Hvprod(1,pADV,v,h);
237 printf(
"%s(%g) = %g\n", f->
name, xADV.val(), yADV.val());
238 printf(
"%s' = %g\n", f->
name, xADV.adj());
239 printf(
"%s\" = %g\n", f->
name, h[0]);
242 yDADV = f->
f2(xDADV);
244 if (
differ(yDADV.val(), yADV.val())) {
246 printf(
"%s(%g): yADV = %g, yDADV = %g, diff = %.2g\n",
247 f->
name, a, yADV.val(), yDADV.val(),
248 yADV.val() - yDADV.val());
250 if (
differ(xADV.adj(), xDADV.adj())) {
252 printf(
"%s'(%g): ADV ==> %g, DADV ==> %g, diff = %.2g\n",
253 f->
name, a, xADV.adj(), xDADV.adj(),
254 xADV.adj() - xDADV.adj());
257 if (
differ(h[0], h1[0])) {
259 printf(
"%s\"(%g): ADV ==> %g, DADV ==> %g, diff = %.2g\n",
260 f->
name, a, h[0], h1[0], h[0] - h1[0]);
265 xFDR = a + tfFDR*v[0];
270 if (
differ(yFDR.val().val(), yADV.val())) {
272 printf(
"%s(%g): yFDR = %g, yADV = %g, diff = %.2g\n",
273 f->
name, a, yFDR.val().val(), yADV.val(),
274 yFDR.val().val() - yADV.val());
276 if (
differ(xFDR.val().adj(), h[0])) {
278 printf(
"%s\"(%g): FDR ==> %g, ADV ==> %g, diff = %.2g\n",
279 f->
name, a, xFDR.val().adj(), h[0],
280 xFDR.val().adj() - h[0]);
285 xAFR = a + tfFR*v[0];
288 if (
differ(yAFR.val().val(), yADV.val())) {
290 printf(
"%s(%g): yAFR = %g, yADV = %g, diff = %.2g\n",
291 f->
name, a, yAFR.val().val(), yADV.val(),
292 yAFR.val().val() - yADV.val());
294 if (
differ(xAFR.adj().val(), xADV.adj())) {
296 printf(
"%s'(%g): AFR ==> %g, ADV ==> %g, diff = %.2g\n",
297 f->
name, a, xAFR.adj().val(), xADV.adj(),
298 xAFR.adj().val() - xADV.adj());
300 if (
differ(xAFR.adj().dx(0), h[0])) {
302 printf(
"%s\"(%g): AFR ==> %g, ADV ==> %g, diff = %.2g\n",
303 f->
name, a, xAFR.adj().dx(0), h[0],
304 xAFR.adj().dx(0) - h[0]);
307 f2e = UT2 +
sizeof(
UT2)/
sizeof(UT2[0]);
308 for(ap = binargs; (a = *ap) != 0.; ap += 2) {
310 for(f2 = UT2; f2 < f2e; f2++) {
311 if (a < f2->xdom[0] || a > f2->
xdom[1])
315 yADV = f2->
f1(xADV, zADV);
317 ADVar::Hvprod(2,pADV,v,h);
318 ADVar::Hvprod(2,pADV,v+1,h+2);
320 printf(
"%s(%g,%g) = %g\n", f2->
name, xADV.val(), zADV.val(), yADV.val());
321 printf(
"%s' = (%g, %g)\n", f2->
name, xADV.adj(), zADV.adj());
322 printf(
"%s\" = (%8g\t%g)\n(%8g\t%g)", f2->
name, h[0],h[1],h[2],h[3]);
326 yDADV = f2->
f2(xDADV, zDADV);
328 if (
differ(yDADV.val(), yADV.val())) {
330 printf(
"%s(%g,%g): yADV = %g, yDADV = %g, diff = %.2g\n",
331 f2->
name, a, c, yADV.val(), yDADV.val(),
332 yADV.val() - yDADV.val());
334 if (
differ(xADV.adj(), xDADV.adj()) ||
differ(zADV.adj(), zDADV.adj())) {
336 printf(
"%s'(%g,%g): ADV ==> (%g,%g) DADV ==> (%g,%g), diff = (%.2g,%.2g)\n",
337 f2->
name, a, c, xADV.adj(), zADV.adj(),
338 xDADV.adj(), zDADV.adj(),
339 xADV.adj() - xDADV.adj(), zADV.adj() - zDADV.adj());
343 for(k = 0; k < 4; k++)
344 if (
differ(h[k], h1[k])) {
346 printf(
"%s\"(%g,%g):\n ADV ==> (%8g\t%8g\t%8g\t%g)\n",
347 f2->
name, a, c, h[0], h[1], h[2], h[3]);
348 printf(
"DADV ==> (%8g\t%8g\t%8g\t%g)\n",
349 h1[0], h1[1], h1[2], h1[3]);
350 printf(
"diff = (%.2g %.2g %.2g %.2g)\n\n",
351 h[0] - h1[0], h[1] - h1[1],
352 h[2] - h1[2], h[3] - h1[3]);
357 xFDR = a + tfFDR*v[0];
358 zFDR = c + tfFDR*v[1];
359 yFDR = f2->
f3(xFDR, zFDR);
363 if (
differ(yFDR.val().val(), yADV.val())) {
365 printf(
"%s(%g,%g): yFDR = %g, yADV = %g, diff = %.2g\n",
366 f2->
name, a, c, yFDR.val().val(), yADV.val(),
367 yFDR.val().val() - yADV.val());
369 if (
differ(xFDR.val().adj(), h[0]) ||
differ(zFDR.val().adj(), h[1])) {
371 printf(
"%s\"(%g,%g) row 1:\nFDR ==> (%g,%g)\nADV ==> (%g,%g)\n",
372 f2->
name, a, c, xFDR.val().adj(), zFDR.val().adj(),
374 printf(
"diff = %.2g %g\n\n",
375 xFDR.val().adj() - h[0], zFDR.val().adj() - h[1]);
378 xFDR = a + tfFDR*v[1];
379 zFDR = c + tfFDR*v[2];
380 yFDR = f2->
f3(xFDR, zFDR);
383 if (
differ(xFDR.val().adj(), h[2]) ||
differ(zFDR.val().adj(), h[3])) {
385 printf(
"%s\"(%g,%g) row 2:\nFDR ==> (%g,%g)\nADV ==> (%g,%g)\n",
386 f2->
name, a, c, xFDR.val().adj(), zFDR.val().adj(),
388 printf(
"diff = %.2g %g\n\n",
389 xFDR.val().adj() - h[2], zFDR.val().adj() - h[3]);
394 xAFR = a + tfFR*v[0];
395 zAFR = c + tfFR*v[1];
396 yAFR = f2->
f4(xAFR, zAFR);
398 if (
differ(yAFR.val().val(), yADV.val())) {
400 printf(
"%s(%g,%g): yAFR = %g, yADV = %g, diff = %.2g\n",
401 f2->
name, a, c, yAFR.val().val(), yADV.val(),
402 yAFR.val().val() - yADV.val());
404 if (
differ(xAFR.adj().val(), xADV.adj()) ||
differ(zAFR.adj().val(), zADV.adj())) {
406 printf(
"%s'(%g,%g):\n AFR ==> (%g,%g)\n ADV ==> (%g,%g)\n",
407 f2->
name, a, c, xAFR.adj().val(), zAFR.adj().val(),
408 xADV.adj(), zADV.adj());
409 printf(
" diff = %.2g %.2g\n\n",
410 xAFR.adj().val() - xADV.adj(),
411 zAFR.adj().val() - zADV.adj());
413 if (
differ(xAFR.adj().dx(0), h[0]) ||
differ(zAFR.adj().dx(0), h[1])) {
415 printf(
"%s\"(%g,%g) row 1:\n AFR ==> (%g,%g)\n",
416 f2->
name, a, c, xAFR.adj().dx(0), zAFR.adj().dx(0));
417 printf(
" ADV = (%g,%g)\n diff = %.2g %.2g\n\n", h[0], h[1],
418 xAFR.adj().dx(0) - h[0],
419 zAFR.adj().dx(0) - h[1]);
422 xAFR = a + tfFR*v[1];
423 zAFR = c + tfFR*v[2];
424 yAFR = f2->
f4(xAFR, zAFR);
426 if (
differ(xAFR.adj().dx(0), h[2]) ||
differ(zAFR.adj().dx(0), h[3])) {
428 printf(
"%s\"(%g,%g) row 2:\n AFR ==> (%g,%g)\n",
429 f2->
name, a, c, xAFR.adj().dx(0), zAFR.adj().dx(0));
430 printf(
" ADV = (%g,%g)\n diff = %.2g %.2g\n\n", h[2], h[3],
431 xAFR.adj().dx(0) - h[2],
432 zAFR.adj().dx(0) - h[3]);
439 if (*s ==
'.' && s[1] ==
'/')
440 for(s += 2; *s ==
'/'; ++s);
441 printf(
"%s: %d errors seen.\n", s, k);
Sacado::RadVec::ADvar< double > ADVar
AFReal(* AFReal_uf)(AFReal &)
Sacado::Fad::SFad< DReal, 1 > FDReal
KOKKOS_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)
static void Hvprod(int n, ADVar **vp, Double *v, Double *hv)
ADVar(* ADVar_uf)(ADVar &)
FDReal(* FDReal_uf2)(FDReal &, FDReal &)
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
ADVar(* ADVar_uf2)(ADVar &, ADVar &)
Sacado::Fad::SFad< double, 1 > FReal
Sacado::Rad2::ADvar< double > DADVar
static int differ(double a, double b)
atan2(expr1.val(), expr2.val())
Sacado::Rad::ADvar< FReal > AFReal
static double dom_acosh[]
AFReal(* AFReal_uf2)(AFReal &, AFReal &)
DADVar(* DADVar_uf2)(DADVar &, DADVar &)
DADVar(* DADVar_uf)(DADVar &)
FDReal(* FDReal_uf)(FDReal &)
Sacado::Rad::ADvar< double > DReal
static double dom_invtrig[]