MCPcopy Index your code
hub / github.com/numpy/numpy / diagonal

Function diagonal

numpy/_core/fromnumeric.py:1709–1836  ·  view source on GitHub ↗

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-

(a, offset=0, axis1=0, axis2=1)

Source from the content-addressed store, hash-verified

1707
1708@array_function_dispatch(_diagonal_dispatcher)
1709def diagonal(a, offset=0, axis1=0, axis2=1):
1710 """
1711 Return specified diagonals.
1712
1713 If `a` is 2-D, returns the diagonal of `a` with the given offset,
1714 i.e., the collection of elements of the form ``a[i, i+offset]``. If
1715 `a` has more than two dimensions, then the axes specified by `axis1`
1716 and `axis2` are used to determine the 2-D sub-array whose diagonal is
1717 returned. The shape of the resulting array can be determined by
1718 removing `axis1` and `axis2` and appending an index to the right equal
1719 to the size of the resulting diagonals.
1720
1721 In versions of NumPy prior to 1.7, this function always returned a new,
1722 independent array containing a copy of the values in the diagonal.
1723
1724 In NumPy 1.7 and 1.8, it continues to return a copy of the diagonal,
1725 but depending on this fact is deprecated. Writing to the resulting
1726 array continues to work as it used to, but a FutureWarning is issued.
1727
1728 Starting in NumPy 1.9 it returns a read-only view on the original array.
1729 Attempting to write to the resulting array will produce an error.
1730
1731 In some future release, it will return a read/write view and writing to
1732 the returned array will alter your original array. The returned array
1733 will have the same type as the input array.
1734
1735 If you don't write to the array returned by this function, then you can
1736 just ignore all of the above.
1737
1738 If you depend on the current behavior, then we suggest copying the
1739 returned array explicitly, i.e., use ``np.diagonal(a).copy()`` instead
1740 of just ``np.diagonal(a)``. This will work with both past and future
1741 versions of NumPy.
1742
1743 Parameters
1744 ----------
1745 a : array_like
1746 Array from which the diagonals are taken.
1747 offset : int, optional
1748 Offset of the diagonal from the main diagonal. Can be positive or
1749 negative. Defaults to main diagonal (0).
1750 axis1 : int, optional
1751 Axis to be used as the first axis of the 2-D sub-arrays from which
1752 the diagonals should be taken. Defaults to first axis (0).
1753 axis2 : int, optional
1754 Axis to be used as the second axis of the 2-D sub-arrays from
1755 which the diagonals should be taken. Defaults to second axis (1).
1756
1757 Returns
1758 -------
1759 array_of_diagonals : ndarray
1760 If `a` is 2-D, then a 1-D array containing the diagonal and of the
1761 same type as `a` is returned unless `a` is a `matrix`, in which case
1762 a 1-D array rather than a (2-D) `matrix` is returned in order to
1763 maintain backward compatibility.
1764
1765 If ``a.ndim > 2``, then the dimensions specified by `axis1` and `axis2`
1766 are removed, and a new axis inserted at the end corresponding to the

Callers 1

diagFunction · 0.90

Calls 2

asanyarrayFunction · 0.85
asarrayFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…