Debian Codegraph @

packages/sacado/test/tradoptest/tradoptest_43.cpp

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