nlcpy.diag

nlcpy.diag(v, k=0)[source]

Extracts a diagonal or constructs a diagonal array.

Parameters
varray_like

If v is a 2-D array, return a copy of its k-th diagonal. If v is a 1-D array, return a 2-D array with v on the k-th diagonal.

kint, optional

Diagonal in question. The default is 0. Use k>0 for diagonals above the main diagonal, and k<0 for diagonals below the main diagonal.

Returns
outndarray

The extracted diagonal or constructed diagonal array.

See also

diagonal

Returns specified diagonals.

diagflat

Creates a two-dimensional array with the flattened input as a diagonal.

Examples

>>> import nlcpy as vp
>>> x = vp.arange(9).reshape((3,3))
>>> x
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>>> vp.diag(x)
array([0, 4, 8])
>>> vp.diag(x, k=1)
array([1, 5])
>>> vp.diag(x, k=-1)
array([3, 7])
>>> vp.diag(vp.diag(x))
array([[0, 0, 0],
       [0, 4, 0],
       [0, 0, 8]])