Author: luc
Date: Fri Aug 17 07:15:28 2012
New Revision: 1374162
URL: http://svn.apache.org/viewvc?rev=1374162&view=rev
Log:
Added log10 to DerivativeStructure and DSCompiler.
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/DSCompiler.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructure.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructureTest.java
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/DSCompiler.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/DSCompiler.java?rev=1374162&r1=1374161&r2=1374162&view=diff
==============================================================================
---
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/DSCompiler.java
(original)
+++
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/DSCompiler.java
Fri Aug 17 07:15:28 2012
@@ -1023,11 +1023,11 @@ public class DSCompiler {
}
- /** Computes of a derivative structure.
+ /** Computes shifted logarithm of a derivative structure.
* @param operand array holding the operand
* @param operandOffset offset of the operand in its array
* @param result array where result must be stored (for
- * logarithm the result array <em>cannot</em> be the input
+ * shifted logarithm the result array <em>cannot</em> be the input
* array)
*/
public void log1p(final double[] operand, final int operandOffset,
@@ -1051,6 +1051,34 @@ public class DSCompiler {
}
+ /** Computes base 10 logarithm of a derivative structure.
+ * @param operand array holding the operand
+ * @param operandOffset offset of the operand in its array
+ * @param result array where result must be stored (for
+ * base 10 logarithm the result array <em>cannot</em> be the input
+ * array)
+ */
+ public void log10(final double[] operand, final int operandOffset,
+ final double[] result, final int resultOffset) {
+
+
+ // create the function value and derivatives
+ double[] function = new double[1 + order];
+ function[0] = FastMath.log10(operand[operandOffset]);
+ if (order > 0) {
+ double inv = 1.0 / operand[operandOffset];
+ double xk = inv / FastMath.log(10.0);
+ for (int i = 1; i <= order; ++i) {
+ function[i] = xk;
+ xk *= -i * inv;
+ }
+ }
+
+ // apply function composition
+ compose(operand, operandOffset, function, result, resultOffset);
+
+ }
+
/** Compute cosine of a derivative structure.
* @param operand array holding the operand
* @param operandOffset offset of the operand in its array
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructure.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructure.java?rev=1374162&r1=1374161&r2=1374162&view=diff
==============================================================================
---
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructure.java
(original)
+++
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructure.java
Fri Aug 17 07:15:28 2012
@@ -636,6 +636,15 @@ public class DerivativeStructure impleme
return result;
}
+ /** Base 10 logarithm.
+ * @return base 10 logarithm of the instance
+ */
+ public DerivativeStructure log10() {
+ final DerivativeStructure result = new DerivativeStructure(compiler);
+ compiler.log10(data, 0, result.data, 0);
+ return result;
+ }
+
/** Cosine operation.
* @return cos(this)
*/
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructureTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructureTest.java?rev=1374162&r1=1374161&r2=1374162&view=diff
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructureTest.java
(original)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructureTest.java
Fri Aug 17 07:15:28 2012
@@ -503,6 +503,22 @@ public class DerivativeStructureTest {
}
@Test
+ public void testLog10Definition() {
+ double[] epsilon = new double[] { 3.0e-16, 3.0e-16, 8.0e-15, 3.0e-13,
8.0e-12 };
+ for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
+ for (double x = 0.1; x < 1.2; x += 0.001) {
+ DerivativeStructure dsX = new DerivativeStructure(1, maxOrder,
0, x);
+ DerivativeStructure log101 = dsX.log10();
+ DerivativeStructure log102 =
dsX.log().divide(FastMath.log(10.0));
+ DerivativeStructure zero = log101.subtract(log102);
+ for (int n = 0; n <= maxOrder; ++n) {
+ Assert.assertEquals(0, zero.getPartialDerivative(n),
epsilon[n]);
+ }
+ }
+ }
+ }
+
+ @Test
public void testLogExp() {
double[] epsilon = new double[] { 2.0e-16, 2.0e-16, 3.0e-16, 2.0e-15,
6.0e-15 };
for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
@@ -533,6 +549,21 @@ public class DerivativeStructureTest {
}
@Test
+ public void testLog10Power() {
+ double[] epsilon = new double[] { 3.0e-16, 3.0e-16, 9.0e-16, 6.0e-15,
6.0e-14 };
+ for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
+ for (double x = 0.1; x < 1.2; x += 0.001) {
+ DerivativeStructure dsX = new DerivativeStructure(1, maxOrder,
0, x);
+ DerivativeStructure rebuiltX = new DerivativeStructure(1,
maxOrder, 10.0).pow(dsX).log10();
+ DerivativeStructure zero = rebuiltX.subtract(dsX);
+ for (int n = 0; n <= maxOrder; ++n) {
+ Assert.assertEquals(0, zero.getPartialDerivative(n),
epsilon[n]);
+ }
+ }
+ }
+ }
+
+ @Test
public void testSinCos() {
double epsilon = 5.0e-16;
for (int maxOrder = 0; maxOrder < 6; ++maxOrder) {