Extract a diagonal or construct a diagonal array.
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
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 in question. The default is 0. Use k>0 for diagonals
above the main diagonal, and k<0 for diagonals below the main
The extracted diagonal or constructed diagonal array.
Return specified diagonals.
Create a 2-D array with the flattened input as a diagonal.
Sum along diagonals.
Upper triangle of an array.
Lower triangle of an array.
>>> x = np.arange(9).reshape((3,3))
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
array([0, 4, 8])
>>> np.diag(x, k=1)
>>> np.diag(x, k=-1)
array([[0, 0, 0],
[0, 4, 0],
[0, 0, 8]])