Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.9/dist-packages/scipy/sparse/_csc.py: 37%

87 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-03-22 06:44 +0000

1"""Compressed Sparse Column matrix format""" 

2__docformat__ = "restructuredtext en" 

3 

4__all__ = ['csc_array', 'csc_matrix', 'isspmatrix_csc'] 

5 

6 

7import numpy as np 

8 

9from ._matrix import spmatrix 

10from ._base import _spbase, sparray 

11from ._sparsetools import csc_tocsr, expandptr 

12from ._sputils import upcast 

13 

14from ._compressed import _cs_matrix 

15 

16 

17class _csc_base(_cs_matrix): 

18 _format = 'csc' 

19 

20 def transpose(self, axes=None, copy=False): 

21 if axes is not None and axes != (1, 0): 

22 raise ValueError("Sparse arrays/matrices do not support " 

23 "an 'axes' parameter because swapping " 

24 "dimensions is the only logical permutation.") 

25 

26 M, N = self.shape 

27 

28 return self._csr_container((self.data, self.indices, 

29 self.indptr), (N, M), copy=copy) 

30 

31 transpose.__doc__ = _spbase.transpose.__doc__ 

32 

33 def __iter__(self): 

34 yield from self.tocsr() 

35 

36 def tocsc(self, copy=False): 

37 if copy: 

38 return self.copy() 

39 else: 

40 return self 

41 

42 tocsc.__doc__ = _spbase.tocsc.__doc__ 

43 

44 def tocsr(self, copy=False): 

45 M,N = self.shape 

46 idx_dtype = self._get_index_dtype((self.indptr, self.indices), 

47 maxval=max(self.nnz, N)) 

48 indptr = np.empty(M + 1, dtype=idx_dtype) 

49 indices = np.empty(self.nnz, dtype=idx_dtype) 

50 data = np.empty(self.nnz, dtype=upcast(self.dtype)) 

51 

52 csc_tocsr(M, N, 

53 self.indptr.astype(idx_dtype), 

54 self.indices.astype(idx_dtype), 

55 self.data, 

56 indptr, 

57 indices, 

58 data) 

59 

60 A = self._csr_container( 

61 (data, indices, indptr), 

62 shape=self.shape, copy=False 

63 ) 

64 A.has_sorted_indices = True 

65 return A 

66 

67 tocsr.__doc__ = _spbase.tocsr.__doc__ 

68 

69 def nonzero(self): 

70 # CSC can't use _cs_matrix's .nonzero method because it 

71 # returns the indices sorted for self transposed. 

72 

73 # Get row and col indices, from _cs_matrix.tocoo 

74 major_dim, minor_dim = self._swap(self.shape) 

75 minor_indices = self.indices 

76 major_indices = np.empty(len(minor_indices), dtype=self.indices.dtype) 

77 expandptr(major_dim, self.indptr, major_indices) 

78 row, col = self._swap((major_indices, minor_indices)) 

79 

80 # Remove explicit zeros 

81 nz_mask = self.data != 0 

82 row = row[nz_mask] 

83 col = col[nz_mask] 

84 

85 # Sort them to be in C-style order 

86 ind = np.argsort(row, kind='mergesort') 

87 row = row[ind] 

88 col = col[ind] 

89 

90 return row, col 

91 

92 nonzero.__doc__ = _cs_matrix.nonzero.__doc__ 

93 

94 def _getrow(self, i): 

95 """Returns a copy of row i of the matrix, as a (1 x n) 

96 CSR matrix (row vector). 

97 """ 

98 M, N = self.shape 

99 i = int(i) 

100 if i < 0: 

101 i += M 

102 if i < 0 or i >= M: 

103 raise IndexError('index (%d) out of range' % i) 

104 return self._get_submatrix(minor=i).tocsr() 

105 

106 def _getcol(self, i): 

107 """Returns a copy of column i of the matrix, as a (m x 1) 

108 CSC matrix (column vector). 

109 """ 

110 M, N = self.shape 

111 i = int(i) 

112 if i < 0: 

113 i += N 

114 if i < 0 or i >= N: 

115 raise IndexError('index (%d) out of range' % i) 

116 return self._get_submatrix(major=i, copy=True) 

117 

118 def _get_intXarray(self, row, col): 

119 return self._major_index_fancy(col)._get_submatrix(minor=row) 

120 

121 def _get_intXslice(self, row, col): 

122 if col.step in (1, None): 

123 return self._get_submatrix(major=col, minor=row, copy=True) 

124 return self._major_slice(col)._get_submatrix(minor=row) 

125 

126 def _get_sliceXint(self, row, col): 

127 if row.step in (1, None): 

128 return self._get_submatrix(major=col, minor=row, copy=True) 

129 return self._get_submatrix(major=col)._minor_slice(row) 

130 

131 def _get_sliceXarray(self, row, col): 

132 return self._major_index_fancy(col)._minor_slice(row) 

133 

134 def _get_arrayXint(self, row, col): 

135 return self._get_submatrix(major=col)._minor_index_fancy(row) 

136 

137 def _get_arrayXslice(self, row, col): 

138 return self._major_slice(col)._minor_index_fancy(row) 

139 

140 # these functions are used by the parent class (_cs_matrix) 

141 # to remove redundancy between csc_array and csr_matrix 

142 @staticmethod 

143 def _swap(x): 

144 """swap the members of x if this is a column-oriented matrix 

145 """ 

146 return x[1], x[0] 

147 

148 

149def isspmatrix_csc(x): 

150 """Is `x` of csc_matrix type? 

151 

152 Parameters 

153 ---------- 

154 x 

155 object to check for being a csc matrix 

156 

157 Returns 

158 ------- 

159 bool 

160 True if `x` is a csc matrix, False otherwise 

161 

162 Examples 

163 -------- 

164 >>> from scipy.sparse import csc_array, csc_matrix, coo_matrix, isspmatrix_csc 

165 >>> isspmatrix_csc(csc_matrix([[5]])) 

166 True 

167 >>> isspmatrix_csc(csc_array([[5]])) 

168 False 

169 >>> isspmatrix_csc(coo_matrix([[5]])) 

170 False 

171 """ 

172 return isinstance(x, csc_matrix) 

173 

174 

175# This namespace class separates array from matrix with isinstance 

176class csc_array(_csc_base, sparray): 

177 """ 

178 Compressed Sparse Column array. 

179 

180 This can be instantiated in several ways: 

181 csc_array(D) 

182 where D is a 2-D ndarray 

183 

184 csc_array(S) 

185 with another sparse array or matrix S (equivalent to S.tocsc()) 

186 

187 csc_array((M, N), [dtype]) 

188 to construct an empty array with shape (M, N) 

189 dtype is optional, defaulting to dtype='d'. 

190 

191 csc_array((data, (row_ind, col_ind)), [shape=(M, N)]) 

192 where ``data``, ``row_ind`` and ``col_ind`` satisfy the 

193 relationship ``a[row_ind[k], col_ind[k]] = data[k]``. 

194 

195 csc_array((data, indices, indptr), [shape=(M, N)]) 

196 is the standard CSC representation where the row indices for 

197 column i are stored in ``indices[indptr[i]:indptr[i+1]]`` 

198 and their corresponding values are stored in 

199 ``data[indptr[i]:indptr[i+1]]``. If the shape parameter is 

200 not supplied, the array dimensions are inferred from 

201 the index arrays. 

202 

203 Attributes 

204 ---------- 

205 dtype : dtype 

206 Data type of the array 

207 shape : 2-tuple 

208 Shape of the array 

209 ndim : int 

210 Number of dimensions (this is always 2) 

211 nnz 

212 size 

213 data 

214 CSC format data array of the array 

215 indices 

216 CSC format index array of the array 

217 indptr 

218 CSC format index pointer array of the array 

219 has_sorted_indices 

220 has_canonical_format 

221 T 

222 

223 Notes 

224 ----- 

225 

226 Sparse arrays can be used in arithmetic operations: they support 

227 addition, subtraction, multiplication, division, and matrix power. 

228 

229 Advantages of the CSC format 

230 - efficient arithmetic operations CSC + CSC, CSC * CSC, etc. 

231 - efficient column slicing 

232 - fast matrix vector products (CSR, BSR may be faster) 

233 

234 Disadvantages of the CSC format 

235 - slow row slicing operations (consider CSR) 

236 - changes to the sparsity structure are expensive (consider LIL or DOK) 

237 

238 Canonical format 

239 - Within each column, indices are sorted by row. 

240 - There are no duplicate entries. 

241 

242 Examples 

243 -------- 

244 

245 >>> import numpy as np 

246 >>> from scipy.sparse import csc_array 

247 >>> csc_array((3, 4), dtype=np.int8).toarray() 

248 array([[0, 0, 0, 0], 

249 [0, 0, 0, 0], 

250 [0, 0, 0, 0]], dtype=int8) 

251 

252 >>> row = np.array([0, 2, 2, 0, 1, 2]) 

253 >>> col = np.array([0, 0, 1, 2, 2, 2]) 

254 >>> data = np.array([1, 2, 3, 4, 5, 6]) 

255 >>> csc_array((data, (row, col)), shape=(3, 3)).toarray() 

256 array([[1, 0, 4], 

257 [0, 0, 5], 

258 [2, 3, 6]]) 

259 

260 >>> indptr = np.array([0, 2, 3, 6]) 

261 >>> indices = np.array([0, 2, 2, 0, 1, 2]) 

262 >>> data = np.array([1, 2, 3, 4, 5, 6]) 

263 >>> csc_array((data, indices, indptr), shape=(3, 3)).toarray() 

264 array([[1, 0, 4], 

265 [0, 0, 5], 

266 [2, 3, 6]]) 

267 

268 """ 

269 

270 

271class csc_matrix(spmatrix, _csc_base): 

272 """ 

273 Compressed Sparse Column matrix. 

274 

275 This can be instantiated in several ways: 

276 csc_matrix(D) 

277 where D is a 2-D ndarray 

278 

279 csc_matrix(S) 

280 with another sparse array or matrix S (equivalent to S.tocsc()) 

281 

282 csc_matrix((M, N), [dtype]) 

283 to construct an empty matrix with shape (M, N) 

284 dtype is optional, defaulting to dtype='d'. 

285 

286 csc_matrix((data, (row_ind, col_ind)), [shape=(M, N)]) 

287 where ``data``, ``row_ind`` and ``col_ind`` satisfy the 

288 relationship ``a[row_ind[k], col_ind[k]] = data[k]``. 

289 

290 csc_matrix((data, indices, indptr), [shape=(M, N)]) 

291 is the standard CSC representation where the row indices for 

292 column i are stored in ``indices[indptr[i]:indptr[i+1]]`` 

293 and their corresponding values are stored in 

294 ``data[indptr[i]:indptr[i+1]]``. If the shape parameter is 

295 not supplied, the matrix dimensions are inferred from 

296 the index arrays. 

297 

298 Attributes 

299 ---------- 

300 dtype : dtype 

301 Data type of the matrix 

302 shape : 2-tuple 

303 Shape of the matrix 

304 ndim : int 

305 Number of dimensions (this is always 2) 

306 nnz 

307 size 

308 data 

309 CSC format data array of the matrix 

310 indices 

311 CSC format index array of the matrix 

312 indptr 

313 CSC format index pointer array of the matrix 

314 has_sorted_indices 

315 has_canonical_format 

316 T 

317 

318 Notes 

319 ----- 

320 

321 Sparse matrices can be used in arithmetic operations: they support 

322 addition, subtraction, multiplication, division, and matrix power. 

323 

324 Advantages of the CSC format 

325 - efficient arithmetic operations CSC + CSC, CSC * CSC, etc. 

326 - efficient column slicing 

327 - fast matrix vector products (CSR, BSR may be faster) 

328 

329 Disadvantages of the CSC format 

330 - slow row slicing operations (consider CSR) 

331 - changes to the sparsity structure are expensive (consider LIL or DOK) 

332 

333 Canonical format 

334 - Within each column, indices are sorted by row. 

335 - There are no duplicate entries. 

336 

337 Examples 

338 -------- 

339 

340 >>> import numpy as np 

341 >>> from scipy.sparse import csc_matrix 

342 >>> csc_matrix((3, 4), dtype=np.int8).toarray() 

343 array([[0, 0, 0, 0], 

344 [0, 0, 0, 0], 

345 [0, 0, 0, 0]], dtype=int8) 

346 

347 >>> row = np.array([0, 2, 2, 0, 1, 2]) 

348 >>> col = np.array([0, 0, 1, 2, 2, 2]) 

349 >>> data = np.array([1, 2, 3, 4, 5, 6]) 

350 >>> csc_matrix((data, (row, col)), shape=(3, 3)).toarray() 

351 array([[1, 0, 4], 

352 [0, 0, 5], 

353 [2, 3, 6]]) 

354 

355 >>> indptr = np.array([0, 2, 3, 6]) 

356 >>> indices = np.array([0, 2, 2, 0, 1, 2]) 

357 >>> data = np.array([1, 2, 3, 4, 5, 6]) 

358 >>> csc_matrix((data, indices, indptr), shape=(3, 3)).toarray() 

359 array([[1, 0, 4], 

360 [0, 0, 5], 

361 [2, 3, 6]]) 

362 

363 """ 

364