5.7 Tuple data type
Stan supports tuples of arbitrary size. The values in a tuple can be of arbitrary type, but the component types must be declared along with the declaration of the tuple. Tuples can be manipulated as a whole, or their elements may be accessed and set individually.
5.7.1 Declaring tuple variables
Tuples are declared with the keyword tuple
followed by a
parenthesized sequence of types, which determine the types of the
respective tuple entries. For example, a tuple with three elements
may be declared as
tuple(int, vector[3], complex) abc;
Tuples must have at least two entries, so the following declarations are illegal.
tuple() nil; // ILLEGAL
tuple(int) n; // ILLEGAL
Tuples can be assigned as a whole if their elements can be assigned
individually. For example, a
can be assigned to b
in the
following example because int
can be promoted to complex
.
tuple(int, real) a;
...tuple(complex, real) b = a;
Tuple types may have elements which are declared as tuples, such as the following example.
tuple(int, tuple(real, complex)) x;
In this case, it would probably be simpler to use a 3-tuple type,
tuple(int, real, complex)
.
Tuples can be declared with constraints anywhere that ordinary variables can (i.e., as top-level block variables). That means any context in which it is legal to have a declaration
real<lower=0> sigma;
real<lower=0, upper=1> theta;
it is legal to have a tuple with constraints such as
tuple(real<lower=0>, real<lower=0, upper=1>) sigma_theta;
5.7.2 Accessing tuple elements
Tuple elements may be accessed directly. For example, with our
declaration of abc
from the last section, Stan uses abc.1
for the
first element, abc.2
for the second, and abc.3
for the third.
These numbers must be integer literals (i.e., they cannot be
variables), and must be within the size of the number of elements of
tuples. The types of elements are as declared, so that abc.1
is of
type int
, abc.2
of type vector[3]
and abc.3
of type complex
.