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/08 01:00:39 UTC

[commons-numbers] 01/03: Correct proj() function to preserve the sign of the imaginary part.

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 bc387815e7c73f2ebfcb010c70ab97484ea80762
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Sun Dec 8 00:30:41 2019 +0000

    Correct proj() function to preserve the sign of the imaginary part.
---
 .../apache/commons/numbers/complex/Complex.java    | 24 ++++++++++++++--------
 .../commons/numbers/complex/ComplexTest.java       |  9 ++++++++
 2 files changed, 24 insertions(+), 9 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 f8af35e..bbee805 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
@@ -262,18 +262,24 @@ public final class Complex implements Serializable  {
     }
 
     /**
-     * Returns projection of this complex number onto the Riemann sphere,
-     * i.e. all infinities (including those with an NaN component)
-     * project onto real infinity, as described in the
-     * <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/cproj.html">
-     * IEEE and ISO C standards</a>.
+     * Returns projection of this complex number onto the Riemann sphere.
      *
-     * @return {@code Complex} projected onto the Riemann sphere.
+     * <p>{@code z} projects to {@code z}, except that all complex infinities (even those
+     * with one infinite part and one NaN part) project to positive infinity on the real axis.
+     *
+     * If {@code z} has an infinite part, then {@code z.proj()} shall be equivalent to:</p>
+     * <pre>
+     *   return Complex.ofCartesian(Double.POSITIVE_INFINITY, Math.copySign(0.0, imag());
+     * </pre>
+     *
+     * @return {@code z} projected onto the Riemann sphere.
+     * @see #isInfinite()
+     * @see <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/cproj.html">
+     * IEEE and ISO C standards: cproj</a>
      */
     public Complex proj() {
-        if (Double.isInfinite(real) ||
-            Double.isInfinite(imaginary)) {
-            return new Complex(Double.POSITIVE_INFINITY, 0);
+        if (isInfinite()) {
+            return new Complex(Double.POSITIVE_INFINITY, Math.copySign(0.0, imaginary));
         }
         return this;
     }
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 69e4c3f..57e03d3 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
@@ -40,6 +40,7 @@ public class ComplexTest {
     private static final Complex oneNegInf = Complex.ofCartesian(1, neginf);
     private static final Complex infOne = Complex.ofCartesian(inf, 1);
     private static final Complex infZero = Complex.ofCartesian(inf, 0);
+    private static final Complex infNegZero = Complex.ofCartesian(inf, -0.0);
     private static final Complex infNan = Complex.ofCartesian(inf, nan);
     private static final Complex infNegInf = Complex.ofCartesian(inf, neginf);
     private static final Complex infInf = Complex.ofCartesian(inf, inf);
@@ -163,8 +164,16 @@ public class ComplexTest {
     public void testProj() {
         final Complex z = Complex.ofCartesian(3.0, 4.0);
         Assertions.assertSame(z, z.proj());
+        // Sign must be the same for projection
         TestUtils.assertSame(infZero, Complex.ofCartesian(inf, 4.0).proj());
+        TestUtils.assertSame(infZero, Complex.ofCartesian(inf, inf).proj());
+        TestUtils.assertSame(infZero, Complex.ofCartesian(inf, nan).proj());
         TestUtils.assertSame(infZero, Complex.ofCartesian(3.0, inf).proj());
+        TestUtils.assertSame(infZero, Complex.ofCartesian(nan, inf).proj());
+        TestUtils.assertSame(infNegZero, Complex.ofCartesian(inf, -4.0).proj());
+        TestUtils.assertSame(infNegZero, Complex.ofCartesian(inf, -inf).proj());
+        TestUtils.assertSame(infNegZero, Complex.ofCartesian(3.0, -inf).proj());
+        TestUtils.assertSame(infNegZero, Complex.ofCartesian(nan, -inf).proj());
     }
 
     @Test