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 087d40af801116902d19aeb12804552b804f0ffd Author: aherbert <[email protected]> AuthorDate: Mon Dec 9 13:01:09 2019 +0000 Clean up sqrt(). Removed the commented out code to compute the sqrt() in polar coordinates. Add note about computing for real only complex numbers to the CReferenceTest. --- .../org/apache/commons/numbers/complex/Complex.java | 7 ------- .../commons/numbers/complex/CReferenceTest.java | 19 ++++++++++--------- 2 files changed, 10 insertions(+), 16 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 d4ddf26..8ce7574 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 @@ -1690,13 +1690,6 @@ public final class Complex implements Serializable { * @return the square root of the complex number. */ private static Complex sqrt(double real, double imaginary) { -// final double abs = getAbsolute(real, imaginary); -// final double sqrtAbs = Math.sqrt(abs); -// final double halfArg = getArgument(real, imaginary) / 2; -// final double re = sqrtAbs * Math.cos(halfArg); -// final double im = sqrtAbs * Math.sin(halfArg); -// return new Complex(re, im); - // Special case for infinite imaginary for all real including nan if (Double.isInfinite(imaginary)) { return new Complex(Double.POSITIVE_INFINITY, imaginary); diff --git a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CReferenceTest.java b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CReferenceTest.java index 22b8cf2..2e817b7 100644 --- a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CReferenceTest.java +++ b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CReferenceTest.java @@ -399,24 +399,25 @@ public class CReferenceTest { @Test public void testSqrt() { - // TODO: GNU g++ computes in polar coordinates. - // Test other reference implementations. - // This gives strange results for real negative and imaginary 0. + // Note: When computed in polar coordinates: + // real = (x^2 + y^2)^0.25 * cos(0.5 * atan2(y, x)) + // If x is positive and y is +/-0.0 atan2 returns +/-0. + // If x is negative and y is +/-0.0 atan2 returns +/-PI. + // This causes problems as + // cos(0.5 * PI) = 6.123233995736766e-17 + // assert: Math.cos(Math.acos(0)) != 0.0 + // Thus polar computation will produce incorrect output when + // there is no imaginary component and real is negative. + // The computation should be done for real only numbers separately. - // Polar result - //assertComplex(-2, 0.0, Complex::sqrt, 8.6595605623549341e-17, 1.4142135623730951); assertComplex(-2, 0.0, Complex::sqrt, 0, 1.4142135623730951); assertComplex(-2, 0.5, Complex::sqrt, 0.17543205637629397, 1.425053124063947, 5); assertComplex(-2, 1, Complex::sqrt, 0.3435607497225126, 1.4553466902253549, 3); assertComplex(-2, 2, Complex::sqrt, 0.64359425290558281, 1.5537739740300374, 2); - // Polar result - //assertComplex(-1, 0.0, Complex::sqrt, 6.123233995736766e-17, 1); assertComplex(-1, 0.0, Complex::sqrt, 0, 1); assertComplex(-1, 0.5, Complex::sqrt, 0.24293413587832291, 1.0290855136357462, 3); assertComplex(-1, 1, Complex::sqrt, 0.45508986056222739, 1.0986841134678098); assertComplex(-1, 2, Complex::sqrt, 0.78615137775742339, 1.2720196495140688); - // Polar result - //assertComplex(-0.5, 0.0, Complex::sqrt, 4.329780281177467e-17, 0.70710678118654757); assertComplex(-0.5, 0.0, Complex::sqrt, 0, 0.70710678118654757); assertComplex(-0.5, 0.5, Complex::sqrt, 0.3217971264527914, 0.77688698701501868, 2); assertComplex(-0.5, 1, Complex::sqrt, 0.55589297025142126, 0.89945371997393353);
