MCPcopy Create free account
hub / github.com/coin-or/CppAD / BoolCases

Function BoolCases

example/sparse/rev_sparse_jac.cpp:25–82  ·  view source on GitHub ↗

vector class, elements of type bool

Source from the content-addressed store, hash-verified

23// define the template function BoolCases<Vector>
24template <class Vector> // vector class, elements of type bool
25bool BoolCases(void)
26{ bool ok = true;
27 using CppAD::AD;
28
29 // domain space vector
30 size_t n = 2;
31 CPPAD_TESTVECTOR(AD<double>) ax(n);
32 ax[0] = 0.;
33 ax[1] = 1.;
34
35 // declare independent variables and start recording
36 CppAD::Independent(ax);
37
38 // range space vector
39 size_t m = 3;
40 CPPAD_TESTVECTOR(AD<double>) ay(m);
41 ay[0] = ax[0];
42 ay[1] = ax[0] * ax[1];
43 ay[2] = ax[1];
44
45 // create f: x -> y and stop tape recording
46 CppAD::ADFun<double> f(ax, ay);
47
48 // sparsity pattern for the identity matrix
49 Vector r(m * m);
50 size_t i, j;
51 for(i = 0; i < m; i++)
52 { for(j = 0; j < m; j++)
53 r[ i * m + j ] = (i == j);
54 }
55
56 // sparsity pattern for F'(x)
57 Vector s(m * n);
58 s = f.RevSparseJac(m, r);
59
60 // check values
61 ok &= (s[ 0 * n + 0 ] == true); // y[0] does depend on x[0]
62 ok &= (s[ 0 * n + 1 ] == false); // y[0] does not depend on x[1]
63 ok &= (s[ 1 * n + 0 ] == true); // y[1] does depend on x[0]
64 ok &= (s[ 1 * n + 1 ] == true); // y[1] does depend on x[1]
65 ok &= (s[ 2 * n + 0 ] == false); // y[2] does not depend on x[0]
66 ok &= (s[ 2 * n + 1 ] == true); // y[2] does depend on x[1]
67
68 // sparsity pattern for F'(x)^T, note R is the identity, so R^T = R
69 bool transpose = true;
70 Vector st(n * m);
71 st = f.RevSparseJac(m, r, transpose);
72
73 // check values
74 ok &= (st[ 0 * m + 0 ] == true); // y[0] does depend on x[0]
75 ok &= (st[ 1 * m + 0 ] == false); // y[0] does not depend on x[1]
76 ok &= (st[ 0 * m + 1 ] == true); // y[1] does depend on x[0]
77 ok &= (st[ 1 * m + 1 ] == true); // y[1] does depend on x[1]
78 ok &= (st[ 0 * m + 2 ] == false); // y[2] does not depend on x[0]
79 ok &= (st[ 1 * m + 2 ] == true); // y[2] does depend on x[1]
80
81 return ok;
82}

Callers

nothing calls this directly

Calls 2

RevSparseJacMethod · 0.80
IndependentFunction · 0.50

Tested by

no test coverage detected