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
|
// 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_jac.cpp}
Forward Mode Jacobian Sparsity: Example and Test
################################################
{xrst_literal
// BEGIN C++
// END C++
}
{xrst_end for_sparse_jac.cpp}
*/
// BEGIN C++
# include <set>
# include <cppad/cppad.hpp>
namespace { // -------------------------------------------------------------
// define the template function BoolCases<Vector>
template <class Vector> // vector class, elements of type bool
bool BoolCases(void)
{ bool ok = true;
using CppAD::AD;
// domain space vector
size_t n = 2;
CPPAD_TESTVECTOR(AD<double>) X(n);
X[0] = 0.;
X[1] = 1.;
// declare independent variables and start recording
CppAD::Independent(X);
// range space vector
size_t m = 3;
CPPAD_TESTVECTOR(AD<double>) Y(m);
Y[0] = X[0];
Y[1] = X[0] * X[1];
Y[2] = X[1];
// create f: X -> Y and stop tape recording
CppAD::ADFun<double> f(X, Y);
// sparsity pattern for the identity matrix
Vector r(n * n);
size_t i, j;
for(i = 0; i < n; i++)
{ for(j = 0; j < n; j++)
r[ i * n + j ] = (i == j);
}
// sparsity pattern for F'(x)
Vector s(m * n);
s = f.ForSparseJac(n, r);
// check values
ok &= (s[ 0 * n + 0 ] == true); // Y[0] does depend on X[0]
ok &= (s[ 0 * n + 1 ] == false); // Y[0] does not depend on X[1]
ok &= (s[ 1 * n + 0 ] == true); // Y[1] does depend on X[0]
ok &= (s[ 1 * n + 1 ] == true); // Y[1] does depend on X[1]
ok &= (s[ 2 * n + 0 ] == false); // Y[2] does not depend on X[0]
ok &= (s[ 2 * n + 1 ] == true); // Y[2] does depend on X[1]
// check that values are stored
ok &= (f.size_forward_bool() > 0);
ok &= (f.size_forward_set() == 0);
// sparsity pattern for F'(x)^T, note R is the identity, so R^T = R
bool transpose = true;
Vector st(n * m);
st = f.ForSparseJac(n, r, transpose);
// check values
ok &= (st[ 0 * m + 0 ] == true); // Y[0] does depend on X[0]
ok &= (st[ 1 * m + 0 ] == false); // Y[0] does not depend on X[1]
ok &= (st[ 0 * m + 1 ] == true); // Y[1] does depend on X[0]
ok &= (st[ 1 * m + 1 ] == true); // Y[1] does depend on X[1]
ok &= (st[ 0 * m + 2 ] == false); // Y[2] does not depend on X[0]
ok &= (st[ 1 * m + 2 ] == true); // Y[2] does depend on X[1]
// check that values are stored
ok &= (f.size_forward_bool() > 0);
ok &= (f.size_forward_set() == 0);
// free values from forward calculation
f.size_forward_bool(0);
ok &= (f.size_forward_bool() == 0);
return ok;
}
// define the template function SetCases<Vector>
template <class Vector> // vector class, elements of type std::set<size_t>
bool SetCases(void)
{ bool ok = true;
using CppAD::AD;
// domain space vector
size_t n = 2;
CPPAD_TESTVECTOR(AD<double>) X(n);
X[0] = 0.;
X[1] = 1.;
// declare independent variables and start recording
CppAD::Independent(X);
// range space vector
size_t m = 3;
CPPAD_TESTVECTOR(AD<double>) Y(m);
Y[0] = X[0];
Y[1] = X[0] * X[1];
Y[2] = X[1];
// create f: X -> Y and stop tape recording
CppAD::ADFun<double> f(X, Y);
// sparsity pattern for the identity matrix
Vector r(n);
size_t i;
for(i = 0; i < n; i++)
{ assert( r[i].empty() );
r[i].insert(i);
}
// sparsity pattern for F'(x)
Vector s(m);
s = f.ForSparseJac(n, r);
// an interator to a standard set
std::set<size_t>::iterator itr;
bool found;
// Y[0] does depend on X[0]
found = s[0].find(0) != s[0].end(); ok &= ( found == true );
// Y[0] does not depend on X[1]
found = s[0].find(1) != s[0].end(); ok &= ( found == false );
// Y[1] does depend on X[0]
found = s[1].find(0) != s[1].end(); ok &= ( found == true );
// Y[1] does depend on X[1]
found = s[1].find(1) != s[1].end(); ok &= ( found == true );
// Y[2] does not depend on X[0]
found = s[2].find(0) != s[2].end(); ok &= ( found == false );
// Y[2] does depend on X[1]
found = s[2].find(1) != s[2].end(); ok &= ( found == true );
// check that values are stored
ok &= (f.size_forward_set() > 0);
ok &= (f.size_forward_bool() == 0);
// sparsity pattern for F'(x)^T
bool transpose = true;
Vector st(n);
st = f.ForSparseJac(n, r, transpose);
// Y[0] does depend on X[0]
found = st[0].find(0) != st[0].end(); ok &= ( found == true );
// Y[0] does not depend on X[1]
found = st[1].find(0) != st[1].end(); ok &= ( found == false );
// Y[1] does depend on X[0]
found = st[0].find(1) != st[0].end(); ok &= ( found == true );
// Y[1] does depend on X[1]
found = st[1].find(1) != st[1].end(); ok &= ( found == true );
// Y[2] does not depend on X[0]
found = st[0].find(2) != st[0].end(); ok &= ( found == false );
// Y[2] does depend on X[1]
found = st[1].find(2) != st[1].end(); ok &= ( found == true );
// check that values are stored
ok &= (f.size_forward_set() > 0);
ok &= (f.size_forward_bool() == 0);
return ok;
}
} // End empty namespace
# include <vector>
# include <valarray>
bool ForSparseJac(void)
{ bool ok = true;
// Run with Vector equal to four different cases
// all of which are Simple Vectors with elements of type bool.
ok &= BoolCases< CppAD::vectorBool >();
ok &= BoolCases< CppAD::vector <bool> >();
ok &= BoolCases< std::vector <bool> >();
ok &= BoolCases< std::valarray <bool> >();
// 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> >();
// ok &= SetCases< std::vector <set> >();
// Do not use valarray because its element access in the const case
// returns a copy instead of a reference
// ok &= SetCases< std::valarray <set> >();
return ok;
}
// END C++
|