| 214 | @concatenate_lookup.register_lazy("scipy") |
| 215 | @take_lookup.register_lazy("scipy") |
| 216 | def register_scipy_sparse(): |
| 217 | import scipy.sparse |
| 218 | |
| 219 | def _concatenate(L, axis=0): |
| 220 | if axis == 0: |
| 221 | return scipy.sparse.vstack(L) |
| 222 | elif axis == 1: |
| 223 | return scipy.sparse.hstack(L) |
| 224 | else: |
| 225 | msg = ( |
| 226 | "Can only concatenate scipy sparse matrices for axis in " |
| 227 | f"{{0, 1}}. Got {axis}" |
| 228 | ) |
| 229 | raise ValueError(msg) |
| 230 | |
| 231 | concatenate_lookup.register(scipy.sparse.spmatrix, _concatenate) |
| 232 | tensordot_lookup.register(scipy.sparse.spmatrix, _tensordot_scipy_sparse) |
| 233 | take_lookup.register(scipy.sparse.spmatrix, sparse_take) |
| 234 | |
| 235 | try: |
| 236 | from scipy.sparse import sparray |
| 237 | except ImportError: |
| 238 | pass # sparray is not available in older scipy versions |
| 239 | else: |
| 240 | concatenate_lookup.register(sparray, _concatenate) |
| 241 | tensordot_lookup.register(sparray, _tensordot_scipy_sparse) |
| 242 | take_lookup.register(sparray, sparse_take) |
| 243 | |
| 244 | |
| 245 | def _tensordot_scipy_sparse(a, b, axes): |