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