diagonal
Extract diagonal from a COO array. The equivalent of numpy.diagonal
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
COO
|
The array to perform the operation on. |
required |
offset
|
int
|
Offset of the diagonal from the main diagonal. Defaults to main diagonal (0). |
0
|
axis1
|
int
|
First axis from which the diagonals should be taken. Defaults to first axis (0). |
0
|
axis2
|
int
|
Second axis from which the diagonals should be taken. Defaults to second axis (1). |
1
|
Examples:
>>> import sparse
>>> x = sparse.as_coo(np.arange(9).reshape(3, 3))
>>> sparse.diagonal(x).todense()
array([0, 4, 8])
>>> sparse.diagonal(x, offset=1).todense()
array([1, 5])
>>> x = sparse.as_coo(np.arange(12).reshape((2, 3, 2)))
>>> x_diag = sparse.diagonal(x, axis1=0, axis2=2)
>>> x_diag.shape
(3, 2)
>>> x_diag.todense()
array([[ 0, 7],
[ 2, 9],
[ 4, 11]])
Returns:
Name | Type | Description |
---|---|---|
out |
COO
|
The result of the operation. |
Raises:
Type | Description |
---|---|
ValueError
|
If a.shape[axis1] != a.shape[axis2] |
See Also
numpy.diagonal
: NumPy equivalent function
Source code in sparse/numba_backend/_coo/common.py
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 |
|