Dealing with lots of integer data. Want to do something simple like `x[start:end] = 1-score[start:end];`

But then stan tells me:

```
No matches for:
int - int[ ]
Available argument signatures for operator-:
vector - vector
row_vector - row_vector
matrix - matrix
vector - real
row_vector - real
matrix - real
real - vector
real - row_vector
real - matrix
```

What’s the most efficient way to handle this? I currently convert score to a vector, but this seems pretty silly…

If you are doing this inside of a UDF function or transformed parameters/model block, do not convert to a vector, because that will be terribly inefficient as it could create “internal parameters” for which Stan would compute gradients unnecessarily.

There is no real way of making this a lot more efficient. `int[]`

or `array[] int`

is handled as a `std::vector<int>`

in the generated C++ code, and there is no magic C++ code that would make subtracting or adding to a std::vector faster. A loop over the array in Stan will be a loop in C++, which is as good as it gets.

So the bottom line is: just write a loop over the integer array without convert this to reals, vectors or arrays of real at any point.

1 Like

Ok. If it was as simple as my example that would make sense I guess. There’s also a pile of other stuff going on in the same calculation, where testing has shown me that looping basically doubles the time taken. But good to know there’s no magic I’m missing. Breaking up the calculation into a few different intermediate objects is ugly but maybe best by the sound of things…

2 Likes