You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2019/12/20 17:58:07 UTC

[commons-numbers] 16/30: Remove use of log1p when value is very small.

This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git

commit 244f6e7a0d93ed92d0748b062d29a59333217b5e
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Thu Dec 19 19:00:43 2019 +0000

    Remove use of log1p when value is very small.
---
 .../src/main/java/org/apache/commons/numbers/complex/Complex.java   | 6 +++---
 .../test/java/org/apache/commons/numbers/complex/ComplexTest.java   | 5 +++++
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java
index 5e6fab3..33b6056 100644
--- a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java
+++ b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java
@@ -1782,9 +1782,9 @@ public final class Complex implements Serializable  {
                         re = Math.log1p((4 * x / y) / (mxp1 * mxp1 / y + y));
                     } else {
                         // Big y, small x, as above but neglect (1-x)^2/y:
-                        // Note: The boost version has no log1p here.
-                        // This will tend towards 0 and log1p(0) = 0 so it may not matter.
-                        re = Math.log1p(4 * x / y / y);
+                        // Note: log1p(v) == v - v^2/2 + v^3/3 ... Taylor series when v is small.
+                        // Here v is so small only the first term matters.
+                        re = 4 * x / y / y;
                     }
                 } else if (x == 1) {
                     // x = 1, small y:
diff --git a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexTest.java b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexTest.java
index 28601fb..b14166b 100644
--- a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexTest.java
+++ b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexTest.java
@@ -2036,5 +2036,10 @@ public class ComplexTest {
         Assertions.assertEquals(1, (1 - safeLower) * (1 - safeLower));
         // Can we assume 1 - y^2 = 1 when y is small
         Assertions.assertEquals(1, 1 - safeLower * safeLower);
+        // Can we assume Math.log1p(4 * x / y / y) = (4 * x / y / y) when big y and small x
+        final double result = 4 * safeLower / safeUpper / safeUpper;
+        Assertions.assertEquals(result, Math.log1p(result));
+        Assertions.assertEquals(result, result - result * result / 2,
+                "Expected log1p Taylor series to be redundant");
     }
 }