NumPy diag extracts a matrix diagonal or builds a diagonal matrix from a vector

The same function name can describe two related operations when the input has different dimensions. NumPy diag extracts entries from a two-dimensional array or places a one-dimensional array on a diagonal. Academia Master Trader presents this English lesson for readers in Somalia, using constructed arrays to explain dimensional roles, diagonal offsets and explicit copying for later edits.

A matrix input produces a diagonal vector

After import numpy as np, define m = np.array([[2,4,6],[7,9,11],[12,14,16]]). The call np.diag(m) returns [2,9,16]. These entries occupy positions where the row and column indices are equal.

The input has shape (3,3), but the extracted result has shape (3,). It contains the diagonal entries rather than their sum. Adding them would be a separate operation, and the function does not keep the other matrix entries in the returned vector.

The main diagonal runs from the upper left towards the lower right. It is not the opposite diagonal connecting the upper right to the lower left. Following row and column indices makes that distinction precise even when some values happen to be equal.

NumPy diagonal extraction

The k argument selects a parallel diagonal

The call np.diag(m,k=1) returns [4,11]. A positive offset selects a diagonal above the main diagonal. Here the selected column index is one greater than the row index.

The call np.diag(m,k=-1) returns [7,14]. A negative offset selects a diagonal below the main diagonal. The selected row index is one greater than the column index in this example.

The output length can change with the offset because a parallel diagonal can contain fewer entries. An offset describes a diagonal's position, not the number of entries to collect. Rectangular matrices can also have diagonals with lengths determined by their available rows and columns.

A vector input constructs a two-dimensional matrix

Define v = np.array([3,5,8]). The call np.diag(v) returns [[3,0,0],[0,5,0],[0,0,8]], with shape (3,3). The vector supplies the main diagonal, and the remaining positions are zero.

The call np.diag(v,k=1) constructs [[0,3,0,0],[0,0,5,0],[0,0,0,8],[0,0,0,0]], with shape (4,4). The extra space allows all three supplied entries to fit on the diagonal above the main diagonal.

Input dimensionality therefore determines whether diag selects existing entries or constructs a matrix. Read that dimensionality before interpreting the result; a vector and a single-row matrix are different inputs.

Constructing a diagonal matrix from an extracted diagonal does not recover the full original matrix. The selected entries return to their diagonal positions, but the other positions become zero. Information outside the selected diagonal was not preserved by the extraction step.

Separate extraction from editing and summation

Do not assume that assigning into an extracted diagonal will update the source matrix. Copy and view behaviour can depend on the NumPy version and extraction route. Use np.diag(m).copy() when an independent, editable diagonal vector is needed.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the input dimensionality, offset and output shape before explaining a diagonal operation.