The current syntax for declaring an array of matrices is not ideal.
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.
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?
matrix[I,J][N] mat;
matrix[N,I,J] mat;
matrix[N;I,J] mat;
matrix[N|I,J] mat;
matrix[N][I,J] mat;
0 voters
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.
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
// ???
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} } };