arrayfire.array module

Array class and helper functions.

class arrayfire.array.Array(src=None, dims=None, dtype=None, is_device=False, offset=None, strides=None)[source]

Bases: arrayfire.base.BaseArray

A multi dimensional array container.

Parameters
srcoptional: array.array, list or C buffer. default: None.
  • When src is array.array or list, the data is copied to create the Array()

  • When src is None, an empty buffer is created.

dimsoptional: tuple of ints. default: (0,)
  • When using the default values of dims, the dims are caclulated as len(src)

dtype: optional: str or arrayfire.Dtype. default: None.
  • if str, must be one of the following:
    • ‘f’ for float

    • ‘d’ for double

    • ‘b’ for bool

    • ‘B’ for unsigned char

    • ‘h’ for signed 16 bit integer

    • ‘H’ for unsigned 16 bit integer

    • ‘i’ for signed 32 bit integer

    • ‘I’ for unsigned 32 bit integer

    • ‘l’ for signed 64 bit integer

    • ‘L’ for unsigned 64 bit integer

    • ‘F’ for 32 bit complex number

    • ‘D’ for 64 bit complex number

  • if arrayfire.Dtype, must be one of the following:
    • Dtype.f32 for float

    • Dtype.f64 for double

    • Dtype.b8 for bool

    • Dtype.u8 for unsigned char

    • Dtype.s16 for signed 16 bit integer

    • Dtype.u16 for unsigned 16 bit integer

    • Dtype.s32 for signed 32 bit integer

    • Dtype.u32 for unsigned 32 bit integer

    • Dtype.s64 for signed 64 bit integer

    • Dtype.u64 for unsigned 64 bit integer

    • Dtype.c32 for 32 bit complex number

    • Dtype.c64 for 64 bit complex number

  • if None, Dtype.f32 is assumed

Examples

Creating an af.Array() from array.array()

>>> import arrayfire as af
>>> import array
>>> a = array.array('f', (1, 2, 3, 4))
>>> b = af.Array(a, (2,2))
>>> af.display(b)
[2 2 1 1]
    1.0000     3.0000
    2.0000     4.0000

Creating an af.Array() from a list

>>> import arrayfire as af
>>> import array
>>> a = [1, 2, 3, 4]
>>> b = af.Array(a)
>>> af.display(b)
[4 1 1 1]
    1.0000
    2.0000
    3.0000
    4.0000

Creating an af.Array() from numpy.array()

>>> import numpy as np
>>> import arrayfire as af
>>> a = np.random.random((2,2))
>>> a
array([[ 0.33042524,  0.36135449],
       [ 0.86748649,  0.42199135]])
>>> b = af.Array(a.ctypes.data, a.shape, a.dtype.char)
>>> af.display(b)
[2 2 1 1]
    0.3304     0.8675
    0.3614     0.4220
Attributes
arr: ctypes.c_void_p

ctypes variable containing af_array from arrayfire library.

Methods

allocated()

Returns the number of bytes allocated by the memory manager for the array.

as_type(ty)

Cast current array to a specified data type

copy()

Performs a deep copy of the array.

device_ptr()

Return the device pointer exclusively held by the array.

dims()

Return the shape of the array as a tuple.

dtype()

Return the data type as a arrayfire.Dtype enum value.

elements()

Return the number of elements in the array.

is_bool()

Check if the array is of type b8.

is_column()

Check if the array is a column i.e. it has a shape of (rows, 1).

is_complex()

Check if the array is of complex type.

is_double()

Check if the array is of double precision floating point type.

is_empty()

Check if the array is empty i.e. it has no elements.

is_floating()

Check if the array is of floating point type.

is_half()

Check if the array is of half floating point type (fp16).

is_integer()

Check if the array is of integer type.

is_linear()

Check if all elements of the array are contiguous.

is_owner()

Check if the array owns the raw pointer or is a derived array.

is_real()

Check if the array is not of complex type.

is_real_floating()

Check if the array is real and of floating point type.

is_row()

Check if the array is a row i.e. it has a shape of (1, cols).

is_scalar()

Check if the array is scalar i.e. it has only one element.

is_single()

Check if the array is of single precision floating point type.

is_sparse()

Check if the array is a sparse matrix.

is_vector()

Check if the array is a vector i.e. it has a shape of one of the following: - (rows, 1) - (1, cols) - (1, 1, vols) - (1, 1, 1, batch).

logical_and(other)

Return self && other.

logical_not()

Return ~self

logical_or(other)

Return self || other.

numdims()

Return the number of dimensions of the array.

offset()

Return the offset, of the first element relative to the raw pointer.

raw_ptr()

Return the device pointer held by the array.

scalar()

Return the first element of the array

strides()

Return the distance in bytes between consecutive elements for each dimension.

to_array([row_major, return_shape])

Return the data as array.array

to_ctype([row_major, return_shape])

Return the data as a ctype C array after copying to host memory

to_list([row_major])

Return the data as list

to_ndarray([output])

Parameters

type()

Return the data type as an int.

property H

Return the hermitian transpose of the array

property T

Return the transpose of the array

allocated()[source]

Returns the number of bytes allocated by the memory manager for the array.

as_type(ty)[source]

Cast current array to a specified data type

Parameters
tyReturn data type
copy()[source]

Performs a deep copy of the array.

Returns
out: af.Array()

An identical copy of self.

device_ptr()[source]

Return the device pointer exclusively held by the array.

Returns
ptrint

Contains location of the device pointer

dims()[source]

Return the shape of the array as a tuple.

dtype()[source]

Return the data type as a arrayfire.Dtype enum value.

elements()[source]

Return the number of elements in the array.

is_bool()[source]

Check if the array is of type b8.

is_column()[source]

Check if the array is a column i.e. it has a shape of (rows, 1).

is_complex()[source]

Check if the array is of complex type.

is_double()[source]

Check if the array is of double precision floating point type.

is_empty()[source]

Check if the array is empty i.e. it has no elements.

is_floating()[source]

Check if the array is of floating point type.

is_half()[source]

Check if the array is of half floating point type (fp16).

is_integer()[source]

Check if the array is of integer type.

is_linear()[source]

Check if all elements of the array are contiguous.

is_owner()[source]

Check if the array owns the raw pointer or is a derived array.

is_real()[source]

Check if the array is not of complex type.

is_real_floating()[source]

Check if the array is real and of floating point type.

is_row()[source]

Check if the array is a row i.e. it has a shape of (1, cols).

is_scalar()[source]

Check if the array is scalar i.e. it has only one element.

is_single()[source]

Check if the array is of single precision floating point type.

is_sparse()[source]

Check if the array is a sparse matrix.

is_vector()[source]

Check if the array is a vector i.e. it has a shape of one of the following: - (rows, 1) - (1, cols) - (1, 1, vols) - (1, 1, 1, batch)

logical_and(other)[source]

Return self && other.

logical_not()[source]

Return ~self

logical_or(other)[source]

Return self || other.

numdims()[source]

Return the number of dimensions of the array.

offset()[source]

Return the offset, of the first element relative to the raw pointer.

Returns
offsetint

The offset in number of elements

raw_ptr()[source]

Return the device pointer held by the array.

Returns
ptrint

Contains location of the device pointer

scalar()[source]

Return the first element of the array

property shape

The shape of the array

strides()[source]

Return the distance in bytes between consecutive elements for each dimension.

Returns
stridestuple

The strides for each dimension

to_array(row_major=False, return_shape=False)[source]

Return the data as array.array

Parameters
row_major: optional: bool. default: False.

Specifies if a transpose needs to occur before copying to host memory.

return_shape: optional: bool. default: False.

Specifies if the shape of the array needs to be returned.

Returns
If return_shape is False:

res: array.array of the appropriate type and length.

else :

(res, dims): array.array and the shape of the array

to_ctype(row_major=False, return_shape=False)[source]

Return the data as a ctype C array after copying to host memory

Parameters
row_major: optional: bool. default: False.

Specifies if a transpose needs to occur before copying to host memory.

return_shape: optional: bool. default: False.

Specifies if the shape of the array needs to be returned.

Returns
If return_shape is False:

res: The ctypes array of the appropriate type and length.

else :

(res, dims): tuple of the ctypes array and the shape of the array

to_list(row_major=False)[source]

Return the data as list

Parameters
row_major: optional: bool. default: False.

Specifies if a transpose needs to occur before copying to host memory.

return_shape: optional: bool. default: False.

Specifies if the shape of the array needs to be returned.

Returns
If return_shape is False:

res: list of the appropriate type and length.

else :

(res, dims): list and the shape of the array

to_ndarray(output=None)[source]
Parameters
output: optional: numpy. default: None
Returns
If output is None: Constructs a numpy.array from arrayfire.Array
If output is not None: copies content of af.array into numpy array.
type()[source]

Return the data type as an int.

arrayfire.array.constant_array(val, d0, d1=None, d2=None, d3=None, dtype=<Dtype.f32: 0>)[source]

Internal function to create a C array. Should not be used externall.

arrayfire.array.display(a, precision=4)[source]

Displays the contents of an array.

Parameters
aaf.Array

Multi dimensional arrayfire array

precision: int. optional.

Specifies the number of precision bits to display

arrayfire.array.get_display_dims_limit()[source]

Gets the dimension limit after which array’s data won’t get presented to the result of str(arr).

Default is None, which means there is no limit.

Returns
  • tuple of the current limit
    • None is there is no limit

arrayfire.array.read_array(filename, index=None, key=None)[source]

Read an array from disk.

Parameters
filenamestr

Location of the data file.

indexint. Optional. Default: None.
  • The index of the array stored in the file.

  • If None, key is used.

keystr. Optional. Default: None.
  • A name / key associated with the array

  • If None, index is used.

arrayfire.array.save_array(key, a, filename, append=False)[source]

Save an array to disk.

Parameters
keystr

A name / key associated with the array

aaf.Array

The array to be stored to disk

filenamestr

Location of the data file.

appendBoolean. optional. default: False.

If the file already exists, specifies if the data should be appended or overwritten.

Returns
indexint

The index of the array stored in the file.

arrayfire.array.set_display_dims_limit(*dims)[source]

Sets the dimension limit after which array’s data won’t get presented to the result of str(arr).

Default is None, which means there is no limit.

Parameters
*dimsdimension limit args
arrayfire.array.transpose(a, conj=False)[source]

Perform the transpose on an input.

Parameters
aaf.Array

Multi dimensional arrayfire array.

conjoptional: bool. default: False.

Flag to specify if a complex conjugate needs to applied for complex inputs.

Returns
outaf.Array

Containing the tranpose of a for all batches.

arrayfire.array.transpose_inplace(a, conj=False)[source]

Perform inplace transpose on an input.

Parameters
aaf.Array
  • Multi dimensional arrayfire array.

  • Contains transposed values on exit.

conjoptional: bool. default: False.

Flag to specify if a complex conjugate needs to applied for complex inputs.