Dense matrix destinations
Gale’s ordinary matrix operators remain allocating and immutable. Callers with a
measured repeated-operation hot path can instead allocate a DMatBuilder once
and reuse it as a single-owner row-major destination.
Matrix multiplication
Section titled “Matrix multiplication”import gale.linalg.*
val destination = DMatBuilder.zeros(a.rows, b.cols)a.gemmInto(b, destination, alpha = 1.0, beta = 0.0)
// Reuse the same open destination for another product.a.gemmInto(b, destination, alpha = 0.5, beta = 1.0)
val result = destination.result() // transfers storage and closes the buildergemmInto implements
destination := alpha * A * B + beta * destinationbeta == 0.0is replacement semantics. The old destination is not read, so existing NaN or infinity cannot contaminate the result through0 * NaN.- Any nonzero
betais accumulation semantics and reads the previous destination. - Row-major, transposed, and general positive-stride inputs are accepted.
- The same pure, Vector, or native GEMM routing policy used by
A * Bis used bygemmInto. TheA.t * Aassign-only case retains the SYRK route.
Fused scale and add
Section titled “Fused scale and add”val destination = DMatBuilder.zeros(a.rows, a.cols)a.linearCombinationInto( b, destination, alpha = 0.5, beta = 0.5)This is replacement semantics:
destination := alpha * A + beta * BIt never reads the old destination. A zero coefficient suppresses reading that input as well, giving an explicit and testable NaN policy. The operation fuses the add and scale into one pass and creates no intermediate matrix.
Safety and lifetime
Section titled “Safety and lifetime”Destination shape is checked before computation. A closed builder and any
internal alias between a destination and an input are rejected deterministically
with LinAlgError; no kernel runs after rejection. Public Gale APIs do not expose
the builder’s storage or provide a mutable matrix view, so aliasing is normally
impossible without internal/test-only access.
DMatBuilder.result() transfers the destination storage to an immutable DMat
without copying and permanently closes the builder. Allocate a new builder for a
new ownership lifetime.
Consuming a builder as QR storage
Section titled “Consuming a builder as QR storage”When a matrix is built only to be factorized, consumeQR transfers the
builder’s owned row-major storage directly into portable QR construction:
import gale.linalg.*
val design = DMatBuilder.zeros(rows = 4, cols = 2)design(0, 0) = 1.0design(0, 1) = 0.0design(1, 0) = 1.0design(1, 1) = 1.0design(2, 0) = 1.0design(2, 1) = 2.0design(3, 0) = 1.0design(3, 1) = 3.0
val workspace = DenseWorkspace.forQR(4, 2)val factor = design.consumeQR(QROptions.Default, workspace)The call closes design exactly as result() would. The returned QR owns
its factor storage and does not retain a mutable builder alias. Use
builder.result().qr when preserving the constructed matrix is part of the
caller contract; use consumeQR when the matrix is deliberately transient.
Use this tier only when destination reuse is part of the caller’s design. For
one-off operations, A * B, A + B, and the other high-level methods remain the
clear default.