Quick syntax question:

If I have

```
vector[N] x = ...;
vector[N] y = ...;
```

can I index, e.g., the sum `x + y`

via `real a = (x+y)[2];`

? Or do I have to do

```
real a = x[2] + y[2];
```

or

```
vector[N] z = x + y;
real a = z[2];
```

?

I know it seems like a silly question, but I am trying to write something that generates Stan code and I realized I don’t know exactly which expressions can be indexed!

Thanks!

You can do `real a = (x+y)[2];`

but by doing that , you waste a lot of computation as the sum is wasted for N-1 values.

Yep! That makes sense. It’s not about which I would choose but about what the code generator might allow. Just trying to get those boundaries correct.

Thanks!

In Stan, expressions all work the same way. Any expression of type `vector`

can be indexed. Because `x`

and `y`

are vectors, so is `(x + y)`

. That means `(x + y)`

can be indexed just like any other vector.

You an always try it out if you have questions like this. Stan works the same way on every platform (unlike C++). There’s a chapter in the reference manual on multi-indexing that explains more.

Thanks! I’ve been reading that section with some…enthusiasm as I try to get my code generation to handle indexing correctly. I’ve got one more, though I take your point, I can just try.

Is “1:5” an actual “array[5] int”? I can see how it might be, and then you could even deprecate the “for” loop and just use “foreach”. But I can also see how it probably isn’t since “1:” only makes sense when used in an indexing context.

No, and it doesn’t even act like one in the sense that you can’t do an assignment like this

```
array[5] int ns = 1:5; // ILLEGAL
```

It’d make sense to make `1:5`

a legal rvalue (something that can show up on the right-hand side of assignments and in expressions), while keeping the efficient implementation for loops. It’d be natural to think of it as an array view. My understanding is that is what Python does. The obstacle to that is that we don’t have generic views implemented for integer arrays.