File: mul_level.cpp

package info (click to toggle)
cppad 2026.00.00.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,584 kB
  • sloc: cpp: 112,960; sh: 6,146; ansic: 179; python: 71; sed: 12; makefile: 10
file content (328 lines) | stat: -rw-r--r-- 10,028 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-22 Bradley M. Bell
// ----------------------------------------------------------------------------


// CPPAD_HAS_* defines and CPPAD_COMPILER_IS_GNUCXX
# include <cppad/configure.hpp>

# if CPPAD_HAS_ADOLC
// adolc examples should suppress conversion warnings
# include <cppad/wno_conversion.hpp>
//
# include <adolc/adouble.h>
# include <adolc/taping.h>
# include <adolc/interfaces.h>
// adouble definitions not in Adolc distribution and
// required in order to use CppAD::AD<adouble>
# include <cppad/example/base_adolc.hpp>
# endif

# include <cppad/cppad.hpp>
# include <limits>

namespace { // BEGIN empty namespace

bool One(void)
{  bool ok = true;                          // initialize test result
   using CppAD::NearEqual;
   double eps = 10. * std::numeric_limits<double>::epsilon();


   typedef CppAD::AD<double>   ADdouble;    // for one level of taping
   typedef CppAD::AD<ADdouble> ADDdouble;   // for two levels of taping
   size_t n = 2;                            // dimension for example

   // value of the independent variables
   CPPAD_TESTVECTOR(ADDdouble) aa_x(n);
   aa_x[0] = 1.; aa_x[1] = 3.; // test conversion double to AD< AD<double> >
   aa_x[0] = 2. * aa_x[0];     // test double * AD< AD<double> >
   CppAD::Independent(aa_x);

   // compute the function f(x) = 2 * x[0] * x[1]
   CPPAD_TESTVECTOR(ADDdouble) aa_f(1);
   aa_f[0] = 2. * aa_x[0] * aa_x[1];
   CppAD::ADFun<ADdouble> F(aa_x, aa_f);

   // value of the independent variables
   CPPAD_TESTVECTOR(ADdouble)   a_x(n);
   a_x[0] = 2.; a_x[1] = 3.;
   Independent(a_x);

   // re-evaluate f(2, 3) (must get deepedence on a_x).
   size_t p = 0;
   CPPAD_TESTVECTOR(ADdouble) a_fp(1);
   a_fp    = F.Forward(p, a_x);
   ok     &= NearEqual(a_fp[0], 2. * a_x[0] * a_x[1], eps, eps);

   // compute the function g(x) = 2 * partial_x[0] f(x) = 4 * x[1]
   p = 1;
   CPPAD_TESTVECTOR(ADdouble) a_dx(n), a_g(1);
   a_dx[0] = 1.; a_dx[1] = 0.;
   a_fp    = F.Forward(p, a_dx);
   a_g[0]  = 2. * a_fp[0];
   CppAD::ADFun<double> G(a_x, a_g);

   // compute partial_x[1] g(x)
   CPPAD_TESTVECTOR(double)  xp(n), gp(1);
   p = 0;
   xp[0] = 4.; xp[1] = 5.;
   gp    = G.Forward(p, xp);
   ok   &= NearEqual(gp[0], 4. * xp[1], eps, eps);

   p = 1;
   xp[0] = 0.; xp[1] = 1.;
   gp    = G.Forward(p, xp);
   ok   &= NearEqual(gp[0], 4., eps, eps);

   return ok;
}

// f(x) = |x|^2 = .5 * ( x[0]^2 + ... + x[n-1]^2 )
template <class Type>
Type f_Two(CPPAD_TESTVECTOR(Type) &x)
{  Type sum;

   // check assignment of AD< AD<double> > = double
   sum  = .5;
   sum += .5;

   size_t i = x.size();
   while(i--)
      sum += x[i] * x[i];

   // check compound assignment AD< AD<double> > -= int
   sum -= 1;

   // check double * AD< AD<double> >
   return .5 * sum;
}

bool Two(void)
{  bool ok = true;                          // initialize test result
   double eps99 = 99.0 * std::numeric_limits<double>::epsilon();

   typedef CppAD::AD<double>   ADdouble;    // for one level of taping
   typedef CppAD::AD<ADdouble> ADDdouble;   // for two levels of taping
   size_t n = 5;                            // dimension for example
   size_t j;                                // a temporary index variable

   CPPAD_TESTVECTOR(double)       x(n);
   CPPAD_TESTVECTOR(ADdouble)   a_x(n);
   CPPAD_TESTVECTOR(ADDdouble) aa_x(n);

   // value of the independent variables
   for(j = 0; j < n; j++)
      a_x[j] = x[j] = double(j); // x[j] = j
   Independent(a_x);                  // a_x is indedendent for ADdouble
   for(j = 0; j < n; j++)
      aa_x[j] = a_x[j];          // track how aa_x depends on a_x
   CppAD::Independent(aa_x);          // aa_x is independent for ADDdouble

   // compute function
   CPPAD_TESTVECTOR(ADDdouble) aa_f(1);    // scalar valued function
   aa_f[0] = f_Two(aa_x);                   // has only one component

   // declare inner function (corresponding to ADDdouble calculation)
   CppAD::ADFun<ADdouble> a_F(aa_x, aa_f);

   // compute f'(x)
   size_t p = 1;                        // order of derivative of a_F
   CPPAD_TESTVECTOR(ADdouble) a_w(1);  // weight vector for a_F
   CPPAD_TESTVECTOR(ADdouble) a_df(n); // value of derivative
   a_w[0] = 1;                          // weighted function same as a_F
   a_df   = a_F.Reverse(p, a_w);        // gradient of f

   // declare outer function (corresponding to ADdouble calculation)
   CppAD::ADFun<double> df(a_x, a_df);

   // compute the d/dx of f'(x) * v = f''(x) * v
   CPPAD_TESTVECTOR(double) v(n);
   CPPAD_TESTVECTOR(double) ddf_v(n);
   for(j = 0; j < n; j++)
      v[j] = double(n - j);
   ddf_v = df.Reverse(p, v);

   // f(x)       = .5 * ( x[0]^2 + x[1]^2 + ... + x[n-1]^2 )
   // f'(x)      = (x[0], x[1], ... , x[n-1])
   // f''(x) * v = ( v[0], v[1],  ... , x[n-1] )
   for(j = 0; j < n; j++)
      ok &= CppAD::NearEqual(ddf_v[j], v[j], eps99, eps99);

   return ok;
}

# if CPPAD_HAS_ADOLC

bool adolc(void)
{  bool ok = true;                   // initialize test result
   double eps99 = 99.0 * std::numeric_limits<double>::epsilon();

   typedef adouble      ADdouble;         // for first level of taping
   typedef CppAD::AD<ADdouble> ADDdouble; // for second level of taping
   size_t n = 5;                          // number independent variables

   CPPAD_TESTVECTOR(double)       x(n);
   CPPAD_TESTVECTOR(ADdouble)   a_x(n);
   CPPAD_TESTVECTOR(ADDdouble) aa_x(n);

   // value of the independent variables
   short tag = 0;         // Adolc setup
   int keep = 1;
   trace_on(tag, keep);
   size_t j;
   for(j = 0; j < n; j++)
   {  x[j] = double(j);           // x[j] = j
      a_x[j] <<= x[j];            // a_x is independent for ADdouble
   }
   for(j = 0; j < n; j++)
      aa_x[j] = a_x[j];          // track how aa_x depends on a_x
   CppAD::Independent(aa_x);          // aa_x is independent for ADDdouble

   // compute function
   CPPAD_TESTVECTOR(ADDdouble) aa_f(1);    // scalar valued function
   aa_f[0] = f_Two(aa_x);                   // has only one component

   // declare inner function (corresponding to ADDdouble calculation)
   CppAD::ADFun<ADdouble> a_F(aa_x, aa_f);

   // compute f'(x)
   size_t p = 1;                        // order of derivative of a_F
   CPPAD_TESTVECTOR(ADdouble) a_w(1);  // weight vector for a_F
   CPPAD_TESTVECTOR(ADdouble) a_df(n); // value of derivative
   a_w[0] = 1;                          // weighted function same as a_F
   a_df   = a_F.Reverse(p, a_w);        // gradient of f

   // declare outer function
   // (corresponding to the tape of adouble operations)
   double df_j;
   for(j = 0; j < n; j++)
      a_df[j] >>= df_j;
   trace_off();

   // compute the d/dx of f'(x) * v = f''(x) * v
   size_t m      = n;                     // # dependent in f'(x)
   double *v = nullptr, *ddf_v = nullptr;
   v     = CPPAD_TRACK_NEW_VEC(m, v);     // track v = new double[m]
   ddf_v = CPPAD_TRACK_NEW_VEC(n, ddf_v); // track ddf_v = new double[n]
   for(j = 0; j < n; j++)
      v[j] = double(n - j);
   fos_reverse(tag, int(m), int(n), v, ddf_v);

   // f(x)       = .5 * ( x[0]^2 + x[1]^2 + ... + x[n-1]^2 )
   // f'(x)      = (x[0], x[1], ... , x[n-1])
   // f''(x) * v = ( v[0], v[1],  ... , x[n-1] )
   for(j = 0; j < n; j++)
      ok &= CppAD::NearEqual(ddf_v[j], v[j], eps99, eps99);

   CPPAD_TRACK_DEL_VEC(v);                 // check usage of delete
   CPPAD_TRACK_DEL_VEC(ddf_v);
   return ok;
}

# endif // CPPAD_HAS_ADOLC

bool std_math(void)
{  bool ok = true;
   using CppAD::AD;
   using CppAD::Independent;
   using CppAD::ADFun;
   double eps = std::numeric_limits<double>::epsilon();


   typedef AD<double>      ADdouble; // for first level of taping
   typedef AD<ADdouble>   ADDdouble; // for second level of taping
   size_t n = 1;         // number independent variables
   size_t m = 1;         // number dependent and independent variables

   CPPAD_TESTVECTOR(double)       x(n),   y(m);
   CPPAD_TESTVECTOR(ADdouble)    ax(n),  ay(m);
   CPPAD_TESTVECTOR(ADDdouble)  aax(n), aay(m);

   // create af(x) = tanh(x)
   aax[0] = 1.;
   Independent( aax );
   aay[0] = tanh(aax[0]);
   ADFun<ADdouble> af(aax, aay);

   // create g(x) = af(x)
   ax[0] = 1.;
   Independent( ax );
   ay = af.Forward(0, ax);
   ADFun<double> g(ax, ay);

   // evaluate h(x) = g(x)
   x[0] = 1.;
   y = g.Forward(0, x);

   // check result
   double check  = tanh(x[0]);
   ok &= CppAD::NearEqual(y[0], check, eps, eps);

   return ok;
}

bool fabs(void)
{  bool ok = true;
   using CppAD::AD;
   using CppAD::Independent;
   using CppAD::ADFun;
   double eps = std::numeric_limits<double>::epsilon();


   typedef AD<double>      ADdouble; // for first level of taping
   typedef AD<ADdouble>   ADDdouble; // for second level of taping
   size_t n = 1;         // number independent variables
   size_t m = 1;         // number dependent and independent variables

   CPPAD_TESTVECTOR(double)       x(n),   y(m);
   CPPAD_TESTVECTOR(ADdouble)    ax(n),  ay(m);
   CPPAD_TESTVECTOR(ADDdouble)  aax(n), aay(m);

   // create af(x) = fabs(x)
   aax[0] = 1.;
   Independent( aax );
   aay[0] = fabs(aax[0]);
   ADFun<ADdouble> af(aax, aay);

   // create g(x) = af'(x)
   ax[0] = 1.;
   Independent( ax );
   ay = af.Jacobian(ax);
   ADFun<double> g(ax, ay);

   // evaluate g(x) at same x as recording
   x[0] = 1.;
   y = g.Forward(0, x);

   // check result
   double check  = 1.;
   ok &= CppAD::NearEqual(y[0], check, eps, eps);

   // evaluate g(x) at different x from recording
   // (but abs is an atomic operation so derivative should work)
   x[0] = -1.;
   y = g.Forward(0, x);

   // check result
   check  = -1.;
   ok &= CppAD::NearEqual(y[0], check, eps, eps);

   return ok;
}

} // END empty namespace

bool mul_level(void)
{  bool ok = true;
   ok     &= One();
   ok     &= Two();
# if CPPAD_HAS_ADOLC
   ok     &= adolc();
# endif
   ok     &= std_math();
   ok     &= fabs();

   return ok;
}