| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Sacado Package |
| 4 |
// |
| 5 |
// Copyright 2006 NTESS and the Sacado contributors. |
| 6 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
/* Try to test all combinations of types and operations */ |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#define ADT_RAD Sacado::Rad:: |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include "Sacado_trad.hpp" |
| 19 |
#include <cstdio> |
| 20 |
|
| 21 |
using std::printf; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
typedef ADT_RAD IndepADvar<double> AI; |
| 26 |
|
| 27 |
typedef ADT_RAD ADvar<double> A; |
| 28 |
|
| 29 |
typedef ADT_RAD ConstADvar<double> C; |
| 30 |
|
| 31 |
typedef ADT_RAD ADvari<double> Ai; |
| 32 |
|
| 33 |
typedef const ADT_RAD IndepADvar<double> cAI; |
| 34 |
|
| 35 |
typedef const ADT_RAD ADvar<double> cA; |
| 36 |
|
| 37 |
typedef const ADT_RAD ConstADvar<double> cC; |
| 38 |
|
| 39 |
typedef const ADT_RAD ADvari<double> cAi; |
| 40 |
|
| 41 |
static int rc; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
/* This is to be run through an awk program that changes lines */ |
| 46 |
|
| 47 |
/* with "BINTEST" or "UNOPTEST" at the beginning of the line into */ |
| 48 |
|
| 49 |
/* a the desired C++ (which we can then inspect). */ |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
void |
| 54 |
|
| 55 |
botch(const char *what, double wanted, double got) |
| 56 |
|
| 57 |
{ |
| 58 |
|
| 59 |
printf("%s: expected %g, got %g, diff = %.2g\n", what, wanted, got, wanted-got); |
| 60 |
|
| 61 |
rc = 1; |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
const double tol = 5e-16; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
int |
| 72 |
|
| 73 |
differ(double a, double b) |
| 74 |
|
| 75 |
{ |
| 76 |
|
| 77 |
double d = a - b; |
| 78 |
|
| 79 |
if (d < 0.) |
| 80 |
|
| 81 |
d = -d; |
| 82 |
|
| 83 |
if (a < 0.) |
| 84 |
|
| 85 |
a = -a; |
| 86 |
|
| 87 |
if (b < 0.) |
| 88 |
|
| 89 |
b = -b; |
| 90 |
|
| 91 |
if (a < b) |
| 92 |
|
| 93 |
a = b; |
| 94 |
|
| 95 |
if (a > 0.) |
| 96 |
|
| 97 |
d /= a; |
| 98 |
|
| 99 |
return d > tol; |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
#ifndef RAD_EQ_ALIAS |
| 106 |
|
| 107 |
#define Plus_dx 1. |
| 108 |
|
| 109 |
#else |
| 110 |
|
| 111 |
#ifdef RAD_AUTO_AD_Const |
| 112 |
|
| 113 |
#define Plus_dx 1. |
| 114 |
|
| 115 |
#else |
| 116 |
|
| 117 |
#define Plus_dx 0. |
| 118 |
|
| 119 |
#endif |
| 120 |
|
| 121 |
#endif |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
int |
| 126 |
|
| 127 |
main(void) |
| 128 |
|
| 129 |
{ |
| 130 |
|
| 131 |
AI xAI, yAI; |
| 132 |
|
| 133 |
A fA, xA, yA; |
| 134 |
|
| 135 |
C xC, yC; |
| 136 |
|
| 137 |
double dx, f, xd; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
rc = 0; |
| 142 |
|
| 143 |
|
| 144 |
/**** Test of atanh ****/ |
| 145 |
|
| 146 |
xd = .6; f = atanh(.6); dx = 1.5625; |
| 147 |
xAI = xd; |
| 148 |
fA = atanh(xAI); |
| 149 |
A::Gradcomp(); |
| 150 |
if (differ(fA.val(), f)) botch("fA = atanh(xAI)", f, fA.val()); |
| 151 |
else if (differ(xAI.adj(), dx)) botch("d atanh(xAI)/dx", dx, xAI.adj()); |
| 152 |
{ |
| 153 |
A::aval_reset(); |
| 154 |
cAI xcAI(xd); |
| 155 |
fA = atanh(xcAI); |
| 156 |
A::Gradcomp(); |
| 157 |
if (differ(fA.val(), f)) botch("fA = atanh(xcAI)", f, fA.val()); |
| 158 |
else if (differ(xcAI.adj(), dx)) botch("d atanh(xcAI)/dx", dx, xcAI.adj()); |
| 159 |
} |
| 160 |
xA = xd; |
| 161 |
fA = atanh(xA); |
| 162 |
A::Gradcomp(); |
| 163 |
if (differ(fA.val(), f)) botch("fA = atanh(xA)", f, fA.val()); |
| 164 |
else if (differ(xA.adj(), dx)) botch("d atanh(xA)/dx", dx, xA.adj()); |
| 165 |
{ |
| 166 |
A::aval_reset(); |
| 167 |
cA xcA(xd); |
| 168 |
fA = atanh(xcA); |
| 169 |
A::Gradcomp(); |
| 170 |
if (differ(fA.val(), f)) botch("fA = atanh(xcA)", f, fA.val()); |
| 171 |
else if (differ(xcA.adj(), dx)) botch("d atanh(xcA)/dx", dx, xcA.adj()); |
| 172 |
} |
| 173 |
xC = xd; |
| 174 |
fA = atanh(xC); |
| 175 |
A::Gradcomp(); |
| 176 |
if (differ(fA.val(), f)) botch("fA = atanh(xC)", f, fA.val()); |
| 177 |
else if (differ(xC.adj(), dx)) botch("d atanh(xC)/dx", dx, xC.adj()); |
| 178 |
{ |
| 179 |
A::aval_reset(); |
| 180 |
cC xcC(xd); |
| 181 |
fA = atanh(xcC); |
| 182 |
A::Gradcomp(); |
| 183 |
if (differ(fA.val(), f)) botch("fA = atanh(xcC)", f, fA.val()); |
| 184 |
else if (differ(xcC.adj(), dx)) botch("d atanh(xcC)/dx", dx, xcC.adj()); |
| 185 |
} |
| 186 |
{ |
| 187 |
cAi xcAi(xd); |
| 188 |
fA = atanh(xcAi); |
| 189 |
A::Gradcomp(); |
| 190 |
if (differ(fA.val(), f)) botch("fA = atanh(xcAi)", f, fA.val()); |
| 191 |
else if (differ(xcAi.aval, dx)) botch("d atanh(xcAi)/dx", dx, xcAi.aval); |
| 192 |
} |
| 193 |
{ |
| 194 |
A::aval_reset(); |
| 195 |
cAi xcAi(xd); |
| 196 |
fA = atanh(xcAi); |
| 197 |
A::Gradcomp(); |
| 198 |
if (differ(fA.val(), f)) botch("fA = atanh(xcAi)", f, fA.val()); |
| 199 |
else if (differ(xcAi.aval, dx)) botch("d atanh(xcAi)/dx", dx, xcAi.aval); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
if (!rc) // chatter for cppunit test, which cannot tolerate silence |
| 204 |
|
| 205 |
printf("OK\n"); |
| 206 |
|
| 207 |
return rc; |
| 208 |
|
| 209 |
} |