NUMBERS-53: Fixes to test classes. Waiting on decision about TestComplex class.
Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/b8670225 Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/b8670225 Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/b8670225 Branch: refs/heads/complex-constructors Commit: b867022591eb365476d0df3dce3e1d559369d011 Parents: 5166d4b Author: Eric Barnhill <[email protected]> Authored: Thu Mar 1 14:14:40 2018 +0100 Committer: Eric Barnhill <[email protected]> Committed: Thu Mar 1 14:14:40 2018 +0100 ---------------------------------------------------------------------- .../commons/numbers/complex/CStandardTest.java | 91 +++--- .../commons/numbers/complex/ComplexTest.java | 283 +++++++++---------- .../numbers/complex/ComplexUtilsTest.java | 58 ++-- 3 files changed, 210 insertions(+), 222 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/b8670225/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CStandardTest.java ---------------------------------------------------------------------- diff --git a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CStandardTest.java b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CStandardTest.java index 861da5a..e7a595e 100644 --- a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CStandardTest.java +++ b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CStandardTest.java @@ -18,9 +18,7 @@ package org.apache.commons.numbers.complex; import org.apache.commons.numbers.complex.Complex; -import org.apache.commons.numbers.complex.ComplexUtils; import org.junit.Assert; -import org.junit.Ignore; import org.junit.Test; public class CStandardTest { @@ -28,49 +26,44 @@ public class CStandardTest { private static final double inf = Double.POSITIVE_INFINITY; private static final double negInf = Double.NEGATIVE_INFINITY; private static final double nan = Double.NaN; - private static final double pi = Math.PI; private static final double piOverFour = Math.PI / 4.0; private static final double piOverTwo = Math.PI / 2.0; private static final double threePiOverFour = 3.0*Math.PI/4.0; - private static final Complex oneOne = new Complex(1, 1); - private static final Complex oneZero = new Complex(1, 0); - private static final Complex oneInf = new Complex(1, inf); - private static final Complex oneNegInf = new Complex(1, negInf); - private static final Complex oneNaN = new Complex(1, nan); - private static final Complex zeroInf = new Complex(0, inf); - private static final Complex zeroNegInf = new Complex(0,negInf); - private static final Complex zeroNaN = new Complex(0, nan); - private static final Complex zeroPiTwo = new Complex(0.0, piOverTwo); - private static final Complex negZeroZero = new Complex(-0.0, 0); - private static final Complex negZeroNan = new Complex(-0.0, nan); - private static final Complex negI = new Complex(0.0, -1.0); - private static final Complex infOne = new Complex(inf, 1); - private static final Complex infZero = new Complex(inf, 0); - private static final Complex infNaN = new Complex(inf, nan); - private static final Complex infNegInf = new Complex(inf, negInf); - private static final Complex infInf = new Complex(inf, inf); - private static final Complex infPiTwo = new Complex(inf, piOverTwo); - private static final Complex infPiFour = new Complex(inf, piOverFour); - private static final Complex infPi = new Complex(inf, Math.PI); - private static final Complex negInfInf = new Complex(negInf, inf); - private static final Complex negInfZero = new Complex(negInf, 0); - private static final Complex negInfOne = new Complex(negInf, 1); - private static final Complex negInfNaN = new Complex(negInf, nan); - private static final Complex negInfNegInf = new Complex(negInf, negInf); - private static final Complex negInfPosInf = new Complex(negInf, inf); - private static final Complex negInfPi = new Complex(negInf, Math.PI); - private static final Complex nanInf = new Complex(nan, inf); - private static final Complex nanNegInf = new Complex(nan, negInf); - private static final Complex nanZero = new Complex(nan, 0); - private static final Complex nanOne = new Complex(nan, 1); - private static final Complex piTwoNaN = new Complex(piOverTwo, nan); - private static final Complex piNegInf = new Complex(Math.PI, negInf); - private static final Complex piTwoNegInf = new Complex(piOverTwo, negInf); - private static final Complex piTwoNegZero = new Complex(piOverTwo, -0.0); - private static final Complex threePiFourNegInf = new Complex(threePiOverFour,negInf); - private static final Complex piFourNegInf = new Complex(piOverFour, negInf); - private static final Complex NAN = new Complex(nan, nan); - + private static final Complex oneOne = Complex.ofCartesian(1, 1); + private static final Complex oneZero = Complex.ofCartesian(1, 0); + private static final Complex oneInf = Complex.ofCartesian(1, inf); + private static final Complex oneNaN = Complex.ofCartesian(1, nan); + private static final Complex zeroInf = Complex.ofCartesian(0, inf); + private static final Complex zeroNegInf = Complex.ofCartesian(0,negInf); + private static final Complex zeroNaN = Complex.ofCartesian(0, nan); + private static final Complex zeroPiTwo = Complex.ofCartesian(0.0, piOverTwo); + private static final Complex negZeroZero = Complex.ofCartesian(-0.0, 0); + private static final Complex negI = Complex.ofCartesian(0.0, -1.0); + private static final Complex infOne = Complex.ofCartesian(inf, 1); + private static final Complex infZero = Complex.ofCartesian(inf, 0); + private static final Complex infNaN = Complex.ofCartesian(inf, nan); + private static final Complex infInf = Complex.ofCartesian(inf, inf); + private static final Complex infPiTwo = Complex.ofCartesian(inf, piOverTwo); + private static final Complex infPiFour = Complex.ofCartesian(inf, piOverFour); + private static final Complex infPi = Complex.ofCartesian(inf, Math.PI); + private static final Complex negInfInf = Complex.ofCartesian(negInf, inf); + private static final Complex negInfZero = Complex.ofCartesian(negInf, 0); + private static final Complex negInfOne = Complex.ofCartesian(negInf, 1); + private static final Complex negInfNaN = Complex.ofCartesian(negInf, nan); + private static final Complex negInfPosInf = Complex.ofCartesian(negInf, inf); + private static final Complex negInfPi = Complex.ofCartesian(negInf, Math.PI); + private static final Complex nanInf = Complex.ofCartesian(nan, inf); + private static final Complex nanNegInf = Complex.ofCartesian(nan, negInf); + private static final Complex nanZero = Complex.ofCartesian(nan, 0); + private static final Complex nanOne = Complex.ofCartesian(nan, 1); + private static final Complex piTwoNaN = Complex.ofCartesian(piOverTwo, nan); + private static final Complex piNegInf = Complex.ofCartesian(Math.PI, negInf); + private static final Complex piTwoNegInf = Complex.ofCartesian(piOverTwo, negInf); + private static final Complex piTwoNegZero = Complex.ofCartesian(piOverTwo, -0.0); + private static final Complex threePiFourNegInf = Complex.ofCartesian(threePiOverFour,negInf); + private static final Complex piFourNegInf = Complex.ofCartesian(piOverFour, negInf); + private static final Complex NAN = Complex.ofCartesian(nan, nan); + public void assertComplex(Complex c1, Complex c2, double realTol, double imagTol) { Assert.assertEquals(c1.getReal(), c2.getReal(), realTol); Assert.assertEquals(c1.getImaginary(), c2.getImaginary(), imagTol); @@ -87,18 +80,18 @@ public class CStandardTest { */ @Test public void testSqrt1() { - Complex z1 = new Complex(-2.0, 0.0); - Complex z2 = new Complex(0.0, Math.sqrt(2)); + Complex z1 = Complex.ofCartesian(-2.0, 0.0); + Complex z2 = Complex.ofCartesian(0.0, Math.sqrt(2)); assertComplex(z1.sqrt(), z2); - z1 = new Complex(-2.0, -0.0); - z2 = new Complex(0.0, -Math.sqrt(2)); + z1 = Complex.ofCartesian(-2.0, -0.0); + z2 = Complex.ofCartesian(0.0, -Math.sqrt(2)); assertComplex(z1.sqrt(), z2); } @Test public void testImplicitTrig() { - Complex z1 = new Complex(3.0); - Complex z2 = new Complex(0.0, 3.0); + Complex z1 = Complex.ofCartesian(3.0); + Complex z2 = Complex.ofCartesian(0.0, 3.0); assertComplex(z1.asin(), negI.multiply(z2.asinh())); assertComplex(z1.atan(), negI.multiply(z2.atanh()), Math.ulp(1), Math.ulp(1)); assertComplex(z1.cos(), z2.cosh()); @@ -252,7 +245,7 @@ public class CStandardTest { @Test public void testLog() { assertComplex(oneOne.log().conj(), oneOne.conj().log()); - assertComplex(negZeroZero.log(), negInfPi); + assertComplex(negZeroZero.log(), negInfPi); assertComplex(Complex.ZERO.log(), negInfZero); assertComplex(oneInf.log(), infPiTwo); assertComplex(oneNaN.log(), NAN); http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/b8670225/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexTest.java ---------------------------------------------------------------------- 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 80b433d..e147fe0 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 @@ -34,62 +34,62 @@ public class ComplexTest { private static final double neginf = Double.NEGATIVE_INFINITY; private static final double nan = Double.NaN; private static final double pi = Math.PI; - private static final Complex oneInf = new Complex(1, inf); - private static final Complex oneNegInf = new Complex(1, neginf); - private static final Complex infOne = new Complex(inf, 1); - private static final Complex infZero = new Complex(inf, 0); - private static final Complex infNaN = new Complex(inf, nan); - private static final Complex infNegInf = new Complex(inf, neginf); - private static final Complex infInf = new Complex(inf, inf); - private static final Complex negInfInf = new Complex(neginf, inf); - private static final Complex negInfZero = new Complex(neginf, 0); - private static final Complex negInfOne = new Complex(neginf, 1); - private static final Complex negInfNaN = new Complex(neginf, nan); - private static final Complex negInfNegInf = new Complex(neginf, neginf); - private static final Complex oneNaN = new Complex(1, nan); - private static final Complex zeroInf = new Complex(0, inf); - private static final Complex zeroNaN = new Complex(0, nan); - private static final Complex nanInf = new Complex(nan, inf); - private static final Complex nanNegInf = new Complex(nan, neginf); - private static final Complex nanZero = new Complex(nan, 0); - private static final Complex NAN = new Complex(nan, nan); + private static final Complex oneInf = Complex.ofCartesian(1, inf); + 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 infNaN = Complex.ofCartesian(inf, nan); + private static final Complex infNegInf = Complex.ofCartesian(inf, neginf); + private static final Complex infInf = Complex.ofCartesian(inf, inf); + private static final Complex negInfInf = Complex.ofCartesian(neginf, inf); + private static final Complex negInfZero = Complex.ofCartesian(neginf, 0); + private static final Complex negInfOne = Complex.ofCartesian(neginf, 1); + private static final Complex negInfNaN = Complex.ofCartesian(neginf, nan); + private static final Complex negInfNegInf = Complex.ofCartesian(neginf, neginf); + private static final Complex oneNaN = Complex.ofCartesian(1, nan); + private static final Complex zeroInf = Complex.ofCartesian(0, inf); + private static final Complex zeroNaN = Complex.ofCartesian(0, nan); + private static final Complex nanInf = Complex.ofCartesian(nan, inf); + private static final Complex nanNegInf = Complex.ofCartesian(nan, neginf); + private static final Complex nanZero = Complex.ofCartesian(nan, 0); + private static final Complex NAN = Complex.ofCartesian(nan, nan); @Test public void testConstructor() { - Complex z = new Complex(3.0, 4.0); + Complex z = Complex.ofCartesian(3.0, 4.0); Assert.assertEquals(3.0, z.getReal(), 1.0e-5); Assert.assertEquals(4.0, z.getImaginary(), 1.0e-5); } @Test public void testConstructorNaN() { - Complex z = new Complex(3.0, Double.NaN); + Complex z = Complex.ofCartesian(3.0, Double.NaN); Assert.assertTrue(z.isNaN()); - z = new Complex(nan, 4.0); + z = Complex.ofCartesian(nan, 4.0); Assert.assertTrue(z.isNaN()); - z = new Complex(3.0, 4.0); + z = Complex.ofCartesian(3.0, 4.0); Assert.assertFalse(z.isNaN()); } @Test public void testAbs() { - Complex z = new Complex(3.0, 4.0); + Complex z = Complex.ofCartesian(3.0, 4.0); Assert.assertEquals(5.0, z.abs(), 1.0e-5); } @Test public void testAbsNaN() { Assert.assertTrue(Double.isNaN(NAN.abs())); - Complex z = new Complex(inf, nan); + Complex z = Complex.ofCartesian(inf, nan); Assert.assertTrue(Double.isNaN(z.abs())); } @Test public void testAdd() { - Complex x = new Complex(3.0, 4.0); - Complex y = new Complex(5.0, 6.0); + Complex x = Complex.ofCartesian(3.0, 4.0); + Complex y = Complex.ofCartesian(5.0, 6.0); Complex z = x.add(y); Assert.assertEquals(8.0, z.getReal(), 1.0e-5); Assert.assertEquals(10.0, z.getImaginary(), 1.0e-5); @@ -97,48 +97,48 @@ public class ComplexTest { @Test public void testAddInf() { - Complex x = new Complex(1, 1); - Complex z = new Complex(inf, 0); + Complex x = Complex.ofCartesian(1, 1); + Complex z = Complex.ofCartesian(inf, 0); Complex w = x.add(z); Assert.assertEquals(w.getImaginary(), 1, 0); Assert.assertEquals(inf, w.getReal(), 0); - x = new Complex(neginf, 0); + x = Complex.ofCartesian(neginf, 0); Assert.assertTrue(Double.isNaN(x.add(z).getReal())); } @Test public void testScalarAdd() { - Complex x = new Complex(3.0, 4.0); + Complex x = Complex.ofCartesian(3.0, 4.0); double yDouble = 2.0; - Complex yComplex = new Complex(yDouble); + Complex yComplex = Complex.ofCartesian(yDouble); Assert.assertEquals(x.add(yComplex), x.add(yDouble)); } @Test public void testScalarAddNaN() { - Complex x = new Complex(3.0, 4.0); + Complex x = Complex.ofCartesian(3.0, 4.0); double yDouble = Double.NaN; - Complex yComplex = new Complex(yDouble); + Complex yComplex = Complex.ofCartesian(yDouble); Assert.assertEquals(x.add(yComplex), x.add(yDouble)); } @Test public void testScalarAddInf() { - Complex x = new Complex(1, 1); + Complex x = Complex.ofCartesian(1, 1); double yDouble = Double.POSITIVE_INFINITY; - Complex yComplex = new Complex(yDouble); + Complex yComplex = Complex.ofCartesian(yDouble); Assert.assertEquals(x.add(yComplex), x.add(yDouble)); - x = new Complex(neginf, 0); + x = Complex.ofCartesian(neginf, 0); Assert.assertEquals(x.add(yComplex), x.add(yDouble)); } @Test public void testConjugate() { - Complex x = new Complex(3.0, 4.0); + Complex x = Complex.ofCartesian(3.0, 4.0); Complex z = x.conjugate(); Assert.assertEquals(3.0, z.getReal(), 1.0e-5); Assert.assertEquals(-4.0, z.getImaginary(), 1.0e-5); @@ -152,16 +152,16 @@ public class ComplexTest { @Test public void testConjugateInfiinite() { - Complex z = new Complex(0, inf); + Complex z = Complex.ofCartesian(0, inf); Assert.assertEquals(neginf, z.conjugate().getImaginary(), 0); - z = new Complex(0, neginf); + z = Complex.ofCartesian(0, neginf); Assert.assertEquals(inf, z.conjugate().getImaginary(), 0); } @Test public void testDivide() { - Complex x = new Complex(3.0, 4.0); - Complex y = new Complex(5.0, 6.0); + Complex x = Complex.ofCartesian(3.0, 4.0); + Complex y = Complex.ofCartesian(5.0, 6.0); Complex z = x.divide(y); Assert.assertEquals(39.0 / 61.0, z.getReal(), 1.0e-5); Assert.assertEquals(2.0 / 61.0, z.getImaginary(), 1.0e-5); @@ -169,22 +169,22 @@ public class ComplexTest { @Test public void testDivideReal() { - Complex x = new Complex(2d, 3d); - Complex y = new Complex(2d, 0d); - Assert.assertEquals(new Complex(1d, 1.5), x.divide(y)); + Complex x = Complex.ofCartesian(2d, 3d); + Complex y = Complex.ofCartesian(2d, 0d); + Assert.assertEquals(Complex.ofCartesian(1d, 1.5), x.divide(y)); } @Test public void testDivideImaginary() { - Complex x = new Complex(2d, 3d); - Complex y = new Complex(0d, 2d); - Assert.assertEquals(new Complex(1.5d, -1d), x.divide(y)); + Complex x = Complex.ofCartesian(2d, 3d); + Complex y = Complex.ofCartesian(0d, 2d); + Assert.assertEquals(Complex.ofCartesian(1.5d, -1d), x.divide(y)); } @Test public void testDivideZero() { - Complex x = new Complex(3.0, 4.0); + Complex x = Complex.ofCartesian(3.0, 4.0); Complex z = x.divide(Complex.ZERO); // Assert.assertEquals(z, Complex.INF); // See MATH-657 Assert.assertEquals(z, NAN); @@ -192,14 +192,14 @@ public class ComplexTest { @Test public void testDivideZeroZero() { - Complex x = new Complex(0.0, 0.0); + Complex x = Complex.ofCartesian(0.0, 0.0); Complex z = x.divide(Complex.ZERO); Assert.assertEquals(z, NAN); } @Test public void testDivideNaN() { - Complex x = new Complex(3.0, 4.0); + Complex x = Complex.ofCartesian(3.0, 4.0); Complex z = x.divide(NAN); Assert.assertTrue(z.isNaN()); } @@ -221,29 +221,29 @@ public class ComplexTest { @Test public void testScalarDivide() { - Complex x = new Complex(3.0, 4.0); + Complex x = Complex.ofCartesian(3.0, 4.0); double yDouble = 2.0; - Complex yComplex = new Complex(yDouble); + Complex yComplex = Complex.ofCartesian(yDouble); Assert.assertEquals(x.divide(yComplex), x.divide(yDouble)); } @Test public void testScalarDivideNaN() { - Complex x = new Complex(3.0, 4.0); + Complex x = Complex.ofCartesian(3.0, 4.0); double yDouble = Double.NaN; - Complex yComplex = new Complex(yDouble); + Complex yComplex = Complex.ofCartesian(yDouble); Assert.assertEquals(x.divide(yComplex), x.divide(yDouble)); } @Test public void testScalarDivideZero() { - Complex x = new Complex(1,1); + Complex x = Complex.ofCartesian(1,1); TestUtils.assertEquals(x.divide(Complex.ZERO), x.divide(0), 0); } @Test public void testReciprocal() { - Complex z = new Complex(5.0, 6.0); + Complex z = Complex.ofCartesian(5.0, 6.0); Complex act = z.reciprocal(); double expRe = 5.0 / 61.0; double expIm = -6.0 / 61.0; @@ -253,7 +253,7 @@ public class ComplexTest { @Test public void testReciprocalReciprocal() { - Complex z = new Complex(5.0, 6.0); + Complex z = Complex.ofCartesian(5.0, 6.0); Complex zRR = z.reciprocal().reciprocal(); final double tol = 1e-14; Assert.assertEquals(zRR.getReal(), z.getReal(), tol); @@ -262,14 +262,14 @@ public class ComplexTest { @Test public void testReciprocalReal() { - Complex z = new Complex(-2.0, 0.0); - Assert.assertTrue(Complex.equals(new Complex(-0.5, 0.0), z.reciprocal())); + Complex z = Complex.ofCartesian(-2.0, 0.0); + Assert.assertTrue(Complex.equals(Complex.ofCartesian(-0.5, 0.0), z.reciprocal())); } @Test public void testReciprocalImaginary() { - Complex z = new Complex(0.0, -2.0); - Assert.assertEquals(new Complex(0.0, 0.5), z.reciprocal()); + Complex z = Complex.ofCartesian(0.0, -2.0); + Assert.assertEquals(Complex.ofCartesian(0.0, 0.5), z.reciprocal()); } @Test @@ -279,8 +279,8 @@ public class ComplexTest { @Test public void testMultiply() { - Complex x = new Complex(3.0, 4.0); - Complex y = new Complex(5.0, 6.0); + Complex x = Complex.ofCartesian(3.0, 4.0); + Complex y = Complex.ofCartesian(5.0, 6.0); Complex z = x.multiply(y); Assert.assertEquals(-9.0, z.getReal(), 1.0e-5); Assert.assertEquals(38.0, z.getImaginary(), 1.0e-5); @@ -294,38 +294,38 @@ public class ComplexTest { @Test public void testScalarMultiply() { - Complex x = new Complex(3.0, 4.0); + Complex x = Complex.ofCartesian(3.0, 4.0); double yDouble = 2.0; - Complex yComplex = new Complex(yDouble); + Complex yComplex = Complex.ofCartesian(yDouble); Assert.assertEquals(x.multiply(yComplex), x.multiply(yDouble)); int zInt = -5; - Complex zComplex = new Complex(zInt); + Complex zComplex = Complex.ofCartesian(zInt); Assert.assertEquals(x.multiply(zComplex), x.multiply(zInt)); } @Test public void testScalarMultiplyNaN() { - Complex x = new Complex(3.0, 4.0); + Complex x = Complex.ofCartesian(3.0, 4.0); double yDouble = Double.NaN; - Complex yComplex = new Complex(yDouble); + Complex yComplex = Complex.ofCartesian(yDouble); Assert.assertEquals(x.multiply(yComplex), x.multiply(yDouble)); } @Test public void testScalarMultiplyInf() { - Complex x = new Complex(1, 1); + Complex x = Complex.ofCartesian(1, 1); double yDouble = Double.POSITIVE_INFINITY; - Complex yComplex = new Complex(yDouble); + Complex yComplex = Complex.ofCartesian(yDouble); Assert.assertEquals(x.multiply(yComplex), x.multiply(yDouble)); yDouble = Double.NEGATIVE_INFINITY; - yComplex = new Complex(yDouble); + yComplex = Complex.ofCartesian(yDouble); Assert.assertEquals(x.multiply(yComplex), x.multiply(yDouble)); } @Test public void testNegate() { - Complex x = new Complex(3.0, 4.0); + Complex x = Complex.ofCartesian(3.0, 4.0); Complex z = x.negate(); Assert.assertEquals(-3.0, z.getReal(), 1.0e-5); Assert.assertEquals(-4.0, z.getImaginary(), 1.0e-5); @@ -339,8 +339,8 @@ public class ComplexTest { @Test public void testSubtract() { - Complex x = new Complex(3.0, 4.0); - Complex y = new Complex(5.0, 6.0); + Complex x = Complex.ofCartesian(3.0, 4.0); + Complex y = Complex.ofCartesian(5.0, 6.0); Complex z = x.subtract(y); Assert.assertEquals(-2.0, z.getReal(), 1.0e-5); Assert.assertEquals(-2.0, z.getImaginary(), 1.0e-5); @@ -348,68 +348,68 @@ public class ComplexTest { @Test public void testSubtractInf() { - Complex x = new Complex(1, 1); - Complex z = new Complex(neginf, 0); + Complex x = Complex.ofCartesian(1, 1); + Complex z = Complex.ofCartesian(neginf, 0); Complex w = x.subtract(z); Assert.assertEquals(w.getImaginary(), 1, 0); Assert.assertEquals(inf, w.getReal(), 0); - x = new Complex(neginf, 0); + x = Complex.ofCartesian(neginf, 0); Assert.assertTrue(Double.isNaN(x.subtract(z).getReal())); } @Test public void testScalarSubtract() { - Complex x = new Complex(3.0, 4.0); + Complex x = Complex.ofCartesian(3.0, 4.0); double yDouble = 2.0; - Complex yComplex = new Complex(yDouble); + Complex yComplex = Complex.ofCartesian(yDouble); Assert.assertEquals(x.subtract(yComplex), x.subtract(yDouble)); } @Test public void testScalarSubtractNaN() { - Complex x = new Complex(3.0, 4.0); + Complex x = Complex.ofCartesian(3.0, 4.0); double yDouble = Double.NaN; - Complex yComplex = new Complex(yDouble); + Complex yComplex = Complex.ofCartesian(yDouble); Assert.assertEquals(x.subtract(yComplex), x.subtract(yDouble)); } @Test public void testScalarSubtractInf() { - Complex x = new Complex(1, 1); + Complex x = Complex.ofCartesian(1, 1); double yDouble = Double.POSITIVE_INFINITY; - Complex yComplex = new Complex(yDouble); + Complex yComplex = Complex.ofCartesian(yDouble); Assert.assertEquals(x.subtract(yComplex), x.subtract(yDouble)); - x = new Complex(neginf, 0); + x = Complex.ofCartesian(neginf, 0); Assert.assertEquals(x.subtract(yComplex), x.subtract(yDouble)); } @Test public void testEqualsNull() { - Complex x = new Complex(3.0, 4.0); + Complex x = Complex.ofCartesian(3.0, 4.0); Assert.assertFalse(x.equals(null)); } @Test(expected=NullPointerException.class) public void testFloatingPointEqualsPrecondition1() { - Complex.equals(new Complex(3.0, 4.0), null, 3); + Complex.equals(Complex.ofCartesian(3.0, 4.0), null, 3); } @Test(expected=NullPointerException.class) public void testFloatingPointEqualsPrecondition2() { - Complex.equals(null, new Complex(3.0, 4.0), 3); + Complex.equals(null, Complex.ofCartesian(3.0, 4.0), 3); } @Test public void testEqualsClass() { - Complex x = new Complex(3.0, 4.0); + Complex x = Complex.ofCartesian(3.0, 4.0); Assert.assertFalse(x.equals(this)); } @Test public void testEqualsSame() { - Complex x = new Complex(3.0, 4.0); + Complex x = Complex.ofCartesian(3.0, 4.0); Assert.assertTrue(x.equals(x)); } @@ -418,8 +418,8 @@ public class ComplexTest { double re = -3.21; double im = 456789e10; - final Complex x = new Complex(re, im); - Complex y = new Complex(re, im); + final Complex x = Complex.ofCartesian(re, im); + Complex y = Complex.ofCartesian(re, im); Assert.assertTrue(x.equals(y)); Assert.assertTrue(Complex.equals(x, y)); @@ -429,21 +429,21 @@ public class ComplexTest { re = Math.nextUp(re); im = Math.nextUp(im); } - y = new Complex(re, im); + y = Complex.ofCartesian(re, im); Assert.assertTrue(Complex.equals(x, y, maxUlps)); re = Math.nextUp(re); im = Math.nextUp(im); - y = new Complex(re, im); + y = Complex.ofCartesian(re, im); Assert.assertFalse(Complex.equals(x, y, maxUlps)); } @Test public void testFloatingPointEqualsNaN() { - Complex c = new Complex(Double.NaN, 1); + Complex c = Complex.ofCartesian(Double.NaN, 1); Assert.assertFalse(Complex.equals(c, c)); - c = new Complex(1, Double.NaN); + c = Complex.ofCartesian(1, Double.NaN); Assert.assertFalse(Complex.equals(c, c)); } @@ -452,8 +452,8 @@ public class ComplexTest { final double re = 153.0000; final double im = 152.9375; final double tol1 = 0.0625; - final Complex x = new Complex(re, im); - final Complex y = new Complex(re + tol1, im + tol1); + final Complex x = Complex.ofCartesian(re, im); + final Complex y = Complex.ofCartesian(re + tol1, im + tol1); Assert.assertTrue(Complex.equals(x, y, tol1)); final double tol2 = 0.0624; @@ -462,8 +462,8 @@ public class ComplexTest { @Test public void testFloatingPointEqualsWithAllowedDeltaNaN() { - final Complex x = new Complex(0, Double.NaN); - final Complex y = new Complex(Double.NaN, 0); + final Complex x = Complex.ofCartesian(0, Double.NaN); + final Complex y = Complex.ofCartesian(Double.NaN, 0); Assert.assertFalse(Complex.equals(x, Complex.ZERO, 0.1)); Assert.assertFalse(Complex.equals(x, x, 0.1)); Assert.assertFalse(Complex.equals(x, y, 0.1)); @@ -476,15 +476,15 @@ public class ComplexTest { final double im = 1e10; final double f = 1 + tol; - final Complex x = new Complex(re, im); - final Complex y = new Complex(re * f, im * f); + final Complex x = Complex.ofCartesian(re, im); + final Complex y = Complex.ofCartesian(re * f, im * f); Assert.assertTrue(Complex.equalsWithRelativeTolerance(x, y, tol)); } @Test public void testFloatingPointEqualsWithRelativeToleranceNaN() { - final Complex x = new Complex(0, Double.NaN); - final Complex y = new Complex(Double.NaN, 0); + final Complex x = Complex.ofCartesian(0, Double.NaN); + final Complex y = Complex.ofCartesian(Double.NaN, 0); Assert.assertFalse(Complex.equalsWithRelativeTolerance(x, Complex.ZERO, 0.1)); Assert.assertFalse(Complex.equalsWithRelativeTolerance(x, x, 0.1)); Assert.assertFalse(Complex.equalsWithRelativeTolerance(x, y, 0.1)); @@ -492,34 +492,34 @@ public class ComplexTest { @Test public void testEqualsTrue() { - Complex x = new Complex(3.0, 4.0); - Complex y = new Complex(3.0, 4.0); + Complex x = Complex.ofCartesian(3.0, 4.0); + Complex y = Complex.ofCartesian(3.0, 4.0); Assert.assertTrue(x.equals(y)); } @Test public void testEqualsRealDifference() { - Complex x = new Complex(0.0, 0.0); - Complex y = new Complex(0.0 + Double.MIN_VALUE, 0.0); + Complex x = Complex.ofCartesian(0.0, 0.0); + Complex y = Complex.ofCartesian(0.0 + Double.MIN_VALUE, 0.0); Assert.assertFalse(x.equals(y)); } @Test public void testEqualsImaginaryDifference() { - Complex x = new Complex(0.0, 0.0); - Complex y = new Complex(0.0, 0.0 + Double.MIN_VALUE); + Complex x = Complex.ofCartesian(0.0, 0.0); + Complex y = Complex.ofCartesian(0.0, 0.0 + Double.MIN_VALUE); Assert.assertFalse(x.equals(y)); } @Test public void testHashCode() { - Complex x = new Complex(0.0, 0.0); - Complex y = new Complex(0.0, 0.0 + Double.MIN_VALUE); + Complex x = Complex.ofCartesian(0.0, 0.0); + Complex y = Complex.ofCartesian(0.0, 0.0 + Double.MIN_VALUE); Assert.assertFalse(x.hashCode()==y.hashCode()); - y = new Complex(0.0 + Double.MIN_VALUE, 0.0); + y = Complex.ofCartesian(0.0 + Double.MIN_VALUE, 0.0); Assert.assertFalse(x.hashCode()==y.hashCode()); - Complex realNaN = new Complex(Double.NaN, 0.0); - Complex imaginaryNaN = new Complex(0.0, Double.NaN); + Complex realNaN = Complex.ofCartesian(Double.NaN, 0.0); + Complex imaginaryNaN = Complex.ofCartesian(0.0, Double.NaN); Assert.assertEquals(realNaN.hashCode(), imaginaryNaN.hashCode()); Assert.assertEquals(imaginaryNaN.hashCode(), NAN.hashCode()); @@ -528,13 +528,13 @@ public class ComplexTest { // different hash codes, "equals" must return false. final String msg = "'equals' not compatible with 'hashCode'"; - x = new Complex(0.0, 0.0); - y = new Complex(0.0, -0.0); + x = Complex.ofCartesian(0.0, 0.0); + y = Complex.ofCartesian(0.0, -0.0); Assert.assertTrue(x.hashCode() != y.hashCode()); Assert.assertFalse(msg, x.equals(y)); - x = new Complex(0.0, 0.0); - y = new Complex(-0.0, 0.0); + x = Complex.ofCartesian(0.0, 0.0); + y = Complex.ofCartesian(-0.0, 0.0); Assert.assertTrue(x.hashCode() != y.hashCode()); Assert.assertFalse(msg, x.equals(y)); } @@ -570,9 +570,9 @@ public class ComplexTest { @Test public void testScalarPow() { - Complex x = new Complex(3, 4); + Complex x = Complex.ofCartesian(3, 4); double yDouble = 5.0; - Complex yComplex = new Complex(yDouble); + Complex yComplex = Complex.ofCartesian(yDouble); Assert.assertEquals(x.pow(yComplex), x.pow(yDouble)); } @@ -580,15 +580,15 @@ public class ComplexTest { public void testScalarPowNaNBase() { Complex x = NAN; double yDouble = 5.0; - Complex yComplex = new Complex(yDouble); + Complex yComplex = Complex.ofCartesian(yDouble); Assert.assertEquals(x.pow(yComplex), x.pow(yDouble)); } @Test public void testScalarPowNaNExponent() { - Complex x = new Complex(3, 4); + Complex x = Complex.ofCartesian(3, 4); double yDouble = Double.NaN; - Complex yComplex = new Complex(yDouble); + Complex yComplex = Complex.ofCartesian(yDouble); Assert.assertEquals(x.pow(yComplex), x.pow(yDouble)); } @Test @@ -609,8 +609,8 @@ public class ComplexTest { @Test public void testSqrt1z() { - Complex z = new Complex(3, 4); - Complex expected = new Complex(4.08033, -2.94094); + Complex z = Complex.ofCartesian(3, 4); + Complex expected = Complex.ofCartesian(4.08033, -2.94094); TestUtils.assertEquals(expected, z.sqrt1z(), 1.0e-5); } @@ -633,7 +633,7 @@ public class ComplexTest { @Test public void testNthRoot_normal_thirdRoot() { // The complex number we want to compute all third-roots for. - Complex z = new Complex(-2,2); + Complex z = Complex.ofCartesian(-2,2); // The List holding all third roots Complex[] thirdRootsOfZ = z.nthRoot(3).toArray(new Complex[0]); // Returned Collection must not be empty! @@ -665,7 +665,7 @@ public class ComplexTest { @Test public void testNthRoot_normal_fourthRoot() { // The complex number we want to compute all third-roots for. - Complex z = new Complex(5,-2); + Complex z = Complex.ofCartesian(5,-2); // The List holding all fourth roots Complex[] fourthRootsOfZ = z.nthRoot(4).toArray(new Complex[0]); // Returned Collection must not be empty! @@ -699,7 +699,7 @@ public class ComplexTest { public void testNthRoot_cornercase_thirdRoot_imaginaryPartEmpty() { // The number 8 has three third roots. One we all already know is the number 2. // But there are two more complex roots. - Complex z = new Complex(8,0); + Complex z = Complex.ofCartesian(8,0); // The List holding all third roots Complex[] thirdRootsOfZ = z.nthRoot(3).toArray(new Complex[0]); // Returned Collection must not be empty! @@ -730,7 +730,7 @@ public class ComplexTest { @Test public void testNthRoot_cornercase_thirdRoot_realPartZero() { // complex number with only imaginary part - Complex z = new Complex(0,2); + Complex z = Complex.ofCartesian(0,2); // The List holding all third roots Complex[] thirdRootsOfZ = z.nthRoot(3).toArray(new Complex[0]); // Returned Collection must not be empty! @@ -751,28 +751,28 @@ public class ComplexTest { */ @Test public void testGetArgument() { - Complex z = new Complex(1, 0); + Complex z = Complex.ofCartesian(1, 0); Assert.assertEquals(0.0, z.getArgument(), 1.0e-12); - z = new Complex(1, 1); + z = Complex.ofCartesian(1, 1); Assert.assertEquals(Math.PI/4, z.getArgument(), 1.0e-12); - z = new Complex(0, 1); + z = Complex.ofCartesian(0, 1); Assert.assertEquals(Math.PI/2, z.getArgument(), 1.0e-12); - z = new Complex(-1, 1); + z = Complex.ofCartesian(-1, 1); Assert.assertEquals(3 * Math.PI/4, z.getArgument(), 1.0e-12); - z = new Complex(-1, 0); + z = Complex.ofCartesian(-1, 0); Assert.assertEquals(Math.PI, z.getArgument(), 1.0e-12); - z = new Complex(-1, -1); + z = Complex.ofCartesian(-1, -1); Assert.assertEquals(-3 * Math.PI/4, z.getArgument(), 1.0e-12); - z = new Complex(0, -1); + z = Complex.ofCartesian(0, -1); Assert.assertEquals(-Math.PI/2, z.getArgument(), 1.0e-12); - z = new Complex(1, -1); + z = Complex.ofCartesian(1, -1); Assert.assertEquals(-Math.PI/4, z.getArgument(), 1.0e-12); } @@ -805,7 +805,7 @@ public class ComplexTest { /* @Test public void testSerial() { - Complex z = new Complex(3.0, 4.0); + Complex z = Complex.ofCartesian(3.0, 4.0); Assert.assertEquals(z, TestUtils.serializeAndRecover(z)); Complex ncmplx = (Complex)TestUtils.serializeAndRecover(oneNaN); Assert.assertEquals(nanZero, ncmplx); Assert.assertTrue(ncmplx.isNaN()); Complex infcmplx = (Complex)TestUtils.serializeAndRecover(infInf); @@ -840,10 +840,5 @@ public class ComplexTest { this(other.getReal(), other.getImaginary()); } - @Override - protected TestComplex createComplex(double real, double imaginary){ - return new TestComplex(real, imaginary); - } - } } http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/b8670225/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexUtilsTest.java ---------------------------------------------------------------------- diff --git a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexUtilsTest.java b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexUtilsTest.java index 8ca143a..b57a6b1 100644 --- a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexUtilsTest.java +++ b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexUtilsTest.java @@ -31,12 +31,12 @@ public class ComplexUtilsTest { private static final double nan = Double.NaN; private static final double pi = Math.PI; - private static final Complex negInfInf = new Complex(negInf, inf); - private static final Complex infNegInf = new Complex(inf, negInf); - private static final Complex infInf = new Complex(inf, inf); - private static final Complex negInfNegInf = new Complex(negInf, negInf); - private static final Complex infNaN = new Complex(inf, nan); - private static final Complex NAN = new Complex(nan, nan); + private static final Complex negInfInf = Complex.ofCartesian(negInf, inf); + private static final Complex infNegInf = Complex.ofCartesian(inf, negInf); + private static final Complex infInf = Complex.ofCartesian(inf, inf); + private static final Complex negInfNegInf = Complex.ofCartesian(negInf, negInf); + private static final Complex infNaN = Complex.ofCartesian(inf, nan); + private static final Complex NAN = Complex.ofCartesian(nan, nan); private static Complex c[]; // complex array with real values even and imag // values odd @@ -109,9 +109,9 @@ public class ComplexUtilsTest { di[i + 1] = i + 1; fi[i] = i; fi[i + 1] = i + 1; - c[i / 2] = new Complex(i, i + 1); - cr[i / 2] = new Complex(i / 2); - ci[i / 2] = new Complex(0, i / 2); + c[i / 2] = Complex.ofCartesian(i, i + 1); + cr[i / 2] = Complex.ofCartesian(i / 2); + ci[i / 2] = Complex.ofCartesian(0, i / 2); sr[i / 2] = i; si[i / 2] = i + 1; sfr[i / 2] = i; @@ -129,9 +129,9 @@ public class ComplexUtilsTest { di2d[i][j + 1] = 10 * i + j + 1; fi2d[i][j] = 10 * i + j; fi2d[i][j + 1] = 10 * i + j + 1; - c2d[i][j / 2] = new Complex(10 * i + j, 10 * i + j + 1); - cr2d[i][j / 2] = new Complex(10 * i + j / 2); - ci2d[i][j / 2] = new Complex(0, 10 * i + j / 2); + c2d[i][j / 2] = Complex.ofCartesian(10 * i + j, 10 * i + j + 1); + cr2d[i][j / 2] = Complex.ofCartesian(10 * i + j / 2); + ci2d[i][j / 2] = Complex.ofCartesian(0, 10 * i + j / 2); } } for (int i = 0; i < 10; i++) { @@ -147,20 +147,20 @@ public class ComplexUtilsTest { di3d[i][j][k + 1] = 100 * i + 10 * j + k + 1; fi3d[i][j][k] = 100 * i + 10 * j + k; fi3d[i][j][k + 1] = 100 * i + 10 * j + k + 1; - c3d[i][j][k / 2] = new Complex(100 * i + 10 * j + k, 100 * i + 10 * j + k + 1); - cr3d[i][j][k / 2] = new Complex(100 * i + 10 * j + k / 2); - ci3d[i][j][k / 2] = new Complex(0, 100 * i + 10 * j + k / 2); + c3d[i][j][k / 2] = Complex.ofCartesian(100 * i + 10 * j + k, 100 * i + 10 * j + k + 1); + cr3d[i][j][k / 2] = Complex.ofCartesian(100 * i + 10 * j + k / 2); + ci3d[i][j][k / 2] = Complex.ofCartesian(0, 100 * i + 10 * j + k / 2); } } } - ansArrayc1r = new Complex[] { new Complex(3), new Complex(4), new Complex(5), new Complex(6), new Complex(7) }; - ansArrayc2r = new Complex[] { new Complex(3), new Complex(5), new Complex(7) }; - ansArrayc1i = new Complex[] { new Complex(0, 3), new Complex(0, 4), new Complex(0, 5), new Complex(0, 6), - new Complex(0, 7) }; - ansArrayc2i = new Complex[] { new Complex(0, 3), new Complex(0, 5), new Complex(0, 7) }; - ansArrayc3 = new Complex[] { new Complex(6, 7), new Complex(8, 9), new Complex(10, 11), new Complex(12, 13), - new Complex(14, 15) }; - ansArrayc4 = new Complex[] { new Complex(6, 7), new Complex(10, 11), new Complex(14, 15) }; + ansArrayc1r = new Complex[] { Complex.ofCartesian(3), Complex.ofCartesian(4), Complex.ofCartesian(5), Complex.ofCartesian(6), Complex.ofCartesian(7) }; + ansArrayc2r = new Complex[] { Complex.ofCartesian(3), Complex.ofCartesian(5), Complex.ofCartesian(7) }; + ansArrayc1i = new Complex[] { Complex.ofCartesian(0, 3), Complex.ofCartesian(0, 4), Complex.ofCartesian(0, 5), Complex.ofCartesian(0, 6), + Complex.ofCartesian(0, 7) }; + ansArrayc2i = new Complex[] { Complex.ofCartesian(0, 3), Complex.ofCartesian(0, 5), Complex.ofCartesian(0, 7) }; + ansArrayc3 = new Complex[] { Complex.ofCartesian(6, 7), Complex.ofCartesian(8, 9), Complex.ofCartesian(10, 11), Complex.ofCartesian(12, 13), + Complex.ofCartesian(14, 15) }; + ansArrayc4 = new Complex[] { Complex.ofCartesian(6, 7), Complex.ofCartesian(10, 11), Complex.ofCartesian(14, 15) }; ansArrayd1r = new double[] { 6, 8, 10, 12, 14 }; ansArrayd1i = new double[] { 7, 9, 11, 13, 15 }; ansArrayd2r = new double[] { 6, 10, 14 }; @@ -200,7 +200,7 @@ public class ComplexUtilsTest { } protected Complex altPolar(double r, double theta) { - return Complex.I.multiply(new Complex(theta, 0)).exp().multiply(new Complex(r, 0)); + return Complex.I.multiply(Complex.ofCartesian(theta, 0)).exp().multiply(Complex.ofCartesian(r, 0)); } @Test(expected = IllegalArgumentException.class) @@ -244,17 +244,17 @@ public class ComplexUtilsTest { public void testExtractionMethods() { setArrays(); // Extract complex from real double array, index 3 - TestUtils.assertSame(new Complex(3), ComplexUtils.extractComplexFromRealArray(d, 3)); + TestUtils.assertSame(Complex.ofCartesian(3), ComplexUtils.extractComplexFromRealArray(d, 3)); // Extract complex from real float array, index 3 - TestUtils.assertSame(new Complex(3), ComplexUtils.extractComplexFromRealArray(f, 3)); + TestUtils.assertSame(Complex.ofCartesian(3), ComplexUtils.extractComplexFromRealArray(f, 3)); // Extract real double from complex array, index 3 TestUtils.assertSame(6, ComplexUtils.extractRealFromComplexArray(c, 3)); // Extract real float from complex array, index 3 TestUtils.assertSame(6, ComplexUtils.extractRealFloatFromComplexArray(c, 3)); // Extract complex from interleaved double array, index 3 - TestUtils.assertSame(new Complex(6, 7), ComplexUtils.extractComplexFromInterleavedArray(d, 3)); + TestUtils.assertSame(Complex.ofCartesian(6, 7), ComplexUtils.extractComplexFromInterleavedArray(d, 3)); // Extract complex from interleaved float array, index 3 - TestUtils.assertSame(new Complex(6, 7), ComplexUtils.extractComplexFromInterleavedArray(f, 3)); + TestUtils.assertSame(Complex.ofCartesian(6, 7), ComplexUtils.extractComplexFromInterleavedArray(f, 3)); // Extract interleaved double from complex array, index 3 TestUtils.assertEquals(msg, new double[] { 6, 7 }, ComplexUtils.extractInterleavedFromComplexArray(c, 3), Math.ulp(1)); @@ -470,7 +470,7 @@ public class ComplexUtilsTest { Complex[] c = new Complex[10]; ComplexUtils.initialize(c); for (Complex cc : c) { - TestUtils.assertEquals(new Complex(0, 0), cc, Math.ulp(0)); + TestUtils.assertEquals(Complex.ofCartesian(0, 0), cc, Math.ulp(0)); } } }
