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:
  • function (callable) -- 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 be array([[0, 0], [1, 1]]) and array([[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 (data-type, optional) -- Data-type of the coordinate arrays passed to function. Default is the default floating point data type for the device where the returned array is allocated.

  • device ({None, string, SyclDevice, SyclQueue}, optional) -- An array API concept of device where the output array is created. The device can be None (the default), an OneAPI filter selector string, an instance of dpctl.SyclDevice corresponding to a non-partitioned SYCL device, an instance of dpctl.SyclQueue, or a Device object returned by dpnp.dpnp_array.dpnp_array.device property.

  • usm_type ({None, "device", "shared", "host"}, optional) -- The type of SYCL USM allocation for the output array. Default is "device".

  • sycl_queue ({None, SyclQueue}, optional) -- A SYCL queue to use for output array allocation and copying.

Returns:

out -- The result of the call to function is passed back directly. Therefore the shape of fromfunction is completely determined by function.

Return type:

dpnp.ndarray

Limitations

Parameter like is supported only with default value None. Otherwise, the function raises NotImplementedError exception.

Notes

This uses numpy.fromfunction and coerces the result to a DPNP array. Keywords other than dtype and like are passed to function.

See also

dpnp.indices

Return an array representing the indices of a grid.

dpnp.meshgrid

Return coordinate matrices from coordinate vectors.

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')