Skip to content

Commit 37cf686

Browse files
edwinsolisfsyurkevi
authored andcommitted
Added more functions docs, added classes, constants, implemented reshape
1 parent e7b7f23 commit 37cf686

File tree

249 files changed

+2785
-833
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

249 files changed

+2785
-833
lines changed

arrayfire/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272

7373
__all__ += [
7474
"constant",
75+
"zeros",
76+
"ones",
7577
"diag",
7678
"identity",
7779
"iota",
@@ -89,6 +91,7 @@
8991
"flip",
9092
"join",
9193
"moddims",
94+
"reshape",
9295
"reorder",
9396
"replace",
9497
"select",
@@ -100,6 +103,8 @@
100103

101104
from arrayfire.library.array_functions import (
102105
constant,
106+
zeros,
107+
ones,
103108
copy_array,
104109
diag,
105110
eval,
@@ -114,6 +119,7 @@
114119
lookup,
115120
lower,
116121
moddims,
122+
reshape,
117123
pad,
118124
range,
119125
reorder,

arrayfire/array_object.py

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -930,8 +930,8 @@ def T(self) -> Array:
930930
931931
Note
932932
----
933-
- The array instance must be two-dimensional. If the array instance is not two-dimensional, an error
934-
should be raised.
933+
- The array instance must be two-dimensional. If the array instance is not two-dimensional, an error should be raised.
934+
935935
"""
936936
if self.ndim < 2:
937937
raise TypeError(f"Array should be at least 2-dimensional. Got {self.ndim}-dimensional array")
@@ -942,6 +942,24 @@ def T(self) -> Array:
942942
@property
943943
@afarray_as_array
944944
def H(self) -> Array:
945+
"""
946+
Hermitian Conjugate of the array.
947+
948+
Returns
949+
-------
950+
Array
951+
Two-dimensional array whose first and last dimensions (axes) are permuted in reverse order relative to
952+
original array with its elements complex conjugated. The returned array must have the same data type as the original array.
953+
954+
Note
955+
----
956+
- The array instance must be two-dimensional. If the array instance is not two-dimensional, an error should be raised.
957+
958+
"""
959+
if self.ndim < 2:
960+
raise TypeError(f"Array should be at least 2-dimensional. Got {self.ndim}-dimensional array")
961+
962+
# TODO add check if out.dtype == self.dtype
945963
return cast(Array, wrapper.transpose(self._arr, True))
946964

947965
@property
@@ -1157,6 +1175,39 @@ def device_pointer(self) -> int:
11571175
def is_locked_array(self) -> bool:
11581176
return wrapper.is_locked_array(self._arr)
11591177

1178+
@afarray_as_array
1179+
def reshape(self, shape) -> Array:
1180+
"""
1181+
Return a copy of this array with the specified shape without changing the data layout.
1182+
1183+
Parameters
1184+
----------
1185+
shape : tuple of int
1186+
The desired shape of the output array. It should be a tuple of integers
1187+
representing the dimensions of the output array. The product of these
1188+
dimensions must match the total number of elements in the input array.
1189+
1190+
Returns
1191+
-------
1192+
out : af.Array
1193+
- An array containing the same data as `array` with the specified shape.
1194+
- The total number of elements in `array` must match the product of the dimensions specified in the `shape` tuple.
1195+
1196+
Raises
1197+
------
1198+
ValueError
1199+
If the total number of elements in the input array does not match the
1200+
product of the dimensions specified in the `shape` tuple.
1201+
1202+
Notes
1203+
-----
1204+
This function modifies the shape of the input array without changing the
1205+
data layout. The resulting array will have the same data, but with a
1206+
different shape as specified by the `shape` parameter.
1207+
"""
1208+
# TODO add examples to doc
1209+
return cast(Array, wrapper.moddims(self._arr, shape))
1210+
11601211
def lock_array(self) -> None:
11611212
return wrapper.lock_array(self._arr)
11621213

arrayfire/library/array_functions.py

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
__all__ = [
22
"constant",
3+
"zeros",
4+
"ones",
35
"diag",
46
"identity",
57
"iota",
@@ -17,6 +19,7 @@
1719
"flip",
1820
"join",
1921
"moddims",
22+
"reshape",
2023
"reorder",
2124
"replace",
2225
"select",
@@ -70,6 +73,59 @@ def constant(scalar: int | float | complex, shape: tuple[int, ...] = (1,), dtype
7073
"""
7174
return cast(Array, wrapper.create_constant_array(scalar, shape, dtype))
7275

76+
def zeros(shape: tuple[int, ...], dtype: Dtype = float32) -> Array:
77+
"""
78+
Create a multi-dimensional array filled with zeros
79+
80+
Parameters
81+
----------
82+
shape : tuple[int, ...], optional, default: (1,)
83+
The shape of the constant array.
84+
85+
dtype : Dtype, optional, default: float32
86+
Data type of the array.
87+
88+
Returns
89+
-------
90+
Array
91+
A multi-dimensional ArrayFire array filled zeros
92+
93+
Notes
94+
-----
95+
The shape parameter determines the dimensions of the resulting array:
96+
- If shape is (x1,), the output is a 1D array of size (x1,).
97+
- If shape is (x1, x2), the output is a 2D array of size (x1, x2).
98+
- If shape is (x1, x2, x3), the output is a 3D array of size (x1, x2, x3).
99+
- If shape is (x1, x2, x3, x4), the output is a 4D array of size (x1, x2, x3, x4).
100+
"""
101+
return constant(0, shape, dtype)
102+
103+
def ones(shape: tuple[int, ...], dtype: Dtype = float32) -> Array:
104+
"""
105+
Create a multi-dimensional array filled with ones
106+
107+
Parameters
108+
----------
109+
shape : tuple[int, ...], optional, default: (1,)
110+
The shape of the constant array.
111+
112+
dtype : Dtype, optional, default: float32
113+
Data type of the array.
114+
115+
Returns
116+
-------
117+
Array
118+
A multi-dimensional ArrayFire array filled ones
119+
120+
Notes
121+
-----
122+
The shape parameter determines the dimensions of the resulting array:
123+
- If shape is (x1,), the output is a 1D array of size (x1,).
124+
- If shape is (x1, x2), the output is a 2D array of size (x1, x2).
125+
- If shape is (x1, x2, x3), the output is a 3D array of size (x1, x2, x3).
126+
- If shape is (x1, x2, x3, x4), the output is a 4D array of size (x1, x2, x3, x4).
127+
"""
128+
return constant(1, shape, dtype)
73129

74130
@afarray_as_array
75131
def diag(array: Array, /, *, diag_index: int = 0, extract: bool = True) -> Array:
@@ -255,8 +311,7 @@ def lower(array: Array, /, *, is_unit_diag: bool = False) -> Array:
255311
Notes
256312
-----
257313
- The function does not alter the elements above the main diagonal; it simply does not include them in the output.
258-
- This function can be useful for mathematical operations that require lower triangular matrices, such as certain
259-
types of matrix factorizations.
314+
- This function can be useful for mathematical operations that require lower triangular matrices, such as certain types of matrix factorizations.
260315
261316
Examples
262317
--------
@@ -312,8 +367,7 @@ def upper(array: Array, /, *, is_unit_diag: bool = False) -> Array:
312367
Notes
313368
-----
314369
- The function does not alter the elements below the main diagonal; it simply does not include them in the output.
315-
- This function can be useful for mathematical operations that require upper triangular matrices, such as certain
316-
types of matrix factorizations.
370+
- This function can be useful for mathematical operations that require upper triangular matrices, such as certain types of matrix factorizations.
317371
318372
Examples
319373
--------
@@ -818,6 +872,40 @@ def moddims(array: Array, shape: tuple[int, ...], /) -> Array:
818872
# TODO add examples to doc
819873
return cast(Array, wrapper.moddims(array.arr, shape))
820874

875+
def reshape(array: Array, shape: tuple[int, ...], /) -> Array:
876+
"""
877+
Modify the shape of the array without changing the data layout.
878+
879+
Parameters
880+
----------
881+
array : af.Array
882+
Multi-dimensional array to be reshaped.
883+
884+
shape : tuple of int
885+
The desired shape of the output array. It should be a tuple of integers
886+
representing the dimensions of the output array. The product of these
887+
dimensions must match the total number of elements in the input array.
888+
889+
Returns
890+
-------
891+
out : af.Array
892+
- An array containing the same data as `array` with the specified shape.
893+
- The total number of elements in `array` must match the product of the
894+
dimensions specified in the `shape` tuple.
895+
896+
Raises
897+
------
898+
ValueError
899+
If the total number of elements in the input array does not match the
900+
product of the dimensions specified in the `shape` tuple.
901+
902+
Notes
903+
-----
904+
This function modifies the shape of the input array without changing the
905+
data layout. The resulting array will have the same data, but with a
906+
different shape as specified by the `shape` parameter.
907+
"""
908+
return moddims(array, shape)
821909

822910
@afarray_as_array
823911
def reorder(array: Array, /, *, shape: tuple[int, ...] = (1, 0, 2, 3)) -> Array:

arrayfire/library/computer_vision.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,7 @@ def sift(
164164
tuple[Features, Array]
165165
A tuple containing:
166166
- An ArrayFire Features object encapsulating the detected keypoints.
167-
- An ArrayFire Array containing the corresponding descriptors for each keypoint. The descriptors are
168-
128-dimensional vectors describing the local appearance around each keypoint.
167+
- An ArrayFire Array containing the corresponding descriptors for each keypoint. The descriptors are 128-dimensional vectors describing the local appearance around each keypoint.
169168
170169
Note
171170
----

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ To build the docs follow these steps:
1010
1. Install the required sphinx packages and extensions from the [dev-requirements.txt](../dev-requirements.txt)
1111
```sh
1212
pip install -r dev-requirements.txt # install sphinx and its extensions
13+
pip install -r requirements.txt # install arrayfire-binary-python-wrapper
1314
```
1415
2. Build docs using sphinx
1516
```sh

docs/array_creation_functions.rst

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
Array Creation Functions
2+
==========================
3+
4+
Functions in this category are used to initialize arrays with specific values or patterns.
5+
6+
.. list-table:: Functions
7+
8+
* - :doc:`af.cast() <functions/cast>`
9+
- Cast an array from one type to another.
10+
* - :doc:`af.constant() <functions/constant>`
11+
- Create an array from a scalar input value.
12+
* - :doc:`af.copy_array() <functions/copy_array>`
13+
- Performs a deep copy of the array.
14+
* - :doc:`af.cplx() <functions/cplx>`
15+
- Creates complex arrays from real and imaginary parts
16+
* - :doc:`af.diag() <functions/diag>`
17+
- Extract the diagonal from an array.
18+
* - :doc:`af.eval() <functions/eval>`
19+
- Evaluate an expression (nonblocking).
20+
* - :doc:`af.gaussian_kernel() <functions/gaussian_kernel>`
21+
- Creates a Gaussian Kernel.
22+
* - :doc:`af.identity() <functions/identity>`
23+
- Generate an identity matrix.
24+
* - :doc:`af.iota() <functions/iota>`
25+
- Generate an array with [0, n-1] values modified to specified dimensions and tiling.
26+
* - :doc:`af.isinf() <functions/isinf>`
27+
- Check if values are infinite.
28+
* - :doc:`af.isnan() <functions/isnan>`
29+
- Check if values are NaN.
30+
* - :doc:`af.iszero() <functions/iszero>`
31+
- Check if values are zero.
32+
* - :doc:`af.lookup() <functions/lookup>`
33+
- Lookup values of an array by indexing with another array.
34+
* - :doc:`af.lower() <functions/lower>`
35+
- Return the lower triangular matrix from an input array.
36+
- Find maximum value from a window.
37+
* - :doc:`af.ones() <functions/ones>`
38+
- Creates an array filled with ones.\
39+
* - :doc:`af.randn() <functions/randn>`
40+
- Create a random array sampled from normal distribution.
41+
* - :doc:`af.randu() <functions/randu>`
42+
- Create a random array sampled from uniform distribution.
43+
* - :doc:`af.range() <functions/range>`
44+
- Generate an array with [0, n-1] values along the a specified dimension and tiled across other dimensions.
45+
* - :doc:`af.upper() <functions/upper>`
46+
- Return the upper triangular matrix from an input array.
47+
* - :doc:`af.where() <functions/where>`
48+
- Locate the indices of the non-zero values in an array.
49+
* - :doc:`af.zeros() <functions/zeros>`
50+
- Creates an array filled with zeros.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
Array Manipulation
2+
===================
3+
4+
These functions help modify the structure or arrangement of arrays.
5+
6+
.. list-table:: Functions
7+
8+
* - :doc:`af.approx1() <functions/approx1>`
9+
- Interpolation across a single dimension.
10+
* - :doc:`af.approx1_uniform() <functions/approx1_uniform>`
11+
- Interpolation across a single dimension in uniform steps
12+
* - :doc:`af.approx2() <functions/approx2>`
13+
- Interpolation along two dimensions.
14+
* - :doc:`af.approx2_uniform() <functions/approx2_uniform>`
15+
- Interpolation along two dimensions in uniform steps
16+
* - :doc:`af.flat() <functions/flat>`
17+
- Flatten an array.
18+
* - :doc:`af.flip() <functions/flip>`
19+
- Flip the input along a specified dimension.
20+
* - :doc:`af.join() <functions/join>`
21+
- Join up to 4 arrays along specified dimension.
22+
* - :doc:`af.pad() <functions/pad>`
23+
- Pad an array.
24+
* - :doc:`af.reorder() <functions/reorder>`
25+
- Reorder an array.
26+
* - :doc:`af.replace() <functions/replace>`
27+
- Replace elements of an array with elements of another array.
28+
* - :doc:`af.reshape() <functions/reshape>`
29+
- Modify the dimensions of an array without changing the order of its elements
30+
* - :doc:`af.resize() <functions/resize>`
31+
- Resize an input image.
32+
* - :doc:`af.rotate() <functions/rotate>`
33+
- Rotate an input image or array.
34+
* - :doc:`af.scale() <functions/scale>`
35+
- Scale an input image.
36+
* - :doc:`af.select() <functions/select>`
37+
- Select elements based on a conditional array.
38+
* - :doc:`af.set_intersect() <functions/set_intersect>`
39+
- Evaluate the intersection of two arrays
40+
* - :doc:`af.set_union() <functions/set_union>`
41+
- Evaluate the union of two arrays
42+
* - :doc:`af.set_unique() <functions/set_unique>`
43+
- Return the unique values in an array
44+
* - :doc:`af.shift() <functions/shift>`
45+
- Shift an array.
46+
* - :doc:`af.sort() <functions/sort>`
47+
- Sort an array over a given dimension
48+
* - :doc:`af.tile() <functions/tile>`
49+
- Generate a tiled array by repeating an array's contents along a specified dimension.
50+
* - :doc:`af.transpose() <functions/transpose>`
51+
- Transpose a matrix.
52+
* - :doc:`af.unwrap() <functions/unwrap>`
53+
- Rearrange windowed sections of an array into columns (or rows)
54+
* - :doc:`af.wrap() <functions/wrap>`
55+
- Performs the opposite of af::unwrap().

0 commit comments

Comments
 (0)