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

20 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-09-23 06:43 +0000

1""" 

2===================================== 

3Sparse matrices (:mod:`scipy.sparse`) 

4===================================== 

5 

6.. currentmodule:: scipy.sparse 

7 

8.. toctree:: 

9 :hidden: 

10 

11 sparse.csgraph 

12 sparse.linalg 

13 

14SciPy 2-D sparse array package for numeric data. 

15 

16.. note:: 

17 

18 This package is switching to an array interface, compatible with 

19 NumPy arrays, from the older matrix interface. We recommend that 

20 you use the array objects (`bsr_array`, `coo_array`, etc.) for 

21 all new work. 

22 

23 When using the array interface, please note that: 

24 

25 - ``x * y`` no longer performs matrix multiplication, but 

26 element-wise multiplication (just like with NumPy arrays). To 

27 make code work with both arrays and matrices, use ``x @ y`` for 

28 matrix multiplication. 

29 - Operations such as `sum`, that used to produce dense matrices, now 

30 produce arrays, whose multiplication behavior differs similarly. 

31 - Sparse arrays currently must be two-dimensional. This also means 

32 that all *slicing* operations on these objects must produce 

33 two-dimensional results, or they will result in an error. This 

34 will be addressed in a future version. 

35 

36 The construction utilities (`eye`, `kron`, `random`, `diags`, etc.) 

37 have not yet been ported, but their results can be wrapped into arrays:: 

38 

39 A = csr_array(eye(3)) 

40 

41Contents 

42======== 

43 

44Sparse array classes 

45-------------------- 

46 

47.. autosummary:: 

48 :toctree: generated/ 

49 

50 bsr_array - Block Sparse Row array 

51 coo_array - A sparse array in COOrdinate format 

52 csc_array - Compressed Sparse Column array 

53 csr_array - Compressed Sparse Row array 

54 dia_array - Sparse array with DIAgonal storage 

55 dok_array - Dictionary Of Keys based sparse array 

56 lil_array - Row-based list of lists sparse array 

57 sparray - Sparse array base class 

58 

59Sparse matrix classes 

60--------------------- 

61 

62.. autosummary:: 

63 :toctree: generated/ 

64 

65 bsr_matrix - Block Sparse Row matrix 

66 coo_matrix - A sparse matrix in COOrdinate format 

67 csc_matrix - Compressed Sparse Column matrix 

68 csr_matrix - Compressed Sparse Row matrix 

69 dia_matrix - Sparse matrix with DIAgonal storage 

70 dok_matrix - Dictionary Of Keys based sparse matrix 

71 lil_matrix - Row-based list of lists sparse matrix 

72 spmatrix - Sparse matrix base class 

73 

74Functions 

75--------- 

76 

77Building sparse arrays: 

78 

79.. autosummary:: 

80 :toctree: generated/ 

81 

82 diags_array - Return a sparse array from diagonals 

83 

84Building sparse matrices: 

85 

86.. autosummary:: 

87 :toctree: generated/ 

88 

89 eye - Sparse MxN matrix whose k-th diagonal is all ones 

90 identity - Identity matrix in sparse format 

91 kron - kronecker product of two sparse matrices 

92 kronsum - kronecker sum of sparse matrices 

93 diags - Return a sparse matrix from diagonals 

94 spdiags - Return a sparse matrix from diagonals 

95 block_diag - Build a block diagonal sparse matrix 

96 tril - Lower triangular portion of a matrix in sparse format 

97 triu - Upper triangular portion of a matrix in sparse format 

98 block - Build a sparse array from sub-blocks 

99 bmat - Build a sparse matrix from sub-blocks 

100 hstack - Stack sparse matrices horizontally (column wise) 

101 vstack - Stack sparse matrices vertically (row wise) 

102 rand - Random values in a given shape 

103 random - Random values in a given shape 

104 

105Save and load sparse matrices: 

106 

107.. autosummary:: 

108 :toctree: generated/ 

109 

110 save_npz - Save a sparse matrix to a file using ``.npz`` format. 

111 load_npz - Load a sparse matrix from a file using ``.npz`` format. 

112 

113Sparse matrix tools: 

114 

115.. autosummary:: 

116 :toctree: generated/ 

117 

118 find 

119 

120Identifying sparse matrices: 

121 

122.. autosummary:: 

123 :toctree: generated/ 

124 

125 issparse 

126 isspmatrix 

127 isspmatrix_csc 

128 isspmatrix_csr 

129 isspmatrix_bsr 

130 isspmatrix_lil 

131 isspmatrix_dok 

132 isspmatrix_coo 

133 isspmatrix_dia 

134 

135Submodules 

136---------- 

137 

138.. autosummary:: 

139 

140 csgraph - Compressed sparse graph routines 

141 linalg - sparse linear algebra routines 

142 

143Exceptions 

144---------- 

145 

146.. autosummary:: 

147 :toctree: generated/ 

148 

149 SparseEfficiencyWarning 

150 SparseWarning 

151 

152 

153Usage information 

154================= 

155 

156There are seven available sparse array types: 

157 

158 1. `csc_array`: Compressed Sparse Column format 

159 2. `csr_array`: Compressed Sparse Row format 

160 3. `bsr_array`: Block Sparse Row format 

161 4. `lil_array`: List of Lists format 

162 5. `dok_array`: Dictionary of Keys format 

163 6. `coo_array`: COOrdinate format (aka IJV, triplet format) 

164 7. `dia_array`: DIAgonal format 

165 

166To construct an array efficiently, use either `dok_array` or `lil_array`. 

167The `lil_array` class supports basic slicing and fancy indexing with a 

168similar syntax to NumPy arrays. As illustrated below, the COO format 

169may also be used to efficiently construct arrays. Despite their 

170similarity to NumPy arrays, it is **strongly discouraged** to use NumPy 

171functions directly on these arrays because NumPy may not properly convert 

172them for computations, leading to unexpected (and incorrect) results. If you 

173do want to apply a NumPy function to these arrays, first check if SciPy has 

174its own implementation for the given sparse array class, or **convert the 

175sparse array to a NumPy array** (e.g., using the ``toarray`` method of the 

176class) first before applying the method. 

177 

178To perform manipulations such as multiplication or inversion, first 

179convert the array to either CSC or CSR format. The `lil_array` format is 

180row-based, so conversion to CSR is efficient, whereas conversion to CSC 

181is less so. 

182 

183All conversions among the CSR, CSC, and COO formats are efficient, 

184linear-time operations. 

185 

186Matrix vector product 

187--------------------- 

188To do a vector product between a sparse array and a vector simply use 

189the array ``dot`` method, as described in its docstring: 

190 

191>>> import numpy as np 

192>>> from scipy.sparse import csr_array 

193>>> A = csr_array([[1, 2, 0], [0, 0, 3], [4, 0, 5]]) 

194>>> v = np.array([1, 0, -1]) 

195>>> A.dot(v) 

196array([ 1, -3, -1], dtype=int64) 

197 

198.. warning:: As of NumPy 1.7, ``np.dot`` is not aware of sparse arrays, 

199 therefore using it will result on unexpected results or errors. 

200 The corresponding dense array should be obtained first instead: 

201 

202 >>> np.dot(A.toarray(), v) 

203 array([ 1, -3, -1], dtype=int64) 

204 

205 but then all the performance advantages would be lost. 

206 

207The CSR format is especially suitable for fast matrix vector products. 

208 

209Example 1 

210--------- 

211Construct a 1000x1000 `lil_array` and add some values to it: 

212 

213>>> from scipy.sparse import lil_array 

214>>> from scipy.sparse.linalg import spsolve 

215>>> from numpy.linalg import solve, norm 

216>>> from numpy.random import rand 

217 

218>>> A = lil_array((1000, 1000)) 

219>>> A[0, :100] = rand(100) 

220>>> A[1, 100:200] = A[0, :100] 

221>>> A.setdiag(rand(1000)) 

222 

223Now convert it to CSR format and solve A x = b for x: 

224 

225>>> A = A.tocsr() 

226>>> b = rand(1000) 

227>>> x = spsolve(A, b) 

228 

229Convert it to a dense array and solve, and check that the result 

230is the same: 

231 

232>>> x_ = solve(A.toarray(), b) 

233 

234Now we can compute norm of the error with: 

235 

236>>> err = norm(x-x_) 

237>>> err < 1e-10 

238True 

239 

240It should be small :) 

241 

242 

243Example 2 

244--------- 

245 

246Construct an array in COO format: 

247 

248>>> from scipy import sparse 

249>>> from numpy import array 

250>>> I = array([0,3,1,0]) 

251>>> J = array([0,3,1,2]) 

252>>> V = array([4,5,7,9]) 

253>>> A = sparse.coo_array((V,(I,J)),shape=(4,4)) 

254 

255Notice that the indices do not need to be sorted. 

256 

257Duplicate (i,j) entries are summed when converting to CSR or CSC. 

258 

259>>> I = array([0,0,1,3,1,0,0]) 

260>>> J = array([0,2,1,3,1,0,0]) 

261>>> V = array([1,1,1,1,1,1,1]) 

262>>> B = sparse.coo_array((V,(I,J)),shape=(4,4)).tocsr() 

263 

264This is useful for constructing finite-element stiffness and mass matrices. 

265 

266Further details 

267--------------- 

268 

269CSR column indices are not necessarily sorted. Likewise for CSC row 

270indices. Use the ``.sorted_indices()`` and ``.sort_indices()`` methods when 

271sorted indices are required (e.g., when passing data to other libraries). 

272 

273""" 

274 

275# Original code by Travis Oliphant. 

276# Modified and extended by Ed Schofield, Robert Cimrman, 

277# Nathan Bell, and Jake Vanderplas. 

278 

279import warnings as _warnings 

280 

281from ._base import * 

282from ._csr import * 

283from ._csc import * 

284from ._lil import * 

285from ._dok import * 

286from ._coo import * 

287from ._dia import * 

288from ._bsr import * 

289from ._construct import * 

290from ._extract import * 

291from ._matrix import spmatrix 

292from ._matrix_io import * 

293 

294# For backward compatibility with v0.19. 

295from . import csgraph 

296 

297# Deprecated namespaces, to be removed in v2.0.0 

298from . import ( 

299 base, bsr, compressed, construct, coo, csc, csr, data, dia, dok, extract, 

300 lil, sparsetools, sputils 

301) 

302 

303__all__ = [s for s in dir() if not s.startswith('_')] 

304 

305# Filter PendingDeprecationWarning for np.matrix introduced with numpy 1.15 

306_warnings.filterwarnings('ignore', message='the matrix subclass is not the recommended way') 

307 

308from scipy._lib._testutils import PytestTester 

309test = PytestTester(__name__) 

310del PytestTester