and I want to check if the ith element is equal to 0
if(A[i] == 0.0) { do stuff }
This generates a compilation error
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Binary infix operator == with functional interpretation logical_eq requires arguments of primitive type (int or real), found left type=vector, right arg type=real.
No matches for:
Conditions in if-else statement must be primitive int or real; found type=ill-formed
error in ‘model11c114eb458a_45c192425899c18b7d0d3f183a06b147’ at line 90, column 9
Yes, it compiles. Here is a trimmed-down version of the code I am trying to compile. The goal is to subset the vector M as part of an effort to generate a prior predictive distribution. I am running rstan Version 2.21.2 on R V4.0.3, all on Linux Mint 20.
test <- "
functions {
int count_urn(int[] sub,vector[] dT,int index) {
int count;
count = 0;
for(i in 1:num_elements(sub)) {
if(sub[i] == index) {
if(dT[i] > 0.0) {
count = count + 1;
}
}
}
return(count);
}
int[] indu(int[] sub,vector[] dT,int index) {
int res[count_urn(sub,dT,index)];
int ci;
ci = 1;
for(i in 1:num_elements(sub)) {
if(sub[i]==index) {
if(dT[i] > 0.0) {
res[ci] = i;
ci = ci + 1;
}
}
}
return(res);
}
}
data {
int sub[100];
vector[100] dT;
}
generated quantities{
vector[100] M;
M[indu(sub,dT,11)];
}
"
stan_model(model_code=test,verbose=TRUE)
The error issued by the compiler is
TRANSLATING MODEL '93da0f9f7e830d4636774afc0967dc4e' FROM Stan CODE TO C++ CODE NOW.
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Binary infix operator >= with functional interpretation logical_gt requires arguments of primitive type (int or real), found left type=vector, right arg type=real.
No matches for:
logical_gt(vector, real)
Available argument signatures for logical_gt:
logical_gt(int, int)
logical_gt(int, real)
logical_gt(real, int)
logical_gt(real, real)
Conditions in if-else statement must be primitive int or real; found type=ill-formed
error in 'modeledf5fe5354e_93da0f9f7e830d4636774afc0967dc4e' at line 10, column 9
-------------------------------------------------
8: for(i in 1:num_elements(sub)) {
9: if(sub[i] == index) {
10: if(dT[i] > 0.0) {
^
11: count = count + 1;
-------------------------------------------------
PARSER EXPECTED: <expression>
Error in stanc(file = file, model_code = model_code, model_name = model_name, :
failed to parse Stan model '93da0f9f7e830d4636774afc0967dc4e' due to the above error.
The issue here is that you’ve declared the input dT as an array of vectors (vector[]). This means that dT[i] is selecting the i-th vector from the array, rather than the i-th element of a vector. If dT is supposed to be a single vector, then you just need to change the declaration to: int count_urn(int[] sub,vector dT,int index)