File: number_skip.cpp

package info (click to toggle)
cppad 2025.00.00.2-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 11,552 kB
  • sloc: cpp: 112,594; sh: 5,972; ansic: 179; python: 71; sed: 12; makefile: 10
file content (73 lines) | stat: -rw-r--r-- 1,708 bytes parent folder | download | duplicates (2)
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
// 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
// ----------------------------------------------------------------------------

/*
{xrst_begin number_skip.cpp}

Number of Variables That Can be Skipped: Example and Test
#########################################################

{xrst_literal
   // BEGIN C++
   // END C++
}

{xrst_end number_skip.cpp}
*/
// BEGIN C++
# include <cppad/cppad.hpp>
bool number_skip(void)
{  bool ok = true;
   using CppAD::AD;

   // independent variable vector
   CppAD::vector< AD<double> > ax(2);
   ax[0] = 0.;
   ax[1] = 1.;
   Independent(ax);

   // Use a conditional expression
   CppAD::vector< AD<double> > ay(1);

   // variable that gets optimized out
   AD<double> az = ax[0] * ax[0];


   // conditional expression
   ay[0] = CondExpLt(ax[0], ax[1], ax[0] + ax[1], ax[0] - ax[1]);

   // create function object F : x -> ay
   CppAD::ADFun<double> f;
   f.Dependent(ax, ay);

   // use zero order to evaluate F[ (3, 4) ]
   CppAD::vector<double>  x( f.Domain() );
   CppAD::vector<double>  y( f.Range() );
   x[0]    = 3.;
   x[1]    = 4.;
   y   = f.Forward(0, x);
   ok &= (y[0] == x[0] + x[1]);

   // before call to optimize
   ok &= f.number_skip() == 0;
   size_t n_var = f.size_var();

   // now optimize the operation sequence
   f.optimize();

   // after optimize, check forward mode result
   x[0]    = 4.;
   x[1]    = 3.;
   y   = f.Forward(0, x);
   ok &= (y[0] == x[0] - x[1]);

   // after optimize, check amount of optimization
   ok &= f.size_var() == n_var - 1;
   ok &= f.number_skip() == 1;

   return ok;
}

// END C++