nums.numpy.diag

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

Extract a diagonal or construct a diagonal array.

This docstring was copied from numpy.diag.

Some inconsistencies with the NumS version may exist.

See the more detailed documentation for numpy.diagonal if you use this function to extract a diagonal and wish to write to the resulting array; whether it returns a copy or a view depends on what version of numpy you are using.

Parameters
  • v (BlockArray) – 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.

  • k (int, 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

out – The extracted diagonal or constructed diagonal array.

Return type

BlockArray

See also

diagonal

Return specified diagonals.

trace

Sum along diagonals.

triu

Upper triangle of an array.

Notes

offset != 0 is currently not supported.

out is currently not supported.

axis1 != 0 or axis2 != 1 is currently not supported.

Examples

The doctests shown below are copied from NumPy. They won’t show the correct result until you operate get().

>>> x = nps.arange(9).reshape((3,3))  
>>> x.get()  
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>>> nps.diag(x).get()  
array([0, 4, 8])
>>> nps.diag(nps.diag(x)).get()  
array([[0, 0, 0],
       [0, 4, 0],
       [0, 0, 8]])