Batching and Vectorization#
A space describes one element; a batch is a leading axis handled by the
v-methods, never a different space. For \(A : X \to Y\),
A.vapply(xs) takes shape (B,) + X.shape and returns
(B,) + Y.shape.
import numpy as np
import spacecore as sc
ctx = sc.Context(sc.NumpyOps(), dtype=np.float64)
X = sc.DenseCoordinateSpace((2,), ctx)
A = sc.DiagonalLinOp(ctx.asarray([2.0, 3.0]), X, ctx)
print(A.vapply(ctx.asarray([[1.0, 1.0], [2.0, 4.0]])))
[[ 2. 3.]
[ 4. 12.]]
Batched methods#
Method |
Batched form of |
|---|---|
|
|
|
|
|
|
|
|
Implement
_vapply_core/_rvapply_corefor a vectorized path. Without one, the base class mapsapplyover the batch with the backend’svmap: native on JAX and Torch, a Python loop on NumPy, which emits aRuntimeWarningonce per class for batches larger than 32.Only the leading axis batches; use
moveaxisfor other layouts. Every leaf of aTreeSpacebatch has the same batch size.When the stack itself is the mathematical object, use
StackedSpace(base, count): a space ofcountcopies ofbase.The solvers (
cg,lsqr,lanczos_smallest,power_iteration,expm_multiply) are unbatched; a batched right-hand side fails the membership check. Loop over the batch.