Class ArrayMath
double[] arrays.
This class provides common operations such as addition, scaling (multiplying by a scalar), and the fused AXPY operation
(out = a + alpha * b). Each operation is available in two forms:
- Functional: returns a newly allocated result array (e.g.,
add(double[], double[])). - Procedural: writes into a caller-provided output buffer (e.g.,
addInto(double[], double[], double[])).
Design & Performance Notes
- Methods operate on primitive arrays to avoid boxing and reduce allocation overhead.
- Simple indexed
for-loops are used on purpose; modern JVMs can hoist bounds checks, unroll loops, and sometimes auto-vectorize tight loops on hot paths. - Procedural variants (
...Into) allow output reuse to reduce GC pressure. - No special-casing for short arrays; on modern CPUs/JITs, well-structured loops scale efficiently.
Preconditions
- All array parameters must be non-null.
- Arrays participating in an operation must have identical lengths.
This class is not instantiable. Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information https://djunits.org. The DJUNITS project is distributed under a three-clause BSD-style license.
- Author:
- Alexander Verbraeck
-
Method Summary
Modifier and TypeMethodDescriptionstatic double[]abs(double[] a) Returns a new array with absolute values for each entryabs(a).static double[]add(double[] a, double c) Returns a new array equal to the element-wise sum with a constanta + c.static double[]add(double[] a, double[] b) Returns a new array equal to the element-wise suma + b.static voidaddInto(double[] a, double[] b, double[] out) Computes the element-wise suma + band writes the result intoout.static double[]axpy(double[] a, double[] b, double alpha) Returns a new array equal to the fused AXPY operationa + alpha * b.static voidaxpyInto(double[] a, double[] b, double alpha, double[] out) Computes the fused AXPY operationout = a + alpha * band writes the result intoout.static double[]divide(double[] a, double[] b) Returns a new array equal to the element-wise divisiona / b.static double[]multiply(double[] a, double[] b) Returns a new array equal to the element-wise multiplicationa * b.static double[]reciprocal(double[] a) Returns a new array equal to the element-wise reciprocal1 / a.static double[]scaleBy(double[] a, double alpha) Returns a new array equal to the element-wise scaled vectoralpha * a.static voidscaleInto(double[] a, double alpha, double[] out) Computes the element-wise scalingalpha * aand writes the result intoout.static double[]subtract(double[] a, double[] b) Returns a new array equal to the element-wise differencea - b.
-
Method Details
-
add
public static double[] add(double[] a, double[] b) Returns a new array equal to the element-wise suma + b.Implementation Note: Uses a simple indexed
for-loop to enable JIT optimizations (bounds-check elimination, loop unrolling, potential auto-vectorization).- Parameters:
a- the left-hand array; must be non-nullb- the right-hand array; must be non-null and the same length asa- Returns:
- a newly allocated array where
out[i] = a[i] + b[i]for all indices - Throws:
NullPointerException- ifaorbis nullIllegalArgumentException- ifa.length != b.length
-
add
public static double[] add(double[] a, double c) Returns a new array equal to the element-wise sum with a constanta + c.Implementation Note: Uses a simple indexed
for-loop to enable JIT optimizations (bounds-check elimination, loop unrolling, potential auto-vectorization).- Parameters:
a- the left-hand array; must be non-nullc- a constant to add to each element- Returns:
- a newly allocated array where
out[i] = a[i] + cfor all indices - Throws:
NullPointerException- ifais null
-
subtract
public static double[] subtract(double[] a, double[] b) Returns a new array equal to the element-wise differencea - b.Implementation Note: Uses a simple indexed
for-loop to enable JIT optimizations (bounds-check elimination, loop unrolling, potential auto-vectorization).- Parameters:
a- the left-hand array; must be non-nullb- the right-hand array; must be non-null and the same length asa- Returns:
- a newly allocated array where
out[i] = a[i] - b[i]for all indices - Throws:
NullPointerException- ifaorbis nullIllegalArgumentException- ifa.length != b.length
-
scaleBy
public static double[] scaleBy(double[] a, double alpha) Returns a new array equal to the element-wise scaled vectoralpha * a.Implementation Note: Uses a simple indexed
for-loop to enable JIT optimizations.- Parameters:
a- the input array; must be non-nullalpha- the scalar multiplier- Returns:
- a newly allocated array where
out[i] = alpha * a[i]for all indices - Throws:
NullPointerException- ifais null
-
abs
public static double[] abs(double[] a) Returns a new array with absolute values for each entryabs(a).Implementation Note: Uses a simple indexed
for-loop to enable JIT optimizations.- Parameters:
a- the input array; must be non-null- Returns:
- a newly allocated array where
out[i] = Math.abs(a[i])for all indices - Throws:
NullPointerException- ifais null
-
axpy
public static double[] axpy(double[] a, double[] b, double alpha) Returns a new array equal to the fused AXPY operationa + alpha * b.This performs a single pass over the data to improve cache locality compared to separate scale and add steps.
Implementation Note: Uses a simple indexed
for-loop to enable JIT optimizations.- Parameters:
a- the left-hand array; must be non-nullb- the right-hand array; must be non-null and the same length asaalpha- the scalar multiplier forb- Returns:
- a newly allocated array where
out[i] = a[i] + alpha * b[i] - Throws:
NullPointerException- ifaorbis nullIllegalArgumentException- ifa.length != b.length
-
multiply
public static double[] multiply(double[] a, double[] b) Returns a new array equal to the element-wise multiplicationa * b.Implementation Note: Uses a simple indexed
for-loop to enable JIT optimizations (bounds-check elimination, loop unrolling, potential auto-vectorization).- Parameters:
a- the left-hand array; must be non-nullb- the right-hand array; must be non-null and the same length asa- Returns:
- a newly allocated array where
out[i] = a[i] * b[i]for all indices - Throws:
NullPointerException- ifaorbis nullIllegalArgumentException- ifa.length != b.length
-
divide
public static double[] divide(double[] a, double[] b) Returns a new array equal to the element-wise divisiona / b.Implementation Note: Uses a simple indexed
for-loop to enable JIT optimizations (bounds-check elimination, loop unrolling, potential auto-vectorization).- Parameters:
a- the left-hand array; must be non-nullb- the right-hand array; must be non-null and the same length asa- Returns:
- a newly allocated array where
out[i] = a[i] / b[i]for all indices - Throws:
NullPointerException- ifaorbis nullIllegalArgumentException- ifa.length != b.length
-
reciprocal
public static double[] reciprocal(double[] a) Returns a new array equal to the element-wise reciprocal1 / a.Implementation Note: Uses a simple indexed
for-loop to enable JIT optimizations (bounds-check elimination, loop unrolling, potential auto-vectorization).- Parameters:
a- the array; must be non-null- Returns:
- a newly allocated array where
out[i] = 1 / a[i]for all indices - Throws:
NullPointerException- ifais null
-
addInto
public static void addInto(double[] a, double[] b, double[] out) Computes the element-wise suma + band writes the result intoout.Implementation Note: Reusing
outavoids allocation and reduces GC pressure in hot paths.- Parameters:
a- the left-hand array; must be non-nullb- the right-hand array; must be non-null and the same length asaout- the destination array; must be non-null and the same length asa- Throws:
NullPointerException- if any argument is nullIllegalArgumentException- if lengths differ amonga,b, andout
-
scaleInto
public static void scaleInto(double[] a, double alpha, double[] out) Computes the element-wise scalingalpha * aand writes the result intoout.Implementation Note: Reusing
outavoids allocation and reduces GC pressure in hot paths.- Parameters:
a- the input array; must be non-nullalpha- the scalar multiplierout- the destination array; must be non-null and the same length asa- Throws:
NullPointerException- ifaoroutis nullIllegalArgumentException- ifout.length != a.length
-
axpyInto
public static void axpyInto(double[] a, double[] b, double alpha, double[] out) Computes the fused AXPY operationout = a + alpha * band writes the result intoout.Implementation Note: Performs a single pass over the arrays to maximize cache locality.
- Parameters:
a- the left-hand array; must be non-nullb- the right-hand array; must be non-null and the same length asaalpha- the scalar multiplier forbout- the destination array; must be non-null and the same length asa- Throws:
NullPointerException- if any argument is nullIllegalArgumentException- if lengths differ amonga,b, andout
-