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