mars.tensor.eye#

mars.tensor.eye(N, M=None, k=0, dtype=None, sparse=False, gpu=None, chunk_size=None, order='C')[source]#

Return a 2-D tensor with ones on the diagonal and zeros elsewhere.

Parameters
  • N (int) – Number of rows in the output.

  • M (int, optional) – Number of columns in the output. If None, defaults to N.

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

  • dtype (data-type, optional) – Data-type of the returned tensor.

  • sparse (bool, optional) – Create sparse tensor if True, False as default

  • gpu (bool, optional) – Allocate the tensor on GPU if True, False as default

  • chunk_size (int or tuple of int or tuple of ints, optional) – Desired chunk size on each dimension

  • order ({'C', 'F'}, optional) – Whether the output should be stored in row-major (C-style) or column-major (Fortran-style) order in memory.

Returns

I – An tensor where all elements are equal to zero, except for the k-th diagonal, whose values are equal to one.

Return type

Tensor of shape (N,M)

See also

identity

(almost) equivalent function

diag

diagonal 2-D tensor from a 1-D tensor specified by the user.

Examples

>>> import mars.tensor as mt
>>> mt.eye(2, dtype=int).execute()
array([[1, 0],
       [0, 1]])
>>> mt.eye(3, k=1).execute()
array([[ 0.,  1.,  0.],
       [ 0.,  0.,  1.],
       [ 0.,  0.,  0.]])