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