_matrix.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. class spmatrix:
  2. """This class provides a base class for all sparse matrix classes.
  3. It cannot be instantiated. Most of the work is provided by subclasses.
  4. """
  5. _allow_nd = (2,)
  6. @property
  7. def _bsr_container(self):
  8. from ._bsr import bsr_matrix
  9. return bsr_matrix
  10. @property
  11. def _coo_container(self):
  12. from ._coo import coo_matrix
  13. return coo_matrix
  14. @property
  15. def _csc_container(self):
  16. from ._csc import csc_matrix
  17. return csc_matrix
  18. @property
  19. def _csr_container(self):
  20. from ._csr import csr_matrix
  21. return csr_matrix
  22. @property
  23. def _dia_container(self):
  24. from ._dia import dia_matrix
  25. return dia_matrix
  26. @property
  27. def _dok_container(self):
  28. from ._dok import dok_matrix
  29. return dok_matrix
  30. @property
  31. def _lil_container(self):
  32. from ._lil import lil_matrix
  33. return lil_matrix
  34. # Restore matrix multiplication
  35. def __mul__(self, other):
  36. return self._matmul_dispatch(other)
  37. def __rmul__(self, other):
  38. return self._rmatmul_dispatch(other)
  39. # Restore matrix power
  40. def __pow__(self, power):
  41. from .linalg import matrix_power
  42. return matrix_power(self, power)
  43. ## Backward compatibility
  44. def set_shape(self, shape):
  45. """Set the shape of the matrix in-place"""
  46. # Make sure copy is False since this is in place
  47. # Make sure format is unchanged because we are doing a __dict__ swap
  48. new_self = self.reshape(shape, copy=False).asformat(self.format)
  49. self.__dict__ = new_self.__dict__
  50. def get_shape(self):
  51. """Get the shape of the matrix"""
  52. return self._shape
  53. shape = property(fget=get_shape, fset=set_shape,
  54. doc="Shape of the matrix")
  55. def asfptype(self):
  56. """Upcast matrix to a floating point format (if necessary)"""
  57. return self._asfptype()
  58. def getmaxprint(self):
  59. """Maximum number of elements to display when printed."""
  60. return self._getmaxprint()
  61. def getformat(self):
  62. """Matrix storage format"""
  63. return self.format
  64. def getnnz(self, axis=None):
  65. """Number of stored values, including explicit zeros.
  66. Parameters
  67. ----------
  68. axis : None, 0, or 1
  69. Select between the number of values across the whole array, in
  70. each column, or in each row.
  71. """
  72. return self._getnnz(axis=axis)
  73. def getH(self):
  74. """Return the Hermitian transpose of this matrix.
  75. See Also
  76. --------
  77. numpy.matrix.getH : NumPy's implementation of `getH` for matrices
  78. """
  79. return self.conjugate().transpose()
  80. def getcol(self, j):
  81. """Returns a copy of column j of the matrix, as an (m x 1) sparse
  82. matrix (column vector).
  83. """
  84. return self._getcol(j)
  85. def getrow(self, i):
  86. """Returns a copy of row i of the matrix, as a (1 x n) sparse
  87. matrix (row vector).
  88. """
  89. return self._getrow(i)
  90. def todense(self, order=None, out=None):
  91. """
  92. Return a dense representation of this sparse matrix.
  93. Parameters
  94. ----------
  95. order : {'C', 'F'}, optional
  96. Whether to store multi-dimensional data in C (row-major)
  97. or Fortran (column-major) order in memory. The default
  98. is 'None', which provides no ordering guarantees.
  99. Cannot be specified in conjunction with the `out`
  100. argument.
  101. out : ndarray, 2-D, optional
  102. If specified, uses this array (or `numpy.matrix`) as the
  103. output buffer instead of allocating a new array to
  104. return. The provided array must have the same shape and
  105. dtype as the sparse matrix on which you are calling the
  106. method.
  107. Returns
  108. -------
  109. arr : numpy.matrix, 2-D
  110. A NumPy matrix object with the same shape and containing
  111. the same data represented by the sparse matrix, with the
  112. requested memory order. If `out` was passed and was an
  113. array (rather than a `numpy.matrix`), it will be filled
  114. with the appropriate values and returned wrapped in a
  115. `numpy.matrix` object that shares the same memory.
  116. """
  117. return super().todense(order, out)
  118. @classmethod
  119. def __class_getitem__(cls, arg, /):
  120. """
  121. Return a parametrized wrapper around the `~scipy.sparse.spmatrix` type.
  122. .. versionadded:: 1.16.0
  123. Returns
  124. -------
  125. alias : types.GenericAlias
  126. A parametrized `~scipy.sparse.spmatrix` type.
  127. Examples
  128. --------
  129. >>> import numpy as np
  130. >>> from scipy.sparse import coo_matrix
  131. >>> coo_matrix[np.int8]
  132. scipy.sparse._coo.coo_matrix[numpy.int8]
  133. """
  134. from types import GenericAlias
  135. return GenericAlias(cls, arg)