diff --git a/src/main/java/org/apache/commons/math3/analysis/differentiation/FiniteDifferencesDifferentiator.java b/src/main/java/org/apache/commons/math3/analysis/differentiation/FiniteDifferencesDifferentiator.java index 76effb9d6..63fcd27d6 100644 --- a/src/main/java/org/apache/commons/math3/analysis/differentiation/FiniteDifferencesDifferentiator.java +++ b/src/main/java/org/apache/commons/math3/analysis/differentiation/FiniteDifferencesDifferentiator.java @@ -28,7 +28,7 @@ import org.apache.commons.math3.exception.NumberIsTooSmallException; /** Univariate functions differentiator using finite differences. *

- * This class creates some wrapper objetcs around regular + * This class creates some wrapper objects around regular * {@link UnivariateFunction univariate functions} (or {@link * UnivariateVectorFunction univariate vector functions} or {@link * UnivariateMatrixFunction univariate matrix functions}). These @@ -136,12 +136,6 @@ public class FiniteDifferencesDifferentiator private DerivativeStructure evaluate(final DerivativeStructure t, final double[] y) throws NumberIsTooLargeException { - // check we can achieve the requested derivation order with the sample - final int order = t.getOrder(); - if (order >= nbPoints) { - throw new NumberIsTooLargeException(order, nbPoints, false); - } - // create divided differences diagonal arrays final double[] top = new double[nbPoints]; final double[] bottom = new double[nbPoints]; @@ -160,6 +154,7 @@ public class FiniteDifferencesDifferentiator } // evaluate interpolation polynomial (represented by top diagonal) at t + final int order = t.getOrder(); final int parameters = t.getFreeParameters(); final double[] derivatives = t.getAllDerivatives(); DerivativeStructure interpolation = new DerivativeStructure(parameters, order, 0.0); @@ -193,6 +188,11 @@ public class FiniteDifferencesDifferentiator public DerivativeStructure value(final DerivativeStructure t) throws MathIllegalArgumentException { + // check we can achieve the requested derivation order with the sample + if (t.getOrder() >= nbPoints) { + throw new NumberIsTooLargeException(t.getOrder(), nbPoints, false); + } + // get sample points centered around t value final double t0 = t.getValue(); final double[] y = new double[nbPoints]; @@ -227,6 +227,11 @@ public class FiniteDifferencesDifferentiator public DerivativeStructure[] value(final DerivativeStructure t) throws MathIllegalArgumentException { + // check we can achieve the requested derivation order with the sample + if (t.getOrder() >= nbPoints) { + throw new NumberIsTooLargeException(t.getOrder(), nbPoints, false); + } + // get sample points centered around t value final double t0 = t.getValue(); double[][] y = null; @@ -272,6 +277,11 @@ public class FiniteDifferencesDifferentiator public DerivativeStructure[][] value(final DerivativeStructure t) throws MathIllegalArgumentException { + // check we can achieve the requested derivation order with the sample + if (t.getOrder() >= nbPoints) { + throw new NumberIsTooLargeException(t.getOrder(), nbPoints, false); + } + // get sample points centered around t value final double t0 = t.getValue(); double[][][] y = null; diff --git a/src/test/java/org/apache/commons/math3/analysis/differentiation/FiniteDifferencesDifferentiatorTest.java b/src/test/java/org/apache/commons/math3/analysis/differentiation/FiniteDifferencesDifferentiatorTest.java index f2487522b..7bc07e6b0 100644 --- a/src/test/java/org/apache/commons/math3/analysis/differentiation/FiniteDifferencesDifferentiatorTest.java +++ b/src/test/java/org/apache/commons/math3/analysis/differentiation/FiniteDifferencesDifferentiatorTest.java @@ -24,7 +24,9 @@ import org.apache.commons.math3.analysis.UnivariateMatrixFunction; import org.apache.commons.math3.analysis.UnivariateVectorFunction; import org.apache.commons.math3.analysis.function.Gaussian; import org.apache.commons.math3.analysis.function.Sin; +import org.apache.commons.math3.exception.MathInternalError; import org.apache.commons.math3.exception.NotPositiveException; +import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; @@ -160,6 +162,45 @@ public class FiniteDifferencesDifferentiatorTest { } + @Test(expected=NumberIsTooLargeException.class) + public void testWrongOrder() { + UnivariateDifferentiableFunction f = + new FiniteDifferencesDifferentiator(3, 0.01).differentiate(new UnivariateFunction() { + public double value(double x) { + // this exception should not be thrown because wrong order + // should be detected before function call + throw new MathInternalError(); + } + }); + f.value(new DerivativeStructure(1, 3, 0, 1.0)); + } + + @Test(expected=NumberIsTooLargeException.class) + public void testWrongOrderVector() { + UnivariateDifferentiableVectorFunction f = + new FiniteDifferencesDifferentiator(3, 0.01).differentiate(new UnivariateVectorFunction() { + public double[] value(double x) { + // this exception should not be thrown because wrong order + // should be detected before function call + throw new MathInternalError(); + } + }); + f.value(new DerivativeStructure(1, 3, 0, 1.0)); + } + + @Test(expected=NumberIsTooLargeException.class) + public void testWrongOrderMatrix() { + UnivariateDifferentiableMatrixFunction f = + new FiniteDifferencesDifferentiator(3, 0.01).differentiate(new UnivariateMatrixFunction() { + public double[][] value(double x) { + // this exception should not be thrown because wrong order + // should be detected before function call + throw new MathInternalError(); + } + }); + f.value(new DerivativeStructure(1, 3, 0, 1.0)); + } + @Test public void testVectorFunction() {