Fixed DerivativeStructure.pow(0.0). x^0 is always 1.0, regardless of x (even if x is NaN in fact).
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/c64856ff Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/c64856ff Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/c64856ff Branch: refs/heads/develop Commit: c64856ff7f49c1c2e3098a36e5951f6e3a6cc366 Parents: 5c341d9 Author: Luc Maisonobe <[email protected]> Authored: Fri Aug 5 15:00:08 2016 +0200 Committer: Emmanuel Bourg <[email protected]> Committed: Fri Aug 5 15:00:08 2016 +0200 ---------------------------------------------------------------------- .../analysis/differentiation/DSCompiler.java | 7 +++++++ .../DerivativeStructureTest.java | 22 ++++++++++++++++++++ 2 files changed, 29 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-math/blob/c64856ff/src/main/java/org/apache/commons/math4/analysis/differentiation/DSCompiler.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/analysis/differentiation/DSCompiler.java b/src/main/java/org/apache/commons/math4/analysis/differentiation/DSCompiler.java index 7515a18..7d5c163 100644 --- a/src/main/java/org/apache/commons/math4/analysis/differentiation/DSCompiler.java +++ b/src/main/java/org/apache/commons/math4/analysis/differentiation/DSCompiler.java @@ -886,6 +886,13 @@ public class DSCompiler { public void pow(final double[] operand, final int operandOffset, final double p, final double[] result, final int resultOffset) { + if (p == 0) { + // special case, x^0 = 1 for all x + result[resultOffset] = 1.0; + Arrays.fill(result, resultOffset + 1, resultOffset + getSize(), 0); + return; + } + // create the function value and derivatives // [x^p, px^(p-1), p(p-1)x^(p-2), ... ] double[] function = new double[1 + order]; http://git-wip-us.apache.org/repos/asf/commons-math/blob/c64856ff/src/test/java/org/apache/commons/math4/analysis/differentiation/DerivativeStructureTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/math4/analysis/differentiation/DerivativeStructureTest.java b/src/test/java/org/apache/commons/math4/analysis/differentiation/DerivativeStructureTest.java index 8d4bf50..8d0c733 100644 --- a/src/test/java/org/apache/commons/math4/analysis/differentiation/DerivativeStructureTest.java +++ b/src/test/java/org/apache/commons/math4/analysis/differentiation/DerivativeStructureTest.java @@ -31,6 +31,7 @@ import org.apache.commons.math4.rng.RandomSource; import org.apache.commons.math4.util.ArithmeticUtils; import org.apache.commons.math4.util.CombinatoricsUtils; import org.apache.commons.math4.util.FastMath; +import org.apache.commons.math4.util.Precision; import org.junit.Assert; import org.junit.Test; @@ -323,6 +324,27 @@ public class DerivativeStructureTest extends ExtendedFieldElementAbstractTest<De Assert.assertTrue(Double.isNaN(zeroZero.getPartialDerivative(1, 1, 0))); } + // very special case: 0^0 where the power is a primitive + DerivativeStructure zeroDsZeroDouble = new DerivativeStructure(3, maxOrder, 0, 0.0).pow(0.0); + boolean first = true; + for (final double d : zeroDsZeroDouble.getAllDerivatives()) { + if (first) { + Assert.assertEquals(1.0, d, Precision.EPSILON); + first = false; + } else { + Assert.assertEquals(0.0, d, Precision.SAFE_MIN); + } + } + DerivativeStructure zeroDsZeroInt = new DerivativeStructure(3, maxOrder, 0, 0.0).pow(0); + first = true; + for (final double d : zeroDsZeroInt.getAllDerivatives()) { + if (first) { + Assert.assertEquals(1.0, d, Precision.EPSILON); + first = false; + } else { + Assert.assertEquals(0.0, d, Precision.SAFE_MIN); + } + } } }
