Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions mpython/sparse_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@

if sparse:

class WrappedSparseArray(sparse.sparray, AnyWrappedArray):
class WrappedSparseArray(sparse.spmatrix, AnyWrappedArray):
"""Base class for sparse arrays."""

def to_dense(self) -> "Array":
return Array.from_any(self.todense())

class SparseArray(sparse.csc_array, _SparseMixin, WrappedSparseArray):
class SparseArray(sparse.csc_matrix, _SparseMixin, WrappedSparseArray):
"""
Matlab sparse arrays (scipy.sparse backend).
Matlab sparse matrices (scipy.sparse backend).

```python
# Instantiate from size
SparseArray(N, M, ...)
SparseArray([N, M, ...])
SparseArray.from_shape([N, M, ...])
SparseArray(N, M)
SparseArray([N, M])
SparseArray.from_shape([N, M])

# Instantiate from existing sparse or dense array
SparseArray(other_array)
Expand All @@ -45,7 +45,7 @@ def __init__(self, *args, **kwargs) -> None:
ndim = len(arg)
return super().__init__(([], [[]] * ndim), shape=arg, **kwargs)
else:
if not isinstance(arg, (np.ndarray, sparse.sparray)):
if not isinstance(arg, (np.ndarray, sparse.spmatrix)):
arg = np.asanyarray(arg)
return super().__init__(arg, **kwargs)

Expand All @@ -71,7 +71,7 @@ def from_coo(cls, values, indices, shape=None, **kw) -> "SparseArray":
New array.
"""
indices = np.asarray(indices)
coo = sparse.coo_array((values, indices), shape=shape, **kw)
coo = sparse.coo_matrix((values, indices), shape=shape, **kw)
return cls.from_any(coo)

@classmethod
Expand Down Expand Up @@ -124,7 +124,7 @@ def from_any(cls, other, **kwargs) -> "SparseArray":
"""
copy = kwargs.pop("copy", None)
inp = other
if not isinstance(other, sparse.sparray):
if not isinstance(other, sparse.spmatrix):
other = np.asanyarray(other, **kwargs)
other = cls(other, **kwargs)
other = _spcopy_if_needed(other, inp, copy)
Expand Down
2 changes: 1 addition & 1 deletion mpython/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def _spcopy_if_needed(out, inp, copy=None):
"""Fallback implementation for asarray(*, copy: bool)"""
if (
out is not None
and isinstance(inp, sparse.sparray)
and isinstance(inp, sparse.spmatrix)
and out.data.data != inp.data.data
):
if copy:
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ dependencies = [
"numpy"
]

[project.optional-dependencies]
sparse = ["scipy>=0.7"]

[tool.setuptools.dynamic]
version = {attr = "mpython._version.__version__"}

Expand Down