Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.9/dist-packages/scipy/sparse/_extract.py: 26%
27 statements
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-14 06:37 +0000
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-14 06:37 +0000
1"""Functions to extract parts of sparse matrices
2"""
4__docformat__ = "restructuredtext en"
6__all__ = ['find', 'tril', 'triu']
9from ._coo import coo_matrix, coo_array
10from ._base import sparray
13def find(A):
14 """Return the indices and values of the nonzero elements of a matrix
16 Parameters
17 ----------
18 A : dense or sparse array or matrix
19 Matrix whose nonzero elements are desired.
21 Returns
22 -------
23 (I,J,V) : tuple of arrays
24 I,J, and V contain the row indices, column indices, and values
25 of the nonzero entries.
28 Examples
29 --------
30 >>> from scipy.sparse import csr_array, find
31 >>> A = csr_array([[7.0, 8.0, 0],[0, 0, 9.0]])
32 >>> find(A)
33 (array([0, 0, 1], dtype=int32),
34 array([0, 1, 2], dtype=int32),
35 array([ 7., 8., 9.]))
37 """
39 A = coo_array(A, copy=True)
40 A.sum_duplicates()
41 # remove explicit zeros
42 nz_mask = A.data != 0
43 return A.row[nz_mask], A.col[nz_mask], A.data[nz_mask]
46def tril(A, k=0, format=None):
47 """Return the lower triangular portion of a sparse array or matrix
49 Returns the elements on or below the k-th diagonal of A.
50 - k = 0 corresponds to the main diagonal
51 - k > 0 is above the main diagonal
52 - k < 0 is below the main diagonal
54 Parameters
55 ----------
56 A : dense or sparse array or matrix
57 Matrix whose lower trianglar portion is desired.
58 k : integer : optional
59 The top-most diagonal of the lower triangle.
60 format : string
61 Sparse format of the result, e.g. format="csr", etc.
63 Returns
64 -------
65 L : sparse matrix
66 Lower triangular portion of A in sparse format.
68 See Also
69 --------
70 triu : upper triangle in sparse format
72 Examples
73 --------
74 >>> from scipy.sparse import csr_array, tril
75 >>> A = csr_array([[1, 2, 0, 0, 3], [4, 5, 0, 6, 7], [0, 0, 8, 9, 0]],
76 ... dtype='int32')
77 >>> A.toarray()
78 array([[1, 2, 0, 0, 3],
79 [4, 5, 0, 6, 7],
80 [0, 0, 8, 9, 0]])
81 >>> tril(A).toarray()
82 array([[1, 0, 0, 0, 0],
83 [4, 5, 0, 0, 0],
84 [0, 0, 8, 0, 0]])
85 >>> tril(A).nnz
86 4
87 >>> tril(A, k=1).toarray()
88 array([[1, 2, 0, 0, 0],
89 [4, 5, 0, 0, 0],
90 [0, 0, 8, 9, 0]])
91 >>> tril(A, k=-1).toarray()
92 array([[0, 0, 0, 0, 0],
93 [4, 0, 0, 0, 0],
94 [0, 0, 0, 0, 0]])
95 >>> tril(A, format='csc')
96 <3x5 sparse array of type '<class 'numpy.int32'>'
97 with 4 stored elements in Compressed Sparse Column format>
99 """
100 coo_sparse = coo_array if isinstance(A, sparray) else coo_matrix
102 # convert to COOrdinate format where things are easy
103 A = coo_sparse(A, copy=False)
104 mask = A.row + k >= A.col
106 row = A.row[mask]
107 col = A.col[mask]
108 data = A.data[mask]
109 new_coo = coo_sparse((data, (row, col)), shape=A.shape, dtype=A.dtype)
110 return new_coo.asformat(format)
113def triu(A, k=0, format=None):
114 """Return the upper triangular portion of a sparse array or matrix
116 Returns the elements on or above the k-th diagonal of A.
117 - k = 0 corresponds to the main diagonal
118 - k > 0 is above the main diagonal
119 - k < 0 is below the main diagonal
121 Parameters
122 ----------
123 A : dense or sparse array or matrix
124 Matrix whose upper trianglar portion is desired.
125 k : integer : optional
126 The bottom-most diagonal of the upper triangle.
127 format : string
128 Sparse format of the result, e.g. format="csr", etc.
130 Returns
131 -------
132 L : sparse array or matrix
133 Upper triangular portion of A in sparse format.
134 Sparse array if A is a sparse array, otherwise matrix.
136 See Also
137 --------
138 tril : lower triangle in sparse format
140 Examples
141 --------
142 >>> from scipy.sparse import csr_array, triu
143 >>> A = csr_array([[1, 2, 0, 0, 3], [4, 5, 0, 6, 7], [0, 0, 8, 9, 0]],
144 ... dtype='int32')
145 >>> A.toarray()
146 array([[1, 2, 0, 0, 3],
147 [4, 5, 0, 6, 7],
148 [0, 0, 8, 9, 0]])
149 >>> triu(A).toarray()
150 array([[1, 2, 0, 0, 3],
151 [0, 5, 0, 6, 7],
152 [0, 0, 8, 9, 0]])
153 >>> triu(A).nnz
154 8
155 >>> triu(A, k=1).toarray()
156 array([[0, 2, 0, 0, 3],
157 [0, 0, 0, 6, 7],
158 [0, 0, 0, 9, 0]])
159 >>> triu(A, k=-1).toarray()
160 array([[1, 2, 0, 0, 3],
161 [4, 5, 0, 6, 7],
162 [0, 0, 8, 9, 0]])
163 >>> triu(A, format='csc')
164 <3x5 sparse array of type '<class 'numpy.int32'>'
165 with 8 stored elements in Compressed Sparse Column format>
167 """
168 coo_sparse = coo_array if isinstance(A, sparray) else coo_matrix
170 # convert to COOrdinate format where things are easy
171 A = coo_sparse(A, copy=False)
172 mask = A.row + k <= A.col
174 row = A.row[mask]
175 col = A.col[mask]
176 data = A.data[mask]
177 new_coo = coo_sparse((data, (row, col)), shape=A.shape, dtype=A.dtype)
178 return new_coo.asformat(format)