ArrayFire is a fantastic library when it comes to speed and performance. One of the things that people overlook when evaluating ArrayFire is the indexing options available to the user.

The ArrayFire library exposes the array data structure to the user. The array stores the data in a column major order. This means that the following code:

<pre>
float[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9};
array A(3, 3, a);
</pre>

Will produce the following matrix:

<a href="http://sandbox.arrayfire.com/wp-content/uploads/2014/06/CodeCogsEqn.png"><img src="http://sandbox.arrayfire.com/wp-content/uploads/2014/06/CodeCogsEqn.png" alt="3x3 Column Major Matrix" width="114" height="67" class="alignnone size-full wp-image-5994" /></a>

Notice how the first three values make up the first column.

When you want to perform an operation on every element of the array you can just use the variable name with the operation. The following command performs adds 5 to each element of the array:

<pre>
A += 5;
</pre>

Note: If you do something like this
<pre>
A = 3;
</pre>
You will resize A to size 1 and the single element will be given the value 3.
To assign a constant value throughout the array, use the <code>constant</code> function as shown below.
<pre>
// If A is already defined
A = constant(3, A.dims());             // Will assign all elements to 3
// If A is not defined
array A = constant(3, n_rows, n_cols); // Creates array of n_rows x n_cols and assigns all elements to 3
</pre>

Sometimes you only want to perform a calculation on a part of the data. In this case you can index into the elements of the matrix using the parenthesis notation. For example, you can access the middle element of the initial array by using:

<pre>
float[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9};
array A(3, 3, a);
array B = A(1, 1);
// B == 5
</pre>

The first number in the parenthesis is the column index, and the second number is the row index. You can index in this way to up to 4 dimensions.

ArrayFire also comes with a host of helper functions which allow you to access whole rows and columns of a matrix. The <code>row</code> and <code>col</code> functions will return an array which is made up of elements in the row an column respectively. You can access a set of rows and columns using the <code>rows</code> and <code>cols</code> functions.

<pre>
float[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9};
array A(3, 3, a);
array B = A.row(2);
array C = A.cols(1,2);
</pre>

<a href="http://sandbox.arrayfire.com/wp-content/uploads/2014/06/CodeCogsEqn-3.png"><img src="http://sandbox.arrayfire.com/wp-content/uploads/2014/06/CodeCogsEqn-3-300x61.png" alt="A.row(2); A.cols(1,2);" width="300" height="61" class="alignnone size-medium wp-image-5995" /></a>

You can also use parenthesis notation to extract rows and columns of a matrix or volume. In order to do this, we will use the span and seq objects to specify which rows and columns we need. 
A few definitions first.
<code>seq</code> return a sequence of values. The simplest, seq(n) returns values from 0 to n-1. seq(m, n) returns values from m to n (both inclusive). Read more about seq <a href="http://www.sandbox.arrayfire.com/docs/classaf_1_1seq.htm">here</a>.
<code>span</code> denotes that the entire dimension be used. It would be the same as seq(dim_size).

A few lines of sample code. The first creates the array A of 4 rows, 3 cols and 2 matrices. Using seq, we can generate a sequence of incremental elements.
The second line, we create B which contains the first 6 elements of A.
<pre>
// Creates A with dims [4, 3, 2] and values 0->24 incremented by 1. 
array A = array(seq(4*3*2), 4, 3, 2);
// Gets the first 6 elements of A irrespective of dimensions. B will be of size [10, 1] (treats A as a 1-dimensional vector).
array B = A(seq(6));
</pre>
<a href="http://sandbox.arrayfire.com/wp-content/uploads/2014/06/image0.png"><img src="http://sandbox.arrayfire.com/wp-content/uploads/2014/06/image0.png" alt="image0" width="235" height="118" class="alignnone size-full wp-image-6000" /></a>

The next two lines show how we can use seq and span for each dimension. 
<pre>
// Gets the first 3 rows and 2 columns of A. In this case, the arguments of seq cannot be greater than the size of the specific dimension of A
array C = A(seq(3), seq(2));
// Gets the first 3 rows and 2 columns of A from all matrices.
array D = A(seq(3), seq(2), span);
</pre>
<a href="http://sandbox.arrayfire.com/wp-content/uploads/2014/06/image1.png"><img src="http://sandbox.arrayfire.com/wp-content/uploads/2014/06/image1-300x69.png" alt="image1" width="300" height="69" class="alignnone size-medium wp-image-6003" /></a>

A couple more examples show other use cases.
<pre>
// Gets the matrix with index 1. span specifies that all values be retrieved.
array E = A(span, span, 1);
// Gets the matrix with index 1 but only rows 1 through 3.
array F = A(seq(1, 3), span, 1);
</pre>
<a href="http://sandbox.arrayfire.com/wp-content/uploads/2014/06/image2.png"><img src="http://sandbox.arrayfire.com/wp-content/uploads/2014/06/image2-300x87.png" alt="image2" width="300" height="87" class="alignnone size-medium wp-image-6004" /></a>

Previously, I wrote that doing <code>A = 0</code> will make A single valued. However, doing <code>A(span) = 0</code> will assign all elements of A to 0.
<code>A(span) = 0</code> is equivalent to <code>A = constant(0, A.dims())</code>. The difference is that while the former writes memory in-place, the latter creates a new array and A now point to the location of the new array and the previous memory is deleted.

The last indexing technique we will discuss in the blog is using relational operators. Arrays when used alongside relational operators can be very powerful. We can do something like this:
<pre>
array A = randu(3, 3);
array B = (A > 0.5);     // B is a b8 array of size A.dims(). if(A(id)>0.5) B(id)=1; else B(id)=0
</pre>
<a href="http://sandbox.arrayfire.com/wp-content/uploads/2014/06/image3.png"><img src="http://sandbox.arrayfire.com/wp-content/uploads/2014/06/image3-300x62.png" alt="image3" width="300" height="62" class="alignnone size-medium wp-image-6007" /></a>

A few more examples of using relational operators in indexing:
<pre>
array C = B.as(f32);     // Storing B as f32 array
array C(C == 0) = -1;    // All values of B1 which are 0 are assigned -1.
array D = A(A > 0.5);    // C is a 1-dimensional array. It contains the elements from A that are greater than 0.5.
</pre>
<a href="http://sandbox.arrayfire.com/wp-content/uploads/2014/06/image4.png"><img src="http://sandbox.arrayfire.com/wp-content/uploads/2014/06/image4-300x44.png" alt="image4" width="400" height="60" class="alignnone size-medium wp-image-6009" /></a>

In the next blog, we will cover various indexing opertations and their impact on performance. Also look for more tips and tricks for indexing in ArrayFire.