本文整理汇总了C++中AVAR::grad方法的典型用法代码示例。如果您正苦于以下问题:C++ AVAR::grad方法的具体用法?C++ AVAR::grad怎么用?C++ AVAR::grad使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AVAR
的用法示例。
在下文中一共展示了AVAR::grad方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: ibeta
TEST(AgradRev,ibeta_dvd) {
using stan::agrad::var;
using stan::math::ibeta;
using stan::agrad::ibeta;
using boost::math::ibeta_derivative;
double a = 0.6;
AVAR b = 0.3;
double c = 0.5;
AVAR f = ibeta(a,b,c);
EXPECT_FLOAT_EQ(0.3121373, f.val());
AVEC x = createAVEC(b);
VEC grad_f;
f.grad(x,grad_f);
EXPECT_FLOAT_EQ(0.7779751,grad_f[0]);
a = 3;
b = 2;
c = 0.2;
f = ibeta(a,b,c);
EXPECT_FLOAT_EQ(0.0272, f.val());
x = createAVEC(b);
f.grad(x,grad_f);
EXPECT_FLOAT_EQ(0.02507405,grad_f[0]);
}
示例2: createAVEC
TEST(AgradRev, log_diff_exp_vv) {
AVAR a = 5.0;
AVAR b = 2.0;
AVAR f = log_diff_exp(a, b);
EXPECT_FLOAT_EQ(std::log(std::exp(5) - std::exp(2)), f.val());
AVEC x = createAVEC(a, b);
VEC grad_f;
f.grad(x,grad_f);
EXPECT_FLOAT_EQ(std::exp(5.0) / (std::exp(5.0) - std::exp(2.0)), grad_f[0]);
EXPECT_FLOAT_EQ(std::exp(2.0) / (std::exp(2.0) - std::exp(5.0)), grad_f[1]);
// underflow example
a = 1000;
b = 10;
f = log_diff_exp(a, b);
EXPECT_FLOAT_EQ(std::log(std::exp(0.0) - std::exp(-990.0)) + 1000.0, f.val());
x = createAVEC(a, b);
f.grad(x,grad_f);
EXPECT_FLOAT_EQ(std::exp(1000.0 - (std::log(std::exp(0.0) - std::exp(-999.0)) + 1000)),
grad_f[0]);
EXPECT_FLOAT_EQ(std::exp(10.0 - (std::log(std::exp(0.0) - std::exp(-999.0)) + 1000)),
grad_f[1]);
}
示例3: a
TEST(AgradRev,bessel_second_kind_int_var) {
int a(0);
AVAR b(4.0);
AVAR f = stan::agrad::bessel_second_kind(a,b);
EXPECT_FLOAT_EQ(-0.01694073932506499190363513444715321824049258989801,f.val());
AVEC x = createAVEC(a,b);
VEC g;
f.grad(x,g);
EXPECT_FLOAT_EQ(0,g[0]);
EXPECT_FLOAT_EQ(-0.39792571055710000525397997245079185227118918162290, g[1]);
a = 1;
b = 3.0;
f = stan::agrad::bessel_second_kind(a,b);
EXPECT_FLOAT_EQ(0.3246744247917999784370128392879532396692751433723549,
f.val());
x = createAVEC(a,b);
f.grad(x,g);
EXPECT_FLOAT_EQ(0,g[0]);
EXPECT_FLOAT_EQ(0.5 * 0.5372504034977141116428784919345973293208741759303264,
g[1]);
b = -4.0;
EXPECT_THROW(stan::agrad::bessel_second_kind(0,b), std::domain_error);
}
示例4: d
TEST(AgradRevMatrix, sum_vector) {
using stan::math::sum;
using stan::math::vector_d;
using stan::math::vector_v;
vector_d d(6);
vector_v v(6);
d << 1, 2, 3, 4, 5, 6;
v << 1, 2, 3, 4, 5, 6;
AVAR output;
output = sum(d);
EXPECT_FLOAT_EQ(21.0, output.val());
output = sum(v);
EXPECT_FLOAT_EQ(21.0, output.val());
std::vector<double> grad;
std::vector<AVAR> x(v.size());
for (int i = 0; i < v.size(); ++i)
x[i] = v(i);
output.grad(x, grad);
EXPECT_EQ(6, grad.size());
for (int i = 0; i < 6; ++i)
EXPECT_FLOAT_EQ(1.0, grad[i]);
d.resize(0);
v.resize(0);
EXPECT_FLOAT_EQ(0.0, sum(d));
EXPECT_FLOAT_EQ(0.0, sum(v).val());
}
示例5: X
TEST(AgradRevMatrix,log_determinant_grad) {
using stan::math::matrix_v;
using stan::math::log_determinant;
matrix_v X(2,2);
AVAR a = 2.0;
AVAR b = 3.0;
AVAR c = 5.0;
AVAR d = 7.0;
X << a, b, c, d;
AVEC x = createAVEC(a,b,c,d);
AVAR f = log_determinant(X);
// det = ad - bc
EXPECT_NEAR(0.0,f.val(),1E-12);
VEC g;
f.grad(x,g);
EXPECT_FLOAT_EQ(-7.0,g[0]);
EXPECT_FLOAT_EQ(5.0,g[1]);
EXPECT_FLOAT_EQ(3.0,g[2]);
EXPECT_FLOAT_EQ(-2.0,g[3]);
}
示例6: x
TEST(AgradRevMatrix,prod) {
using stan::math::prod;
using stan::math::vector_d;
using stan::math::vector_v;
vector_d vd;
vector_v vv;
EXPECT_FLOAT_EQ(1.0,prod(vd));
EXPECT_FLOAT_EQ(1.0,prod(vv).val());
vd = vector_d(1);
vv = vector_v(1);
vd << 2.0;
vv << 2.0;
EXPECT_FLOAT_EQ(2.0,prod(vd));
EXPECT_FLOAT_EQ(2.0,prod(vv).val());
vd = vector_d(2);
vd << 2.0, 3.0;
vv = vector_v(2);
vv << 2.0, 3.0;
AVEC x(2);
x[0] = vv[0];
x[1] = vv[1];
AVAR f = prod(vv);
EXPECT_FLOAT_EQ(6.0,prod(vd));
EXPECT_FLOAT_EQ(6.0,f.val());
VEC g;
f.grad(x,g);
EXPECT_FLOAT_EQ(3.0,g[0]);
EXPECT_FLOAT_EQ(2.0,g[1]);
}
示例7: a
TEST(AgradRevMatrix, distance_vv) {
using stan::math::vector_v;
vector_v a(3), b(3);
AVAR c;
for (int i = -1; i < 2; i++) { // a = (-1, 0, 1), b = (1, 2, 3)
a(i+1) = i;
b(i+1) = i + 2;
}
c = stan::math::distance(a, b);
EXPECT_FLOAT_EQ(3.464102, c.val());
AVEC ab;
VEC grad;
for (size_t i = 0; i < 3; i++) {
ab.push_back(a[i]);
ab.push_back(b[i]);
}
c.grad(ab, grad);
EXPECT_FLOAT_EQ((a(0).val() - b(0).val()) / 3.464102, grad[0]);
EXPECT_FLOAT_EQ(-(a(0).val() - b(0).val()) / 3.464102, grad[1]);
EXPECT_FLOAT_EQ((a(1).val() - b(1).val()) / 3.464102, grad[2]);
EXPECT_FLOAT_EQ(-(a(1).val() - b(1).val()) / 3.464102, grad[3]);
EXPECT_FLOAT_EQ((a(2).val() - b(2).val()) / 3.464102, grad[4]);
EXPECT_FLOAT_EQ(-(a(2).val() - b(2).val()) / 3.464102, grad[5]);
}
示例8: test_log1m_exp
void test_log1m_exp(double val) {
using stan::math::log1m_exp;
using stan::agrad::log1m_exp;
using stan::agrad::exp;
using std::exp;
AVAR a(val);
AVEC x = createAVEC(a);
AVAR f = log1m_exp(a);
EXPECT_FLOAT_EQ(log1m_exp(val), f.val());
VEC g;
f.grad(x,g);
double f_val = f.val();
AVAR a2(val);
AVEC x2 = createAVEC(a2);
AVAR f2 = log(1.0 - exp(a2));
VEC g2;
f2.grad(x2,g2);
EXPECT_EQ(1U,g.size());
EXPECT_EQ(1U,g2.size());
EXPECT_FLOAT_EQ(g2[0],g[0]);
EXPECT_FLOAT_EQ(g2[0],-1/boost::math::expm1(-val)); // analytic deriv
EXPECT_FLOAT_EQ(f2.val(),f_val);
}
示例9: asinh
TEST(AgradRev,asinh_boundry) {
double inf = std::numeric_limits<double>::infinity();
AVAR a = inf;
AVAR f = asinh(a);
EXPECT_FLOAT_EQ(inf, f.val());
AVEC x = createAVEC(a);
VEC g;
f.grad(x,g);
EXPECT_FLOAT_EQ(0.0, g[0]);
AVAR b = -inf;
AVAR e = asinh(b);
EXPECT_FLOAT_EQ(-inf,e.val());
AVEC y = createAVEC(b);
VEC h;
e.grad(y,h);
EXPECT_FLOAT_EQ(0.0, h[0]);
}
示例10: a
TEST(AgradRev,sqrt_a) {
AVAR a(5.0);
AVAR f = sqrt(a);
EXPECT_FLOAT_EQ(sqrt(5.0),f.val());
AVEC x = createAVEC(a);
VEC g;
f.grad(x,g);
EXPECT_FLOAT_EQ((1.0/2.0) * pow(5.0,-0.5), g[0]);
}
示例11: a
TEST(AgradRev,plus_plus_a_3) {
AVAR a(5.0);
AVAR f = ++a;
++a; // reassignment loses connection to f
AVEC x = createAVEC(a);
VEC g;
f.grad(x,g);
EXPECT_FLOAT_EQ(0.0,g[0]);
}
示例12: a
TEST(AgradRev,exp_a) {
AVAR a(6.0);
AVAR f = exp(a); // mix exp() functs w/o namespace
EXPECT_FLOAT_EQ(exp(6.0),f.val());
AVEC x = createAVEC(a);
VEC g;
f.grad(x,g);
EXPECT_FLOAT_EQ(exp(6.0),g[0]);
}
示例13: createAVEC
TEST(AgradRev,expm1) {
AVAR a = 1.3;
AVAR f = expm1(a);
EXPECT_FLOAT_EQ(boost::math::expm1(1.3), f.val());
AVEC x = createAVEC(a);
VEC grad_f;
f.grad(x,grad_f);
EXPECT_FLOAT_EQ(std::exp(1.3), grad_f[0]);
}
示例14: lgamma
TEST(AgradRev,lgamma) {
AVAR a = 3.0;
AVAR f = lgamma(a);
EXPECT_FLOAT_EQ(lgamma(3.0),f.val());
AVEC x = createAVEC(a);
VEC grad_f;
f.grad(x,grad_f);
EXPECT_FLOAT_EQ(boost::math::digamma(3.0),grad_f[0]);
}
示例15: createAVEC
TEST(AgradRev,a_times_a) {
AVAR a = 2.0;
AVAR f = a * a;
EXPECT_FLOAT_EQ(4.0,f.val());
AVEC x = createAVEC(a);
VEC grad_f;
f.grad(x,grad_f);
EXPECT_FLOAT_EQ(4.0,grad_f[0]);
}