You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by lu...@apache.org on 2012/09/18 11:07:54 UTC
svn commit: r1387064 - in /commons/proper/math/trunk/src:
main/java/org/apache/commons/math3/analysis/differentiation/FiniteDifferencesDifferentiator.java
test/java/org/apache/commons/math3/analysis/differentiation/FiniteDifferencesDifferentiatorTest.java
Author: luc
Date: Tue Sep 18 09:07:54 2012
New Revision: 1387064
URL: http://svn.apache.org/viewvc?rev=1387064&view=rev
Log:
Fail early on wrong derivation order.
If the user asks for a derivation order that is too large for the number
of points in the finite differences algorithm, we now detect it before
the first call to the underlying function.
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/FiniteDifferencesDifferentiator.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/differentiation/FiniteDifferencesDifferentiatorTest.java
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/FiniteDifferencesDifferentiator.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/FiniteDifferencesDifferentiator.java?rev=1387064&r1=1387063&r2=1387064&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/FiniteDifferencesDifferentiator.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/FiniteDifferencesDifferentiator.java Tue Sep 18 09:07:54 2012
@@ -28,7 +28,7 @@ import org.apache.commons.math3.exceptio
/** Univariate functions differentiator using finite differences.
* <p>
- * 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 FiniteDifferencesDifferenti
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 FiniteDifferencesDifferenti
}
// 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 FiniteDifferencesDifferenti
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 FiniteDifferencesDifferenti
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 FiniteDifferencesDifferenti
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;
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/differentiation/FiniteDifferencesDifferentiatorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/differentiation/FiniteDifferencesDifferentiatorTest.java?rev=1387064&r1=1387063&r2=1387064&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/differentiation/FiniteDifferencesDifferentiatorTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/differentiation/FiniteDifferencesDifferentiatorTest.java Tue Sep 18 09:07:54 2012
@@ -24,7 +24,9 @@ import org.apache.commons.math3.analysis
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 FiniteDifferencesDifferenti
}
+ @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() {