Added support for x^y in DerivativeStructure and DSCompiler.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1371808 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2012-08-10 18:58:58 +00:00
parent d05fac054e
commit 33877ac5ed
3 changed files with 55 additions and 0 deletions

View File

@ -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

View File

@ -428,6 +428,19 @@ public class DerivativeStructure implements FieldElement<DerivativeStructure>, S
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
*/

View File

@ -381,6 +381,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 };