Author: luc
Date: Sat Aug 18 18:08:02 2012
New Revision: 1374626
URL: http://svn.apache.org/viewvc?rev=1374626&view=rev
Log:
Added compose to the public API of DerivativeStructure.
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructure.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructureTest.java
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructure.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructure.java?rev=1374626&r1=1374625&r2=1374626&view=diff
==============================================================================
---
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructure.java
(original)
+++
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructure.java
Sat Aug 18 18:08:02 2012
@@ -514,6 +514,23 @@ public class DerivativeStructure impleme
}
}
+ /** Compute composition of the instance by a univariate function.
+ * @param f array of value and derivatives of the function at
+ * the current point (i.e. [f({@link #getValue()}),
+ * f'({@link #getValue()}), f''({@link #getValue()})...]).
+ * @return f(this)
+ * @exception DimensionMismatchException if the number of derivatives
+ * in the array is not equal to {@link #getOrder() order} + 1
+ */
+ public DerivativeStructure compose(final double[] f) {
+ if (f.length != getOrder() + 1) {
+ throw new DimensionMismatchException(f.length, getOrder() + 1);
+ }
+ final DerivativeStructure result = new DerivativeStructure(compiler);
+ compiler.compose(data, 0, f, result.data, 0);
+ return result;
+ }
+
/** {@inheritDoc} */
public DerivativeStructure reciprocal() {
final DerivativeStructure result = new DerivativeStructure(compiler);
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructureTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructureTest.java?rev=1374626&r1=1374625&r2=1374626&view=diff
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructureTest.java
(original)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructureTest.java
Sat Aug 18 18:08:02 2012
@@ -20,6 +20,7 @@ package org.apache.commons.math3.analysi
import java.util.Arrays;
import java.util.List;
+import org.apache.commons.math3.analysis.polynomials.PolynomialFunction;
import org.apache.commons.math3.exception.DimensionMismatchException;
import org.apache.commons.math3.exception.NumberIsTooLargeException;
import org.apache.commons.math3.util.ArithmeticUtils;
@@ -974,6 +975,41 @@ public class DerivativeStructureTest {
}
}
+ @Test(expected=DimensionMismatchException.class)
+ public void testComposeMismatchedDimensions() {
+ new DerivativeStructure(1, 3, 0, 1.2).compose(new double[3]);
+ }
+
+ @Test
+ public void testCompose() {
+ double[] epsilon = new double[] { 1.0e-20, 5.0e-14, 2.0e-13, 3.0e-13,
2.0e-13, 1.0e-20 };
+ PolynomialFunction poly =
+ new PolynomialFunction(new double[] { 1.0, 2.0, 3.0, 4.0, 5.0,
6.0 });
+ for (int maxOrder = 0; maxOrder < 6; ++maxOrder) {
+ PolynomialFunction[] p = new PolynomialFunction[maxOrder + 1];
+ p[0] = poly;
+ for (int i = 1; i <= maxOrder; ++i) {
+ p[i] = p[i - 1].polynomialDerivative();
+ }
+ for (double x = 0.1; x < 1.2; x += 0.001) {
+ DerivativeStructure dsX = new DerivativeStructure(1, maxOrder,
0, x);
+ DerivativeStructure dsY1 = dsX.getField().getZero();
+ for (int i = poly.degree(); i >= 0; --i) {
+ dsY1 = dsY1.multiply(dsX).add(poly.getCoefficients()[i]);
+ }
+ double[] f = new double[maxOrder + 1];
+ for (int i = 0; i < f.length; ++i) {
+ f[i] = p[i].value(x);
+ }
+ DerivativeStructure dsY2 = dsX.compose(f);
+ DerivativeStructure zero = dsY1.subtract(dsY2);
+ for (int n = 0; n <= maxOrder; ++n) {
+ Assert.assertEquals(0.0, zero.getPartialDerivative(n),
epsilon[n]);
+ }
+ }
+ }
+ }
+
@Test
public void testField() {
for (int maxOrder = 1; maxOrder < 5; ++maxOrder) {