eye

eye(n_rows: int, n_cols: int | None = None, /, *, k: int = 0, dtype: dtype | None = None, device: device | None = None) array

Returns a two-dimensional array with ones on the kth diagonal and zeros elsewhere.

Note

An output array having a complex floating-point data type must have the value 1 + 0j along the kth diagonal and 0 + 0j elsewhere.

Parameters:
  • n_rows (int) – number of rows in the output array.

  • n_cols (Optional[int]) – number of columns in the output array. If None, the default number of columns in the output array is equal to n_rows. Default: None.

  • k (int) – index of the diagonal. A positive value refers to an upper diagonal, a negative value to a lower diagonal, and 0 to the main diagonal. Default: 0.

  • dtype (Optional[dtype]) – output array data type. If dtype is None, the output array data type must be the default real-valued floating-point data type. Default: None.

  • device (Optional[device]) – device on which to place the created array. Default: None.

Returns:

out (array) – an array where all elements are equal to zero, except for the kth diagonal, whose values are equal to one.

Notes

Changed in version 2022.12: Added complex data type support.