From dd9b7b477a8ca235cc3dc69e9f58f804417fa117 Mon Sep 17 00:00:00 2001 From: Luc Maisonobe Date: Tue, 13 Oct 2009 15:56:51 +0000 Subject: [PATCH] improved array structure in Romberg integrator since the integrator uses only two rows at a time, we allocate only two arrays and reuse them git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@824822 13f79535-47bb-0310-9956-ffa450edef68 --- .../integration/RombergIntegrator.java | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/apache/commons/math/analysis/integration/RombergIntegrator.java b/src/main/java/org/apache/commons/math/analysis/integration/RombergIntegrator.java index 37107c831..83bf56f4d 100644 --- a/src/main/java/org/apache/commons/math/analysis/integration/RombergIntegrator.java +++ b/src/main/java/org/apache/commons/math/analysis/integration/RombergIntegrator.java @@ -67,37 +67,41 @@ public class RombergIntegrator extends UnivariateRealIntegratorImpl { final double min, final double max) throws MaxIterationsExceededException, FunctionEvaluationException, IllegalArgumentException { - int i = 1, j, m = maximalIterationCount + 1; - // Array structure here can be improved for better space - // efficiency because only the lower triangle is used. - double r, t[][] = new double[m][m], s, olds; + final int m = maximalIterationCount + 1; + double previousRow[] = new double[m]; + double currentRow[] = new double[m]; clearResult(); verifyInterval(min, max); verifyIterationCount(); TrapezoidIntegrator qtrap = new TrapezoidIntegrator(); - t[0][0] = qtrap.stage(f, min, max, 0); - olds = t[0][0]; - while (i <= maximalIterationCount) { - t[i][0] = qtrap.stage(f, min, max, i); - for (j = 1; j <= i; j++) { + currentRow[0] = qtrap.stage(f, min, max, 0); + double olds = currentRow[0]; + for (int i = 1; i <= maximalIterationCount; ++i) { + + // switch rows + final double[] tmpRow = previousRow; + previousRow = currentRow; + currentRow = tmpRow; + + currentRow[0] = qtrap.stage(f, min, max, i); + for (int j = 1; j <= i; j++) { // Richardson extrapolation coefficient - r = (1L << (2 * j)) -1; - t[i][j] = t[i][j-1] + (t[i][j-1] - t[i-1][j-1]) / r; + final double r = (1L << (2 * j)) - 1; + final double tIJm1 = currentRow[j - 1]; + currentRow[j] = tIJm1 + (tIJm1 - previousRow[j - 1]) / r; } - s = t[i][i]; + final double s = currentRow[i]; if (i >= minimalIterationCount) { - final double delta = Math.abs(s - olds); - final double rLimit = - relativeAccuracy * (Math.abs(olds) + Math.abs(s)) * 0.5; + final double delta = Math.abs(s - olds); + final double rLimit = relativeAccuracy * (Math.abs(olds) + Math.abs(s)) * 0.5; if ((delta <= rLimit) || (delta <= absoluteAccuracy)) { setResult(s, i); return result; } } olds = s; - i++; } throw new MaxIterationsExceededException(maximalIterationCount); }