Coverage for /usr/lib/python3/dist-packages/scipy/sparse/_matrix.py: 42%
79 statements
« prev ^ index » next coverage.py v7.4.4, created at 2025-06-14 15:25 +0200
« prev ^ index » next coverage.py v7.4.4, created at 2025-06-14 15:25 +0200
1from ._sputils import isintlike, isscalarlike
4class spmatrix:
5 """This class provides a base class for all sparse matrix classes.
7 It cannot be instantiated. Most of the work is provided by subclasses.
8 """
9 _is_array = False
11 @property
12 def _bsr_container(self):
13 from ._bsr import bsr_matrix
14 return bsr_matrix
16 @property
17 def _coo_container(self):
18 from ._coo import coo_matrix
19 return coo_matrix
21 @property
22 def _csc_container(self):
23 from ._csc import csc_matrix
24 return csc_matrix
26 @property
27 def _csr_container(self):
28 from ._csr import csr_matrix
29 return csr_matrix
31 @property
32 def _dia_container(self):
33 from ._dia import dia_matrix
34 return dia_matrix
36 @property
37 def _dok_container(self):
38 from ._dok import dok_matrix
39 return dok_matrix
41 @property
42 def _lil_container(self):
43 from ._lil import lil_matrix
44 return lil_matrix
46 # Restore matrix multiplication
47 def __mul__(self, other):
48 return self._mul_dispatch(other)
50 def __rmul__(self, other):
51 return self._rmul_dispatch(other)
53 # Restore matrix power
54 def __pow__(self, other):
55 M, N = self.shape
56 if M != N:
57 raise TypeError('sparse matrix is not square')
59 if isintlike(other):
60 other = int(other)
61 if other < 0:
62 raise ValueError('exponent must be >= 0')
64 if other == 0:
65 from ._construct import eye
66 return eye(M, dtype=self.dtype)
68 if other == 1:
69 return self.copy()
71 tmp = self.__pow__(other // 2)
72 if other % 2:
73 return self @ tmp @ tmp
74 else:
75 return tmp @ tmp
77 if isscalarlike(other):
78 raise ValueError('exponent must be an integer')
79 return NotImplemented
81 ## Backward compatibility
83 def set_shape(self, shape):
84 """Set the shape of the matrix in-place"""
85 # Make sure copy is False since this is in place
86 # Make sure format is unchanged because we are doing a __dict__ swap
87 new_self = self.reshape(shape, copy=False).asformat(self.format)
88 self.__dict__ = new_self.__dict__
90 def get_shape(self):
91 """Get the shape of the matrix"""
92 return self._shape
94 shape = property(fget=get_shape, fset=set_shape,
95 doc="Shape of the matrix")
97 def asfptype(self):
98 """Upcast array to a floating point format (if necessary)"""
99 return self._asfptype()
101 def getmaxprint(self):
102 """Maximum number of elements to display when printed."""
103 return self._getmaxprint()
105 def getformat(self):
106 """Matrix storage format"""
107 return self.format
109 def getnnz(self, axis=None):
110 """Number of stored values, including explicit zeros.
112 Parameters
113 ----------
114 axis : None, 0, or 1
115 Select between the number of values across the whole array, in
116 each column, or in each row.
117 """
118 return self._getnnz(axis=axis)
120 def getH(self):
121 """Return the Hermitian transpose of this array.
123 See Also
124 --------
125 numpy.matrix.getH : NumPy's implementation of `getH` for matrices
126 """
127 return self.conjugate().transpose()
129 def getcol(self, j):
130 """Returns a copy of column j of the array, as an (m x 1) sparse
131 array (column vector).
132 """
133 return self._getcol(j)
135 def getrow(self, i):
136 """Returns a copy of row i of the array, as a (1 x n) sparse
137 array (row vector).
138 """
139 return self._getrow(i)
142def _array_doc_to_matrix(docstr):
143 # For opimized builds with stripped docstrings
144 if docstr is None:
145 return None
146 return (
147 docstr.replace('sparse arrays', 'sparse matrices')
148 .replace('sparse array', 'sparse matrix')
149 )