Eye / Identity

Shipping
array_eye

numpy eye(n, m, k): an n×m matrix with ones on the k-th diagonal (identity when square)

Signature

Outputs

  • matrixMatrixAn n×m matrix with ones on the k-th diagonal and zeros elsewhere; the identity matrix when m = n and k = 0.

Parameters

KeyTypeDefaultNotes
nint3Number of rows.
mint0Number of columns; 0 means square (= rows), giving the identity matrix.
kint0Index of the diagonal of ones: 0 = main, >0 above, <0 below.

Description

Eye is numpy.eye(n, m, k): an matrix with ones on the -th diagonal and zeros everywhere else. With the default (a sentinel meaning "square", so ) and it is the identity matrix .

The Diagonal (k) parameter shifts the band of ones: moves it above the main diagonal, below. Off-diagonal placements build shift/companion-style skeletons. Unlike the other builders, Eye's output is always rank-2 (a Matrix) and carries no unit. It is a pure source: no inputs, output depends only on , , .

Mathematics

Examples

The 3×3 identity

Leave Rows = 3, Columns = 0, Diagonal = 0. Because Columns = 0 means square, you get .

A super-diagonal shift

Set Rows = 4, Columns = 4, Diagonal = 1. Ones sit one place above the main diagonal — a nilpotent up-shift matrix.

Applications

  • The identity for a linear-algebra pipeline (solve, inverse, eigen — as $\mathbf{A}\mathbf{x}=\mathbf{I}$ probes or regularizers).
  • Selecting/permuting rows and columns via a shifted-diagonal matrix.
  • Building finite-difference or shift operators from off-diagonal bands.

Neat

Columns uses 0 as a 'square' sentinel — a common friendly-default trick: leave it blank and you get the identity without repeating the row count.

The fill loop is a single pass: for each row i it sets column i+k to 1 only when that column is in range, so out-of-band k values simply yield an all-zero matrix instead of erroring.

Known issues

The output is always a rank-2 Matrix and never carries a physical unit.

A k with magnitude ≥ the matrix width produces an all-zeros matrix (no diagonal falls inside the bounds).

See also

arraymatrixsourcenumpyidentitydiagonallinear-algebrastateless