File: norm_sq.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 (385 lines) | stat: -rw-r--r-- 11,344 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-24 Bradley M. Bell
// ----------------------------------------------------------------------------

/*
{xrst_begin atomic_four_norm_sq.cpp}

Atomic Euclidean Norm Squared: Example and Test
###############################################

Function
********
This example demonstrates using :ref:`atomic_four-name`
to define the operation
:math:`g : \B{R}^n \rightarrow \B{R}` where

.. math::

   g(x) =  x_0^2 + \cdots + x_{n-1}^2

Purpose
*******
This atomic function demonstrates the following cases:

#. an arbitrary number of arguments *n*
#. zero and first order forward mode.
#. first order derivatives using reverse mode.

Define Atomic Function
**********************
{xrst_literal
   // BEGIN_DEFINE_ATOMIC_FUNCTION
   // END_DEFINE_ATOMIC_FUNCTION
}

Use Atomic Function
*******************
{xrst_literal
   // BEGIN_USE_ATOMIC_FUNCTION
   // END_USE_ATOMIC_FUNCTION
}

{xrst_end atomic_four_norm_sq.cpp}
*/
# include <cppad/cppad.hpp>

// BEGIN_DEFINE_ATOMIC_FUNCTION
// empty namespace
namespace {
   // BEGIN CONSTRUCTOR
   class atomic_norm_sq : public CppAD::atomic_four<double> {
   public:
      atomic_norm_sq(const std::string& name) :
      CppAD::atomic_four<double>(name)
      { }
   // END CONSTRUCTOR
   private:
      // BEGIN FOR_TYPE
      bool for_type(
         size_t                                     call_id     ,
         const CppAD::vector<CppAD::ad_type_enum>&  type_x      ,
         CppAD::vector<CppAD::ad_type_enum>&        type_y      ) override
      {  assert( call_id == 0 );       // default value
         assert(type_y.size() == 1 );  // m
         //
         // type_y
         size_t n     = type_x.size();
         type_y[0] = CppAD::constant_enum;
         for(size_t j = 0; j < n; ++j)
            type_y[0] = std::max(type_y[0], type_x[j]);
         return true;
      }
      // END FOR_TYPE
      // BEGIN FORWARD
      bool forward(
         size_t                             call_id     ,
         const CppAD::vector<bool>&         select_y    ,
         size_t                             order_low   ,
         size_t                             order_up    ,
         const CppAD::vector<double>&       tx          ,
         CppAD::vector<double>&             ty          ) override
      {
         size_t q = order_up + 1;
         size_t n = tx.size() / q;
   # ifndef NDEBUG
         size_t m = ty.size() / q;
         assert( call_id == 0 );
         assert( m == 1 );
         assert( m == select_y.size() );
   # endif
         // ok
         bool ok = order_up <= 1 && order_low <= order_up;
         if ( ! ok )
            return ok;
         //
         // sum = x_0^0 * x_0^0 + x_1^0 * x_1^0 + ...
         double sum = 0.0;
         for(size_t j = 0; j < n; ++j)
         {  double xj0 = tx[ j * q + 0];
            sum       += xj0 * xj0;
         }
         //
         // ty[0] = sum
         if( order_low <= 0 )
            ty[0] = sum;
         if( order_up < 1 )
            return ok;

         // sum = x_0^0 * x_0^1 + x_1^0 ^ x_1^1 + ...
         sum   = 0.0;
         for(size_t j = 0; j < n; ++j)
         {  double xj0 = tx[ j * q + 0];
            double xj1 = tx[ j * q + 1];
            sum       += xj0 * xj1;
         }
         // ty[1] = 2.0 * sum
         assert( order_up == 1 );
         ty[1] = 2.0 * sum;
         return ok;
      }
      // END FORWARD
      // BEGIN REVERSE
      bool reverse(
         size_t                              call_id     ,
         const CppAD::vector<bool>&          select_x    ,
         size_t                              order_up    ,
         const CppAD::vector<double>&        tx          ,
         const CppAD::vector<double>&        ty          ,
         CppAD::vector<double>&              px          ,
         const CppAD::vector<double>&        py          ) override
      {
         size_t q = order_up + 1;
         size_t n = tx.size() / q;
   # ifndef NDEBUG
         size_t m = ty.size() / q;
         assert( call_id == 0 );
         assert( m == 1 );
         assert( px.size() == tx.size() );
         assert( py.size() == ty.size() );
         assert( n == select_x.size() );
   # endif
         // ok
         bool ok = order_up == 0;
         if ( ! ok )
            return ok;

         // first order reverse mode
         for(size_t j = 0; j < n; ++j)
         {  // x_0^0
            double xj0 = tx[ j * q + 0];
            //
            // H( {x_j^k} ) = G[ F( {x_j^k} ), {x_j^k} ]
            double dF = 2.0 * xj0; // partial F w.r.t x_j^0
            double dG = py[0];     // partial of G w.r.t. y[0]
            double dH = dG * dF;   // partial of H w.r.t. x_j^0

            // px[j]
            px[j] = dH;
         }
         return ok;
      }
      // END REVERSE
      // BEGIN JAC_SPARSITY
      // Use deprecated version of this callback to test that is still works
      // (missing the ident_zero_x argument).
      bool jac_sparsity(
         size_t                                     call_id     ,
         bool                                       dependency  ,
         // const CppAD::vector<bool>&              ident_zero_x,
         const CppAD::vector<bool>&                 select_x    ,
         const CppAD::vector<bool>&                 select_y    ,
         CppAD::sparse_rc< CppAD::vector<size_t> >& pattern_out ) override
      {  size_t n = select_x.size();
         size_t m = select_y.size();
# ifndef NDEBUG
         assert( call_id == 0 );
         assert( m == 1 );
# endif
         // nnz
         size_t nnz = 0;
         if( select_y[0] )
         {  for(size_t j = 0; j < n; ++j)
            {  if( select_x[j] )
                  ++nnz;
            }
         }
         // pattern_out
         pattern_out.resize(m, n, nnz);
         size_t k = 0;
         if( select_y[0] )
         {  for(size_t j = 0; j < n; ++j)
            {  if( select_x[j] )
                  pattern_out.set(k++, 0, j);
            }
         }
         assert( k == nnz );
         return true;
      }
      // END JAC_SPARSITY
      // BEGIN HES_SPARSITY
      // Use deprecated version of this callback to test that is still works
      // (missing the ident_zero_x argument).
      bool hes_sparsity(
         size_t                                     call_id     ,
         // const CppAD::vector<bool>&              ident_zero_x,
         const CppAD::vector<bool>&                 select_x    ,
         const CppAD::vector<bool>&                 select_y    ,
         CppAD::sparse_rc< CppAD::vector<size_t> >& pattern_out ) override
      {  size_t n = select_x.size();
# ifndef NDEBUG
         size_t m = select_y.size();
         assert( call_id == 0 );
         assert( m == 1 );
# endif
         // nnz
         size_t nnz = 0;
         if( select_y[0] )
         {  for(size_t j = 0; j < n; ++j)
            {  if( select_x[j] )
                  ++nnz;
            }
         }
         // pattern_out
         pattern_out.resize(n, n, nnz);
         size_t k = 0;
         if( select_y[0] )
         {  for(size_t j = 0; j < n; ++j)
            {  if( select_x[j] )
                  pattern_out.set(k++, j, j);
            }
         }
         return true;
      }
      // END HES_SPARSITY
      // BEGIN REV_DEPEND
      bool rev_depend(
         size_t                                     call_id     ,
         CppAD::vector<bool>&                       depend_x    ,
         const CppAD::vector<bool>&                 depend_y    ) override
      {  size_t n = depend_x.size();
# ifndef NDEBUG
         size_t m = depend_y.size();
         assert( call_id == 0 );
         assert( m == 1 );
# endif
         for(size_t j = 0; j < n; ++j)
            depend_x[j] = depend_y[0];
         //
         return true;
      }
      // END REV_DEPEND
   };
}
// END_DEFINE_ATOMIC_FUNCTION

// BEGIN_USE_ATOMIC_FUNCTION
bool norm_sq(void)
{  // ok, eps
   bool ok    = true;
   double eps = 10. * CppAD::numeric_limits<double>::epsilon();
   //
   // atom_norm_sq
   atomic_norm_sq afun("atomic_norm_sq");
   //
   // n, m
   size_t n = 2;
   size_t m = 1;
   //
   // x
   CPPAD_TESTVECTOR(double) x(n);
   for(size_t j = 0; j < n; ++j)
      x[j] = 1.0 / (double(j) + 1.0);
   //
   // ax
   CPPAD_TESTVECTOR( CppAD::AD<double> ) ax(n);
   for(size_t j = 0; j < n; ++j)
      ax[j] = x[j];
   CppAD::Independent(ax);
   //
   // ay
   CPPAD_TESTVECTOR( CppAD::AD<double> ) ay(m);
   afun(ax, ay);
   //
   // f
   CppAD::ADFun<double> f;
   f.Dependent (ax, ay);
   //
   // check
   double check = 0.0;
   for(size_t j = 0; j < n; ++j)
      check += x[j] * x[j];
   //
   // ok
   // check ay[0]
   ok &= CppAD::NearEqual( Value(ay[0]) , check,  eps, eps);
   //
   // ok
   // check zero order forward mode
   CPPAD_TESTVECTOR(double) y(m);
   y   = f.Forward(0, x);
   ok &= CppAD::NearEqual(y[0] , check,  eps, eps);
   //
   // n2, check
   size_t n2  = n / 2;
   check      = 2.0 * x[n2];
   //
   // ok
   // check first order forward mode partial w.r.t. x[n2]
   CPPAD_TESTVECTOR(double) x1(n), y1(m);
   for(size_t j = 0; j < n; ++j)
         x1[j] = 0.0;
   x1[n2] = 1.0;
   y1     = f.Forward(1, x1);
   ok    &= CppAD::NearEqual(y1[0] , check,  eps, eps);
   //
   // ok
   // first order reverse mode
   size_t q = 1;
   CPPAD_TESTVECTOR(double)  w(m), dw(n * q);
   w[0]  = 1.;
   dw    = f.Reverse(q, w);
   for(size_t j = 0; j < n; ++j)
   {  check = 2.0 * x[j];
      ok &= CppAD::NearEqual(dw[j] , check,  eps, eps);
   }
   //
   // pattern_out
   // reverse mode Jacobian sparstiy pattern
   CppAD::sparse_rc< CPPAD_TESTVECTOR(size_t) > pattern_in, pattern_out;
   pattern_in.resize(m, m, m);
   for(size_t i = 0; i < m; ++i)
      pattern_in.set(i, i, i);
   bool transpose     = false;
   bool dependency    = false;
   bool internal_bool = false;
   f.rev_jac_sparsity(
      pattern_in, transpose, dependency, internal_bool, pattern_out
   );
   //
   // ok
   ok &= pattern_out.nnz() == n;
   CPPAD_TESTVECTOR(size_t) row_major  = pattern_out.row_major();
   for(size_t j = 0; j < n; ++j)
   {  size_t r = pattern_out.row()[ row_major[j] ];
      size_t c = pattern_out.col()[ row_major[j] ];
      ok      &= r == 0 && c == j;
   }
   //
   // pattern_out
   // forward mode Hessian sparsity pattern
   CPPAD_TESTVECTOR(bool) select_x(n), select_y(m);
   for(size_t j = 0; j < n; ++j)
      select_x[j] = true;
   for(size_t i = 0; i < m; ++i)
      select_y[i] = true;
   internal_bool = false;
   f.for_hes_sparsity(
      select_x, select_y, internal_bool, pattern_out
   );
   //
   // ok
   ok &= pattern_out.nnz() == n;
   row_major  = pattern_out.row_major();
   for(size_t j = 0; j < n; ++j)
   {  size_t r   = pattern_out.row()[ row_major[j] ];
      size_t c   = pattern_out.col()[ row_major[j] ];
      ok        &= r == j && c == j;
   }
   //
   // optimize
   // this uses the rev_depend override above
   f.optimize("val_graph no_conditional_skip");
   //
   // ok
   // check zero order forward mode (on optimized version of f)
   y     = f.Forward(0, x);
   check = 0.0;
   for(size_t j = 0; j < n; ++j)
      check += x[j] * x[j];
   ok &= CppAD::NearEqual(y[0] , check,  eps, eps);
   //
   return ok;
}
// END_USE_ATOMIC_FUNCTION