4.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.
T[] rep_array(T x, int n)
Return the n array with every entry assigned to x.
T[,] rep_array(T x, int m, int n)
Return the m by n array with every entry assigned to x.
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.
For example, rep_array(1.0,5) produces a real array (type real[])
of size 5 with all values set to 1.0. On the other hand,
rep_array(1,5) produces an integer array (type 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
real y[5];
int x[5];
x = rep_array(1,5); // ok
y = rep_array(1.0,5); // ok
x = rep_array(1.0,5); // illegal
y = rep_array(1,5); // illegal
x = y; // illegal
y = x; // illegal
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;
vector[5] a[3];
a = rep_array(v,3); // fill a with copies of v
a[2,4] = 9.0; // v[4], a[1,4], a[2,4] unchanged
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.
real a[5,6];
real b[3,4,5,6];
b = rep_array(a,3,4); // make (3 x 4) copies of a
b[1,1,1,1] = 27.9; // a[1,1] unchanged
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.