Added signum to DerivativeStructure.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1373781 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2012-08-16 10:07:59 +00:00
parent 7b090c5eec
commit d26fd0236a
2 changed files with 34 additions and 0 deletions

View File

@ -402,6 +402,17 @@ public class DerivativeStructure implements FieldElement<DerivativeStructure>, 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.

View File

@ -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() {