docs icon indicating copy to clipboard operation
docs copied to clipboard

Clarify index range construction and its difference from R behaviour

Open andrjohns opened this issue 1 year ago • 2 comments

Summary:

A colleague has mentioned that it would be helpful for the manual to be more explicit about Stan's behaviour with range indexing that involves operations.

For example:

transformed data {
  array[5] int Y = {1, 2, 3, 4, 5};
  int i = 3;
  int p = 5;

  array[3] int Y_subset = Y[p-i:p-1];  // Y[2:4]
}

It would be good to be explicit that the Stan will parse the indexing in Y[p-i:p-1] as:

(p-i):(p-1)

As opposed to:

p - (i:p) - 1

Noting that the latter behaviour is how the indexing would be parsed in R:

> i <- 3
> p <- 5
> p-i:p-1
[1]  1  0 -1

Current Version:

v2.18.0

andrjohns avatar May 17 '23 07:05 andrjohns

Adding that clarification could be made at least in the two following places

  • colon : as in indexing could be mentioned in the Operator Precedence Table https://mc-stan.org/docs/reference-manual/arithmetic-expressions.html
  • index range construction could be clarified in https://mc-stan.org/docs/reference-manual/language-multi-indexing.html

avehtari avatar May 17 '23 07:05 avehtari

The colon in indexing isn't really an operator in Stan, since we don't allow 1:2 as an expression, only as part of for loops multi-indexing. We could put it in the operator table, but I worry that would cause even more potential confusion

WardBrian avatar May 17 '23 13:05 WardBrian