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

The same NumPy function can perform two different structural operations depending on the number of dimensions in its input. A matrix supplies a diagonal to extract, while a vector supplies entries for a new diagonal matrix. Academia Master Trader presents this English lesson for readers in the United Arab Emirates, using constructed integers to explain both directions, the sign of an offset and the difference between extracting entries and summing them.

Read the main diagonal of a matrix

After import numpy as np, define m = np.array([[2,4,6],[8,10,12],[14,16,18]]). It has shape (3,3). The call np.diag(m) returns [2,10,18], a one dimensional result with shape (3,).

These entries occupy positions where the row and column indices are equal: (0,0), (1,1) and (2,2). The default offset is zero, which identifies the main diagonal. The function returns the entries in sequence. It does not add them into a single total or select every entry from the upper triangle.

NumPy diagonal extraction

Positive offsets select diagonals above the main one

The call np.diag(m,k=1) returns [4,12], with shape (2,). Its positions are (0,1) and (1,2). The column index exceeds the row index by one.

The call np.diag(m,k=-1) returns [8,16], also with shape (2,). Here the positions are (1,0) and (2,1), below the main diagonal. The sign of k selects the side, while its magnitude specifies the distance from the main diagonal. Offsetting the diagonal can change the length of the extracted sequence.

A vector constructs a new two dimensional arrangement

Define v = np.array([3,7]). The call np.diag(v) returns [[3,0],[0,7]], with shape (2,2). The supplied entries occupy the main diagonal, and the other positions contain zero.

With np.diag(v,k=1), the result is [[0,3,0],[0,0,7],[0,0,0]], with shape (3,3). The supplied entries move to the diagonal immediately above the main one. The output needs an additional row and column to accommodate that offset. This construction is different from extracting a shorter diagonal from an existing matrix.

Extraction followed by construction does not restore every entry

The call np.diag(np.diag(m)) returns [[2,0,0],[0,10,0],[0,0,18]]. It preserves the original main diagonal but does not reconstruct the off diagonal entries four, six, eight, twelve, fourteen and sixteen.

The inner call extracts only the main diagonal. The outer call constructs a new matrix from that extracted vector, filling the other positions with zeros. Read nested operations from the inside out, and state what information each step retains. Equal output shape does not imply recovery of the original matrix.

Keep extraction separate from totals and editing

The main diagonal entries in this example sum to thirty, but np.diag(m) returns the three entries rather than that total. NumPy documents trace as the operation for summing along diagonals. Choose the operation that matches the requested result.

For editing an extracted diagonal, consult the documentation for the installed NumPy version. The diag reference warns that copy or view behavior can depend on version. Do not assume that writing to an extracted result is supported or changes the source. Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the input dimensionality and offset before interpreting the output.