One of the most common questions we see on the <a href="https://groups.google.com/forum/#!forum/arrayfire-users">ArrayFire Google Group</a> pertains to methods to manipulate the dimensions of ArrayFire array objects. Thus we will continue the "<a href="http://sandbox.arrayfire.com/tag/learning-arrayfire-from-scratch/">Learning ArrayFire from scratch</a>" blog series by providing a detailed discussion of the numerous functions that permit you to change the dimentionality, flatten, flip, join, shift, transpose, and tile arrays.

<!--more-->

ArrayFire provides several different methods for manipulating arrays and matrices. The functionality includes:
<ul>
 	<li><a href="#moddims">moddims()</a> - change the dimensions of an array without changing the data</li>
 	<li><a href="#array">array()</a> - create a (shallow) copy of an array with different dimensions.</li>
 	<li><a href="#flat">flat()</a> - flatten an array to one dimension</li>
 	<li><a href="#flip">flip()</a> - flip an array along a dimension</li>
 	<li><a href="#join">join()</a> - join up to 4 arrays</li>
 	<li><a href="#reorder">reorder()</a> - changes the dimension order within the array</li>
 	<li><a href="#shift">shift()</a> - shifts data along a dimension</li>
 	<li><a href="#tile">tile()</a> - repeats an array along a dimension</li>
 	<li><a href="#transpose">transpose()</a> - performs a matrix transpose</li>
 	<li><a href="#transpose">T()</a> - transpose a matrix or vector (shorthand notation)</li>
 	<li><a href="#transpose">H()</a> - Hermitian Transpose (conjugate-transpose) a matrix</li>
</ul>
Below we provide several examples of these functions and their use. However, if you come to this page far in the future, we suggest you <a href="http://arrayfire.org/docs/matrixmanipulation.htm">visit the documentation on this topic</a> to see the latest information on these topics.
<a name="flat"></a>
<h4>flat()</h4>
The flat() function flattens an array to one dimension:
<pre class="lang:default decode:true ">a [3 3 1 1]
1.0000 4.0000 7.0000
2.0000 5.0000 8.0000
3.0000 6.0000 9.0000

flat(a) [9 1 1 1]
1.0000
2.0000
3.0000
4.0000
5.0000
6.0000
7.0000
8.0000
9.0000</pre>
The flat function can be called from C and C++ as follows:
<pre class="lang:default decode:true">af_err af_flat(af_array* out, const af_array in) 
– C interface for flat() function

array af::flat(const array&amp; in)
– C++ interface for flat() function</pre>
<a name="flip"></a>
<h4>flip()</h4>
The flip() function flips the contents of an array along a chosen dimension. In the example below, we show the 5x2 array flipped along the zeroth (i.e. within a column) and first (e.g. across rows) axes:
<pre class="lang:default decode:true ">a [5 2 1 1]
1.0000 6.0000
2.0000 7.0000
3.0000 8.0000
4.0000 9.0000
5.0000 10.0000

flip(a, 0) [5 2 1 1]
5.0000 10.0000
4.0000 9.0000
3.0000 8.0000
2.0000 7.0000
1.0000 6.0000

flip(a, 1) [5 2 1 1]
6.0000 1.0000
7.0000 2.0000
8.0000 3.0000
9.0000 4.0000
10.0000 5.0000</pre>
The flip function can be called from C and C++ as follows:
<pre class="lang:default decode:true">af_err af_flip(af_array *out, const af_array in, const unsigned dim) 
– C interface for flip()

array af::flip(const array &amp;in, const unsigned dim) 
– C++ interface for flip()</pre>
<a name="join"></a>
<h4>join()</h4>
The join() function joins arrays along a specific dimension. The C++ interface can join up to four arrays whereas the C interface supports up to 10 arrays. Here is an example of how to use join an array to itself:
<pre class="lang:default decode:true ">a [5 1 1 1]
1.0000
2.0000
3.0000
4.0000
5.0000

join(0, a, a) [10 1 1 1]
1.0000
2.0000
3.0000
4.0000
5.0000
1.0000
2.0000
3.0000
4.0000
5.0000

join(1, a, a) [5 2 1 1]
1.0000 1.0000
2.0000 2.0000
3.0000 3.0000
4.0000 4.0000
5.0000 5.0000</pre>
The join function has several candidate functions in C:
<pre class="lang:default decode:true">af_err af_join(af_array *out, const int dim, const af_array first, const af_array second) 
– C interface function to join 2 arrays along a dimension

af_err af_join_many(af_array *out, const int dim, const unsigned n_arrays, const af_array *inputs) 
– C interface function to join up to 10 arrays along a dimension</pre>
and in C++:
<pre class="lang:default decode:true">array af::join(const int dim, const array &amp;first, const array &amp;second) 
– Joins 2 arrays along a dimension

array af::join(const int dim, const array &amp;first, const array &amp;second, const array &amp;third) 
– Joins 3 arrays along a dimension.

array af::join(const int dim, const array &amp;first, const array &amp;second, const array &amp;third, const array &amp;fourth) 
– Joins 4 arrays along a dimension</pre>
<a name="join"></a>

<a name="moddims"></a>
<h4>moddims()</h4>
The moddims() function changes the dimensions of an array without changing its data or order. Note that this function modifies only the metadata associated with the array. It does not modify the content of the array. Here is an example of moddims() converting an 8x1 array into a 2x4 and then back to a 8x1:
<pre class="lang:default decode:true">a [8 1 1 1]
1.0000
2.0000
1.0000
2.0000
1.0000
2.0000
1.0000
2.0000

af::dim4 new_dims(2, 4);
moddims(a, new_dims) [2 4 1 1]
1.0000 1.0000 1.0000 1.0000
2.0000 2.0000 2.0000 2.0000

moddims(a, a.elements(), 1, 1, 1) [8 1 1 1]
1.0000
2.0000
1.0000
2.0000
1.0000
2.0000
1.0000
2.0000</pre>
The moddims function has a single form in the C API:
<pre class="lang:default decode:true">af_err af_moddims(af_array *out, const af_array in, const unsigned ndims, const dim_t *const dims) 
– C interface to mod dimensions of an array</pre>
And several overloaded candidates in the C++ API:
<pre class="lang:default decode:true">array af::moddims(const array &amp;in, const unsigned ndims, const dim_t *const dims) 
– mods number of dimensions to match ndims as specidied in the array dims

array af::moddims(const array &amp;in, const dim4 &amp;dims) 
– mods dimensions as specified by dims

array af::moddims(const array &amp;in, const dim_t d0, const dim_t d1=1, const dim_t d2=1, const dim_t d3=1) 
– mods dimensions of an array</pre>
<a name="moddims"></a>

<a name="reorder"></a>
<h4>reorder()</h4>
The reorder() function modifies the order of data within an array by exchanging data according to the change in dimensionality. The linear ordering of data within the array is preserved.
<pre class="lang:default decode:true ">a [2 2 3 1]
1.0000 3.0000
2.0000 4.0000
1.0000 3.0000
2.0000 4.0000
1.0000 3.0000
2.0000 4.0000

reorder(a, 1, 0, 2) [2 2 3 1] //equivalent to a transpose
1.0000 2.0000
3.0000 4.0000
1.0000 2.0000
3.0000 4.0000
1.0000 2.0000
3.0000 4.0000

reorder(a, 2, 0, 1) [3 2 2 1]
1.0000 2.0000
1.0000 2.0000
1.0000 2.0000
3.0000 4.0000
3.0000 4.0000
3.0000 4.0000</pre>
The reorder function has several candidates functions in the C/C++ APIs:
<pre class="lang:default decode:true">af_err af_reorder(af_array *out, const af_array in, const unsigned x, const unsigned y, const unsigned z, const unsigned w) 
– C interface for reordering function

array af::reorder(const array &amp;in, const unsigned x, const unsigned y=1, const unsigned z=2, const unsigned w=3) 
– Reorders dimensions of an array</pre>
<a name="reorder"></a>

<a name="shift"></a>
<h4>shift()</h4>
The shift() function shifts data in a circular buffer fashion along a chosen dimension. Consider the following example:
<pre class="lang:default decode:true ">a [3 5 1 1]
0.0000 0.0000 0.0000 0.0000 0.0000
3.0000 4.0000 5.0000 1.0000 2.0000
3.0000 4.0000 5.0000 1.0000 2.0000

shift(a, 0, 2 ) [3 5 1 1]
0.0000 0.0000 0.0000 0.0000 0.0000
1.0000 2.0000 3.0000 4.0000 5.0000
1.0000 2.0000 3.0000 4.0000 5.0000

shift(a, -1, 2 ) [3 5 1 1]
1.0000 2.0000 3.0000 4.0000 5.0000
1.0000 2.0000 3.0000 4.0000 5.0000
0.0000 0.0000 0.0000 0.0000 0.0000</pre>
The shift function can be called from C and C++ as follows:
<pre class="lang:default decode:true">af_err af_shift(af_array *out, const af_array in, const int x, const int y, const int z, const int w) 
– C interface for shifting an array

array af::shift(const array &amp;in, const int x, const int y=0, const int z=0, const int w=0) 
– Shifts array along specified dimensions</pre>
<a name="shift"></a>

<a name="tile"></a>
<h4>tile()</h4>
The tile() function repeats an array along the specified dimension. For example below we show how to tile an array along the zeroth and first dimensions of an array:
<pre class="lang:default decode:true">a [3 1 1 1]
1.0000
2.0000
3.0000

// Repeat array a twice in the zeroth dimension
tile(a, 2) [6 1 1 1]
1.0000
2.0000
3.0000
1.0000
2.0000
3.0000

// Repeat array a twice along both the zeroth and first dimensions
tile(a, 2, 2) [6 2 1 1]
1.0000 1.0000
2.0000 2.0000
3.0000 3.0000
1.0000 1.0000
2.0000 2.0000
3.0000 3.0000

// Repeat array a twice along the first and three times along 
// the second dimension.
af::dim4 tile_dims(1, 2, 3);
tile(a, tile_dims) [3 2 3 1]
1.0000 1.0000
2.0000 2.0000
3.0000 3.0000
1.0000 1.0000
2.0000 2.0000
3.0000 3.0000
1.0000 1.0000
2.0000 2.0000
3.0000 3.0000</pre>
The C interface for tile is as follows:
<pre class="lang:default decode:true">af_err af_tile(af_array *out, const af_array in, const unsigned x, const unsigned y, const unsigned z, const unsigned w) 
– C interface for tiling an array</pre>
The C++ interface has two overloads
<pre class="lang:default decode:true">array af::tile(const array &amp;in, const unsigned x, const unsigned y=1, const unsigned z=1, const unsigned w=1) 
– Tiles array along specified dimensions

array af::tile(const array &amp;in, const dim4 &amp;dims) 
– Tile an array according to a dim4 object</pre>
<a name="tile"></a>

<a name="transpose"></a>
<h4>transpose()</h4>
The transpose() function performs a standard matrix transpose. The input array must have the dimensions of a 2D-matrix.
<pre class="lang:default decode:true">a [3 3 1 1]
1.0000 3.0000 3.0000
2.0000 1.0000 3.0000
2.0000 2.0000 1.0000

transpose(a) [3 3 1 1]
1.0000 2.0000 2.0000
3.0000 1.0000 2.0000
3.0000 3.0000 1.0000</pre>
The C interfaces for transpose are as follows:
<pre class="lang:default decode:true">af_err af_transpose(af_array *out, af_array in, const bool conjugate) 
– C interface to transpose a matrix.

af_err af_transpose_inplace(af_array in, const bool conjugate) 
– C interface to transpose a matrix in-place.</pre>
The C++ interface has two primary functions and two shorthand versions:
<pre class="lang:default decode:true">array af::transpose(const array &amp;in, const bool conjugate=false) 
– Transposes a matrix.

void af::transposeInPlace(array &amp;in, const bool conjugate=false) 
– Transposes a matrix in-place.

__array af::T() 
– Transpose a matrix

__array af::H() 
– Conjugate Transpose (Hermitian transpose) of a matrix</pre>
Here is an example of how the shorthand versions might be used:
<pre class="lang:default decode:true ">array x = randu(2, 2, f32);
af_print(x.T()); // transpose (real)
array c = randu(2, 2, c32);
af_print(c.T()); // transpose (complex)
af_print(c.H()); // Hermitian (conjugate) transpose</pre>
<a name="transpose"></a>

<a name="array"></a>
<h4>array()</h4>
array() can be used to create a (shallow) copy of a matrix with different dimensions. The total number of elements must remain the same. This function is a wrapper over the moddims() function discussed earlier.

Combining re-ordering functions to enumerate grid coordinates

By using a combination of the array restructuring functions, one can quickly code complex manipulation patterns with a few lines of code. For example, consider generating (x,y) coordinates for a grid where each axis goes from 1 to n. Instead of using several loops to populate our arrays we can just use a small combination of the above functions.
<pre class="lang:default decode:true ">unsigned n=3;
af::array xy = join(1,
    tile(seq(1, n), n),
    flat( transpose(tile(seq(1, n), 1, n)) )
);

xy [9 2 1 1]
1.0000 1.0000
2.0000 1.0000
3.0000 1.0000
1.0000 2.0000
2.0000 2.0000
3.0000 2.0000
1.0000 3.0000
2.0000 3.0000
3.0000 3.0000</pre>
We hope you have found these instructions to be useful. As always, if you have questions about ArrayFire, feel free to drop us a line on the <a href="https://groups.google.com/forum/#!forum/arrayfire-users">Google Group</a>.