_sparse.py 875 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from abc import ABC
  2. __all__ = ["SparseABC", "issparse"]
  3. class SparseABC(ABC):
  4. pass
  5. def issparse(x):
  6. """Is `x` of a sparse array or sparse matrix type?
  7. Parameters
  8. ----------
  9. x
  10. object to check for being a sparse array or sparse matrix
  11. Returns
  12. -------
  13. bool
  14. True if `x` is a sparse array or a sparse matrix, False otherwise
  15. Notes
  16. -----
  17. Use `isinstance(x, sp.sparse.sparray)` to check between an array or matrix.
  18. Use `a.format` to check the sparse format, e.g. `a.format == 'csr'`.
  19. Examples
  20. --------
  21. >>> import numpy as np
  22. >>> from scipy.sparse import csr_array, csr_matrix, issparse
  23. >>> issparse(csr_matrix([[5]]))
  24. True
  25. >>> issparse(csr_array([[5]]))
  26. True
  27. >>> issparse(np.array([[5]]))
  28. False
  29. >>> issparse(5)
  30. False
  31. """
  32. return isinstance(x, SparseABC)