本文整理汇总了C++中CppAD::Independent方法的典型用法代码示例。如果您正苦于以下问题:C++ CppAD::Independent方法的具体用法?C++ CppAD::Independent怎么用?C++ CppAD::Independent使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CppAD
的用法示例。
在下文中一共展示了CppAD::Independent方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: mul_level
bool mul_level(void)
{ bool ok = true;
using CppAD::checkpoint;
using CppAD::ADFun;
using CppAD::Independent;
// domain dimension for this problem
size_t n = 10;
size_t m = 1;
// checkpoint version of the function F(x)
a2vector a2x(n), a2y(m);
for(size_t j = 0; j < n; j++)
a2x[j] = a2double(j + 1);
//
// could also use bool_sparsity_enum or set_sparsity_enum
checkpoint<a1double> atom_f("atom_f", f_algo, a2x, a2y);
//
// Record a version of y = f(x) without checkpointing
Independent(a2x);
f_algo(a2x, a2y);
ADFun<a1double> check_not(a2x, a2y);
//
// number of variables in a tape of f_algo that does not use checkpointing
size_t size_not = check_not.size_var();
//
// Record a version of y = f(x) with checkpointing
Independent(a2x);
atom_f(a2x, a2y);
ADFun<a1double> check_yes(a2x, a2y);
//
// f_algo is represented by one atomic operation in this tape
ok &= check_yes.size_var() < size_not;
//
// now record operations at a1double level
a1vector a1x(n), a1y(m);
for(size_t j = 0; j < n; j++)
a1x[j] = a1double(j + 1);
//
// without checkpointing
Independent(a1x);
a1y = check_not.Forward(0, a1x);
ADFun<double> with_not(a1x, a1y);
//
// should have the same size
ok &= with_not.size_var() == size_not;
//
// with checkpointing
Independent(a1x);
a1y = check_yes.Forward(0, a1x);
ADFun<double> with_yes(a1x, a1y);
//
// f_algo is nolonger represented by one atomic operation in this tape
ok &= with_yes.size_var() == size_not;
//
return ok;
}
示例2: CompareChange
bool CompareChange(void)
{ bool ok = true;
using CppAD::AD;
using CppAD::ADFun;
using CppAD::Independent;
// domain space vector
size_t n = 2;
CPPAD_TEST_VECTOR< AD<double> > X(n);
X[0] = 3.;
X[1] = 4.;
// declare independent variables and start tape recording
CppAD::Independent(X);
// range space vector
size_t m = 1;
CPPAD_TEST_VECTOR< AD<double> > Y(m);
Y[0] = Minimum(X[0], X[1]);
// create f: x -> y and stop tape recording
ADFun<double> f(X, Y);
// evaluate zero mode Forward where conditional has the same result
// note that f.CompareChange is not defined when NDEBUG is true
CPPAD_TEST_VECTOR<double> x(n);
CPPAD_TEST_VECTOR<double> y(m);
x[0] = 3.5;
x[1] = 4.;
y = f.Forward(0, x);
ok &= (y[0] == x[0]);
ok &= (y[0] == Minimum(x[0], x[1]));
ok &= (f.CompareChange() == 0);
// evaluate zero mode Forward where conditional has different result
x[0] = 4.;
x[1] = 3.;
y = f.Forward(0, x);
ok &= (y[0] == x[0]);
ok &= (y[0] != Minimum(x[0], x[1]));
ok &= (f.CompareChange() == 1);
// re-tape to obtain the new AD operation sequence
X[0] = 4.;
X[1] = 3.;
Independent(X);
Y[0] = Minimum(X[0], X[1]);
// stop tape and store result in f
f.Dependent(Y);
// evaluate the function at new argument values
y = f.Forward(0, x);
ok &= (y[0] == x[1]);
ok &= (y[0] == Minimum(x[0], x[1]));
ok &= (f.CompareChange() == 0);
return ok;
}