bundles / scipy 2.0.0.dev0+git20260907.41eeb59 / scipy / sparse / _construct / identity
function
scipy.sparse._construct:identity
source: build-install/usr/lib/python3.14/site-packages/scipy/sparse/_construct.py:573
Signature
def identity(n, dtype = d, format = None)Summary
Identity matrix in sparse format.
Extended Summary
Returns an identity matrix with shape (n, n) using a given sparse format and dtype. This differs from eye_array in that it has a square shape with ones only on the main diagonal. It is thus the multiplicative identity. eye_array allows rectangular shapes and the diagonal can be offset from the main one.
Parameters
n: intShape of the identity matrix.
dtype: dtype, optionalData type of the matrix
format: str, optionalSparse format of the result, e.g., format="csr", etc.
Returns
new_matrix: sparse matrixA square sparse matrix with ones on the main diagonal and zeros elsewhere.
Examples
import scipy as sp
✓sp.sparse.eye_array(3, dtype='int8', format='dia')
✗<DIAgonal sparse array of dtype 'int8' with 3 stored elements (1 diagonals) and shape (3, 3)>
See also
Aliases
scipy.sparse.identity