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
|
// 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
// ----------------------------------------------------------------------------
/*
Two old Sub examples now used just for valiadation testing
*/
# include <cppad/cppad.hpp>
namespace { // BEGIN empty namespace
bool One(void)
{ bool ok = true;
using namespace CppAD;
// independent variable vector, indices, values, and declaration
CPPAD_TESTVECTOR(AD<double>) U(2);
size_t s = 0;
size_t t = 1;
U[s] = 3.;
U[t] = 2.;
Independent(U);
// dependent variable vector and indices
CPPAD_TESTVECTOR(AD<double>) Z(3);
size_t x = 0;
size_t y = 1;
size_t z = 2;
// dependent variable values
Z[x] = U[s] - U[t]; // AD<double> - AD<double>
Z[y] = Z[x] - 1.; // AD<double> - double
Z[z] = 1. - Z[y]; // double - AD<double>
// create f: U -> Z and vectors used for derivative calculations
ADFun<double> f(U, Z);
CPPAD_TESTVECTOR(double) v( f.Domain() );
CPPAD_TESTVECTOR(double) w( f.Range() );
// check function values
ok &= ( Z[x] == 3. - 2. );
ok &= ( Z[y] == 3. - 2. - 1. );
ok &= ( Z[z] == 1. - 3. + 2. + 1. );
// forward computation of partials w.r.t. s
v[s] = 1.;
v[t] = 0.;
w = f.Forward(1, v);
ok &= ( w[x] == 1. ); // dx/ds
ok &= ( w[y] == 1. ); // dy/ds
ok &= ( w[z] == -1. ); // dz/ds
// reverse computation of second partials of z
CPPAD_TESTVECTOR(double) r( f.Domain() * 2 );
w[x] = 0.;
w[y] = 0.;
w[z] = 1.;
r = f.Reverse(2, w);
ok &= ( r[2 * s + 1] == 0. ); // d^2 z / (ds ds)
ok &= ( r[2 * t + 1] == 0. ); // d^2 z / (ds dt)
return ok;
}
bool Two(void)
{ bool ok = true;
using namespace CppAD;
double eps99 = 99.0 * std::numeric_limits<double>::epsilon();
// independent variable vector
double u0 = .5;
CPPAD_TESTVECTOR(AD<double>) U(1);
U[0] = u0;
Independent(U);
AD<double> a = 2. * U[0] - 1.; // AD<double> - double
AD<double> b = a - 2; // AD<double> - int
AD<double> c = 3. - b; // double - AD<double>
AD<double> d = 4 - c; // int - AD<double>
// dependent variable vector
CPPAD_TESTVECTOR(AD<double>) Z(1);
Z[0] = U[0] - d; // AD<double> - AD<double>
// create f: U -> Z and vectors used for derivative calculations
ADFun<double> f(U, Z);
CPPAD_TESTVECTOR(double) v(1);
CPPAD_TESTVECTOR(double) w(1);
// check value
ok &= NearEqual(Value(Z[0]) , u0-4+3-2*u0+1+2, eps99 , eps99);
// forward computation of partials w.r.t. u
size_t j;
size_t p = 5;
double jfac = 1.;
double value = -1.;
v[0] = 1.;
for(j = 1; j < p; j++)
{ jfac *= double(j);
w = f.Forward(j, v);
ok &= NearEqual(w[0], value/jfac, eps99, eps99); // d^jz/du^j
v[0] = 0.;
value = 0.;
}
// reverse computation of partials of Taylor coefficients
CPPAD_TESTVECTOR(double) r(p);
w[0] = 1.;
r = f.Reverse(p, w);
jfac = 1.;
value = -1.;
for(j = 0; j < p; j++)
{ ok &= NearEqual(r[j], value/jfac, eps99, eps99); // d^jz/du^j
jfac *= double(j + 1);
value = 0.;
}
return ok;
}
bool Three(void)
{ bool ok = true;
using namespace CppAD;
// special cases where tests above check OK and SubpvOp
// implementation is known to be wrong.
// Probably two minuses make a plus.
size_t n = 1;
CPPAD_TESTVECTOR(AD<double>) X(n);
X[0] = 1.;
Independent(X);
size_t m = 1;
CPPAD_TESTVECTOR(AD<double>) Y(m);
Y[0] = 1. - X[0];
ADFun<double> f(X, Y);
CPPAD_TESTVECTOR(double) w(m), dw(n);
w[0] = 1.;
dw = f.Reverse(1, w);
ok &= (dw[0] == -1.);
return ok;
}
bool Four(void)
{ bool ok = true;
using namespace CppAD;
// special cases where parameter number is equal to
// variable index in result.
size_t n = 1;
CPPAD_TESTVECTOR(AD<double>) X(n);
X[0] = 1.;
Independent(X);
size_t m = 1;
CPPAD_TESTVECTOR(AD<double>) Y(m);
if( 0. < X[0] && X[0] < 10. )
Y[0] = X[0] - 2.;
else
Y[0] = X[0] - 2.;
ADFun<double> f(X, Y);
CPPAD_TESTVECTOR(double) y(m), x(n);
x[0] = 1.;
y = f.Forward(0, x);
ok &= (y[0] == -1.);
CPPAD_TESTVECTOR(double) dy(m), dx(n);
dx[0] = 1.;
dy = f.Forward(1, dx);
ok &= (dy[0] == 1.);
return ok;
}
} // END empty namespace
bool Sub(void)
{ bool ok = true;
ok &= One();
ok &= Two();
ok &= Three();
ok &= Four();
return ok;
}
|