本文整理汇总了C++中CppAD::isnan方法的典型用法代码示例。如果您正苦于以下问题:C++ CppAD::isnan方法的具体用法?C++ CppAD::isnan怎么用?C++ CppAD::isnan使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CppAD
的用法示例。
在下文中一共展示了CppAD::isnan方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: CondExp
bool CondExp(void)
{ bool ok = true;
using CppAD::isnan;
using CppAD::AD;
using CppAD::NearEqual;
using CppAD::log;
using CppAD::abs;
double eps = 100. * CppAD::numeric_limits<double>::epsilon();
double fmax = std::numeric_limits<double>::max();
// domain space vector
size_t n = 5;
CPPAD_TESTVECTOR(AD<double>) X(n);
size_t j;
for(j = 0; j < n; j++)
X[j] = 1.;
// declare independent variables and start tape recording
CppAD::Independent(X);
AD<double> Sum = 0.;
AD<double> Zero = 0.;
for(j = 0; j < n; j++)
{ // if x_j > 0, add x_j * log( x_j ) to the sum
Sum += CppAD::CondExpGt(X[j], Zero, X[j] * log(X[j]), Zero);
}
// range space vector
size_t m = 1;
CPPAD_TESTVECTOR(AD<double>) Y(m);
Y[0] = Sum;
// create f: X -> Y and stop tape recording
CppAD::ADFun<double> f(X, Y);
// vectors for arguments to the function object f
CPPAD_TESTVECTOR(double) x(n); // argument values
CPPAD_TESTVECTOR(double) y(m); // function values
CPPAD_TESTVECTOR(double) w(m); // function weights
CPPAD_TESTVECTOR(double) dw(n); // derivative of weighted function
// a case where x[j] > 0 for all j
double check = 0.;
for(j = 0; j < n; j++)
{ x[j] = double(j + 1);
check += x[j] * log( x[j] );
}
// function value
y = f.Forward(0, x);
ok &= NearEqual(y[0], check, eps, eps);
// compute derivative of y[0]
w[0] = 1.;
dw = f.Reverse(1, w);
for(j = 0; j < n; j++)
ok &= NearEqual(dw[j], log(x[j]) + 1., eps, eps);
// a case where x[3] is equal to zero
check -= x[3] * log( x[3] );
x[3] = 0.;
// function value
y = f.Forward(0, x);
ok &= NearEqual(y[0], check, eps, eps);
// check derivative of y[0]
f.check_for_nan(false);
w[0] = 1.;
dw = f.Reverse(1, w);
for(j = 0; j < n; j++)
{ if( x[j] > 0 )
ok &= NearEqual(dw[j], log(x[j]) + 1., eps, eps);
else
{ // In this case computing dw[j] is computed using
// log(x[j]) + x[j] / x[j]
// which has limit minus infinity but computes as nan.
ok &= ( isnan( dw[j] ) || dw[j] <= -fmax );
}
}
return ok;
}