Skip to contents

Creates an n x n identity matrix.

nv_eye_like() is a variant where dtype and device default to those of like.

Usage

nv_eye(n, dtype = NULL, device = NULL)

nv_eye_like(like, n, dtype = NULL, device = NULL)

Arguments

n

(integer(1))
Size of the identity matrix.

dtype

(NULL | character(1) | DataType)
Data type of the result. Can be any data type; NULL (default) uses the default float data type (see default_dtypes()). For nv_eye_like(), NULL uses dtype(like).

device

(NULL | character(1) | device)
The device the data lives on, given either as:

  • a device string naming the platform (e.g. "cpu", "cuda", "cuda:<n>"), which is resolved against the backend in use, or

  • a device object as returned by nv_device(): a PJRTDevice for the "pjrt" backend or a quickr_device for the "quickr" backend. Because a device object is backend-specific, it also determines the backend.

The default (NULL) uses default_device().

like

(AnvlArray)
Existing array whose attributes are used as defaults (only for nv_eye_like()).

Value

(arrayish)
Has the given dtype and shape (n, n): ones on the diagonal, zeros elsewhere.

See also

nv_diag() for general diagonal matrices.

Examples

# a 3x3 identity matrix
nv_eye(3L)
#> AnvlArray
#>  1 0 0
#>  0 1 0
#>  0 0 1
#> [ CPUf32{3,3} ] 

# `_like` takes the data type and device from an existing array
x <- nv_fill(0, shape = c(3, 3), dtype = "f64")
nv_eye_like(x, 3L)
#> AnvlArray
#>  1 0 0
#>  0 1 0
#>  0 0 1
#> [ CPUf64{3,3} ]