|
| 1 | +Array and Matrix Manipulation |
| 2 | +============================= |
| 3 | +ArrayFire provides several different methods for manipulating arrays and matrices. The functionality includes: |
| 4 | + |
| 5 | +* moddims() - change the dimensions of an array without changing the data |
| 6 | +* array() - create a (shallow) copy of an array with different dimensions. |
| 7 | +* flat() - flatten an array to one dimension |
| 8 | +* flip() - flip an array along a dimension |
| 9 | +* join() - join up to 4 arrays |
| 10 | +* reorder() - changes the dimension order within the array |
| 11 | +* shift() - shifts data along a dimension |
| 12 | +* tile() - repeats an array along a dimension |
| 13 | +* transpose() - performs a matrix transpose |
| 14 | +* T() - transpose a matrix or vector (shorthand notation) |
| 15 | +* H() - Hermitian Transpose (conjugate-transpose) a matrix |
| 16 | + |
| 17 | +Below we provide several examples of these functions and their use. |
| 18 | + |
| 19 | +flat() |
| 20 | +====== |
| 21 | +The **flat()** function flattens an array to one dimension: |
| 22 | + |
| 23 | +.. literalinclude:: arrayandmatrixmanipulation.py |
| 24 | + :language: python |
| 25 | + :start-after: [manipulation1-snippet] |
| 26 | + :end-before: [manipulation1-endsnippet] |
| 27 | + |
| 28 | +The **flat** function can be called from Python as follows: |
| 29 | + |
| 30 | +.. admonition:: Function |
| 31 | + |
| 32 | + af.flat(array) - Python function for flattening an array |
| 33 | + |
| 34 | +flip() |
| 35 | +====== |
| 36 | +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: |
| 37 | + |
| 38 | + |
| 39 | +.. literalinclude:: arrayandmatrixmanipulation.py |
| 40 | + :language: python |
| 41 | + :start-after: [manipulation2-snippet] |
| 42 | + :end-before: [manipulation2-endsnippet] |
| 43 | + |
| 44 | +The **flip** function can be called from Python as follows: |
| 45 | + |
| 46 | +.. admonition:: Function |
| 47 | + |
| 48 | + af.flip(array) - Python function for flipping an array |
| 49 | + |
| 50 | + |
| 51 | +join() |
| 52 | +====== |
| 53 | + |
| 54 | +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: |
| 55 | + |
| 56 | +.. literalinclude:: arrayandmatrixmanipulation.py |
| 57 | + :language: python |
| 58 | + :start-after: [manipulation3-snippet] |
| 59 | + :end-before: [manipulation3-endsnippet] |
| 60 | + |
| 61 | + |
| 62 | +The **join** function can be called from Python as follows: |
| 63 | + |
| 64 | +.. admonition:: Function |
| 65 | + |
| 66 | + af.join(0, array, array1) - Python function for joining arrays along a specified axis |
| 67 | + |
| 68 | +moddims() |
| 69 | +========= |
| 70 | + |
| 71 | +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: |
| 72 | + |
| 73 | +.. literalinclude:: arrayandmatrixmanipulation.py |
| 74 | + :language: python |
| 75 | + :start-after: [manipulation4-snippet] |
| 76 | + :end-before: [manipulation4-endsnippet] |
| 77 | + |
| 78 | +The moddims function has a single form in the Python API: |
| 79 | + |
| 80 | +.. admonition:: Function |
| 81 | + |
| 82 | + af.moddims(array, (3,2)) - Python function for modifying dimensions of an array |
| 83 | + |
| 84 | + |
| 85 | +reorder() |
| 86 | +========= |
| 87 | +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. |
| 88 | + |
| 89 | +.. literalinclude:: arrayandmatrixmanipulation.py |
| 90 | + :language: python |
| 91 | + :start-after: [manipulation5-snippet] |
| 92 | + :end-before: [manipulation5-endsnippet] |
| 93 | + |
| 94 | +shift() |
| 95 | +======= |
| 96 | +The **shift()** function shifts data in a circular buffer fashion along a chosen dimension. Consider the following example: |
| 97 | + |
| 98 | + |
| 99 | +.. literalinclude:: arrayandmatrixmanipulation.py |
| 100 | + :language: python |
| 101 | + :start-after: [manipulation6-snippet] |
| 102 | + :end-before: [manipulation6-endsnippet] |
| 103 | + |
| 104 | +The shift function can be called from Python as follows: |
| 105 | +.. admonition:: Function |
| 106 | + |
| 107 | + af.shift(array, (3,2)) - Python function for shifting arrays along specified dimension |
| 108 | + |
| 109 | +tile() |
| 110 | +====== |
| 111 | +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: |
| 112 | + |
| 113 | +.. literalinclude:: arrayandmatrixmanipulation.py |
| 114 | + :language: python |
| 115 | + :start-after: [manipulation7-snippet] |
| 116 | + :end-before: [manipulation7-endsnippet] |
| 117 | + |
| 118 | +.. admonition:: Function |
| 119 | + |
| 120 | + af.tile(array, (3,2)) - Python function that tiles arrays along specified dimensions |
| 121 | + |
| 122 | + |
| 123 | +transpose() |
| 124 | +=========== |
| 125 | +The **transpose()** function performs a standard matrix transpose. The input array must have the dimensions of a 2D-matrix. |
| 126 | + |
| 127 | +.. literalinclude:: arrayandmatrixmanipulation.py |
| 128 | + :language: python |
| 129 | + :start-after: [manipulation8-snippet] |
| 130 | + :end-before: [manipulation8-endsnippet] |
| 131 | + |
| 132 | + |
| 133 | +The python interface for transpose is as follows: |
| 134 | + |
| 135 | +.. admonition:: Function |
| 136 | + |
| 137 | + af.transpose(array) - Python function to transpose matrix in place |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | +array() |
| 142 | +======= |
| 143 | +**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. |
| 144 | + |
0 commit comments