Array and Matrix Manipulation
ArrayFire provides several different methods for manipulating arrays and matrices. The functionality includes:
moddims() - change the dimensions of an array without changing the data
Array() - create a (shallow) copy of an array with different dimensions.
flat() - flatten an array to one dimension
flip() - flip an array along a dimension
join() - join up to 4 arrays
reorder() - changes the dimension order within the array
shift() - shifts data along a dimension
tile() - repeats an array along a dimension
transpose() - performs a matrix transpose
Array property T - transpose a matrix or vector (shorthand notation)
Array property H - Hermitian Transpose (conjugate-transpose) a matrix
Below we provide several examples of these functions and their use.
flat()
The flat() function flattens an array to one dimension:
import arrayfire as af
# Creates a 3x3 array filled with random numbers between [0, 1)
a = af.randu((3, 3))
# Flattens the array 'a' into a 1-dimensional column vector
flat_a = af.flat(a)
# Display the original array 'a'
print(a)
The flat function can be called from Python as follows:
Function
af.flat(array) - Python function for flattening an array
flip()
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:
import arrayfire as af
# Generate a 5x2 array of uniformly distributed random numbers between [0, 1)
a = af.randu((5, 2))
# Print the original array 'a'
print("Original array 'a' [5 2 1 1]")
print(a)
# Flip the array 'a' along both axes (rows and columns)
flip_a = af.flip(a)
# Print the flipped array 'flip_a'
print("\nFlipped array 'flip_a' [5 2 1 1]")
print(flip_a)
The flip function can be called from Python as follows:
Function
af.flip(array) - Python function for flipping an array
join()
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:
import arrayfire as af
# Generate a 1-dimensional array 'a' of size 5 filled with uniformly distributed random numbers between [0, 1)
a = af.randu((5,))
# Print the original array 'a'
print("Original array 'a' [5 1 1 1]")
print(a)
# Join the array 'a' with itself along axis 0
a_join = af.join(0, a, a)
# Print the joined array 'a_join'
print("\nJoined array 'a_join' [10 1 1 1]")
print(a_join)
The join function can be called from Python as follows:
Function
af.join(0, array, array1) - Python function for joining arrays along a specified axis
moddims()
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:
import arrayfire as af
a = af.randu((8,))
print(a)
moddims_a = af.moddims(a, (2, 4))
print(moddims_a)
moddims_b = af.moddims(a, (len(a),))
print(moddims_b)
The moddims function has a single form in the Python API:
Function
af.moddims(array, (3,2)) - Python function for modifying dimensions of an array
reorder()
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.
import arrayfire as af
a = af.randu((2, 2, 3, 1))
print(a)
a_reorder = af.reorder(a, ())
shift()
The shift() function shifts data in a circular buffer fashion along a chosen dimension. Consider the following example:
import arrayfire as af
a = af.randu((3, 5))
print(a)
a_shift = af.shift(a, (0, 2))
print(a_shift)
a_shift1 = af.shift(a, (-1, 2))
print(a_shift1)
The shift function can be called from Python as follows: .. admonition:: Function
af.shift(array, (3,2)) - Python function for shifting arrays along specified dimension
tile()
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:
import arrayfire as af
a = af.randu((3,)) # [3,1,1,1]
print(a)
a_tile = af.tile(a, (2,))
print(a_tile)
a_tile1 = af.tile(a, (2, 2))
print(a_tile1)
a_tile2 = af.tile(a, (1, 2, 3))
print(a_tile2)
Function
af.tile(array, (3,2)) - Python function that tiles arrays along specified dimensions
transpose()
The transpose() function performs a standard matrix transpose. The input array must have the dimensions of a 2D-matrix.
import arrayfire as af
a = af.randu((3, 3))
print(a) # [3 3 1 1]
""" 0.3949 0.8465 0.3709
0.3561 0.9399 0.2751
0.6097 0.6802 0.2720"""
a_transpose = af.transpose(a)
print(a_transpose) # [3 3 1 1]
""" 0.3949 0.3561 0.6097
0.8465 0.9399 0.6802
0.3709 0.2751 0.2720"""
The python interface for transpose is as follows:
Function
af.transpose(array) - Python function to transpose matrix in place
Array()
Array() can be used to create a (shallow) copy of another Array with dimensions different to the original. The total number of elements must remain the same. This function is a wrapper over the moddims() function discussed earlier.