# New array declaration syntax

**URL:** <https://discourse.mc-stan.org/t/new-array-declaration-syntax/16011>\
**Category:** Developers\
**Created:** [June 17, 2020, 6:44pm UTC](https://discourse.mc-stan.org/t/new-array-declaration-syntax/16011 "2020-06-17T18:44:41Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![nhuurre](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@nhuurre](https://discourse.mc-stan.org/u/nhuurre)\
**Post date:** [June 17, 2020, 6:44pm UTC](https://discourse.mc-stan.org/t/new-array-declaration-syntax/16011/1 "2020-06-17T18:44:41Z")

</div>

The current syntax for declaring an array of matrices is not ideal.

```stan
matrix[I,J] mat[N];

```

This declares that `mat` is a array with N elements; each element being an I\times J-matrix.

The problem is that the type is split into `matrix[I,J]` and `[N]`. A pull request on stanc3 repo proposes to allow a contiguous type and deprecate the current array-size-after-identifier syntax.

> <https://github.com/stan-dev/stanc3/pull/560>

  
The proposed syntax for the type is simply `matrix[I,J][N]`. This is logical (because `T[N]` is a size `N` array of elements of type `T`) but potentially confusing (because the dimensions are in different order when indexing elements as `mat[n,i,j]`). I didn’t find any discussion of this choice so I’m opening a poll here.
# How to declare an array of I\times J matrices?

_Poll ([view on site](https://discourse.mc-stan.org/t/new-array-declaration-syntax/16011/1))_

The second option is there just to catch people who aren’t paying attention. `cholesky_factor_cov[2,2,2]` would be ambiguous because currently `cholesky_factor_cov` can have either one or two dimensions (one dimension declaration denotes a square matrix, like it does for `cholesky_factor_corr`, `cov_matrix` and `corr_matrix`)

One thing that may be worth considering is compatibility with the [proposed ragged array syntax](https://github.com/stan-dev/design-docs/blob/ragged/designs/0003-ragged-containers.md).

```stan
row_vector[{3, 2}] w = { [a, b, c], [d, f] };

```

It’s not clear to me if the plan is to allow composing ragged and rectangular dimensions

```stan
// ???
row_vector[2][{3, 2}] u = { { [a, b], [c, d], [d, f] }, { [g, h], [i, j] } };
// ?!?!?!
real[2, {3, 2}] v = { { {a, b, c}, {d, f} }, { {g, h, i}, {j, k} } };

```

---

<div class="post-metadata">

**Author:** ![anon75146577](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon75146577](https://discourse.mc-stan.org/u/anon75146577)\
**Post date:** [June 17, 2020, 6:54pm UTC](https://discourse.mc-stan.org/t/new-array-declaration-syntax/16011/2 "2020-06-17T18:54:13Z")

</div>

Will there be a corresponding change from

```
int x[N];

```

to

```
int[N] x

```

Also I think it should be `matrix[N][I,J] mat_array` because you’d reference it `mat_array[n][i,j]` (I think? This might not be true. Regardless, my vote is make the order respect how you would refer to an element.)

---

<div class="post-metadata">

**Author:** ![nhuurre](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@nhuurre](https://discourse.mc-stan.org/u/nhuurre)\
**Post date:** [June 17, 2020, 6:57pm UTC](https://discourse.mc-stan.org/t/new-array-declaration-syntax/16011/3 "2020-06-17T18:57:44Z")

</div>

> [@anon75146577](#):
>
> Will there be a corresponding change from

Yes.

> [@anon75146577](#):
>
> Also I think it should be `matrix[N][I,J] mat_array`

I was going to add that option but figured no one would want it or at least people who want `matrix[I,J][N]` would object vehemently.

> [@anon75146577](#):
>
> you’d reference it `mat_array[n][i,j]`

Both `mat_array[n][i,j]` and `mat_array[n,i,j]` are valid and refer to the same element.

---

<div class="post-metadata">

**Author:** ![anon75146577](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon75146577](https://discourse.mc-stan.org/u/anon75146577)\
**Post date:** [June 17, 2020, 7:01pm UTC](https://discourse.mc-stan.org/t/new-array-declaration-syntax/16011/4 "2020-06-17T19:01:40Z")

</div>

> [@nhuurre](#):
>
> I was going to add that option but figured no one would want it or at least people who want `matrix[I,J][N]` would object vehemently.

I think it would be confusing to need to put the array index in different places when declaring and when accessing. If I could recant my vote for `matrix[I, J][N]` I would.

---

<div class="post-metadata">

**Author:** ![rok\_cesnovar](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@rok\_cesnovar](https://discourse.mc-stan.org/u/rok_cesnovar)\
**Post date:** [June 17, 2020, 7:04pm UTC](https://discourse.mc-stan.org/t/new-array-declaration-syntax/16011/5 "2020-06-17T19:04:51Z")

</div>

> [@anon75146577](#):
>
> Also I think it should be `matrix[N][I,J] mat_array`

I agree. I would found that the most logical.

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [June 17, 2020, 7:07pm UTC](https://discourse.mc-stan.org/t/new-array-declaration-syntax/16011/6 "2020-06-17T19:07:14Z")

</div>

> [@nhuurre](#):
>
> The current syntax for declaring an array of matrices is not ideal.

Thanks for working on this!

> [@nhuurre](#):
>
> I was going to add that option but figured no one would want it or at least people who want `matrix[I,J][N]` would object vehemently.

I think it’s worth adding this option to the poll if it’s possible to edit polls (or start a new poll). I think I’d lean towards voting for it for the reason given by @anon75146577.

---

<div class="post-metadata">

**Author:** ![anon75146577](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon75146577](https://discourse.mc-stan.org/u/anon75146577)\
**Post date:** [June 17, 2020, 7:11pm UTC](https://discourse.mc-stan.org/t/new-array-declaration-syntax/16011/7 "2020-06-17T19:11:01Z")

</div>

Just so other people don’t need to read the git discussion, here is @Bob_Carpenter reasoning out `matrix[i,j][n]`. [https://github.com/stan-dev/stanc3/pull/560#issuecomment-645433810](https://github.com/stan-dev/stanc3/pull/560#issuecomment-645433810)

And yeah. This is complicated. A really bad suggestion would be to make it something explicit like `array<matrix[I,J], N>` and `arrary<int, N>` instead. (Maybe with square brackets)

---

<div class="post-metadata">

**Author:** ![nhuurre](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@nhuurre](https://discourse.mc-stan.org/u/nhuurre)\
**Post date:** [June 17, 2020, 7:11pm UTC](https://discourse.mc-stan.org/t/new-array-declaration-syntax/16011/8 "2020-06-17T19:11:41Z")

</div>

> [@jonah](#):
>
> Thanks for working on this!

Thanks goes to @rybern it’s his pull request.

> [@jonah](#):
>
> if it’s possible to edit polls (or start a new poll).

Discourse said I couldn’t edit it after five minutes but deleting it and making a new one in it’s place worked.

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [June 17, 2020, 7:13pm UTC](https://discourse.mc-stan.org/t/new-array-declaration-syntax/16011/9 "2020-06-17T19:13:26Z")

</div>

> [@anon75146577](#):
>
> Just so other people don’t need to read the git discussion, here is @Bob_Carpenter reasoning out `matrix[i,j][n]` . [https://github.com/stan-dev/stanc3/pull/560#issuecomment-645433810](https://github.com/stan-dev/stanc3/pull/560#issuecomment-645433810)

As usual Bob makes good points. Now I’m torn.

---

<div class="post-metadata">

**Author:** ![anon75146577](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon75146577](https://discourse.mc-stan.org/u/anon75146577)\
**Post date:** [June 17, 2020, 7:16pm UTC](https://discourse.mc-stan.org/t/new-array-declaration-syntax/16011/10 "2020-06-17T19:16:39Z")

</div>

> [@jonah](#):
>
> As usual Bob makes good points. Now I’m torn.

It is frustrating when people are always right :p I think I like the current version more than any of the proposals OR the more verbose `array<type, dimensions>` syntax (even though that’s a bit of a BIG change, it is generalizable to arbitrarily complex types)

---

<div class="post-metadata">

**Author:** ![anon75146577](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon75146577](https://discourse.mc-stan.org/u/anon75146577)\
**Post date:** [June 17, 2020, 7:17pm UTC](https://discourse.mc-stan.org/t/new-array-declaration-syntax/16011/11 "2020-06-17T19:17:52Z")

</div>

Actually. Maybe a different question: What is the proposed `tuple` syntax and can we harmonize this with that?

---

<div class="post-metadata">

**Author:** ![nhuurre](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@nhuurre](https://discourse.mc-stan.org/u/nhuurre)\
**Post date:** [June 17, 2020, 7:21pm UTC](https://discourse.mc-stan.org/t/new-array-declaration-syntax/16011/12 "2020-06-17T19:21:21Z")

</div>

Tuple syntax is just

```stan
(vector[3], vector[3]) vec_t; // vec_t.1 and vec_t.2 are vectors

```

Tuples require contiguous types but there’s no overlap otherwise.

> <https://github.com/stan-dev/design-docs/blob/a215ea3e78a87e7582e2b77736028b599bdbda2f/designs/tuples_structs.md>

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [June 17, 2020, 7:53pm UTC](https://discourse.mc-stan.org/t/new-array-declaration-syntax/16011/13 "2020-06-17T19:53:50Z")

</div>

so glad to see tuples coming along!

agree with Bob on this one.

---

<div class="post-metadata">

**Author:** ![nhuurre](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@nhuurre](https://discourse.mc-stan.org/u/nhuurre)\
**Post date:** [June 17, 2020, 8:22pm UTC](https://discourse.mc-stan.org/t/new-array-declaration-syntax/16011/14 "2020-06-17T20:22:54Z")

</div>

I see people find Bob’s arguments persuasive. I agree – to an extent. In particular, it is the reason I consider `matrix[N][I,J]` unworkable. However, if I thought it was obviously the right solution I wouldn’t have started this thread.

Keep in mind that the pull request does not really implement the general rule that `T[N]` is always an array of `T` for any `T`. Types like `real[2][3]` and `vector[2][3][4]` are not allowed; they must be substituted by the synonyms `real[3,2]` and `vector[2][4,3]`.  
What I would like to have is to add `vector[4,3|2]` as a synonym for `vector[2][4,3]` and disallow the latter, just like the current PR disallows `vector[2][3][4]`.  
In my opinion making `vector[N|K]` shorthand for `(vector[K])[N]` is no worse than making `real[N,M]` shorthand for `(real[M])[N]`.

---

<div class="post-metadata">

**Author:** ![anon75146577](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon75146577](https://discourse.mc-stan.org/u/anon75146577)\
**Post date:** [June 17, 2020, 8:28pm UTC](https://discourse.mc-stan.org/t/new-array-declaration-syntax/16011/15 "2020-06-17T20:28:32Z")

</div>

> [@nhuurre](#):
>
> However, if I thought it was obviously the right solution I wouldn’t have started this thread.

I think it is he right solution within the category of solutions being considered, but worse than the current practice. And that the swapping of the indices will lead to no end of woe.

---

<div class="post-metadata">

**Author:** ![rok\_cesnovar](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@rok\_cesnovar](https://discourse.mc-stan.org/u/rok_cesnovar)\
**Post date:** [June 18, 2020, 9:52am UTC](https://discourse.mc-stan.org/t/new-array-declaration-syntax/16011/16 "2020-06-18T09:52:02Z")

</div>

I might have missed this info, but is the implementation of tuples waiting on this, as in it cant be implemented with what we currently have? I might have misunderstood.

---

<div class="post-metadata">

**Author:** ![Charles\_Driver](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/charles_driver/32/6524_2.png) [@Charles\_Driver](https://discourse.mc-stan.org/u/Charles_Driver)\
**Post date:** [June 18, 2020, 11:25am UTC](https://discourse.mc-stan.org/t/new-array-declaration-syntax/16011/17 "2020-06-18T11:25:32Z")

</div>

Seems likely to get messy to me, what are the arguments against explicitly calling for an array, such as  
`array[3] matrix[2,2] myFancyArray`  
? Is it just the extra typing? This would retain correct ordering and minimise confusion, I think… also function more similar to lists in R.

---

<div class="post-metadata">

**Author:** ![spinkney](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@spinkney](https://discourse.mc-stan.org/u/spinkney)\
**Post date:** [June 18, 2020, 12:13pm UTC](https://discourse.mc-stan.org/t/new-array-declaration-syntax/16011/18 "2020-06-18T12:13:38Z")

</div>

I initially thought `matrix[N][I,J] mat;` too but switched to `matrix[I,J][N] mat;` after seeing Bob show the multidimensional array case. I think the single dimension array looks cleaner with the first but once the dimensions of the array \> 1 then I prefer Bob’s option and generality wins.

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [June 18, 2020, 3:10pm UTC](https://discourse.mc-stan.org/t/new-array-declaration-syntax/16011/19 "2020-06-18T15:10:24Z")

</div>

> [@rok\_cesnovar](#):
>
> , but is the implementation of tuples waiting on this, as in it cant be implemented with what we currently have? I might have misunderstood.

yes, entirely correct - see comment above: [New array declaration syntax - #12 by nhuurre](https://discourse.mc-stan.org/t/new-array-declaration-syntax/16011/12)

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [June 18, 2020, 4:29pm UTC](https://discourse.mc-stan.org/t/new-array-declaration-syntax/16011/20 "2020-06-18T16:29:08Z")

</div>

> [@Charles\_Driver](#):
>
> Seems likely to get messy to me, what are the arguments against explicitly calling for an array, such as  
> `array[3] matrix[2,2] myFancyArray`

I like this a lot. The (minimal) extra typing seems like a small price to pay for the clarity (the types are clear and so is the order of indexing):

```no-highlight
array[K1] vector[N] x1; // x1[k1] is an N-vector 
array[K1,K2] vector[N] x2; // x2[k1,k2] is an N-vector
array[K1,K2,K3] matrix[I,J] x3; // x3[k1,k2,k3] is an IxJ matrix

```

@nhuurre @Bob_Carpenter @rybern (tagging people who were reviewing the PR) are there good reasons to avoid this?

[Next page](https://discourse.mc-stan.org/t/new-array-declaration-syntax/16011.md?page=2)
