From 5237114cb4e917d73cf2910a41a93f3836d0e210 Mon Sep 17 00:00:00 2001 From: Aki Vehtari Date: Sun, 4 Aug 2024 18:54:36 +0300 Subject: [PATCH 1/3] clarify range indexing, fixes #638 --- src/reference-manual/expressions.qmd | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/reference-manual/expressions.qmd b/src/reference-manual/expressions.qmd index 186c3853f..88a31bc4d 100644 --- a/src/reference-manual/expressions.qmd +++ b/src/reference-manual/expressions.qmd @@ -782,8 +782,10 @@ In addition to single integer indexes, as described in [the language indexing section](#language-indexing.section), Stan supports multiple indexing. Multiple indexes can be integer arrays of indexes, lower bounds, upper bounds, lower and upper bounds, or simply shorthand for -all of the indexes. A complete table of index types is given in the -following table. +all of the indexes. If the upper bound is smaller than the lower bound, +the range is empty (unlike, e.g. in R). Upper bound and lower bound can be +expressions that evaluate to integer. A complete table of index types is +given in the following table. ##### Indexing Options Table {- #index-types-table} @@ -797,6 +799,8 @@ following table. | lower bound | `a[3:]` | `a[3]`, ..., `a[N]` | | upper bound | `a[:5]` | `a[1]`, ..., `a[5]` | | range | `a[2:7]` | `a[2]`, ..., `a[7]` | + | range | `a[7:2]` | [] | + | range | `a[5-3:5+2]` | `a[2]`, ..., `a[7]` | | all | `a[:]` | `a[1]`, ..., `a[N]` | | all | `a[]` | `a[1]`, ..., `a[N]` | From c88aaaf994177e9d0052ae81e22a881916bb25da Mon Sep 17 00:00:00 2001 From: Aki Vehtari Date: Mon, 5 Aug 2024 09:45:14 +0300 Subject: [PATCH 2/3] grammar fixes by Bob --- src/reference-manual/expressions.qmd | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/reference-manual/expressions.qmd b/src/reference-manual/expressions.qmd index 88a31bc4d..2de40d217 100644 --- a/src/reference-manual/expressions.qmd +++ b/src/reference-manual/expressions.qmd @@ -783,8 +783,8 @@ In addition to single integer indexes, as described in Multiple indexes can be integer arrays of indexes, lower bounds, upper bounds, lower and upper bounds, or simply shorthand for all of the indexes. If the upper bound is smaller than the lower bound, -the range is empty (unlike, e.g. in R). Upper bound and lower bound can be -expressions that evaluate to integer. A complete table of index types is +the range is empty (unlike, e.g., in R). The upper bound and lower bound can be +expressions that evaluate to integer. A complete list of index types is given in the following table. ##### Indexing Options Table {- #index-types-table} From 1de2a6bc5aad691b33143920b5f6c865484535ef Mon Sep 17 00:00:00 2001 From: Aki Vehtari Date: Mon, 5 Aug 2024 15:24:31 +0300 Subject: [PATCH 3/3] mention how to create a decreasing sequence for indexing --- src/reference-manual/expressions.qmd | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/reference-manual/expressions.qmd b/src/reference-manual/expressions.qmd index 2de40d217..f67b3740d 100644 --- a/src/reference-manual/expressions.qmd +++ b/src/reference-manual/expressions.qmd @@ -804,6 +804,13 @@ given in the following table. | all | `a[:]` | `a[1]`, ..., `a[N]` | | all | `a[]` | `a[1]`, ..., `a[N]` | +The range indexing with `:` allows only increasing sequences. Indexing +with a decereasing sequence can be made by creating an integer array in +the following way: +```stan + array[6] int ii = reverse(linspaced_int_array(6, 2, 7)); +``` +Then `a[ii]` evaluates to `a[7]`, ..., `a[2]`. ### Multiple index semantics {-}