File: for_sparse_hes.cpp

package info (click to toggle)
cppad 2025.00.00.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,552 kB
  • sloc: cpp: 112,594; sh: 5,972; ansic: 179; python: 71; sed: 12; makefile: 10
file content (191 lines) | stat: -rw-r--r-- 5,104 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
// 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 for_sparse_hes.cpp}

Forward Mode Hessian Sparsity: Example and Test
###############################################

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

{xrst_end for_sparse_hes.cpp}
*/
// BEGIN C++

# include <cppad/cppad.hpp>
namespace { // -------------------------------------------------------------

// expected sparsity pattern
bool check_f0[] = {
   false, false, false,  // partials w.r.t x0 and (x0, x1, x2)
   false, false, false,  // partials w.r.t x1 and (x0, x1, x2)
   false, false, true    // partials w.r.t x2 and (x0, x1, x2)
};
bool check_f1[] = {
   false,  true, false,  // partials w.r.t x0 and (x0, x1, x2)
   true,  false, false,  // partials w.r.t x1 and (x0, x1, x2)
   false, false, false   // partials w.r.t x2 and (x0, x1, x2)
};

// define the template function BoolCases<Vector> in empty namespace
template <class Vector> // vector class, elements of type bool
bool BoolCases(bool optimize)
{  bool ok = true;
   using CppAD::AD;

   // domain space vector
   size_t n = 3;
   CPPAD_TESTVECTOR(AD<double>) ax(n);
   ax[0] = 0.;
   ax[1] = 1.;
   ax[2] = 2.;

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

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

   // create f: x -> y and stop tape recording
   CppAD::ADFun<double> f(ax, ay);
   if( optimize )
      f.optimize();

   // sparsity pattern for diagonal of identity matrix
   Vector r(n);
   size_t i, j;
   for(i = 0; i < n; i++)
      r[ i ] = true;

   // compute sparsity pattern for H(x) = F_0^{(2)} (x)
   Vector s(m);
   for(i = 0; i < m; i++)
      s[i] = false;
   s[0] = true;
   Vector h(n * n);
   h    = f.ForSparseHes(r, s);

   // check values
   for(i = 0; i < n; i++)
      for(j = 0; j < n; j++)
         ok &= (h[ i * n + j ] == check_f0[ i * n + j ] );

   // compute sparsity pattern for H(x) = F_1^{(2)} (x)
   for(i = 0; i < m; i++)
      s[i] = false;
   s[1] = true;
   h    = f.ForSparseHes(r, s);

   // check values
   for(i = 0; i < n; i++)
      for(j = 0; j < n; j++)
         ok &= (h[ i * n + j ] == check_f1[ i * n + j ] );

   return ok;
}
// define the template function SetCases<Vector> in empty namespace
template <class Vector> // vector class, elements of type std::set<size_t>
bool SetCases(bool optimize)
{  bool ok = true;
   using CppAD::AD;

   // domain space vector
   size_t n = 3;
   CPPAD_TESTVECTOR(AD<double>) ax(n);
   ax[0] = 0.;
   ax[1] = 1.;
   ax[2] = 2.;

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

   // range space vector
   size_t m = 2;
   CPPAD_TESTVECTOR(AD<double>) ay(m);
   ay[0] = sin( ax[2] );
   ay[1] = ax[0] * ax[1];

   // create f: x -> y and stop tape recording
   CppAD::ADFun<double> f(ax, ay);
   if( optimize )
      f.optimize();

   // sparsity pattern for the diagonal of the identity matrix
   Vector r(1);
   size_t i;
   for(i = 0; i < n; i++)
      r[0].insert(i);

   // compute sparsity pattern for H(x) = F_0^{(2)} (x)
   Vector s(1);
   assert( s[0].empty() );
   s[0].insert(0);
   Vector h(n);
   h    = f.ForSparseHes(r, s);

   // check values
   std::set<size_t>::iterator itr;
   size_t j;
   for(i = 0; i < n; i++)
   {  for(j = 0; j < n; j++)
      {  bool found = h[i].find(j) != h[i].end();
         ok        &= (found == check_f0[i * n + j]);
      }
   }

   // compute sparsity pattern for H(x) = F_1^{(2)} (x)
   s[0].clear();
   assert( s[0].empty() );
   s[0].insert(1);
   h    = f.ForSparseHes(r, s);

   // check values
   for(i = 0; i < n; i++)
   {  for(j = 0; j < n; j++)
      {  bool found = h[i].find(j) != h[i].end();
         ok        &= (found == check_f1[i * n + j]);
      }
   }

   return ok;
}
} // End empty namespace

# include <vector>
# include <valarray>
bool for_sparse_hes(void)
{  bool ok = true;
   for(size_t k = 0; k < 2; k++)
   {  bool optimize = bool(k);

      // Run with Vector equal to four different cases
      // all of which are Simple Vectors with elements of type bool.
      ok &= BoolCases< CppAD::vector  <bool> >(optimize);
      ok &= BoolCases< CppAD::vectorBool     >(optimize);
      ok &= BoolCases< std::vector    <bool> >(optimize);
      ok &= BoolCases< std::valarray  <bool> >(optimize);

      // Run with Vector equal to two different cases both of which are
      // Simple Vectors with elements of type std::set<size_t>
      typedef std::set<size_t> set;
      ok &= SetCases< CppAD::vector  <set> >(optimize);
      ok &= SetCases< std::vector    <set> >(optimize);

      // Do not use valarray because its element access in the const case
      // returns a copy instead of a reference
      // ok &= SetCases< std::valarray  <set> >(optimize);
   }
   return ok;
}


// END C++