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