File: fun_assign.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 (103 lines) | stat: -rw-r--r-- 3,145 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
// 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 fun_assign.cpp}

ADFun Assignment: Example and Test
##################################

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

{xrst_end fun_assign.cpp}
*/
// BEGIN C++
# include <cppad/cppad.hpp>
# include <limits>

bool fun_assign(void)
{  bool ok = true;
   using CppAD::AD;
   using CppAD::NearEqual;
   size_t i, j;

   // ten times machine percision
   double eps = 10. * CppAD::numeric_limits<double>::epsilon();

   // an empty ADFun<double> object
   CppAD::ADFun<double> g;

   // domain space vector
   size_t n  = 3;
   CPPAD_TESTVECTOR(AD<double>) x(n);
   for(j = 0; j < n; j++)
      x[j] = AD<double>(j + 2);

   // declare independent variables and start tape recording
   CppAD::Independent(x);

   // range space vector
   size_t m = 2;
   CPPAD_TESTVECTOR(AD<double>) y(m);
   y[0] = x[0] + x[0] * x[1];
   y[1] = x[1] * x[2] + x[2];

   // Store operation sequence, and order zero forward results, in f.
   // This assignment will use move semantics
   CppAD::ADFun<double> f;
   f = CppAD::ADFun<double>(x, y);

   // sparsity pattern for the identity matrix
   CPPAD_TESTVECTOR(std::set<size_t>) r(n);
   for(j = 0; j < n; j++)
      r[j].insert(j);

   // Store forward mode sparsity pattern in f
   f.ForSparseJac(n, r);

   // make a copy of f in g
   g = f;

   // check values that should be equal
   ok &= ( g.size_order() == f.size_order() );
   ok &= ( (g.size_forward_bool() > 0) == (f.size_forward_bool() > 0) );
   ok &= ( (g.size_forward_set() > 0)  == (f.size_forward_set() > 0) );

   // Use zero order Taylor coefficient from f for first order
   // calculation using g.
   CPPAD_TESTVECTOR(double) dx(n), dy(m);
   for(i = 0; i < n; i++)
      dx[i] = 0.;
   dx[1] = 1;
   dy    = g.Forward(1, dx);
   ok &= NearEqual(dy[0], x[0], eps, eps); // partial y[0] w.r.t x[1]
   ok &= NearEqual(dy[1], x[2], eps, eps); // partial y[1] w.r.t x[1]

   // Use forward Jacobian sparsity pattern from f to calculate
   // Hessian sparsity pattern using g.
   CPPAD_TESTVECTOR(std::set<size_t>) s(1), h(n);
   s[0].insert(0); // Compute sparsity pattern for Hessian of y[0]
   h =  f.RevSparseHes(n, s);

   // check sparsity pattern for Hessian of y[0] = x[0] + x[0] * x[1]
   ok  &= ( h[0].find(0) == h[0].end() ); // zero     w.r.t x[0], x[0]
   ok  &= ( h[0].find(1) != h[0].end() ); // non-zero w.r.t x[0], x[1]
   ok  &= ( h[0].find(2) == h[0].end() ); // zero     w.r.t x[0], x[2]

   ok  &= ( h[1].find(0) != h[1].end() ); // non-zero w.r.t x[1], x[0]
   ok  &= ( h[1].find(1) == h[1].end() ); // zero     w.r.t x[1], x[1]
   ok  &= ( h[1].find(2) == h[1].end() ); // zero     w.r.t x[1], x[2]

   ok  &= ( h[2].find(0) == h[2].end() ); // zero     w.r.t x[2], x[0]
   ok  &= ( h[2].find(1) == h[2].end() ); // zero     w.r.t x[2], x[1]
   ok  &= ( h[2].find(2) == h[2].end() ); // zero     w.r.t x[2], x[2]

   return ok;
}

// END C++