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