Passing integer array into Stan function

I have a function in Stan that takes as input an array of integers, Index. The function uses the indices to sum over a vector:

I.e.

sum(II[Index[i]:])

I input the Index array in the data block

int M3;
int Index[M3];

and input the array of integers into the function as

(...,int Index)

I am receiving the following error

Too many indexes, expression dimensions=0, indexes found=1

Is this because Stan only allows a single integer value to be passed to the function? I need the array to be integers because Index[i] must be an integer to perform the following.

sum(II[Index[i]:])

So I cannot define Index to be of type “vector”.

Does anyone know how I can pass an array of integers into a function? Or if the error means something else?

The variable named Index in your data block and the function parameter Index are independent of each other. As such, the function signature you wrote down specified the type as int for the function parameter. If you want this to be an int array, you should write (..., array[] int Index)

1 Like

I tried to input as

array[] int Index

and received an error

PARSER EXPECTED: <argument declaration or close paren ) to end argument declarations>

If you’re using an older version (such as rstan from CRAN) you may need to make it int[] index instead.

1 Like