|
|
Many different kinds of matrix manipulation routines are available:
The flat() function flattens an array to one dimension. ``` 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
``` The flat function has the following overloads:
The flip() function flips the contents of an array along a chosen dimension. ``` 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 ``` The flip function has the following overloads:
The join() function can join up to 4 arrays together. ``` 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 ``` The join function has several overloads:
The moddims() function changes the dimensions of an array without changing its data or order. It is important to remember that the function only modifies the metadata associated with the array and does not actually modify the content of the array. ``` 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 ``` The moddims function has several overloads:
The reorder() function changes the order of the dimensions within the array. This actually alters the underlying data of the array. ``` 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 ``` The reorder function the following several overloads:
The shift() function shifts data in a circular buffer fashion along a chosen dimension. ``` 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 ``` The shift function has the following overloads:
The tile() function repeats an array along a dimension ``` a [3 1 1 1] 1.0000 2.0000 3.0000
tile(a, 2) [6 1 1 1] 1.0000 2.0000 3.0000 1.0000 2.0000 3.0000
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
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
``` The tile function has several overloads:
The transpose() function performs a standard matrix transpose. The input array must have the dimensions of a 2D-matrix. ``` 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
``` The transpose function has several overloads:
array() can be used to create a (shallow) copy of a matrix with different dimensions. The number of elements must remain the same as the original array.
The T() and H() methods can be used to form the matrix or vector transpose .
By using a combination of the array restructuring functions, we 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. ``` 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 ```
Functions provided by arrayfire offer ease and flexibility for efficiently manipulating the structure of arrays. The provided functions can be used as building blocks to generate, shift, or prepare data to any form imaginable!