array_api_extra.create_diagonal

array_api_extra.create_diagonal(x, /, *, offset=0, xp)

Construct a diagonal array.

Parameters:
  • x (array) – A 1-D array

  • offset (int, optional) – Offset from the leading diagonal (default is 0). Use positive ints for diagonals above the leading diagonal, and negative ints for diagonals below the leading diagonal.

  • xp (array_namespace) – The standard-compatible namespace for x.

Returns:

res – A 2-D array with x on the diagonal (offset by offset).

Return type:

array

Examples

>>> import array_api_strict as xp
>>> import array_api_extra as xpx
>>> x = xp.asarray([2, 4, 8])
>>> xpx.create_diagonal(x, xp=xp)
Array([[2, 0, 0],
       [0, 4, 0],
       [0, 0, 8]], dtype=array_api_strict.int64)
>>> xpx.create_diagonal(x, offset=-2, xp=xp)
Array([[0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [2, 0, 0, 0, 0],
       [0, 4, 0, 0, 0],
       [0, 0, 8, 0, 0]], dtype=array_api_strict.int64)