本文整理汇总了C++中AVEC类的典型用法代码示例。如果您正苦于以下问题:C++ AVEC类的具体用法?C++ AVEC怎么用?C++ AVEC使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了AVEC类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: TEST
TEST(AgradRevMatrix, trace_gen_inv_quad_form_ldlt_grad_vvd) {
using stan::math::sum;
using stan::agrad::matrix_v;
using stan::math::matrix_d;
matrix_v av(4,4);
matrix_d ad(4,4);
matrix_d bd(4,2);
matrix_d cd(2,2);
matrix_v cv(2,2);
AVAR res;
AVEC vars;
VEC grad;
size_t i,j,pos;
bd << 100, 10,
0, 1,
-3, -3,
5, 2;
ad << 9.0, 3.0, 3.0, 3.0,
3.0, 10.0, 2.0, 2.0,
3.0, 2.0, 7.0, 1.0,
3.0, 2.0, 1.0, 112.0;
av << 9.0, 3.0, 3.0, 3.0,
3.0, 10.0, 2.0, 2.0,
3.0, 2.0, 7.0, 1.0,
3.0, 2.0, 1.0, 112.0;
cd.setIdentity(2,2);
cv.setIdentity(2,2);
stan::math::LDLT_factor<stan::agrad::var,-1,-1> ldlt_av;
ldlt_av.compute(av);
ASSERT_TRUE(ldlt_av.success());
matrix_d ainv(ad.inverse());
matrix_d dqda(-ainv*bd*cd.transpose()*bd.transpose()*ainv);
matrix_d dqdc(bd.transpose()*ainv.transpose()*bd);
// var-var
res = trace_gen_inv_quad_form_ldlt(cv,ldlt_av,bd);
vars.clear();
for (i = 0; i < 2; i++)
for (j = 0; j < 2; j++)
vars.push_back(cv(i,j));
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
vars.push_back(av(i,j));
grad = cgradvec(res,vars);
pos = 0;
for (i = 0; i < 2; i++)
for (j = 0; j < 2; j++, pos++)
EXPECT_FLOAT_EQ(grad[pos], dqdc(i,j));
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++, pos++)
EXPECT_FLOAT_EQ(grad[pos], dqda(i,j));
}
示例2: TEST
TEST(AgradRevMatrix, trace_gen_quad_form_mat_grad_vdv) {
using stan::math::trace_gen_quad_form;
using stan::math::sum;
using stan::agrad::matrix_v;
using stan::math::matrix_d;
matrix_v av(4,4);
matrix_d ad(4,4);
matrix_d bd(4,2);
matrix_v bv(4,2);
matrix_d cd(2,2);
matrix_v cv(2,2);
AVAR res;
AVEC vars;
VEC grad;
size_t i,j,pos;
bd << 100, 10,
0, 1,
-3, -3,
5, 2;
bv << 100, 10,
0, 1,
-3, -3,
5, 2;
ad << 2.0, 3.0, 4.0, 5.0,
6.0, 10.0, 2.0, 2.0,
7.0, 2.0, 7.0, 1.0,
8.0, 2.0, 1.0, 112.0;
av << 2.0, 3.0, 4.0, 5.0,
6.0, 10.0, 2.0, 2.0,
7.0, 2.0, 7.0, 1.0,
8.0, 2.0, 1.0, 112.0;
cd.setIdentity(2,2);
cv.setIdentity(2,2);
matrix_d dqdb(ad*bd*cd.transpose() + ad.transpose()*bd*cd);
matrix_d dqdc(bd.transpose()*ad.transpose()*bd);
res = trace_gen_quad_form(cv,ad,bv);
vars.clear();
for (i = 0; i < 2; i++)
for (j = 0; j < 2; j++)
vars.push_back(cv(i,j));
for (i = 0; i < 4; i++)
for (j = 0; j < 2; j++)
vars.push_back(bv(i,j));
grad = cgradvec(res,vars);
pos = 0;
for (i = 0; i < 2; i++)
for (j = 0; j < 2; j++, pos++)
EXPECT_FLOAT_EQ(grad[pos], dqdc(i,j));
for (i = 0; i < 4; i++)
for (j = 0; j < 2; j++, pos++)
EXPECT_FLOAT_EQ(grad[pos], dqdb(i,j));
}
示例3: TEST
TEST(AgradRevMatrix, quad_form_sym_mat_grad_vv) {
using stan::math::quad_form_sym;
using stan::math::sum;
using stan::agrad::matrix_v;
using stan::math::matrix_d;
matrix_v av(4,4);
matrix_d ad(4,4);
matrix_d bd(4,2);
matrix_v bv(4,2);
AVAR res;
AVEC vars;
VEC grad;
size_t i,j,pos;
bd << 100, 10,
0, 1,
-3, -3,
5, 2;
bv << 100, 10,
0, 1,
-3, -3,
5, 2;
ad << 2.0, 3.0, 4.0, 5.0,
3.0, 10.0, 2.0, 2.0,
4.0, 2.0, 7.0, 1.0,
5.0, 2.0, 1.0, 112.0;
av << 2.0, 3.0, 4.0, 5.0,
3.0, 10.0, 2.0, 2.0,
4.0, 2.0, 7.0, 1.0,
5.0, 2.0, 1.0, 112.0;
matrix_d dqda(bd*matrix_d::Ones(2,2)*bd.transpose());
matrix_d dqdb((ad*bd + ad.transpose()*bd)*matrix_d::Ones(2,2));
// var-var
res = sum(quad_form_sym(av,bv));
vars.clear();
for (i = 0; i < 4; i++)
for (j = 0; j < 2; j++)
vars.push_back(bv(i,j));
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
vars.push_back(av(i,j));
grad = cgradvec(res,vars);
pos = 0;
for (i = 0; i < 4; i++)
for (j = 0; j < 2; j++, pos++)
EXPECT_FLOAT_EQ(grad[pos], dqdb(i,j));
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++, pos++)
EXPECT_FLOAT_EQ(grad[pos], dqda(i,j));
}
示例4: TEST
TEST(AgradRevMatrix, append_col_row_vector) {
row_vector_v a(3);
row_vector_v a_square(3);
RowVectorXd b(3);
a << 2.0, 3.0, 9.0;
b << 4.0, 3.0, 0.0;
AVEC x;
for (int i = 0; i < 3; ++i) {
x.push_back(a(i));
a_square(i) = square(a(i));
}
AVAR append_col_ab = sum(append_col(a_square, b));
VEC g = cgradvec(append_col_ab, x);
size_t idx = 0;
for (int i = 0; i < 3; i++)
EXPECT_FLOAT_EQ(a(i).val()*2.0, g[idx++]);
set_zero_all_adjoints();
append_col_ab = sum(append_col(a_square, 2.1));
g = cgradvec(append_col_ab, x);
idx = 0;
for (int i = 0; i < 3; i++)
EXPECT_FLOAT_EQ(a(i).val()*2.0, g[idx++]);
set_zero_all_adjoints();
append_col_ab = sum(append_col(2.1, a_square));
g = cgradvec(append_col_ab, x);
idx = 0;
for (int i = 0; i < 3; i++)
EXPECT_FLOAT_EQ(a(i).val()*2.0, g[idx++]);
set_zero_all_adjoints();
append_col_ab = sum(append_col(a_square(2)*3.0, b));
append_col_ab.grad();
EXPECT_FLOAT_EQ(a(2).val()*6.0, x[2].adj());
set_zero_all_adjoints();
append_col_ab = sum(append_col(b, a_square(1)*3.0));
append_col_ab.grad();
EXPECT_FLOAT_EQ(a(1).val()*6.0, x[1].adj());
stan::math::recover_memory();
}
示例5: TEST
TEST(AgradRevMatrix, dot_product_vd) {
AVEC a;
VEC b;
AVAR c;
for (int i = -1; i < 2; i++) { // a = (-1, 0, 1), b = (1, 2, 3)
a.push_back(i);
b.push_back(i + 2);
}
c = dot_product(&a[0], &b[0], 3);
EXPECT_EQ(2, c);
VEC grad;
c.grad(a, grad);
EXPECT_EQ(grad[0], 1);
EXPECT_EQ(grad[1], 2);
EXPECT_EQ(grad[2], 3);
}
示例6: TEST
TEST(AgradRevMatrix, trace_quad_form_ldlt_mat_grad_dv) {
using stan::math::sum;
using stan::math::matrix_v;
using stan::math::matrix_d;
matrix_d ad(4,4);
matrix_d bd(4,2);
matrix_v bv(4,2);
AVAR res;
AVEC vars;
VEC grad;
size_t i,j,pos;
bd << 100, 10,
0, 1,
-3, -3,
5, 2;
bv << 100, 10,
0, 1,
-3, -3,
5, 2;
ad << 9.0, 3.0, 3.0, 3.0,
3.0, 10.0, 2.0, 2.0,
3.0, 2.0, 7.0, 1.0,
3.0, 2.0, 1.0, 112.0;
stan::math::LDLT_factor<double,-1,-1> ldlt_ad;
ldlt_ad.compute(ad);
ASSERT_TRUE(ldlt_ad.success());
matrix_d ainv(ad.inverse());
matrix_d dqdb(ainv*bd + ainv.transpose()*bd);
// var-var
res = trace_inv_quad_form_ldlt(ldlt_ad,bv);
vars.clear();
for (i = 0; i < 4; i++)
for (j = 0; j < 2; j++)
vars.push_back(bv(i,j));
grad = cgradvec(res,vars);
pos = 0;
for (i = 0; i < 4; i++)
for (j = 0; j < 2; j++, pos++)
EXPECT_FLOAT_EQ(grad[pos], dqdb(i,j));
}
示例7: test_sort_indices_desc
void test_sort_indices_desc(VEC val) {
AVEC x;
for(size_t i=0U; i<val.size(); i++)
x.push_back(AVAR(val[i]));
std::vector<int> val_sorted = sort_indices_desc(val);
std::vector<int> x_sorted = sort_indices_desc(x);
for(size_t i=0U; i<val.size(); i++)
EXPECT_EQ(val_sorted[i],x_sorted[i]);
for(size_t i=0U; i<val.size(); i++)
for(size_t j=0U; j<val.size(); j++)
if(val_sorted[i] == val[j])
EXPECT_EQ(x_sorted[i],x[j]);
else
EXPECT_FALSE(x_sorted[i]==x[j]);
}
示例8: test_sort_asc
void test_sort_asc(VEC val) {
using stan::math::sort_asc;
using stan::agrad::sort_asc;
AVEC x;
for(size_t i=0U; i<val.size(); i++)
x.push_back(AVAR(val[i]));
VEC val_sorted = sort_asc(val);
AVEC x_sorted = sort_asc(x);
for(size_t i=0U; i<val.size(); i++)
EXPECT_EQ(val_sorted[i],x_sorted[i].val());
for(size_t i=0U; i<val.size(); i++)
for(size_t j=0U; j<val.size(); j++)
if(val_sorted[i] == val[j])
EXPECT_EQ(x_sorted[i],x[j]);
else
EXPECT_FALSE(x_sorted[i]==x[j]);
}
示例9: TEST
TEST(AgradRevMatrix, distance_vd) {
using stan::math::vector_d;
using stan::math::vector_v;
vector_v a(3);
vector_d 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 av;
VEC grad;
for (size_t i = 0; i < 3; i++) {
av.push_back(a[i]);
}
c.grad(av, grad);
EXPECT_FLOAT_EQ((a(0).val() - b(0)) / 3.464102, grad[0]);
EXPECT_FLOAT_EQ((a(1).val() - b(1)) / 3.464102, grad[1]);
EXPECT_FLOAT_EQ((a(2).val() - b(2)) / 3.464102, grad[2]);
}
示例10: createAVEC
AVEC createAVEC(AVAR x1, AVAR x2) {
AVEC v;
v.push_back(x1);
v.push_back(x2);
return v;
}