From 6a06f4fdaac13ac1fff12fd4ac47539f9541193b Mon Sep 17 00:00:00 2001 From: Gilles Sadowski Date: Mon, 6 Apr 2020 13:35:19 +0200 Subject: [PATCH] Avoid multiple accesses to the same array's location. --- .../interpolation/SplineInterpolator.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/apache/commons/math4/analysis/interpolation/SplineInterpolator.java b/src/main/java/org/apache/commons/math4/analysis/interpolation/SplineInterpolator.java index 1b23bba3d..22f4586f5 100644 --- a/src/main/java/org/apache/commons/math4/analysis/interpolation/SplineInterpolator.java +++ b/src/main/java/org/apache/commons/math4/analysis/interpolation/SplineInterpolator.java @@ -91,10 +91,14 @@ public class SplineInterpolator implements UnivariateInterpolator { final double[] z = new double[n + 1]; double g = 0; for (int i = 1; i < n; i++) { - g = 2d * (x[i+1] - x[i - 1]) - h[i - 1] * mu[i -1]; - mu[i] = h[i] / g; - z[i] = (3d * (y[i + 1] * h[i - 1] - y[i] * (x[i + 1] - x[i - 1])+ y[i - 1] * h[i]) / - (h[i - 1] * h[i]) - h[i - 1] * z[i - 1]) / g; + final double xIp1 = x[i + 1]; + final double xIm1 = x[i - 1]; + final double hIm1 = h[i - 1]; + final double hI = h[i]; + g = 2d * (xIp1 - xIm1) - hIm1 * mu[i -1]; + mu[i] = hI / g; + z[i] = (3d * (y[i + 1] * hIm1 - y[i] * (xIp1 - xIm1)+ y[i - 1] * hI) / + (hIm1 * hI) - hIm1 * z[i - 1]) / g; } // cubic spline coefficients -- b is linear, c quadratic, d is cubic (original y's are constants) @@ -103,9 +107,12 @@ public class SplineInterpolator implements UnivariateInterpolator { final double[] d = new double[n]; for (int j = n -1; j >=0; j--) { - c[j] = z[j] - mu[j] * c[j + 1]; - b[j] = (y[j + 1] - y[j]) / h[j] - h[j] * (c[j + 1] + 2d * c[j]) / 3d; - d[j] = (c[j + 1] - c[j]) / (3d * h[j]); + final double cJp1 = c[j + 1]; + final double cJ = z[j] - mu[j] * cJp1; + final double hJ = h[j]; + b[j] = (y[j + 1] - y[j]) / hJ - hJ * (cJp1 + 2d * cJ) / 3d; + c[j] = cJ; + d[j] = (cJp1 - cJ) / (3d * hJ); } final PolynomialFunction[] polynomials = new PolynomialFunction[n];