MATH-1613: Decrease maximum number of iterations for consistency with internal counter.

This commit is contained in:
Gilles Sadowski 2021-06-25 14:50:01 +02:00
parent cd54910edc
commit 97dd402d10
4 changed files with 36 additions and 36 deletions

View File

@ -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);

View File

@ -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();
}
}
}

View File

@ -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);

View File

@ -93,6 +93,11 @@ Caveat:
nightmare was one of the main reasons for creating more focused
components.]
">
<action dev="erans" type="fix" issue="MATH-1613">
Decrease maximum number of iterations in "SimpsonIntegrator" and
"TrapezoidIntegrator" (due to the available range of the internal
counter of function evaluations).
</action>
<action dev="erans" type="fix" issue="MATH-1431" due-to="Artem Onuchin">
"EmpiricalDistribution" handles empty bin.
</action>