dpnp.diagflat

dpnp.diagflat(v, /, k=0, *, device=None, usm_type=None, sycl_queue=None)[source]

Create a two-dimensional array with the flattened input as a diagonal.

For full documentation refer to numpy.diagflat.

Parameters:
  • v (array_like) -- Input data, which is flattened and set as the k-th diagonal of the output, in any form that can be converted to an array. This includes scalars, lists, lists of tuples, tuples, tuples of tuples, tuples of lists, and ndarrays.

  • k (int, optional) -- Diagonal to set; 0, the default, corresponds to the "main" diagonal, a positive (negative) k giving the number of the diagonal above (below) the main.

  • 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: None.

  • 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:

out -- The 2-D output array.

Return type:

dpnp.ndarray

See also

dpnp.diag

Extract a diagonal or construct a diagonal array.

dpnp.diagonal

Return specified diagonals.

dpnp.trace

Return sum along diagonals.

Examples

>>> import dpnp as np
>>> x0 = np.array([[1, 2], [3, 4]])
>>> np.diagflat(x0)
array([[1, 0, 0, 0],
       [0, 2, 0, 0],
       [0, 0, 3, 0],
       [0, 0, 0, 4]])
>>> np.diagflat(x0, 1)
array([[0, 1, 0, 0, 0],
       [0, 0, 2, 0, 0],
       [0, 0, 0, 3, 0],
       [0, 0, 0, 0, 4],
       [0, 0, 0, 0, 0]])

Creating an array on a different device or with a specified usm_type

>>> x = np.diagflat(x0) # default case
>>> x, x.device, x.usm_type
array([[1, 0, 0, 0],
       [0, 2, 0, 0],
       [0, 0, 3, 0],
       [0, 0, 0, 4]]), Device(level_zero:gpu:0), 'device')
>>> y = np.diagflat(x0, device="cpu")
>>> y, y.device, y.usm_type
array([[1, 0, 0, 0],
       [0, 2, 0, 0],
       [0, 0, 3, 0],
       [0, 0, 0, 4]]), Device(opencl:cpu:0), 'device')
>>> z = np.diagflat(x0, usm_type="host")
>>> z, z.device, z.usm_type
array([[1, 0, 0, 0],
       [0, 2, 0, 0],
       [0, 0, 3, 0],
       [0, 0, 0, 4]]), Device(level_zero:gpu:0), 'host')