Reusable primitive workspaces
Gale keeps ordinary one-shot APIs allocating and simple. Repeated numerical
pipelines can opt into DenseWorkspace, a single-owner, grow-only execution
resource that reuses primitive Double and index scratch on the JVM and
Scala.js. Workspaces never own result storage and never expose their arrays.
Checked requirements
Section titled “Checked requirements”ScratchRequirement describes simultaneously live primitive cells:
import gale.linalg.*import gale.spectral.*
val qr = DenseWorkspace.qrRequirement(rows = 200, cols = 40).toOption.getval eigen = Eigen.symmetricScratchRequirement( order = 40, vectors = EigenVectors.ValuesOnly).toOption.getCounts are non-negative Int-addressable quantities. Construction and
simultaneous addition use Either[LinAlgError, ScratchRequirement], so negative
counts and overflow are reported before allocation. A zero-sized problem reports
ScratchRequirement.empty.
Composition names encode lifetimes:
a.simultaneous(b)adds Double counts and index counts component-wise because both regions must coexist. Addition is checked for overflow.a.alternative(b)takes component-wise maxima because only one branch runs; maxima of already checked counts cannot overflow, so this operation is total.
Both operations are commutative and associative for valid counts; the empty requirement is their identity. These rules let a larger algorithm report one requirement without exposing its internal array layout.
Ownership and lifetime
Section titled “Ownership and lifetime”val requirement = Eigen .symmetricScratchRequirement(128, EigenVectors.ValuesOnly) .toOption .getval workspace = DenseWorkspace.forRequirement(requirement)
val first = Eigen.eigSymmetricWith( a, EigenSelection.All, EigenVectors.ValuesOnly, workspace).toOption.get
val second = Eigen.eigSymmetricWith( b, EigenSelection.All, EigenVectors.ValuesOnly, workspace).toOption.getreserve grows the Double and index regions independently and never shrinks
them. A request already covered by the current capacities retains both backing
identities. Returned matrices, vectors, factors, and sparse structures always
own their storage: reusing a workspace cannot mutate first above.
A workspace is mutable, single-owner execution state. It is not thread-safe and must not be used concurrently. Give each concurrent worker its own instance. Keeping one workspace for a long-lived sequential pipeline is safe.
Supported algorithms
Section titled “Supported algorithms”The portable reuse tier covers these independently measured families:
DenseWorkspace.qrRequirement(rows, cols, options)andDMat.qrWith(options, workspace)reuse Householder, panel, and pivot-norm scratch. Use the options-aware requirement when column pivoting is enabled. The ordinaryqrfacade creates a suitable workspace internally.DenseWorkspace.qrSolveRequirement(observations, rightHandSides)andQR.solveLeastSquaresWith(rhs, workspace)keep the transformed right-hand side in scratch while returning an independently owned coefficient vector or matrix.DenseWorkspace.forQRSolveis the allocating convenience.DMat.qrScaledRows(scales, options, workspace)factorsdiag(scales) * Awithout constructing that temporary matrix. The matchingQR.solveLeastSquaresScaledRowsWithtransforms a scaled right-hand side in caller-owned scratch. Scales are algebraic row multipliers: they may be negative or zero, and they are not implicitly square-rooted statistical weights.Eigen.symmetricScratchRequirement(order, vectors)andEigen.eigSymmetricWith(...)reuse dense symmetric reduction scratch. This is deliberately the pure Gale route: an optional provider cannot promise to use the caller’s workspace. OrdinaryEigen.eigSymmetric(...)retains provider routing and remains the default facade.CSR.canonicalizeScratchRequirementandCSR.canonicalizeWith(workspace)reuse per-row Double/index sorting buffers. The returned canonical CSR owns its exact-sized structure and values. Ordinarycanonicalizecreates its workspace internally.
The values-only symmetric eigensolver needs n*n + n Double cells: the
symmetrized reduction matrix and tridiagonal off-diagonal. When vectors are
requested, the n*n eigenvector matrix is result storage rather than scratch,
so the reported requirement is only n Double cells. CSR canonicalization
reports nnz cells of each primitive kind, the widest possible row.
Factor and solve in one sequential pipeline
Section titled “Factor and solve in one sequential pipeline”Factorization scratch and solve scratch are not live at the same time. Size one workspace with the component-wise maximum of their requirements:
import gale.linalg.*
val options = QROptions(pivoting = QRPivoting.Column)
val factorScratch = DenseWorkspace .qrRequirement(rows = 200, cols = 40, options) .orThrowval solveScratch = DenseWorkspace .qrSolveRequirement(observations = 200, rightHandSides = 3) .orThrow
val shared = DenseWorkspace.forRequirement( factorScratch.alternative(solveScratch))The factor and returned coefficients own their storage. Reusing shared after
either call cannot mutate them. Do not use simultaneous here: that operation
is for scratch regions that must coexist.
Use a workspace only after profiling identifies a repeated allocation hot path. It does not change numerical results, ordering, diagnostics, or error semantics.