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

1from ._sputils import isintlike, isscalarlike 

2 

3 

4class spmatrix: 

5 """This class provides a base class for all sparse matrix classes. 

6 

7 It cannot be instantiated. Most of the work is provided by subclasses. 

8 """ 

9 _is_array = False 

10 

11 @property 

12 def _bsr_container(self): 

13 from ._bsr import bsr_matrix 

14 return bsr_matrix 

15 

16 @property 

17 def _coo_container(self): 

18 from ._coo import coo_matrix 

19 return coo_matrix 

20 

21 @property 

22 def _csc_container(self): 

23 from ._csc import csc_matrix 

24 return csc_matrix 

25 

26 @property 

27 def _csr_container(self): 

28 from ._csr import csr_matrix 

29 return csr_matrix 

30 

31 @property 

32 def _dia_container(self): 

33 from ._dia import dia_matrix 

34 return dia_matrix 

35 

36 @property 

37 def _dok_container(self): 

38 from ._dok import dok_matrix 

39 return dok_matrix 

40 

41 @property 

42 def _lil_container(self): 

43 from ._lil import lil_matrix 

44 return lil_matrix 

45 

46 # Restore matrix multiplication 

47 def __mul__(self, other): 

48 return self._mul_dispatch(other) 

49 

50 def __rmul__(self, other): 

51 return self._rmul_dispatch(other) 

52 

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') 

58 

59 if isintlike(other): 

60 other = int(other) 

61 if other < 0: 

62 raise ValueError('exponent must be >= 0') 

63 

64 if other == 0: 

65 from ._construct import eye 

66 return eye(M, dtype=self.dtype) 

67 

68 if other == 1: 

69 return self.copy() 

70 

71 tmp = self.__pow__(other // 2) 

72 if other % 2: 

73 return self @ tmp @ tmp 

74 else: 

75 return tmp @ tmp 

76 

77 if isscalarlike(other): 

78 raise ValueError('exponent must be an integer') 

79 return NotImplemented 

80 

81 ## Backward compatibility 

82 

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__ 

89 

90 def get_shape(self): 

91 """Get the shape of the matrix""" 

92 return self._shape 

93 

94 shape = property(fget=get_shape, fset=set_shape, 

95 doc="Shape of the matrix") 

96 

97 def asfptype(self): 

98 """Upcast array to a floating point format (if necessary)""" 

99 return self._asfptype() 

100 

101 def getmaxprint(self): 

102 """Maximum number of elements to display when printed.""" 

103 return self._getmaxprint() 

104 

105 def getformat(self): 

106 """Matrix storage format""" 

107 return self.format 

108 

109 def getnnz(self, axis=None): 

110 """Number of stored values, including explicit zeros. 

111 

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) 

119 

120 def getH(self): 

121 """Return the Hermitian transpose of this array. 

122 

123 See Also 

124 -------- 

125 numpy.matrix.getH : NumPy's implementation of `getH` for matrices 

126 """ 

127 return self.conjugate().transpose() 

128 

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) 

134 

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) 

140 

141 

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 )