dpnp.fromfunction¶
- dpnp.fromfunction(function, shape, *, dtype=<class 'float'>, like=None, device=None, usm_type='device', sycl_queue=None, **kwargs)[source]¶
Construct an array by executing a function over each coordinate.
The resulting array therefore has a value
fn(x, y, z)at coordinate(x, y, z).For full documentation refer to
numpy.fromfunction.- Parameters:
- functioncallable
The function is called with N parameters, where N is the rank of shape. Each parameter represents the coordinates of the array varying along a specific axis. For example, if shape were
(2, 2), then the parameters would bearray([[0, 0], [1, 1]])andarray([[0, 1], [0, 1]]).- shape(N,) tuple of ints
Shape of the output array, which also determines the shape of the coordinate arrays passed to function.
- dtype{None, str, dtype object}, optional
Data-type of the coordinate arrays passed to function. If
None, uses a default floating point data type for the device on which the returned array is allocated.Default:
float.- device{None, string, SyclDevice, SyclQueue, Device}, optional
An array API concept of device where the output array is created. device can be
None, a oneAPI filter selector string, an instance ofdpctl.SyclDevicecorresponding to a non-partitioned SYCL device, an instance ofdpctl.SyclQueue, or adpnp.tensor.Deviceobject returned bydpnp.ndarray.device.Default:
None.- usm_type{None, "device", "shared", "host"}, optional
The type of SYCL USM allocation for the output array.
Default:
"device".- sycl_queue{None, SyclQueue}, optional
A SYCL queue to use for output array allocation and copying. The sycl_queue can be passed as
None(the default), which means to get the SYCL queue from device keyword if present or to use a default queue.Default:
None.
- Returns:
- outdpnp.ndarray
The result of the call to function is passed back directly. Therefore the shape of fromfunction is completely determined by function.
Limitations
Parameter like is supported only with default value
None. Otherwise, the function raisesNotImplementedErrorexception.See also
dpnp.indicesReturn an array representing the indices of a grid.
dpnp.meshgridReturn coordinate matrices from coordinate vectors.
Notes
This uses
numpy.fromfunctionand coerces the result to a DPNP array. Keywords other than dtype and like are passed to function.Examples
>>> import dpnp as np >>> np.fromfunction(lambda i, j: i, (2, 2), dtype=float) array([[0., 0.], [1., 1.]])
>>> np.fromfunction(lambda i, j: j, (2, 2), dtype=float) array([[0., 1.], [0., 1.]])
>>> np.fromfunction(lambda i, j: i == j, (3, 3), dtype=int) array([[ True, False, False], [False, True, False], [False, False, True]])
>>> np.fromfunction(lambda i, j: i + j, (3, 3), dtype=int) array([[0, 1, 2], [1, 2, 3], [2, 3, 4]])
Creating an array on a different device or with a specified usm_type
>>> x = np.fromfunction(lambda i, j: i - j, (3, 3)) # default case >>> x.device, x.usm_type (Device(level_zero:gpu:0), 'device')
>>> y = np.fromfunction(lambda i, j: i - j, (3, 3), device='cpu') >>> y.device, y.usm_type (Device(opencl:cpu:0), 'device')
>>> z = np.fromfunction(lambda i, j: i - j, (3, 3), usm_type="host") >>> z.device, z.usm_type (Device(level_zero:gpu:0), 'host')