Author: luc Date: Fri Aug 10 18:58:58 2012 New Revision: 1371808 URL: http://svn.apache.org/viewvc?rev=1371808&view=rev Log: Added support for x^y in DerivativeStructure and DSCompiler.
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/DSCompiler.java 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/DSCompiler.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/DSCompiler.java?rev=1371808&r1=1371807&r2=1371808&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/DSCompiler.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/DSCompiler.java Fri Aug 10 18:58:58 2012 @@ -899,6 +899,26 @@ public class DSCompiler { } + /** Compute power of a derivative structure. + * @param x array holding the base + * @param xOffset offset of the base in its array + * @param y array holding the exponent + * @param yOffset offset of the exponent in its array + * @param result array where result must be stored (for + * power the result array <em>cannot</em> be the input + * array) + * @param resultOffset offset of the result in its array + */ + public void pow(final double[] x, final int xOffset, + final double[] y, final int yOffset, + final double[] result, final int resultOffset) { + final double[] logX = new double[getSize()]; + log(x, xOffset, logX, 0); + final double[] yLogX = new double[getSize()]; + multiply(logX, 0, y, yOffset, yLogX, 0); + exp(yLogX, 0, result, resultOffset); + } + /** Compute n<sup>th</sup> root of a derivative structure. * @param operand array holding the operand * @param operandOffset offset of the operand in its array 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=1371808&r1=1371807&r2=1371808&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 Fri Aug 10 18:58:58 2012 @@ -428,6 +428,19 @@ public class DerivativeStructure impleme return result; } + /** Power operation. + * @param e exponent + * @return this<sup>e</sup> + * @exception DimensionMismatchException if number of free parameters or orders are inconsistent + */ + public DerivativeStructure pow(final DerivativeStructure e) + throws DimensionMismatchException { + compiler.checkCompatibility(e.compiler); + final DerivativeStructure result = new DerivativeStructure(compiler); + compiler.pow(data, 0, e.data, 0, result.data, 0); + return result; + } + /** Exponential. * @return exponential of the instance */ 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=1371808&r1=1371807&r2=1371808&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 Fri Aug 10 18:58:58 2012 @@ -382,6 +382,28 @@ public class DerivativeStructureTest { } @Test + public void testPowReciprocalPow() { + double[] epsilon = new double[] { 2.0e-15, 2.0e-14, 3.0e-13, 8.0e-12, 3.0e-10 }; + for (int maxOrder = 0; maxOrder < 5; ++maxOrder) { + for (double x = 0.1; x < 1.2; x += 0.01) { + DerivativeStructure dsX = new DerivativeStructure(2, maxOrder, 0, x); + for (double y = 0.1; y < 1.2; y += 0.01) { + DerivativeStructure dsY = new DerivativeStructure(2, maxOrder, 1, y); + DerivativeStructure rebuiltX = dsX.pow(dsY).pow(dsY.reciprocal()); + DerivativeStructure zero = rebuiltX.subtract(dsX); + for (int n = 0; n <= maxOrder; ++n) { + for (int m = 0; m <= maxOrder; ++m) { + if (n + m <= maxOrder) { + Assert.assertEquals(0.0, zero.getPartialDerivative(n, m), epsilon[n + m]); + } + } + } + } + } + } + } + + @Test public void testExp() { double[] epsilon = new double[] { 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16 }; for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {