Module mpython.sparse_array
Classes
class SparseArray (*args, **kwargs)
-
Expand source code
class SparseArray(_SparseMixin, Array): """ Matlab sparse arrays (dense backend). ```python # Instantiate from size SparseArray(N, M, ...) SparseArray([N, M, ...]) SparseArray.from_shape([N, M, ...]) # Instantiate from existing sparse or dense array SparseArray(other_array) SparseArray.from_any(other_array) # Other options SparseArray(..., dtype=None, *, copy=None) ``` !!! warning Lists or vectors of integers can be interpreted as shapes or as dense arrays to copy. They are interpreted as shapes by the `SparseArray` constructor. To ensure that they are interpreted as dense arrays to copy, usse `SparseArray.from_any`. !!! note This is not really a sparse array, but a dense array that gets converted to a sparse array when passed to matlab. """ def to_dense(self) -> "Array": return np.ndarray.view(self, Array) @classmethod def from_coo(cls, values, indices, shape=None, **kw) -> "SparseArray": """ Build a sparse array from indices and values. Parameters ---------- values : (N,) ArrayLike Values to set at each index. indices : (D, N) ArrayLike Indices of nonzero elements. shape : list[int] | None Shape of the array. dtype : np.dtype | None Target data type. Same as `values` by default. Returns ------- array : SparseArray New array. """ dtype = kw.get("dtype", None) indices = np.asarray(indices) values = np.asarray(values, dtype=dtype) if shape is None: shape = (1 + indices.max(-1)).astype(np.uint64).tolist() if dtype is None: dtype = values.dtype obj = cls.from_shape(shape, dtype=dtype) obj[tuple(indices)] = values return obj
Matlab sparse arrays (dense backend).
# Instantiate from size SparseArray(N, M, ...) SparseArray([N, M, ...]) SparseArray.from_shape([N, M, ...]) # Instantiate from existing sparse or dense array SparseArray(other_array) SparseArray.from_any(other_array) # Other options SparseArray(..., dtype=None, *, copy=None)
Warning
Lists or vectors of integers can be interpreted as shapes or as dense arrays to copy. They are interpreted as shapes by the
SparseArray
constructor. To ensure that they are interpreted as dense arrays to copy, usseArray.from_any()
.Note
This is not really a sparse array, but a dense array that gets converted to a sparse array when passed to matlab.
Ancestors
- mpython.core.mixin_types._SparseMixin
- Array
- mpython.core.mixin_types._ListishMixin
- WrappedArray
- numpy.ndarray
- AnyWrappedArray
- AnyMatlabArray
- MatlabType
Static methods
def from_coo(values, indices, shape=None, **kw) ‑> SparseArray
-
Build a sparse array from indices and values.
Parameters
values
:(N,) ArrayLike
- Values to set at each index.
indices
:(D, N) ArrayLike
- Indices of nonzero elements.
shape
:list[int] | None
- Shape of the array.
dtype
:np.dtype | None
- Target data type. Same as
values
by default.
Returns
array
:SparseArray
- New array.
Methods
def to_dense(self) ‑> Array
-
Expand source code
def to_dense(self) -> "Array": return np.ndarray.view(self, Array)
Inherited members