Reductions, norms, and elementwise numerics
Gale vectors and matrices carry the everyday reductions as members: sums,
means, extrema and their positions, per-axis reductions, and norms. Named
elementwise functions and the log-domain operations (logSumExp, softmax,
logSoftmax) live in gale.linalg.Numerics. Everything here is portable
gale-core code and runs on the JVM and Scala.js.
Reduce a vector
Section titled “Reduce a vector”import gale.linalg.*
val x = Vec(3.0, -1.0, 4.0, -1.0, 5.0, -9.0)
assert(x.sum == 1.0)assert(x.mean == 1.0 / 6.0)assert(x.max == 5.0 && x.argmax == 4)assert(x.min == -9.0 && x.argmin == 5)assert(x.norm1 == 23.0)assert(x.normInf == 9.0)x.norm2// res6: Double = 11.532562594670797Views reduce in place: x.slice(1, 4).sum, a matrix row, or a column of a
row-major matrix (a strided view) needs no copy.
argmax and argmin return the first position of a tie. If any entry is
NaN, max and min are NaN and argmax/argmin point at the first NaN, so a
NaN cannot hide behind a larger value.
val tied = Vec(2.0, 7.0, 7.0, 1.0)assert(tied.argmax == 1)
val withNaN = Vec(1.0, Double.NaN, 9.0)assert(withNaN.argmax == 1 && withNaN.max.isNaN)assert(withNaN.sum.isNaN && withNaN.mean.isNaN && withNaN.norm1.isNaN)Sums, means, and norms follow IEEE arithmetic, so a NaN entry makes them NaN,
and so does +Inf added to -Inf. Gale guarantees which results are NaN,
not the sign or payload bits of a NaN, so test with isNaN rather than
comparing bit patterns.
Choose sum or sumExact
Section titled “Choose sum or sumExact”sum is the fast path. It keeps several partial sums, so its last bits may
differ from a left-to-right loop and between JVM and Scala.js. sumExact
computes the exact total and rounds it once, so it is the same on every
platform and for every order of the entries. Use it for cancellation-prone
data or when a result must be reproducible bit for bit.
val cancelling = Vec(1e100, 1.0, -1e100, 3.0)cancelling.sum// res10: Double = 0.0assert(cancelling.sumExact == 4.0)Empty inputs
Section titled “Empty inputs”A sum of nothing is 0.0, and so is every norm of an empty vector or matrix.
There is no meaningful mean, maximum, or position of an empty input, so those
throw the typed LinAlgError.EmptyInput:
val empty = Vec()assert(empty.sum == 0.0 && empty.normInf == 0.0)
val failure = try empty.max None catch case error: LinAlgError.EmptyInput => Some(error.operation)// failure: Option[String] = Some("DVec.max")assert(failure.contains("DVec.max"))Reduce a matrix, whole or per axis
Section titled “Reduce a matrix, whole or per axis”The whole-matrix forms reduce every entry; argmax returns (row, col) of the
first maximum in row-major order, whatever the storage layout.
Per-axis forms take an Axis, named after what the result is indexed by:
Axis.Rows gives one value per row, and Axis.Cols gives one value per column.
In Breeze terms, a.sum(Axis.Rows) is sum(a(*, ::)) and a.sum(Axis.Cols)
is sum(a(::, *)). In NumPy terms they are axis=1 and axis=0.
val a = Matrix(2, 3)( 1.0, 2.0, 3.0, 4.0, 5.0, 9.0)
assert(a.sum == 24.0)assert(a.argmax == (1, 2))assert(a.sum(Axis.Rows).toSeq == Seq(6.0, 18.0))assert(a.sum(Axis.Cols).toSeq == Seq(5.0, 7.0, 12.0))assert(a.mean(Axis.Cols).toSeq == Seq(2.5, 3.5, 6.0))assert(a.max(Axis.Rows).toSeq == Seq(3.0, 9.0))Matrix norms
Section titled “Matrix norms”norm1 is the largest absolute column sum, normInf the largest absolute row
sum, and normFrobenius the square root of the sum of squares. The Frobenius
norm scales internally, so it does not overflow for entries near 1e300.
val b = Matrix(2, 2)( 1.0, -2.0, -3.0, 4.0)assert(b.norm1 == 6.0)assert(b.normInf == 7.0)assert(math.abs(b.normFrobenius - math.sqrt(30.0)) < 1e-15)
val huge = Matrix.tabulate(2, 2)((_, _) => 1e300)assert(math.abs(huge.normFrobenius - 2e300) < 1e286)Elementwise functions
Section titled “Elementwise functions”Elementwise operations are explicit in Gale. a.pointwise.map(f) (from
gale.syntax.all.*) applies any function. Numerics.exp, log, log1p,
expm1, and sigmoid are the fast paths for common ones: they apply the same
scalar function, so Numerics.exp(a) equals a.pointwise.map(math.exp) value
for value, but runs as a primitive loop. Results are new, owned row-major
matrices or contiguous vectors.
import gale.syntax.all.*
val z = Matrix(2, 2)( 0.0, 1.0, -1.0, 2.0)assert(Numerics.exp(z).valuesRowMajor == z.pointwise.map(math.exp).valuesRowMajor)
val probabilities = Numerics.sigmoid(Vec(-1000.0, 0.0, 1000.0))assert(probabilities.toSeq == Seq(0.0, 0.5, 1.0))
val deepTail = Numerics.sigmoid(Vec(-720.0))(0)// deepTail: Double = 2.0322308024E-313assert(deepTail > 0.0 && deepTail == math.exp(-720.0))sigmoid uses two forms. For x >= -36 it evaluates 1 / (1 + exp(-x)),
the same expression Breeze uses. Below -36 it evaluates t / (1 + t) with
t = exp(x), so it never overflows: where exp(-x) would be infinite (below
about -709.78), the result is the subnormal exp(x) rather than 0.0. Large
inputs saturate to 1.0; inputs below about -745 round to 0.0. The result
is within 3 ulps of the exact value.
Log-sum-exp and softmax
Section titled “Log-sum-exp and softmax”logSumExp, softmax, and logSoftmax subtract the maximum first, so inputs
of magnitude 1000 neither overflow nor underflow to a useless result.
val logits = Vec(1000.0, 1000.0, 999.0)val lse = Numerics.logSumExp(logits)// lse: Double = 1000.8619948040582assert(math.abs(lse - (1000.0 + math.log(2.0 + math.exp(-1.0)))) < 1e-12)
val p = Numerics.softmax(logits)assert(math.abs(p.sumExact - 1.0) < 1e-15)val logP = Numerics.logSoftmax(logits)assert(math.abs(logP(0) - (1000.0 - lse)) < 1e-12)For matrices, the forms without an axis treat every entry as one input; the
Axis overloads give one distribution per row or per column:
val scores = Matrix(2, 3)( 1.0, 2.0, 3.0, 10.0, 10.0, 10.0)val perRow = Numerics.softmax(scores, Axis.Rows)assert(math.abs(perRow.row(0).sum - 1.0) < 1e-15)assert(perRow.row(1).toSeq.forall(v => math.abs(v - 1.0 / 3.0) < 1e-15))Numerics.logSumExp(scores, Axis.Rows).toSeq// res32: Seq[Double] = Vector(3.4076059644443806, 11.09861228866811)An empty logSumExp is -Inf (the log of an empty sum). If a line contains
NaN, contains +Inf, or is entirely -Inf, softmax and logSoftmax return
NaN for that whole line.
Coming from Breeze: Breeze’s softmax(v) returns the scalar log-sum-exp.
Its Gale equivalent is Numerics.logSumExp(v); Gale’s Numerics.softmax(v) is
the normalized-exponential vector.
Accumulate values across batches
Section titled “Accumulate values across batches”Use gale.numeric.ExactSum when values arrive incrementally or separate
batches must be merged without rounding each batch’s total first. add adds
one Double; addAll merges another accumulator’s exact state.
import gale.numeric.ExactSum
val firstBatch = ExactSum.zero()assert(firstBatch.add(1e100).isRight)assert(firstBatch.add(1.0).isRight)val secondBatch = ExactSum.zero()assert(secondBatch.add(-1e100).isRight)assert(secondBatch.add(3.0).isRight)assert(firstBatch.addAll(secondBatch).isRight)assert(firstBatch.value == 4.0)The final total retains the small terms lost by ordinary intermediate rounding. For finite inputs within capacity, input order and merge order do not change that result. Keep each mutable accumulator within one worker; merge the batches when their producers have finished.
add and addAll return Either[ExactSumError.CapacityExceeded, Unit];
successful calls reuse one result object. value rounds the exact total once
to nearest, ties to even, and copy() owns independent storage. The supported capacity is ExactSum.MaxTerms (2^60)
nonzero finite inputs, including inputs inherited through merges. Cancellation
keeps the input count; zeros and nonfinite inputs do not consume capacity.
A capacity refusal leaves the builder unchanged. Builders are not thread-safe.
Nonfinite inputs follow a separate IEEE addition channel; NaN payload bits and
their ordering are unspecified.
Divide exact totals before rounding
Section titled “Divide exact totals before rounding”total.ratio(normalizer) returns Either[ExactSumError, Double] by dividing
the two exact finite totals before rounding once to nearest, ties to even.
Both totals may exceed Double range. The readout
allocates integer scratch storage and leaves both builders and their capacity
unchanged. It refuses nonfinite input channels (NonFiniteRatio) and exact
zero denominators (ZeroNormalizer). True ratio overflow returns signed
infinity; underflow rounds to subnormals or signed zero. An exact zero numerator
returns positive zero. Products supplied as inputs are still rounded before
accumulation; this is an exact sum and ratio, not an exact dot product.
The following ratio stays finite even though the numerator’s rounded sum overflows:
val numerator = ExactSum.zero()val denominator = ExactSum.zero()assert(numerator.add(Double.MaxValue).isRight)assert(numerator.add(Double.MaxValue).isRight)assert(denominator.add(1.0).isRight)assert(denominator.add(1.0).isRight)assert(numerator.value == Double.PositiveInfinity)assert(numerator.ratio(denominator) == Right(Double.MaxValue))Capacity and storage
Section titled “Capacity and storage”ExactSum.resources (also available as builder.resources) exposes immutable
numerical capacity facts without normalizing or rounding state. State, copying
and value report their Long-array payloads; ratio reports a conservative
BigInt scratch estimate and maximum operand bit widths. These are source-level
numerical payload estimates, with object, host and GC overhead excluded. They do
not establish a heap/RSS limit or a JavaScript engine allocation bound. Consumers
must account for their own retained results, staging and any native workspace.
The separate ExactSum.add and addAll contracts expose only
ExactSumError.CapacityExceeded; ratio/normalization errors belong to the
normalized readout methods, not accumulator insertion.
The complete NaN, empty-input, and determinism rules are in the numerical contract.