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.
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 ({None, str, dtype object}, 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, 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
of dpctl.SyclDevice corresponding to a non-partitioned SYCL
device, an instance of dpctl.SyclQueue, or a
dpctl.tensor.Device object returned by
dpnp.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.
out -- The result of the call to function is passed back directly. Therefore the shape of fromfunction is completely determined by function.
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.indicesReturn an array representing the indices of a grid.
dpnp.meshgridReturn 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')