Author: luc
Date: Thu Aug 16 10:07:08 2012
New Revision: 1373778

URL: http://svn.apache.org/viewvc?rev=1373778&view=rev
Log:
Added expm1 and log1p to DSCompiler and DerivativeStructure.

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=1373778&r1=1373777&r2=1373778&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
 Thu Aug 16 10:07:08 2012
@@ -974,6 +974,27 @@ public class DSCompiler {
 
     }
 
+    /** Compute exp(x) - 1 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
+     * exponential the result array <em>cannot</em> be the input
+     * array)
+     * @param resultOffset offset of the result in its array
+     */
+    public void expm1(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.expm1(operand[operandOffset]);
+        Arrays.fill(function, 1, 1 + order, 
FastMath.exp(operand[operandOffset]));
+
+        // apply function composition
+        compose(operand, operandOffset, function, result, resultOffset);
+
+    }
+
     /** Compute natural logarithm of a derivative structure.
      * @param operand array holding the operand
      * @param operandOffset offset of the operand in its array
@@ -1002,6 +1023,34 @@ public class DSCompiler {
 
     }
 
+    /** Computes 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
+     * array)
+     */
+    public void log1p(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.log1p(operand[operandOffset]);
+        if (order > 0) {
+            double inv = 1.0 / (1.0 + operand[operandOffset]);
+            double xk  = inv;
+            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=1373778&r1=1373777&r2=1373778&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
 Thu Aug 16 10:07:08 2012
@@ -497,6 +497,15 @@ public class DerivativeStructure impleme
         return result;
     }
 
+    /** Exponential minus 1.
+     * @return exponential minus one of the instance
+     */
+    public DerivativeStructure expm1() {
+        final DerivativeStructure result = new DerivativeStructure(compiler);
+        compiler.expm1(data, 0, result.data, 0);
+        return result;
+    }
+
     /** Natural logarithm.
      * @return logarithm of the instance
      */
@@ -506,6 +515,15 @@ public class DerivativeStructure impleme
         return result;
     }
 
+    /** Shifted natural logarithm.
+     * @return logarithm of one plus the instance
+     */
+    public DerivativeStructure log1p() {
+        final DerivativeStructure result = new DerivativeStructure(compiler);
+        compiler.log1p(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=1373778&r1=1373777&r2=1373778&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
 Thu Aug 16 10:07:08 2012
@@ -418,6 +418,22 @@ public class DerivativeStructureTest {
     }
 
     @Test
+    public void testExpm1Definition() {
+        double epsilon = 3.0e-16;
+        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 expm11 = dsX.expm1();
+                DerivativeStructure expm12 = 
dsX.exp().subtract(dsX.getField().getOne());
+                DerivativeStructure zero = expm11.subtract(expm12);
+                for (int n = 0; n <= maxOrder; ++n) {
+                    Assert.assertEquals(0, zero.getPartialDerivative(n), 
epsilon);
+                }
+            }
+        }
+    }
+
+    @Test
     public void testLog() {
         double[] epsilon = new double[] { 1.0e-16, 1.0e-16, 3.0e-14, 7.0e-13, 
3.0e-11 };
         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
@@ -433,6 +449,22 @@ public class DerivativeStructureTest {
     }
 
     @Test
+    public void testLog1pDefinition() {
+        double epsilon = 3.0e-16;
+        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 log1p1 = dsX.log1p();
+                DerivativeStructure log1p2 = 
dsX.add(dsX.getField().getOne()).log();
+                DerivativeStructure zero = log1p1.subtract(log1p2);
+                for (int n = 0; n <= maxOrder; ++n) {
+                    Assert.assertEquals(0, zero.getPartialDerivative(n), 
epsilon);
+                }
+            }
+        }
+    }
+
+    @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) {
@@ -448,6 +480,21 @@ public class DerivativeStructureTest {
     }
 
     @Test
+    public void testLog1pExpm1() {
+        double[] epsilon = new double[] { 6.0e-17, 3.0e-16, 5.0e-16, 9.0e-16, 
6.0e-15 };
+        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 = dsX.expm1().log1p();
+                DerivativeStructure zero = rebuiltX.subtract(dsX);
+                for (int n = 0; n <= maxOrder; ++n) {
+                    Assert.assertEquals(0.0, zero.getPartialDerivative(n), 
epsilon[n]);
+                }
+            }
+        }
+    }
+
+    @Test
     public void testSinCos() {
         double epsilon = 5.0e-16;
         for (int maxOrder = 0; maxOrder < 6; ++maxOrder) {


Reply via email to