From 97dd402d1019f751ae43825a83d679a07a2beaa2 Mon Sep 17 00:00:00 2001 From: Gilles Sadowski Date: Fri, 25 Jun 2021 14:50:01 +0200 Subject: [PATCH] MATH-1613: Decrease maximum number of iterations for consistency with internal counter. --- .../integration/SimpsonIntegrator.java | 28 +++++++++---------- .../integration/TrapezoidIntegrator.java | 25 ++++++++--------- .../integration/SimpsonIntegratorTest.java | 14 +++++----- src/changes/changes.xml | 5 ++++ 4 files changed, 36 insertions(+), 36 deletions(-) diff --git a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/analysis/integration/SimpsonIntegrator.java b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/analysis/integration/SimpsonIntegrator.java index a770a43cd..2ce475bd5 100644 --- a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/analysis/integration/SimpsonIntegrator.java +++ b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/analysis/integration/SimpsonIntegrator.java @@ -40,9 +40,8 @@ import org.apache.commons.math4.legacy.core.jdkmath.AccurateMath; * @since 1.2 */ public class SimpsonIntegrator extends BaseAbstractUnivariateIntegrator { - /** Maximal number of iterations for Simpson. */ - public static final int SIMPSON_MAX_ITERATIONS_COUNT = 63; + private static final int SIMPSON_MAX_ITERATIONS_COUNT = 30; /** * Build a Simpson integrator with given accuracies and iterations counts. @@ -50,13 +49,13 @@ public class SimpsonIntegrator extends BaseAbstractUnivariateIntegrator { * @param absoluteAccuracy absolute accuracy of the result * @param minimalIterationCount Minimum number of iterations. * @param maximalIterationCount Maximum number of iterations. - * It must be less than or equal to {@link #SIMPSON_MAX_ITERATIONS_COUNT}. - * @exception org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException if minimal number of iterations - * is not strictly positive - * @exception org.apache.commons.math4.legacy.exception.NumberIsTooSmallException if maximal number of iterations + * It must be less than or equal to 30. + * @throws org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException + * if {@code minimalIterationCount <= 0}. + * @throws org.apache.commons.math4.legacy.exception.NumberIsTooSmallException + * if {@code maximalIterationCount < minimalIterationCount}. * is lesser than or equal to the minimal number of iterations - * @exception NumberIsTooLargeException if maximal number of iterations - * is greater than {@link #SIMPSON_MAX_ITERATIONS_COUNT} + * @throws NumberIsTooLargeException if {@code maximalIterationCount > 30}. */ public SimpsonIntegrator(final double relativeAccuracy, final double absoluteAccuracy, @@ -73,13 +72,13 @@ public class SimpsonIntegrator extends BaseAbstractUnivariateIntegrator { * Build a Simpson integrator with given iteration counts. * @param minimalIterationCount Minimum number of iterations. * @param maximalIterationCount Maximum number of iterations. - * It must be less than or equal to {@link #SIMPSON_MAX_ITERATIONS_COUNT}. - * @exception org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException if minimal number of iterations - * is not strictly positive - * @exception org.apache.commons.math4.legacy.exception.NumberIsTooSmallException if maximal number of iterations + * It must be less than or equal to 30. + * @throws org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException + * if {@code minimalIterationCount <= 0}. + * @throws org.apache.commons.math4.legacy.exception.NumberIsTooSmallException + * if {@code maximalIterationCount < minimalIterationCount}. * is lesser than or equal to the minimal number of iterations - * @exception NumberIsTooLargeException if maximal number of iterations - * is greater than {@link #SIMPSON_MAX_ITERATIONS_COUNT} + * @throws NumberIsTooLargeException if {@code maximalIterationCount > 30}. */ public SimpsonIntegrator(final int minimalIterationCount, final int maximalIterationCount) { @@ -92,7 +91,6 @@ public class SimpsonIntegrator extends BaseAbstractUnivariateIntegrator { /** * Construct an integrator with default settings. - * (max iteration count set to {@link #SIMPSON_MAX_ITERATIONS_COUNT}) */ public SimpsonIntegrator() { super(DEFAULT_MIN_ITERATIONS_COUNT, SIMPSON_MAX_ITERATIONS_COUNT); diff --git a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/analysis/integration/TrapezoidIntegrator.java b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/analysis/integration/TrapezoidIntegrator.java index 8a1f5dacd..f009df0bb 100644 --- a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/analysis/integration/TrapezoidIntegrator.java +++ b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/analysis/integration/TrapezoidIntegrator.java @@ -39,9 +39,8 @@ import org.apache.commons.math4.legacy.core.jdkmath.AccurateMath; * @since 1.2 */ public class TrapezoidIntegrator extends BaseAbstractUnivariateIntegrator { - /** Maximum number of iterations for trapezoid. */ - private static final int TRAPEZOID_MAX_ITERATIONS_COUNT = 63; + private static final int TRAPEZOID_MAX_ITERATIONS_COUNT = 30; /** Intermediate result. */ private double s; @@ -52,12 +51,12 @@ public class TrapezoidIntegrator extends BaseAbstractUnivariateIntegrator { * @param absoluteAccuracy absolute accuracy of the result * @param minimalIterationCount minimum number of iterations * @param maximalIterationCount maximum number of iterations - * @exception org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException if minimal number of iterations - * is not strictly positive - * @exception org.apache.commons.math4.legacy.exception.NumberIsTooSmallException if maximal number of iterations + * @throws org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException + * if {@code minimalIterationCount <= 0}. + * @throws org.apache.commons.math4.legacy.exception.NumberIsTooSmallException + * if {@code maximalIterationCount < minimalIterationCount}. * is lesser than or equal to the minimal number of iterations - * @exception NumberIsTooLargeException if maximal number of iterations - * is greater than 63. + * @throws NumberIsTooLargeException if {@code maximalIterationCount > 30}. */ public TrapezoidIntegrator(final double relativeAccuracy, final double absoluteAccuracy, @@ -74,12 +73,12 @@ public class TrapezoidIntegrator extends BaseAbstractUnivariateIntegrator { * Build a trapezoid integrator with given iteration counts. * @param minimalIterationCount minimum number of iterations * @param maximalIterationCount maximum number of iterations - * @exception org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException if minimal number of iterations - * is not strictly positive - * @exception org.apache.commons.math4.legacy.exception.NumberIsTooSmallException if maximal number of iterations + * @throws org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException + * if {@code minimalIterationCount <= 0}. + * @throws org.apache.commons.math4.legacy.exception.NumberIsTooSmallException + * if {@code maximalIterationCount < minimalIterationCount}. * is lesser than or equal to the minimal number of iterations - * @exception NumberIsTooLargeException if maximal number of iterations - * is greater than 63. + * @throws NumberIsTooLargeException if {@code maximalIterationCount > 30}. */ public TrapezoidIntegrator(final int minimalIterationCount, final int maximalIterationCount) { @@ -92,7 +91,6 @@ public class TrapezoidIntegrator extends BaseAbstractUnivariateIntegrator { /** * Construct a trapezoid integrator with default settings. - * (max iteration count set to {@link #TRAPEZOID_MAX_ITERATIONS_COUNT}) */ public TrapezoidIntegrator() { super(DEFAULT_MIN_ITERATIONS_COUNT, TRAPEZOID_MAX_ITERATIONS_COUNT); @@ -158,6 +156,5 @@ public class TrapezoidIntegrator extends BaseAbstractUnivariateIntegrator { oldt = t; iterations.increment(); } - } } diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/analysis/integration/SimpsonIntegratorTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/analysis/integration/SimpsonIntegratorTest.java index 672db79c1..0284ea08f 100644 --- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/analysis/integration/SimpsonIntegratorTest.java +++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/analysis/integration/SimpsonIntegratorTest.java @@ -21,6 +21,7 @@ import org.apache.commons.math4.legacy.analysis.UnivariateFunction; import org.apache.commons.math4.legacy.analysis.function.Identity; import org.apache.commons.math4.legacy.analysis.function.Inverse; import org.apache.commons.math4.legacy.analysis.function.Sin; +import org.apache.commons.math4.legacy.analysis.polynomials.PolynomialsUtils; import org.apache.commons.math4.legacy.exception.NumberIsTooLargeException; import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException; import org.apache.commons.math4.legacy.core.jdkmath.AccurateMath; @@ -36,6 +37,7 @@ import org.junit.Test; * */ public final class SimpsonIntegratorTest { + private static final int SIMPSON_MAX_ITERATIONS_COUNT = 30; /** * Test of integrator for the sine function. @@ -114,7 +116,7 @@ public final class SimpsonIntegratorTest { } try { // bad iteration limits - new SimpsonIntegrator(10, SimpsonIntegrator.SIMPSON_MAX_ITERATIONS_COUNT + 1); + new SimpsonIntegrator(10, SIMPSON_MAX_ITERATIONS_COUNT + 1); Assert.fail("Expecting NumberIsTooLargeException - bad iteration limits"); } catch (NumberIsTooLargeException ex) { // expected @@ -136,8 +138,7 @@ public final class SimpsonIntegratorTest { @Test public void testIterationIsPossibleWhenMinimalIterationCountIs1() { UnivariateFunction f = new Sin(); - UnivariateIntegrator integrator = new SimpsonIntegrator(1, - SimpsonIntegrator.SIMPSON_MAX_ITERATIONS_COUNT); + UnivariateIntegrator integrator = new SimpsonIntegrator(1, SIMPSON_MAX_ITERATIONS_COUNT); // The range or result is not relevant. // This sum should not converge at 1 iteration. // This tests iteration occurred. @@ -156,8 +157,7 @@ public final class SimpsonIntegratorTest { public void testConvergenceIsPossibleAtIteration1() { // A linear function y=x should converge immediately UnivariateFunction f = new Identity(); - UnivariateIntegrator integrator = new SimpsonIntegrator(1, - SimpsonIntegrator.SIMPSON_MAX_ITERATIONS_COUNT); + UnivariateIntegrator integrator = new SimpsonIntegrator(1, SIMPSON_MAX_ITERATIONS_COUNT); double min, max, expected, result, tolerance; @@ -282,7 +282,7 @@ public final class SimpsonIntegratorTest { // Set convergence criteria to force immediate convergence UnivariateIntegrator integrator = new SimpsonIntegrator( 0, Double.POSITIVE_INFINITY, - 1, SimpsonIntegrator.SIMPSON_MAX_ITERATIONS_COUNT); + 1, SIMPSON_MAX_ITERATIONS_COUNT); double min, max, expected, result, tolerance; // MATH-1458: minimalIterationCount==1 computes incorrect @@ -364,7 +364,7 @@ public final class SimpsonIntegratorTest { // Use minimalIterationCount>1 UnivariateIntegrator integrator = new SimpsonIntegrator( 0, absoluteAccuracy, - 2, SimpsonIntegrator.SIMPSON_MAX_ITERATIONS_COUNT); + 2, SIMPSON_MAX_ITERATIONS_COUNT); result = integrator.integrate(evaluations, f, min, max); diff --git a/src/changes/changes.xml b/src/changes/changes.xml index b4b1900c0..ddee0b76f 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -93,6 +93,11 @@ Caveat: nightmare was one of the main reasons for creating more focused components.] "> + + Decrease maximum number of iterations in "SimpsonIntegrator" and + "TrapezoidIntegrator" (due to the available range of the internal + counter of function evaluations). + "EmpiricalDistribution" handles empty bin.