Sacado Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
tradoptest_48.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
15using std::printf;
16
17
18
19typedef ADT_RAD IndepADvar<double> AI;
20
21typedef ADT_RAD ADvar<double> A;
22
23typedef ADT_RAD ConstADvar<double> C;
24
25typedef ADT_RAD ADvari<double> Ai;
26
27typedef const ADT_RAD IndepADvar<double> cAI;
28
29typedef const ADT_RAD ADvar<double> cA;
30
31typedef const ADT_RAD ConstADvar<double> cC;
32
33typedef const ADT_RAD ADvari<double> cAi;
34
35static 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
49botch(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
67differ(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
121main(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 cos ****/
139
140 xd = 3.; f = cos(3.); dx = -sin(3.);
141 xAI = xd;
142 fA = cos(xAI);
143 A::Gradcomp();
144 if (differ(fA.val(), f)) botch("fA = cos(xAI)", f, fA.val());
145 else if (differ(xAI.adj(), dx)) botch("d cos(xAI)/dx", dx, xAI.adj());
146 {
147 A::aval_reset();
148 cAI xcAI(xd);
149 fA = cos(xcAI);
150 A::Gradcomp();
151 if (differ(fA.val(), f)) botch("fA = cos(xcAI)", f, fA.val());
152 else if (differ(xcAI.adj(), dx)) botch("d cos(xcAI)/dx", dx, xcAI.adj());
153 }
154 xA = xd;
155 fA = cos(xA);
156 A::Gradcomp();
157 if (differ(fA.val(), f)) botch("fA = cos(xA)", f, fA.val());
158 else if (differ(xA.adj(), dx)) botch("d cos(xA)/dx", dx, xA.adj());
159 {
160 A::aval_reset();
161 cA xcA(xd);
162 fA = cos(xcA);
163 A::Gradcomp();
164 if (differ(fA.val(), f)) botch("fA = cos(xcA)", f, fA.val());
165 else if (differ(xcA.adj(), dx)) botch("d cos(xcA)/dx", dx, xcA.adj());
166 }
167 xC = xd;
168 fA = cos(xC);
169 A::Gradcomp();
170 if (differ(fA.val(), f)) botch("fA = cos(xC)", f, fA.val());
171 else if (differ(xC.adj(), dx)) botch("d cos(xC)/dx", dx, xC.adj());
172 {
173 A::aval_reset();
174 cC xcC(xd);
175 fA = cos(xcC);
176 A::Gradcomp();
177 if (differ(fA.val(), f)) botch("fA = cos(xcC)", f, fA.val());
178 else if (differ(xcC.adj(), dx)) botch("d cos(xcC)/dx", dx, xcC.adj());
179 }
180 {
181 cAi xcAi(xd);
182 fA = cos(xcAi);
183 A::Gradcomp();
184 if (differ(fA.val(), f)) botch("fA = cos(xcAi)", f, fA.val());
185 else if (differ(xcAi.aval, dx)) botch("d cos(xcAi)/dx", dx, xcAi.aval);
186 }
187 {
188 A::aval_reset();
189 cAi xcAi(xd);
190 fA = cos(xcAi);
191 A::Gradcomp();
192 if (differ(fA.val(), f)) botch("fA = cos(xcAi)", f, fA.val());
193 else if (differ(xcAi.aval, dx)) botch("d cos(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 }
cos(expr.val())
expr expr dx(i)
sin(expr.val())
ADT_RAD IndepADvar< double > AI
const ADT_RAD ConstADvar< double > cC
const ADT_RAD IndepADvar< double > cAI
const ADT_RAD ADvari< double > cAi
const ADT_RAD ADvar< double > cA
ADT_RAD IndepADvar< double > AI
const ADT_RAD ConstADvar< double > cC
#define ADT_RAD
const ADT_RAD IndepADvar< double > cAI
ADT_RAD ADvar< double > A
const double tol
int main(void)
const ADT_RAD ADvari< double > cAi
static int rc
ADT_RAD ConstADvar< double > C
void botch(const char *what, double wanted, double got)
const ADT_RAD ADvar< double > cA
ADT_RAD ADvari< double > Ai
int differ(double a, double b)