File: cond_exp_ad.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 (329 lines) | stat: -rw-r--r-- 7,898 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
329
// 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
// ----------------------------------------------------------------------------

/*
Test of CondExp with AD< AD< Base > > types
*/

# include <cppad/cppad.hpp>

typedef CppAD::AD< double >     ADdouble;
typedef CppAD::AD< ADdouble > ADADdouble;

namespace { // BEGIN empty namespace

bool CondExpADOne(void)
{  bool ok = true;

   using namespace CppAD;
   size_t n = 3;
   size_t m = 8;

   // ADdouble independent variable vector
   CPPAD_TESTVECTOR( ADdouble ) Xa(n);
   Xa[0] = -1.;
   Xa[1] =  0.;
   Xa[2] =  1.;
   Independent(Xa);

   // ADdouble independent variable vector
   CPPAD_TESTVECTOR( ADADdouble ) Xaa(n);
   Xaa[0] = Xa[0];
   Xaa[1] = Xa[1];
   Xaa[2] = Xa[2];
   Independent(Xaa);

   // ADADdouble parameter
   ADADdouble p = ADADdouble(Xa[0]);
   ADADdouble q = ADADdouble(Xa[1]);
   ADADdouble r = ADADdouble(Xa[2]);

   // ADADdouble dependent variable vector
   CPPAD_TESTVECTOR( ADADdouble ) Yaa(m);

   // CondExp(parameter, parameter, parameter)
   Yaa[0] = CondExp(p, q, r);

   // CondExp(parameter, parameter, variable)
   Yaa[1] = CondExp(p, q, Xaa[2]);

   // CondExp(parameter, variable, parameter)
   Yaa[2] = CondExp(p, Xaa[1], r);

   // CondExp(parameter, variable, variable)
   Yaa[3] = CondExp(p, Xaa[1], Xaa[2]);

   // CondExp(variable, variable, variable)
   Yaa[5] = CondExp(Xaa[0], Xaa[1], Xaa[2]);

   // CondExp(variable, variable, parameter)
   Yaa[4] = CondExp(Xaa[0], Xaa[1], r);

   // CondExp(variable, parameter, variable)
   Yaa[6] =  CondExp(Xaa[0], q, Xaa[2]);

   // CondExp(variable, parameter, parameter)
   Yaa[7] =  CondExp(Xaa[0], q, r);

   // create fa: Xaa -> Yaa function object
   ADFun< ADdouble > fa(Xaa, Yaa);

   // function values
   CPPAD_TESTVECTOR( ADdouble ) Ya(m);
   Ya  = fa.Forward(0, Xa);

   // create f: Xa -> Ya function object
   ADFun<double> f(Xa, Ya);

   // check result of function evaluation
   CPPAD_TESTVECTOR(double) x(n);
   CPPAD_TESTVECTOR(double) y(m);
   x[0] = 1.;
   x[1] = 0.;
   x[2] = -1.;
   y = f.Forward(0, x);
   size_t i;
   for(i = 0; i < m; i++)
   {  // y[i] = CondExp(x[0], x[1], x[2])
      if( x[0] > 0 )
         ok &= (y[i] == x[1]);
      else
         ok &= (y[i] == x[2]);
   }

   // check forward mode derivatives
   CPPAD_TESTVECTOR(double) dx(n);
   CPPAD_TESTVECTOR(double) dy(m);
   dx[0] = 1.;
   dx[1] = 2.;
   dx[2] = 3.;
   dy    = f.Forward(1, dx);
   for(i = 0; i < m; i++)
   {  if( x[0] > 0. )
         ok &= (dy[i] == dx[1]);
      else
         ok &= (dy[i] == dx[2]);
   }

   // calculate Jacobian
   CPPAD_TESTVECTOR(double) J(m * n);
   size_t j;
   for(i = 0; i < m; i++)
   {  for(j = 0; j < n; j++)
         J[i * n + j] = 0.;
      if( x[0] > 0. )
         J[i * n + 1] = 1.;
      else
         J[i * n + 2] = 1.;
   }

   // check reverse mode derivatives
   for(i = 0; i < m; i++)
      dy[i] = double(i);
   dx    = f.Reverse(1, dy);
   double sum;
   for(j = 0; j < n; j++)
   {  sum = 0;
      for(i = 0; i < m; i++)
         sum += dy[i] * J[i * n + j];
      ok &= (sum == dx[j]);
   }

   // forward mode computation of sparsity pattern
   CPPAD_TESTVECTOR(bool) Px(n * n);
   for(i = 0; i < n; i++)
   {  for(j = 0; j < n; j++)
         Px[i * n + j] = false;
      Px[i * n + i] = true;
   }
   CPPAD_TESTVECTOR(bool) Py(m * n);
   Py = f.ForSparseJac(n, Px);
   for(i = 0; i < m; i++)
   {  ok &= Py[ i * n + 0 ] == false;
      ok &= Py[ i * n + 1 ] == true;
      ok &= Py[ i * n + 2 ] == true;
   }

   // reverse mode computation of sparsity pattern
   Py.resize(m * m);
   for(i = 0; i < m; i++)
   {  for(j = 0; j < m; j++)
         Py[i * m + j] = false;
      Py[i * m + i] = true;
   }
   Px.resize(m * n);
   Px = f.RevSparseJac(m, Py);
   for(i = 0; i < m; i++)
   {  for(j = 0; j < n; j++)
         ok &= ( Px[i * n + j] == ( j > 0 ) );
   }

   return ok;
}
bool CondExpADTwo(void)
{  bool ok = true;

   using namespace CppAD;
   size_t n = 3;
   size_t m = 8;

   // ADdouble independent variable vector
   CPPAD_TESTVECTOR( ADdouble ) Xa(n);
   Xa[0] = -1.;
   Xa[1] =  0.;
   Xa[2] =  1.;
   Independent(Xa);

   // use VecAD so that sparsity results are local
   VecAD<double> Va(1);
   ADdouble zero = 0.;
   Va[zero]      = Xa[0];

   // ADdouble independent variable vector
   CPPAD_TESTVECTOR( ADADdouble ) Xaa(n);
   Xaa[0] = ADdouble( Va[zero] );
   Xaa[1] = Xa[1];
   Xaa[2] = Xa[2];
   Independent(Xaa);

   // ADADdouble parameter
   ADADdouble p = ADADdouble(Xa[0]);
   ADADdouble q = ADADdouble(Xa[1]);
   ADADdouble r = ADADdouble(Xa[2]);

   // ADADdouble dependent variable vector
   CPPAD_TESTVECTOR( ADADdouble ) Yaa(m);

   // CondExp(parameter, parameter, parameter)
   Yaa[0] = CondExp(p, q, r);

   // CondExp(parameter, parameter, variable)
   Yaa[1] = CondExp(p, q, Xaa[2]);

   // CondExp(parameter, variable, parameter)
   Yaa[2] = CondExp(p, Xaa[1], r);

   // CondExp(parameter, variable, variable)
   Yaa[3] = CondExp(p, Xaa[1], Xaa[2]);

   // CondExp(variable, variable, variable)
   Yaa[5] = CondExp(Xaa[0], Xaa[1], Xaa[2]);

   // CondExp(variable, variable, parameter)
   Yaa[4] = CondExp(Xaa[0], Xaa[1], r);

   // CondExp(variable, parameter, variable)
   Yaa[6] =  CondExp(Xaa[0], q, Xaa[2]);

   // CondExp(variable, parameter, parameter)
   Yaa[7] =  CondExp(Xaa[0], q, r);

   // create fa: Xaa -> Yaa function object
   ADFun< ADdouble > fa(Xaa, Yaa);

   // function values
   CPPAD_TESTVECTOR( ADdouble ) Ya(m);
   Ya  = fa.Forward(0, Xa);

   // create f: Xa -> Ya function object
   ADFun<double> f(Xa, Ya);

   // check use_VecAD
   ok &= f.use_VecAD();

   // check result of function evaluation
   CPPAD_TESTVECTOR(double) x(n);
   CPPAD_TESTVECTOR(double) y(m);
   x[0] = 1.;
   x[1] = 0.;
   x[2] = -1.;
   y = f.Forward(0, x);
   size_t i;
   for(i = 0; i < m; i++)
   {  // y[i] = CondExp(x[0], x[1], x[2])
      if( x[0] > 0 )
         ok &= (y[i] == x[1]);
      else
         ok &= (y[i] == x[2]);
   }

   // check forward mode derivatives
   CPPAD_TESTVECTOR(double) dx(n);
   CPPAD_TESTVECTOR(double) dy(m);
   dx[0] = 1.;
   dx[1] = 2.;
   dx[2] = 3.;
   dy    = f.Forward(1, dx);
   for(i = 0; i < m; i++)
   {  if( x[0] > 0. )
         ok &= (dy[i] == dx[1]);
      else
         ok &= (dy[i] == dx[2]);
   }

   // calculate Jacobian
   CPPAD_TESTVECTOR(double) J(m * n);
   size_t j;
   for(i = 0; i < m; i++)
   {  for(j = 0; j < n; j++)
         J[i * n + j] = 0.;
      if( x[0] > 0. )
         J[i * n + 1] = 1.;
      else
         J[i * n + 2] = 1.;
   }

   // check reverse mode derivatives
   for(i = 0; i < m; i++)
      dy[i] = double(i);
   dx    = f.Reverse(1, dy);
   double sum;
   for(j = 0; j < n; j++)
   {  sum = 0;
      for(i = 0; i < m; i++)
         sum += dy[i] * J[i * n + j];
      ok &= (sum == dx[j]);
   }

   // forward mode computation of sparsity pattern
   CPPAD_TESTVECTOR(bool) Px(n * n);
   for(i = 0; i < n; i++)
   {  for(j = 0; j < n; j++)
         Px[i * n + j] = false;
      Px[i * n + i] = true;
   }
   CPPAD_TESTVECTOR(bool) Py(m * n);
   Py = f.ForSparseJac(n, Px);
   for(i = 0; i < m; i++)
   {  for(j = 0; j < n; j++)
         // sparsity pattern works for both true and false cases.
         ok &= ( Py[i * n + j] == (j > 0) );
   }

   // reverse mode computation of sparsity pattern
   Py.resize(m * m);
   for(i = 0; i < m; i++)
   {  for(j = 0; j < m; j++)
         Py[i * m + j] = false;
      Py[i * m + i] = true;
   }
   Px.resize(m * n);
   Px = f.RevSparseJac(m, Py);
   for(i = 0; i < m; i++)
   {  for(j = 0; j < n; j++)
         ok &= ( Px[i * n + j] == (j > 0) );
   }

   return ok;
}

} // END empty namespace

bool CondExpAD(void)
{  bool ok = true;
   ok     &= CondExpADOne();
   ok     &= CondExpADTwo();
   return ok;
}