From d26fd0236a9456d1c3f7cce709208f86158c7ad2 Mon Sep 17 00:00:00 2001 From: Luc Maisonobe Date: Thu, 16 Aug 2012 10:07:59 +0000 Subject: [PATCH] Added signum to DerivativeStructure. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1373781 13f79535-47bb-0310-9956-ffa450edef68 --- .../differentiation/DerivativeStructure.java | 11 +++++++++ .../DerivativeStructureTest.java | 23 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/main/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructure.java b/src/main/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructure.java index 2f0270217..12b1471f2 100644 --- a/src/main/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructure.java +++ b/src/main/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructure.java @@ -402,6 +402,17 @@ public class DerivativeStructure implements FieldElement, S return FastMath.round(data[0]); } + /** Compute the signum of the instance. + * The signum is -1 for negative numbers, +1 for positive numbers and 0 otherwise + * @param a number on which evaluation is done + * @return -1.0, -0.0, +0.0, +1.0 or NaN depending on sign of a + */ + public DerivativeStructure signum() { + return new DerivativeStructure(compiler.getFreeParameters(), + compiler.getOrder(), + FastMath.signum(data[0])); + } + /** * Returns the instance with the sign of the argument. * A NaN {@code sign} argument is treated as positive. diff --git a/src/test/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructureTest.java b/src/test/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructureTest.java index 60135c523..a329939e8 100644 --- a/src/test/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructureTest.java +++ b/src/test/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructureTest.java @@ -837,6 +837,29 @@ public class DerivativeStructureTest { } + @Test + public void testSignum() { + + DerivativeStructure minusOne = new DerivativeStructure(1, 1, 0, -1.0); + Assert.assertEquals(-1.0, minusOne.signum().getPartialDerivative(0), 1.0e-15); + Assert.assertEquals( 0.0, minusOne.signum().getPartialDerivative(1), 1.0e-15); + + DerivativeStructure plusOne = new DerivativeStructure(1, 1, 0, +1.0); + Assert.assertEquals(+1.0, plusOne.signum().getPartialDerivative(0), 1.0e-15); + Assert.assertEquals( 0.0, plusOne.signum().getPartialDerivative(1), 1.0e-15); + + DerivativeStructure minusZero = new DerivativeStructure(1, 1, 0, -0.0); + Assert.assertEquals(-0.0, minusZero.signum().getPartialDerivative(0), 1.0e-15); + Assert.assertTrue(Double.doubleToLongBits(minusZero.signum().getValue()) < 0); + Assert.assertEquals( 0.0, minusZero.signum().getPartialDerivative(1), 1.0e-15); + + DerivativeStructure plusZero = new DerivativeStructure(1, 1, 0, +0.0); + Assert.assertEquals(+0.0, plusZero.signum().getPartialDerivative(0), 1.0e-15); + Assert.assertTrue(Double.doubleToLongBits(plusZero.signum().getValue()) == 0); + Assert.assertEquals( 0.0, plusZero.signum().getPartialDerivative(1), 1.0e-15); + + } + @Test public void testCeilFloorRintLong() {