Dealing with the main diagonal / triangles of a matrix

Top

Questions to David Rotermund

This is an optional topic!

numpy.fill_diagonal

numpy.fill_diagonal(a, val, wrap=False)

Fill the main diagonal of the given array of any dimensionality.

For an array a with a.ndim >= 2, the diagonal is the list of locations with indices a[i, …, i] all identical. This function modifies the input array in-place, it does not return a value.

numpy.diag_indices

numpy.diag_indices(n, ndim=2)

Return the indices to access the main diagonal of an array.

This returns a tuple of indices that can be used to access the main diagonal of an array a with a.ndim >= 2 dimensions and shape (n, n, …, n). For a.ndim = 2 this is the usual diagonal, for a.ndim > 2 this is the set of indices to access a[i, i, …, i] for i = [0..n-1].

numpy.diag_indices_from

numpy.diag_indices_from(arr)

Return the indices to access the main diagonal of an n-dimensional array.

numpy.diag

numpy.diag(v, k=0)

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 are using.

numpy.diagonal

numpy.diagonal(a, offset=0, axis1=0, axis2=1)

Return specified diagonals.

If a is 2-D, returns the diagonal of a with the given offset, i.e., the collection of elements of the form a[i, i+offset]. If a has more than two dimensions, then the axes specified by axis1 and axis2 are used to determine the 2-D sub-array whose diagonal is returned. The shape of the resulting array can be determined by removing axis1 and axis2 and appending an index to the right equal to the size of the resulting diagonals.

numpy.tril_indices_from

numpy.tril_indices_from(arr, k=0)

Return the indices for the lower-triangle of arr.

numpy.tril_indices

numpy.tril_indices(n, k=0, m=None)

Return the indices for the lower-triangle of an (n, m) array.

numpy.triu_indices_from

numpy.triu_indices_from(arr, k=0)

Return the indices for the upper-triangle of arr.

numpy.triu_indices

numpy.triu_indices(n, k=0, m=None)

Return the indices for the upper-triangle of an (n, m) array.

The source code is Open Source and can be found on GitHub.