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