Added toDegrees and toRadians to DerivativeStructure.

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

View File

@ -757,6 +757,28 @@ public class DerivativeStructure implements FieldElement<DerivativeStructure>, S
return result;
}
/** Convert radians to degrees, with error of less than 0.5 ULP
* @return instance converted into degrees
*/
public DerivativeStructure toDegrees() {
final DerivativeStructure ds = new DerivativeStructure(compiler);
for (int i = 0; i < ds.data.length; ++i) {
ds.data[i] = FastMath.toDegrees(data[i]);
}
return ds;
}
/** Convert degrees to radians, with error of less than 0.5 ULP
* @return instance converted into radians
*/
public DerivativeStructure toRadians() {
final DerivativeStructure ds = new DerivativeStructure(compiler);
for (int i = 0; i < ds.data.length; ++i) {
ds.data[i] = FastMath.toRadians(data[i]);
}
return ds;
}
/** Evaluate Taylor expansion a derivative structure.
* @param delta parameters offsets (&Delta;x, &Delta;y, ...)
* @return value of the Taylor expansion at x + &Delta;x, y + &Delta;y, ...

View File

@ -892,6 +892,57 @@ public class DerivativeStructureTest {
Assert.assertEquals(-1.0, minusOne.copySign(Double.NaN).getPartialDerivative(1), 1.0e-15);
}
@Test
public void testToDegreesDefinition() {
double epsilon = 3.0e-16;
for (int maxOrder = 0; maxOrder < 6; ++maxOrder) {
for (double x = 0.1; x < 1.2; x += 0.001) {
DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x);
Assert.assertEquals(FastMath.toDegrees(x), dsX.toDegrees().getValue(), epsilon);
for (int n = 1; n <= maxOrder; ++n) {
if (n == 1) {
Assert.assertEquals(180 / FastMath.PI, dsX.toDegrees().getPartialDerivative(1), epsilon);
} else {
Assert.assertEquals(0.0, dsX.toDegrees().getPartialDerivative(n), epsilon);
}
}
}
}
}
@Test
public void testToRadiansDefinition() {
double epsilon = 3.0e-16;
for (int maxOrder = 0; maxOrder < 6; ++maxOrder) {
for (double x = 0.1; x < 1.2; x += 0.001) {
DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x);
Assert.assertEquals(FastMath.toRadians(x), dsX.toRadians().getValue(), epsilon);
for (int n = 1; n <= maxOrder; ++n) {
if (n == 1) {
Assert.assertEquals(FastMath.PI / 180, dsX.toRadians().getPartialDerivative(1), epsilon);
} else {
Assert.assertEquals(0.0, dsX.toRadians().getPartialDerivative(n), epsilon);
}
}
}
}
}
@Test
public void testDegRad() {
double epsilon = 3.0e-16;
for (int maxOrder = 0; maxOrder < 6; ++maxOrder) {
for (double x = 0.1; x < 1.2; x += 0.001) {
DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x);
DerivativeStructure rebuiltX = dsX.toDegrees().toRadians();
DerivativeStructure zero = rebuiltX.subtract(dsX);
for (int n = 0; n <= maxOrder; ++n) {
Assert.assertEquals(0.0, zero.getPartialDerivative(n), epsilon);
}
}
}
}
@Test
public void testField() {
for (int maxOrder = 1; maxOrder < 5; ++maxOrder) {