Skip to content

Commit

Permalink
Merge pull request #2147 from stan-dev/bugfix/issue-2146-compound-dec…
Browse files Browse the repository at this point in the history
…lare-define-matrix

Fixes #2146. Bugfix/issue 2146 compound declare define matrix
  • Loading branch information
syclik authored Dec 3, 2016
2 parents aa226b4 + 4ea63be commit 0621f0c
Show file tree
Hide file tree
Showing 8 changed files with 605 additions and 343 deletions.
438 changes: 223 additions & 215 deletions src/stan/lang/generator.hpp

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/stan/lang/grammars/var_decls_grammar_def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ BOOST_FUSION_ADAPT_STRUCT(stan::lang::matrix_var_decl,
(stan::lang::expression, M_)
(stan::lang::expression, N_)
(std::string, name_)
(std::vector<stan::lang::expression>, dims_) )
(std::vector<stan::lang::expression>, dims_)
(stan::lang::expression, def_) )

BOOST_FUSION_ADAPT_STRUCT(stan::lang::unit_vector_var_decl,
(stan::lang::expression, K_)
Expand Down
63 changes: 59 additions & 4 deletions src/test/test-models/good/declare-define-conditional-op.stan
Original file line number Diff line number Diff line change
@@ -1,15 +1,44 @@
data {
int<lower=0> N;
int<lower=0,upper=1> y[N];
}
real x;
}
transformed data {
int td_i1 = 1 ? N : N;
int td_i2 = 1 ? N : y[1];
int td_i3 = 1 ? N : -3;
int td_i4 = 1 ? 1 : 4;
real td_r1 = 1 ? 1.0 : 2.0;
real td_r2 = 1 ? 1.0 : x;
real td_r3 = 1 ? x : 2.0;
real td_r4 = 0 ? x : x;
real td_r5 = 0 ? N : x;
}
parameters {
real<lower=0,upper=1> theta;
real z;
}
transformed parameters {
real y1 = 1 ? y[1] : z;
real tp_r1 = 1 ? 1.0 : 2.0;
real tp_r2 = 1 ? 1.0 : x;
real tp_r3 = 1 ? x : 2.0;
real tp_r4 = 0 ? x : x;
real tp_r5 = 0 ? N : x;

real tp_r6 = 1 ? x : tp_r1;
real tp_r7 = 1 ? tp_r1 : tp_r2;
real tp_r8 = 1 ? N : tp_r1;

{
real loc_y1 = 1 ? y[1] : z;
real local_r1 = 1 ? 1.0 : 2.0;
real local_r2 = 1 ? 1.0 : x;
real local_r3 = 1 ? x : 2.0;
real local_r4 = 0 ? x : x;
real local_r5 = 0 ? N : x;

real local_r6 = 1 ? x : tp_r1;
real local_r7 = 1 ? tp_r1 : tp_r2;
real local_r8 = 1 ? N : tp_r1;
}
}
model {
Expand All @@ -18,6 +47,32 @@ model {
y[n] ~ bernoulli(theta);
}
generated quantities {
real y2 = 1 ? y[2] : z;
int gq_i1 = 1 ? N : N;
int gq_i2 = 1 ? N : y[1];
int gq_i3 = 1 ? N : -3;
int gq_i4 = 1 ? 1 : 4;

real gq_r1 = 1 ? 1.0 : 2.0;
real gq_r2 = 1 ? 1.0 : x;
real gq_r3 = 1 ? x : 2.0;
real gq_r4 = 0 ? x : x;
real gq_r5 = 0 ? N : x;

real gq_r6 = 1 ? x : tp_r1;
real gq_r7 = 1 ? tp_r1 : tp_r2;
real gq_r8 = 1 ? N : tp_r1;

{
real local_r1 = 1 ? 1.0 : 2.0;
real local_r2 = 1 ? 1.0 : x;
real local_r3 = 1 ? x : 2.0;
real local_r4 = 0 ? x : x;
real local_r5 = 0 ? N : x;

real local_r6 = 1 ? x : tp_r1;
real local_r7 = 1 ? tp_r1 : tp_r2;
real local_r8 = 1 ? N : tp_r1;
}

}

152 changes: 120 additions & 32 deletions src/test/test-models/good/declare-define-var-double.stan
Original file line number Diff line number Diff line change
@@ -1,67 +1,155 @@
functions {
real foo() {
real lf1 = 1; // real_d <- int_d
real lf2 = 2.0; // real_d <- real_d
real lf3[5];
real lf4[5] = lf3;
real foo(real a1) {
int lf0 = 2;
real lf1 = a1;
real lf2 = lf1;
real lf3[lf0];
real lf4[lf0] = lf3;
print("foo, lf1: ", lf1);
print("foo, lf2: ", lf1);
print("foo, lf4: ", lf4);
lf1 = lf3[1] + lf4[1];
return lf1;
}
}
data {
int<lower=0> N;
int<lower=0,upper=1> y[N];
real d[3,3];
}
transformed data {
real td1 = 1; // real_d <- int_d
int td0 = 3;
real td1 = 123; // real_d <- int_d
real td2 = 2.0; // real_d <- real_d
real td3 = td0;
real td4 = td3;
real td_a1[3];
real td_a2[3] = td_a1; // real_arr_d <- real_arr_d
td1 = 2;

// local variables
real td5 = td_a2[1];
real td_a3[3, 3] = d;
real td_a4[3] = td_a3[2];
print("td1: ",td1);
print("td2: ",td2);
print("td3: ",td3);
print("td4: ",td4);
print("td5: ",td5);
print("td_a2: ",td_a2);
print("td_a4: ",td_a4);
{
real ltd1 = 1; // real_d <- int_d
real ltd2 = 2.0; // real_d <- real_d
real ltd3[4];
real ltd4[4] = ltd3;
ltd1 = 5;
real ltd3 = td1;
real ltd4[td0];
real ltd5[td0] = ltd4;
print("ltd1: ",ltd1);
print("ltd2: ",ltd2);
print("ltd3: ",ltd3);
print("ltd5: ",ltd5);
}
}
parameters {
real<lower=0,upper=1> theta;
}
transformed parameters {
// block variables
real d_tp1 = 1.0;
real d_tp2 = td1;
real d_tp3 = td0;
real d_tp4 = d[1,2];
real d_tp_a1[3] = td_a4;
real d_tp_a2[3] = td_a3[1];
real d_tp_a3[3, 3] = d;

real p_tp2 = d_tp1;
real p_tp4 = d_tp_a1[1];
real p_tp_a1[3] = d_tp_a1;
real p_tp_a2[3] = d_tp_a3[3];


real tp1 = 1; // real_p <- int_d
real tp2 = 2.0; // real_p <- real_d
real tp3 = tp2; // real_p <- real_p
real tp4[5];
real tp5[5] = tp4;
tp3 = tp1 + tp2;
real tp4[td0];
real tp5[td0] = tp4;

// local variables
print("d_tp1 = ", d_tp1);
print("d_tp2 = ", d_tp2, " should be td1 = ", td1, " which should be 123");
print("d_tp3 = ", d_tp3);
print("d_tp4 = ", d_tp4);
print("d_tp_a1 = ", d_tp_a1);
print("d_tp_a2 = ", d_tp_a2);
print("d_tp_a3 = ", d_tp_a3);

print("p_tp2 = ", p_tp2);
print("p_tp4 = ", p_tp4);
print("p_tp_a1 = ", p_tp_a1);
print("p_tp_a2 = ", p_tp_a2);

print("tp1: ",tp1);
print("tp2: ",tp2);
print("tp3: ",tp3);
print("tp5: ",tp5);
tp1 = foo(tp3);
print("tp1: ",tp1);
{
real lp1 = 1; // real_p <- int_d
real lp2 = 2.0; // real_p <- real_d
real lp1 = td0; // real_p <- int_d
real lp2 = 9.0; // real_p <- real_d
real lp3 = tp2; // real_p <- real_p
real lp4[6];
real lp5[6] = lp4;
lp3 = lp1 + lp2;
real lp4[td0];
real lp5[td0] = lp4;
print("lp1: ",lp1);
print("lp2: ",lp2);
print("lp3: ",lp3);
print("lp5: ",lp5);
}
}
model {
// local variables
real lm1 = 1; // real_p <- int_d
real lm2 = 2.0; // real_p <- real_d
real lm3 = tp2; // real_p <- real_p
lm3 = lm1 + lm2;
theta ~ beta(1,1);
for (n in 1:N)
y[n] ~ bernoulli(theta);
}
generated quantities {
real gq1 = 1; // real_d <- int_d
real gq2 = 2.0; // real_d <- real_d
real gq3[7];
real gq4[7] = gq3;
gq2 = 5.0;
real gq_d_tp1 = 1.0;
real gq_d_tp2 = td1;
real gq_d_tp3 = td0;
real gq_d_tp4 = d[1,2];
real gq_d_tp_a1[3] = td_a4;
real gq_d_tp_a2[3] = td_a3[1];
real gq_d_tp_a3[3, 3] = d;

real gq_p_tp2 = d_tp1;
real gq_p_tp4 = d_tp_a1[1];
real gq_p_tp_a1[3] = d_tp_a1;
real gq_p_tp_a2[3] = d_tp_a3[3];
print("gq_d_tp1 = ", gq_d_tp1);
print("gq_d_tp2 = ", gq_d_tp2, " should be td1 = ", td1, " which should be 123");
print("gq_d_tp3 = ", gq_d_tp3);
print("gq_d_tp4 = ", gq_d_tp4);
print("gq_d_tp_a1 = ", gq_d_tp_a1);
print("gq_d_tp_a2 = ", gq_d_tp_a2);
print("gq_d_tp_a3 = ", gq_d_tp_a3);

print("gq_p_tp2 = ", gq_p_tp2);
print("gq_p_tp4 = ", gq_p_tp4);
print("gq_p_tp_a1 = ", gq_p_tp_a1);
print("gq_p_tp_a2 = ", gq_p_tp_a2);
{
real lgq1 = 1; // real_d <- int_d
real lgq2 = 2.0; // real_d <- real_d
real lgq3;
real lgq4[8];
real lgq5[8] = lgq4;
lgq3 = lgq1 + lgq2;
real lqd2a = lgq2;
real lgq3[td0];
real lgq3a = lgq3[1];
real lgq4[td0] = lgq3;
real lgq5[3] = d[1];
print("lgq1: ",lgq1);
print("lgq2: ",lgq2);
print("lgq2a: ",lqd2a);
print("lgq3: ",lgq3);
print("lgq3a: ",lgq3a);
print("lgq4: ",lgq4);
print("lgq5: ",lgq5);
}
}
32 changes: 27 additions & 5 deletions src/test/test-models/good/declare-define-var-int.stan
Original file line number Diff line number Diff line change
@@ -1,29 +1,51 @@
functions {
int foo() {
int lf1 = 1;
int lf1 = 3;
print("foo ",lf1);
return lf1;
}
}
data {
int n;
int d[n,n];
}
transformed data {
int td1 = 1;
int td_a1[5];
int td_a2[5] = td_a1;
int td2 = td1;
int td_a1[n] = d[1];
int td_a2[n] = td_a1;
int td_a3[n,n] = d;
int td_a4[n] = td_a3[n];
int td3 = td_a3[2,2];
print("td1 = ", td1);
print("td2 = ", td2);
print("td3 = ", td3);
print("td_a3 = ", td_a3);
print("transformed data td2 ",td2);
print("transformed data td_a2 ",td_a2);
print("transformed data td_a4 ",td_a4);

}
transformed parameters {
real p1;
{
int lp1 = 1;
print("transformed param ",lp1);
}
}
model {
// local variables
int lm1 = 1;
int lm1 = 4;
print("local int ",lm1);
print(foo());
}
generated quantities {
int gq1 = 1;
print("gq1 ",gq1);
gq1 = 2;
{
int lgq1 = 1;
int lgq1 = 2;
print("gq2 ",lgq1);
lgq1 = 2;
}
}
Loading

0 comments on commit 0621f0c

Please sign in to comment.