identity

arrayfire.identity(shape: tuple[int, ...], dtype: ~arrayfire_wrapper.dtypes.Dtype = arrayfire.float32(typecode<f>)) Array

Create an identity matrix or batch of identity matrices.

Parameters

shapetuple[int, …]

The shape of the resulting identity array or batch of arrays. Must have at least 2 values.

dtypeDtype, optional, default: float32

Data type of the array.

Returns

Array

A multi-dimensional ArrayFire array where the first two dimensions form an identity matrix or batch of matrices.

Notes

The shape parameter determines the dimensions of the resulting array: - If shape is (x1, x2), the output is a 2D array of size (x1, x2). - If shape is (x1, x2, x3), the output is a 3D array of size (x1, x2, x3). - If shape is (x1, x2, x3, x4), the output is a 4D array of size (x1, x2, x3, x4).

Raises

ValueError

If shape is not a tuple or has less than two values.

Examples

>>> import arrayfire as af
>>> af.identity((3, 3))  # Create a 3x3 identity matrix
[3 3 1 1]
    1.0000     0.0000     0.0000
    0.0000     1.0000     0.0000
    0.0000     0.0000     1.0000
>>> af.identity((2, 2, 3))  # Create a batch of 3 identity 2x2 matrices
[2 2 3 1]
    1.0000     0.0000     1.0000     0.0000     1.0000     0.0000
    0.0000     1.0000     0.0000     1.0000     0.0000     1.0000