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 x, int n)
Return the n array with every entry assigned to x.
(T x, int m, int n)
Return the m by n array with every entry assigned to x.
(T x, int k, int m, int n)
Return the k by m by n array with every entry assigned to x.
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; int x; 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.,
rep_array(v,27) is a size 27 array consisting of 27 copies of the vector
vector v; vector a; a = rep_array(v,3); // fill a with copies of v a[2,4] = 9.0; // v, 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