Skip to content

Commit 0c354ea

Browse files
dschultj-bowhay
andauthored
MAINT/DOC: sparse: nD cleanup and docs (scipy#22129)
* remove spurious whitespace * align checking for 3D across formats (and test) * fix idx_dtype error in broadcast_to * change test_base name mean->sum in some places * add docstrings to dot and tensordot * fixing typos in doc_strings --------- Co-authored-by: Jake Bowhay <[email protected]>
1 parent bb44a07 commit 0c354ea

File tree

7 files changed

+231
-78
lines changed

7 files changed

+231
-78
lines changed

scipy/sparse/_compressed.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,11 @@ def __init__(self, arg1, shape=None, dtype=None, copy=False, *, maxprint=None):
8686
except Exception as e:
8787
raise ValueError(f"unrecognized {self.__class__.__name__} "
8888
f"constructor input: {arg1}") from e
89-
if isinstance(self, sparray) and arg1.ndim < 2 and self.format == "csc":
89+
if isinstance(self, sparray) and arg1.ndim != 2 and self.format == "csc":
9090
raise ValueError(f"CSC arrays don't support {arg1.ndim}D input. Use 2D")
91+
if arg1.ndim > 2:
92+
raise ValueError(f"CSR arrays don't yet support {arg1.ndim}D.")
93+
9194
coo = self._coo_container(arg1, dtype=dtype)
9295
arrays = coo._coo_to_compressed(self._swap)
9396
self.indptr, self.indices, self.data, self._shape = arrays
@@ -1415,17 +1418,17 @@ def _divide_sparse(self, other):
14151418
def _broadcast_to(self, shape, copy=False):
14161419
if self.shape == shape:
14171420
return self.copy() if copy else self
1418-
1421+
14191422
shape = check_shape(shape, allow_nd=(self._allow_nd))
14201423

14211424
if broadcast_shapes(self.shape, shape) != shape:
14221425
raise ValueError("cannot be broadcast")
1423-
1426+
14241427
if len(self.shape) == 1 and len(shape) == 1:
14251428
self.sum_duplicates()
14261429
if self.nnz == 0: # array has no non zero elements
14271430
return self.__class__(shape, dtype=self.dtype, copy=False)
1428-
1431+
14291432
N = shape[0]
14301433
data = np.full(N, self.data[0])
14311434
indices = np.arange(0,N)
@@ -1434,14 +1437,14 @@ def _broadcast_to(self, shape, copy=False):
14341437

14351438
# treat 1D as a 2D row
14361439
old_shape = self._shape_as_2d
1437-
1440+
14381441
if len(shape) != 2:
14391442
ndim = len(shape)
14401443
raise ValueError(f'CSR/CSC broadcast_to cannot have shape >2D. Got {ndim}D')
1441-
1444+
14421445
if self.nnz == 0: # array has no non zero elements
14431446
return self.__class__(shape, dtype=self.dtype, copy=False)
1444-
1447+
14451448
self.sum_duplicates()
14461449
M, N = self._swap(shape)
14471450
oM, oN = self._swap(old_shape)

0 commit comments

Comments
 (0)