Skip to content

Commit e1535f4

Browse files
Merge pull request #796 from stan-dev/clarify-range-indexing
clarify range indexing, fixes #638
2 parents c9fe5d7 + 1de2a6b commit e1535f4

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/reference-manual/expressions.qmd

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -782,8 +782,10 @@ In addition to single integer indexes, as described in
782782
[the language indexing section](#language-indexing.section), Stan supports multiple indexing.
783783
Multiple indexes can be integer arrays of indexes, lower
784784
bounds, upper bounds, lower and upper bounds, or simply shorthand for
785-
all of the indexes. A complete table of index types is given in the
786-
following table.
785+
all of the indexes. If the upper bound is smaller than the lower bound,
786+
the range is empty (unlike, e.g., in R). The upper bound and lower bound can be
787+
expressions that evaluate to integer. A complete list of index types is
788+
given in the following table.
787789

788790
##### Indexing Options Table {- #index-types-table}
789791

@@ -797,9 +799,18 @@ following table.
797799
| lower bound | `a[3:]` | `a[3]`, ..., `a[N]` |
798800
| upper bound | `a[:5]` | `a[1]`, ..., `a[5]` |
799801
| range | `a[2:7]` | `a[2]`, ..., `a[7]` |
802+
| range | `a[7:2]` | [] |
803+
| range | `a[5-3:5+2]` | `a[2]`, ..., `a[7]` |
800804
| all | `a[:]` | `a[1]`, ..., `a[N]` |
801805
| all | `a[]` | `a[1]`, ..., `a[N]` |
802806

807+
The range indexing with `:` allows only increasing sequences. Indexing
808+
with a decereasing sequence can be made by creating an integer array in
809+
the following way:
810+
```stan
811+
array[6] int ii = reverse(linspaced_int_array(6, 2, 7));
812+
```
813+
Then `a[ii]` evaluates to `a[7]`, ..., `a[2]`.
803814

804815
### Multiple index semantics {-}
805816

0 commit comments

Comments
 (0)