5.3 Array broadcasting
The following operations create arrays by repeating elements to fill an array of a specified size. These operations work for all input types T, including reals, integers, vectors, row vectors, matrices, or arrays.
array[] T
rep_array
(T x, int n)
Return the n array with every entry assigned to x.
Available since 2.0
array [,] T
rep_array
(T x, int m, int n)
Return the m by n array with every entry assigned to x.
Available since 2.0
array[,,] T
rep_array
(T x, int k, int m, int n)
Return the k by m by n array with every entry assigned to x.
Available since 2.0
For example, rep_array(1.0,5)
produces a real array (type array[] real
)
of size 5 with all values set to 1.0. On the other hand,
rep_array(1,5)
produces an integer array (type array[] int
) of size 5
with all values set to 1. This distinction is important because it is
not possible to assign an integer array to a real array. For example,
the following example contrasts legal with illegal array creation and
assignment
array[5] real y;
array[5] int x;
1, 5); // ok
x = rep_array(1.0, 5); // ok
y = rep_array(
1.0, 5); // illegal
x = rep_array(1, 5); // illegal
y = rep_array(
// illegal
x = y; // illegal y = x;
If the value being repeated v
is a vector (i.e., T
is vector
),
then rep_array(v, 27)
is a size 27 array consisting of 27 copies of
the vector v
.
vector[5] v;
array[3] vector[5] a;
3); // fill a with copies of v
a = rep_array(v, 2, 4] = 9.0; // v[4], a[1, 4], a[3, 4] unchanged a[
If the type T of x is itself an array type, then the result will be an
array with one, two, or three added dimensions, depending on which of
the rep_array
functions is called. For instance, consider the
following legal code snippet.
array[5, 6] real a;
array[3, 4, 5, 6] real b;
3, 4); // make (3 x 4) copies of a
b = rep_array(a, 1, 1, 1, 1] = 27.9; // a[1, 1] unchanged b[
After the assignment to b
, the value for b[j, k, m, n]
is equal to
a[m, n]
where it is defined, for j
in 1:3
, k
in 1:4
, m
in
1:5
, and n
in 1:6
.