diff --git a/src/main/java/org/apache/commons/math4/analysis/integration/RombergIntegrator.java b/src/main/java/org/apache/commons/math4/analysis/integration/RombergIntegrator.java index 6205caa570..0ecb2f1dda 100644 --- a/src/main/java/org/apache/commons/math4/analysis/integration/RombergIntegrator.java +++ b/src/main/java/org/apache/commons/math4/analysis/integration/RombergIntegrator.java @@ -20,7 +20,6 @@ import org.apache.commons.math4.exception.NotStrictlyPositiveException; import org.apache.commons.math4.exception.NumberIsTooLargeException; import org.apache.commons.math4.exception.NumberIsTooSmallException; -import org.apache.commons.math4.exception.TooManyEvaluationsException; import org.apache.commons.math4.util.FastMath; /** @@ -58,7 +57,7 @@ public RombergIntegrator(final double relativeAccuracy, final double absoluteAccuracy, final int minimalIterationCount, final int maximalIterationCount) - throws NotStrictlyPositiveException, NumberIsTooSmallException, NumberIsTooLargeException { + throws NumberIsTooSmallException, NumberIsTooLargeException { super(relativeAccuracy, absoluteAccuracy, minimalIterationCount, maximalIterationCount); if (maximalIterationCount > ROMBERG_MAX_ITERATIONS_COUNT) { throw new NumberIsTooLargeException(maximalIterationCount, @@ -80,7 +79,7 @@ public RombergIntegrator(final double relativeAccuracy, */ public RombergIntegrator(final int minimalIterationCount, final int maximalIterationCount) - throws NotStrictlyPositiveException, NumberIsTooSmallException, NumberIsTooLargeException { + throws NumberIsTooSmallException, NumberIsTooLargeException { super(minimalIterationCount, maximalIterationCount); if (maximalIterationCount > ROMBERG_MAX_ITERATIONS_COUNT) { throw new NumberIsTooLargeException(maximalIterationCount, @@ -99,14 +98,14 @@ public RombergIntegrator() { /** {@inheritDoc} */ @Override protected double doIntegrate() - throws TooManyEvaluationsException, MaxCountExceededException { + throws MaxCountExceededException { final int m = iterations.getMaximalCount() + 1; - double previousRow[] = new double[m]; - double currentRow[] = new double[m]; + double[] previousRow = new double[m]; + double[] currentRow = new double[m]; TrapezoidIntegrator qtrap = new TrapezoidIntegrator(); - currentRow[0] = qtrap.stage(this, 0); + currentRow[0] = qtrap.stage(this); iterations.increment(); double olds = currentRow[0]; while (true) { diff --git a/src/main/java/org/apache/commons/math4/analysis/integration/SimpsonIntegrator.java b/src/main/java/org/apache/commons/math4/analysis/integration/SimpsonIntegrator.java index 19e61086ec..ef9236846a 100644 --- a/src/main/java/org/apache/commons/math4/analysis/integration/SimpsonIntegrator.java +++ b/src/main/java/org/apache/commons/math4/analysis/integration/SimpsonIntegrator.java @@ -16,7 +16,6 @@ */ package org.apache.commons.math4.analysis.integration; -import org.apache.commons.math4.exception.NotStrictlyPositiveException; import org.apache.commons.math4.exception.NumberIsTooLargeException; import org.apache.commons.math4.exception.NumberIsTooSmallException; import org.apache.commons.math4.util.FastMath; @@ -44,8 +43,6 @@ public class SimpsonIntegrator extends BaseAbstractUnivariateIntegrator { * @param minimalIterationCount minimum number of iterations * @param maximalIterationCount maximum number of iterations * (must be less than or equal to {@link #SIMPSON_MAX_ITERATIONS_COUNT}) - * @exception NotStrictlyPositiveException if minimal number of iterations - * is not strictly positive * @exception NumberIsTooSmallException if maximal number of iterations * is lesser than or equal to the minimal number of iterations * @exception NumberIsTooLargeException if maximal number of iterations @@ -55,7 +52,7 @@ public SimpsonIntegrator(final double relativeAccuracy, final double absoluteAccuracy, final int minimalIterationCount, final int maximalIterationCount) - throws NotStrictlyPositiveException, NumberIsTooSmallException, NumberIsTooLargeException { + throws NumberIsTooSmallException, NumberIsTooLargeException { super(relativeAccuracy, absoluteAccuracy, minimalIterationCount, maximalIterationCount); if (maximalIterationCount > SIMPSON_MAX_ITERATIONS_COUNT) { throw new NumberIsTooLargeException(maximalIterationCount, @@ -68,8 +65,6 @@ public SimpsonIntegrator(final double relativeAccuracy, * @param minimalIterationCount minimum number of iterations * @param maximalIterationCount maximum number of iterations * (must be less than or equal to {@link #SIMPSON_MAX_ITERATIONS_COUNT}) - * @exception NotStrictlyPositiveException if minimal number of iterations - * is not strictly positive * @exception NumberIsTooSmallException if maximal number of iterations * is lesser than or equal to the minimal number of iterations * @exception NumberIsTooLargeException if maximal number of iterations @@ -77,7 +72,7 @@ public SimpsonIntegrator(final double relativeAccuracy, */ public SimpsonIntegrator(final int minimalIterationCount, final int maximalIterationCount) - throws NotStrictlyPositiveException, NumberIsTooSmallException, NumberIsTooLargeException { + throws NumberIsTooSmallException, NumberIsTooLargeException { super(minimalIterationCount, maximalIterationCount); if (maximalIterationCount > SIMPSON_MAX_ITERATIONS_COUNT) { throw new NumberIsTooLargeException(maximalIterationCount, @@ -100,7 +95,7 @@ protected double doIntegrate() { // So we set the first sum using two trapezoid stages. final TrapezoidIntegrator qtrap = new TrapezoidIntegrator(); - final double s0 = qtrap.stage(this, 0); + final double s0 = qtrap.stage(this); double oldt = qtrap.stage(this, 1); double olds = (4 * oldt - s0) / 3.0; while (true) { diff --git a/src/main/java/org/apache/commons/math4/analysis/integration/TrapezoidIntegrator.java b/src/main/java/org/apache/commons/math4/analysis/integration/TrapezoidIntegrator.java index 0d8a7fbddb..60c97b868d 100644 --- a/src/main/java/org/apache/commons/math4/analysis/integration/TrapezoidIntegrator.java +++ b/src/main/java/org/apache/commons/math4/analysis/integration/TrapezoidIntegrator.java @@ -16,12 +16,7 @@ */ package org.apache.commons.math4.analysis.integration; -import org.apache.commons.math4.exception.MathIllegalArgumentException; -import org.apache.commons.math4.exception.MaxCountExceededException; -import org.apache.commons.math4.exception.NotStrictlyPositiveException; -import org.apache.commons.math4.exception.NumberIsTooLargeException; -import org.apache.commons.math4.exception.NumberIsTooSmallException; -import org.apache.commons.math4.exception.TooManyEvaluationsException; +import org.apache.commons.math4.exception.*; import org.apache.commons.math4.util.FastMath; /** @@ -59,7 +54,7 @@ public TrapezoidIntegrator(final double relativeAccuracy, final double absoluteAccuracy, final int minimalIterationCount, final int maximalIterationCount) - throws NotStrictlyPositiveException, NumberIsTooSmallException, NumberIsTooLargeException { + throws NumberIsTooSmallException, NumberIsTooLargeException { super(relativeAccuracy, absoluteAccuracy, minimalIterationCount, maximalIterationCount); if (maximalIterationCount > TRAPEZOID_MAX_ITERATIONS_COUNT) { throw new NumberIsTooLargeException(maximalIterationCount, @@ -80,7 +75,7 @@ public TrapezoidIntegrator(final double relativeAccuracy, */ public TrapezoidIntegrator(final int minimalIterationCount, final int maximalIterationCount) - throws NotStrictlyPositiveException, NumberIsTooSmallException, NumberIsTooLargeException { + throws NumberIsTooSmallException, NumberIsTooLargeException { super(minimalIterationCount, maximalIterationCount); if (maximalIterationCount > TRAPEZOID_MAX_ITERATIONS_COUNT) { throw new NumberIsTooLargeException(maximalIterationCount, @@ -97,9 +92,9 @@ public TrapezoidIntegrator() { } /** - * Compute the n-th stage integral of trapezoid rule. This function - * should only be called by API integrate() in the package. - * To save time it does not verify arguments - caller does. + * Compute the n-th stage (from first to up) integral of trapezoid rule. + * This function should only be called by API integrate() + * in the package. To save time it does not verify arguments - caller does. *

* The interval is divided equally into 2^n sections rather than an * arbitrary m sections because this configuration can best utilize the @@ -112,39 +107,58 @@ public TrapezoidIntegrator() { * is exceeded. */ double stage(final BaseAbstractUnivariateIntegrator baseIntegrator, final int n) - throws TooManyEvaluationsException { - - if (n == 0) { - final double max = baseIntegrator.getMax(); - final double min = baseIntegrator.getMin(); - s = 0.5 * (max - min) * - (baseIntegrator.computeObjectiveValue(min) + - baseIntegrator.computeObjectiveValue(max)); - return s; - } else { - final long np = 1L << (n-1); // number of new points in this stage - double sum = 0; - final double max = baseIntegrator.getMax(); - final double min = baseIntegrator.getMin(); - // spacing between adjacent new points - final double spacing = (max - min) / np; - double x = min + 0.5 * spacing; // the first new point - for (long i = 0; i < np; i++) { - sum += baseIntegrator.computeObjectiveValue(x); - x += spacing; - } - // add the new sum to previously calculated result - s = 0.5 * (s + sum * spacing); - return s; + throws TooManyEvaluationsException { + + if (n == 0) + throw new NumberIsTooSmallException(0, 1, true); + + final long np = 1L << (n - 1); // number of new points in this stage + double sum = 0; + final double max = baseIntegrator.getMax(); + final double min = baseIntegrator.getMin(); + // spacing between adjacent new points + final double spacing = (max - min) / np; + double x = min + 0.5 * spacing; // the first new point + for (long i = 0; i < np; i++) { + sum += baseIntegrator.computeObjectiveValue(x); + x += spacing; } + // add the new sum to previously calculated result + s = 0.5 * (s + sum * spacing); + return s; + } + + /** + * Compute the zero stage integral of trapezoid rule. This function + * should only be called by API integrate() in the package. + * To save time it does not verify arguments - caller does. + *

+ * The interval is divided equally into 2^n sections rather than an + * arbitrary m sections because this configuration can best utilize the + * already computed values.

+ * + * @param baseIntegrator integrator holding integration parameters + * @return the value of n-th stage integral + * @throws TooManyEvaluationsException if the maximal number of evaluations + * is exceeded. + */ + double stage(final BaseAbstractUnivariateIntegrator baseIntegrator) + throws TooManyEvaluationsException { + + final double max = baseIntegrator.getMax(); + final double min = baseIntegrator.getMin(); + s = 0.5 * (max - min) * + (baseIntegrator.computeObjectiveValue(min) + + baseIntegrator.computeObjectiveValue(max)); + return s; } /** {@inheritDoc} */ @Override protected double doIntegrate() - throws MathIllegalArgumentException, TooManyEvaluationsException, MaxCountExceededException { + throws MathIllegalArgumentException, MaxCountExceededException { - double oldt = stage(this, 0); + double oldt = stage(this); iterations.increment(); while (true) { final int i = iterations.getCount();