http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/a752ab8d/src/test/java/org/apache/commons/complex/ComplexTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/complex/ComplexTest.java b/src/test/java/org/apache/commons/complex/ComplexTest.java deleted file mode 100644 index 7617128..0000000 --- a/src/test/java/org/apache/commons/complex/ComplexTest.java +++ /dev/null @@ -1,1477 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.commons.complex; - -import java.util.List; - -import org.apache.commons.complex.Complex; -import org.apache.commons.complex.ComplexUtils; -import org.junit.Assert; -import org.junit.Ignore; -import org.junit.Test; - - -/** - */ -public class ComplexTest { - - - private double inf = Double.POSITIVE_INFINITY; - private double neginf = Double.NEGATIVE_INFINITY; - private double nan = Double.NaN; - private double pi = Math.PI; - private Complex oneInf = new Complex(1, inf); - private Complex oneNegInf = new Complex(1, neginf); - private Complex infOne = new Complex(inf, 1); - private Complex infZero = new Complex(inf, 0); - private Complex infNaN = new Complex(inf, nan); - private Complex infNegInf = new Complex(inf, neginf); - private Complex infInf = new Complex(inf, inf); - private Complex negInfInf = new Complex(neginf, inf); - private Complex negInfZero = new Complex(neginf, 0); - private Complex negInfOne = new Complex(neginf, 1); - private Complex negInfNaN = new Complex(neginf, nan); - private Complex negInfNegInf = new Complex(neginf, neginf); - private Complex oneNaN = new Complex(1, nan); - private Complex zeroInf = new Complex(0, inf); - private Complex zeroNaN = new Complex(0, nan); - private Complex nanInf = new Complex(nan, inf); - private Complex nanNegInf = new Complex(nan, neginf); - private Complex nanZero = new Complex(nan, 0); - - @Test - public void testConstructor() { - Complex z = new Complex(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); - Assert.assertTrue(z.isNaN()); - - z = new Complex(nan, 4.0); - Assert.assertTrue(z.isNaN()); - - z = new Complex(3.0, 4.0); - Assert.assertFalse(z.isNaN()); - } - - @Test - public void testAbs() { - Complex z = new Complex(3.0, 4.0); - Assert.assertEquals(5.0, z.abs(), 1.0e-5); - } - - @Test - public void testAbsNaN() { - Assert.assertTrue(Double.isNaN(Complex.NaN.abs())); - Complex z = new Complex(inf, nan); - Assert.assertTrue(Double.isNaN(z.abs())); - } - - @Test - public void testAbsInfinite() { - Complex z = new Complex(inf, 0); - Assert.assertEquals(inf, z.abs(), 0); - z = new Complex(0, neginf); - Assert.assertEquals(inf, z.abs(), 0); - z = new Complex(inf, neginf); - Assert.assertEquals(inf, z.abs(), 0); - } - - @Test - public void testAdd() { - Complex x = new Complex(3.0, 4.0); - Complex y = new Complex(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); - } - - @Test - public void testAddNaN() { - Complex x = new Complex(3.0, 4.0); - Complex z = x.add(Complex.NaN); - Assert.assertSame(Complex.NaN, z); - z = new Complex(1, nan); - Complex w = x.add(z); - Assert.assertSame(Complex.NaN, w); - } - - @Test - public void testAddInf() { - Complex x = new Complex(1, 1); - Complex z = new Complex(inf, 0); - Complex w = x.add(z); - Assert.assertEquals(w.getImaginary(), 1, 0); - Assert.assertEquals(inf, w.getReal(), 0); - - x = new Complex(neginf, 0); - Assert.assertTrue(Double.isNaN(x.add(z).getReal())); - } - - - @Test - public void testScalarAdd() { - Complex x = new Complex(3.0, 4.0); - double yDouble = 2.0; - Complex yComplex = new Complex(yDouble); - Assert.assertEquals(x.add(yComplex), x.add(yDouble)); - } - - @Test - public void testScalarAddNaN() { - Complex x = new Complex(3.0, 4.0); - double yDouble = Double.NaN; - Complex yComplex = new Complex(yDouble); - Assert.assertEquals(x.add(yComplex), x.add(yDouble)); - } - - @Test - public void testScalarAddInf() { - Complex x = new Complex(1, 1); - double yDouble = Double.POSITIVE_INFINITY; - - Complex yComplex = new Complex(yDouble); - Assert.assertEquals(x.add(yComplex), x.add(yDouble)); - - x = new Complex(neginf, 0); - Assert.assertEquals(x.add(yComplex), x.add(yDouble)); - } - - @Test - public void testConjugate() { - Complex x = new Complex(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); - } - - @Test - public void testConjugateNaN() { - Complex z = Complex.NaN.conjugate(); - Assert.assertTrue(z.isNaN()); - } - - @Test - public void testConjugateInfiinite() { - Complex z = new Complex(0, inf); - Assert.assertEquals(neginf, z.conjugate().getImaginary(), 0); - z = new Complex(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 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); - } - - @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)); - - } - - @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)); - } - - @Test - public void testDivideInf() { - Complex x = new Complex(3, 4); - Complex w = new Complex(neginf, inf); - Assert.assertTrue(x.divide(w).equals(Complex.ZERO)); - - Complex z = w.divide(x); - Assert.assertTrue(Double.isNaN(z.getReal())); - Assert.assertEquals(inf, z.getImaginary(), 0); - - w = new Complex(inf, inf); - z = w.divide(x); - Assert.assertTrue(Double.isNaN(z.getImaginary())); - Assert.assertEquals(inf, z.getReal(), 0); - - w = new Complex(1, inf); - z = w.divide(w); - Assert.assertTrue(Double.isNaN(z.getReal())); - Assert.assertTrue(Double.isNaN(z.getImaginary())); - } - - @Test - public void testDivideZero() { - Complex x = new Complex(3.0, 4.0); - Complex z = x.divide(Complex.ZERO); - // Assert.assertEquals(z, Complex.INF); // See MATH-657 - Assert.assertEquals(z, Complex.NaN); - } - - @Test - public void testDivideZeroZero() { - Complex x = new Complex(0.0, 0.0); - Complex z = x.divide(Complex.ZERO); - Assert.assertEquals(z, Complex.NaN); - } - - @Test - public void testDivideNaN() { - Complex x = new Complex(3.0, 4.0); - Complex z = x.divide(Complex.NaN); - Assert.assertTrue(z.isNaN()); - } - - @Test - public void testDivideNaNInf() { - Complex z = oneInf.divide(Complex.ONE); - Assert.assertTrue(Double.isNaN(z.getReal())); - Assert.assertEquals(inf, z.getImaginary(), 0); - - z = negInfNegInf.divide(oneNaN); - Assert.assertTrue(Double.isNaN(z.getReal())); - Assert.assertTrue(Double.isNaN(z.getImaginary())); - - z = negInfInf.divide(Complex.ONE); - Assert.assertTrue(Double.isNaN(z.getReal())); - Assert.assertTrue(Double.isNaN(z.getImaginary())); - } - - @Test - public void testScalarDivide() { - Complex x = new Complex(3.0, 4.0); - double yDouble = 2.0; - Complex yComplex = new Complex(yDouble); - Assert.assertEquals(x.divide(yComplex), x.divide(yDouble)); - } - - @Test - public void testScalarDivideNaN() { - Complex x = new Complex(3.0, 4.0); - double yDouble = Double.NaN; - Complex yComplex = new Complex(yDouble); - Assert.assertEquals(x.divide(yComplex), x.divide(yDouble)); - } - - @Test - public void testScalarDivideZero() { - Complex x = new Complex(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 act = z.reciprocal(); - double expRe = 5.0 / 61.0; - double expIm = -6.0 / 61.0; - Assert.assertEquals(expRe, act.getReal(), Math.ulp(expRe)); - Assert.assertEquals(expIm, act.getImaginary(), Math.ulp(expIm)); - } - - @Test - public void testReciprocalReal() { - Complex z = new Complex(-2.0, 0.0); - Assert.assertTrue(Complex.equals(new Complex(-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()); - } - - @Test - public void testReciprocalInf() { - Complex z = new Complex(neginf, inf); - Assert.assertTrue(z.reciprocal().equals(Complex.ZERO)); - - z = new Complex(1, inf).reciprocal(); - Assert.assertEquals(z, Complex.ZERO); - } - - @Test - public void testReciprocalZero() { - Assert.assertEquals(Complex.ZERO.reciprocal(), Complex.INF); - } - - @Test - public void testReciprocalNaN() { - Assert.assertTrue(Complex.NaN.reciprocal().isNaN()); - } - - @Test - public void testMultiply() { - Complex x = new Complex(3.0, 4.0); - Complex y = new Complex(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); - } - - @Test - public void testMultiplyNaN() { - Complex x = new Complex(3.0, 4.0); - Complex z = x.multiply(Complex.NaN); - Assert.assertSame(Complex.NaN, z); - z = Complex.NaN.multiply(5); - Assert.assertSame(Complex.NaN, z); - } - - @Test - public void testMultiplyInfInf() { - // Assert.assertTrue(infInf.multiply(infInf).isNaN()); // MATH-620 - Assert.assertTrue(infInf.multiply(infInf).isInfinite()); - } - - @Test - public void testMultiplyNaNInf() { - Complex z = new Complex(1,1); - Complex w = z.multiply(infOne); - Assert.assertEquals(w.getReal(), inf, 0); - Assert.assertEquals(w.getImaginary(), inf, 0); - - // [MATH-164] - Assert.assertTrue(new Complex( 1,0).multiply(infInf).equals(Complex.INF)); - Assert.assertTrue(new Complex(-1,0).multiply(infInf).equals(Complex.INF)); - Assert.assertTrue(new Complex( 1,0).multiply(negInfZero).equals(Complex.INF)); - - w = oneInf.multiply(oneNegInf); - Assert.assertEquals(w.getReal(), inf, 0); - Assert.assertEquals(w.getImaginary(), inf, 0); - - w = negInfNegInf.multiply(oneNaN); - Assert.assertTrue(Double.isNaN(w.getReal())); - Assert.assertTrue(Double.isNaN(w.getImaginary())); - - z = new Complex(1, neginf); - Assert.assertSame(Complex.INF, z.multiply(z)); - } - - @Test - public void testScalarMultiply() { - Complex x = new Complex(3.0, 4.0); - double yDouble = 2.0; - Complex yComplex = new Complex(yDouble); - Assert.assertEquals(x.multiply(yComplex), x.multiply(yDouble)); - int zInt = -5; - Complex zComplex = new Complex(zInt); - Assert.assertEquals(x.multiply(zComplex), x.multiply(zInt)); - } - - @Test - public void testScalarMultiplyNaN() { - Complex x = new Complex(3.0, 4.0); - double yDouble = Double.NaN; - Complex yComplex = new Complex(yDouble); - Assert.assertEquals(x.multiply(yComplex), x.multiply(yDouble)); - } - - @Test - public void testScalarMultiplyInf() { - Complex x = new Complex(1, 1); - double yDouble = Double.POSITIVE_INFINITY; - Complex yComplex = new Complex(yDouble); - Assert.assertEquals(x.multiply(yComplex), x.multiply(yDouble)); - - yDouble = Double.NEGATIVE_INFINITY; - yComplex = new Complex(yDouble); - Assert.assertEquals(x.multiply(yComplex), x.multiply(yDouble)); - } - - @Test - public void testNegate() { - Complex x = new Complex(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); - } - - @Test - public void testNegateNaN() { - Complex z = Complex.NaN.negate(); - Assert.assertTrue(z.isNaN()); - } - - @Test - public void testSubtract() { - Complex x = new Complex(3.0, 4.0); - Complex y = new Complex(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); - } - - @Test - public void testSubtractNaN() { - Complex x = new Complex(3.0, 4.0); - Complex z = x.subtract(Complex.NaN); - Assert.assertSame(Complex.NaN, z); - z = new Complex(1, nan); - Complex w = x.subtract(z); - Assert.assertSame(Complex.NaN, w); - } - - @Test - public void testSubtractInf() { - Complex x = new Complex(1, 1); - Complex z = new Complex(neginf, 0); - Complex w = x.subtract(z); - Assert.assertEquals(w.getImaginary(), 1, 0); - Assert.assertEquals(inf, w.getReal(), 0); - - x = new Complex(neginf, 0); - Assert.assertTrue(Double.isNaN(x.subtract(z).getReal())); - } - - @Test - public void testScalarSubtract() { - Complex x = new Complex(3.0, 4.0); - double yDouble = 2.0; - Complex yComplex = new Complex(yDouble); - Assert.assertEquals(x.subtract(yComplex), x.subtract(yDouble)); - } - - @Test - public void testScalarSubtractNaN() { - Complex x = new Complex(3.0, 4.0); - double yDouble = Double.NaN; - Complex yComplex = new Complex(yDouble); - Assert.assertEquals(x.subtract(yComplex), x.subtract(yDouble)); - } - - @Test - public void testScalarSubtractInf() { - Complex x = new Complex(1, 1); - double yDouble = Double.POSITIVE_INFINITY; - Complex yComplex = new Complex(yDouble); - Assert.assertEquals(x.subtract(yComplex), x.subtract(yDouble)); - - x = new Complex(neginf, 0); - Assert.assertEquals(x.subtract(yComplex), x.subtract(yDouble)); - } - - - @Test - public void testEqualsNull() { - Complex x = new Complex(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); - } - @Test(expected=NullPointerException.class) - public void testFloatingPointEqualsPrecondition2() { - Complex.equals(null, new Complex(3.0, 4.0), 3); - } - - @Test - public void testEqualsClass() { - Complex x = new Complex(3.0, 4.0); - Assert.assertFalse(x.equals(this)); - } - - @Test - public void testEqualsSame() { - Complex x = new Complex(3.0, 4.0); - Assert.assertTrue(x.equals(x)); - } - - @Test - public void testFloatingPointEquals() { - double re = -3.21; - double im = 456789e10; - - final Complex x = new Complex(re, im); - Complex y = new Complex(re, im); - - Assert.assertTrue(x.equals(y)); - Assert.assertTrue(Complex.equals(x, y)); - - final int maxUlps = 5; - for (int i = 0; i < maxUlps; i++) { - re = Math.nextUp(re); - im = Math.nextUp(im); - } - y = new Complex(re, im); - Assert.assertTrue(Complex.equals(x, y, maxUlps)); - - re = Math.nextUp(re); - im = Math.nextUp(im); - y = new Complex(re, im); - Assert.assertFalse(Complex.equals(x, y, maxUlps)); - } - - @Test - public void testFloatingPointEqualsNaN() { - Complex c = new Complex(Double.NaN, 1); - Assert.assertFalse(Complex.equals(c, c)); - - c = new Complex(1, Double.NaN); - Assert.assertFalse(Complex.equals(c, c)); - } - - @Test - public void testFloatingPointEqualsWithAllowedDelta() { - 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); - Assert.assertTrue(Complex.equals(x, y, tol1)); - - final double tol2 = 0.0624; - Assert.assertFalse(Complex.equals(x, y, tol2)); - } - - @Test - public void testFloatingPointEqualsWithAllowedDeltaNaN() { - final Complex x = new Complex(0, Double.NaN); - final Complex y = new Complex(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)); - } - - @Test - public void testFloatingPointEqualsWithRelativeTolerance() { - final double tol = 1e-4; - final double re = 1; - 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); - 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); - 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)); - } - - @Test - public void testEqualsTrue() { - Complex x = new Complex(3.0, 4.0); - Complex y = new Complex(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); - 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); - Assert.assertFalse(x.equals(y)); - } - - @Test - public void testEqualsNaN() { - Complex realNaN = new Complex(Double.NaN, 0.0); - Complex imaginaryNaN = new Complex(0.0, Double.NaN); - Complex complexNaN = Complex.NaN; - Assert.assertTrue(realNaN.equals(imaginaryNaN)); - Assert.assertTrue(imaginaryNaN.equals(complexNaN)); - Assert.assertTrue(realNaN.equals(complexNaN)); - } - - @Test - public void testHashCode() { - Complex x = new Complex(0.0, 0.0); - Complex y = new Complex(0.0, 0.0 + Double.MIN_VALUE); - Assert.assertFalse(x.hashCode()==y.hashCode()); - y = new Complex(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); - Assert.assertEquals(realNaN.hashCode(), imaginaryNaN.hashCode()); - Assert.assertEquals(imaginaryNaN.hashCode(), Complex.NaN.hashCode()); - - // MATH-1118 - // "equals" and "hashCode" must be compatible: if two objects have - // 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); - 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); - Assert.assertTrue(x.hashCode() != y.hashCode()); - Assert.assertFalse(msg, x.equals(y)); - } - - @Test - public void testAcos() { - Complex z = new Complex(3, 4); - Complex expected = new Complex(0.936812, -2.30551); - TestUtils.assertEquals(expected, z.acos(), 1.0e-5); - TestUtils.assertEquals(new Complex(Math.acos(0), 0), - Complex.ZERO.acos(), 1.0e-12); - } - - @Test - public void testAcosInf() { - TestUtils.assertSame(Complex.NaN, oneInf.acos()); - TestUtils.assertSame(Complex.NaN, oneNegInf.acos()); - TestUtils.assertSame(Complex.NaN, infOne.acos()); - TestUtils.assertSame(Complex.NaN, negInfOne.acos()); - TestUtils.assertSame(Complex.NaN, infInf.acos()); - TestUtils.assertSame(Complex.NaN, infNegInf.acos()); - TestUtils.assertSame(Complex.NaN, negInfInf.acos()); - TestUtils.assertSame(Complex.NaN, negInfNegInf.acos()); - } - - @Test - public void testAcosNaN() { - Assert.assertTrue(Complex.NaN.acos().isNaN()); - } - - @Test - public void testAsin() { - Complex z = new Complex(3, 4); - Complex expected = new Complex(0.633984, 2.30551); - TestUtils.assertEquals(expected, z.asin(), 1.0e-5); - } - - @Test - public void testAsinNaN() { - Assert.assertTrue(Complex.NaN.asin().isNaN()); - } - - @Test - public void testAsinInf() { - TestUtils.assertSame(Complex.NaN, oneInf.asin()); - TestUtils.assertSame(Complex.NaN, oneNegInf.asin()); - TestUtils.assertSame(Complex.NaN, infOne.asin()); - TestUtils.assertSame(Complex.NaN, negInfOne.asin()); - TestUtils.assertSame(Complex.NaN, infInf.asin()); - TestUtils.assertSame(Complex.NaN, infNegInf.asin()); - TestUtils.assertSame(Complex.NaN, negInfInf.asin()); - TestUtils.assertSame(Complex.NaN, negInfNegInf.asin()); - } - - - @Test - public void testAtan() { - Complex z = new Complex(3, 4); - Complex expected = new Complex(1.44831, 0.158997); - TestUtils.assertEquals(expected, z.atan(), 1.0e-5); - } - - @Test - public void testAtanInf() { - TestUtils.assertSame(Complex.NaN, oneInf.atan()); - TestUtils.assertSame(Complex.NaN, oneNegInf.atan()); - TestUtils.assertSame(Complex.NaN, infOne.atan()); - TestUtils.assertSame(Complex.NaN, negInfOne.atan()); - TestUtils.assertSame(Complex.NaN, infInf.atan()); - TestUtils.assertSame(Complex.NaN, infNegInf.atan()); - TestUtils.assertSame(Complex.NaN, negInfInf.atan()); - TestUtils.assertSame(Complex.NaN, negInfNegInf.atan()); - } - - @Test - public void testAtanI() { - Assert.assertTrue(Complex.I.atan().isNaN()); - } - - @Test - public void testAtanNaN() { - Assert.assertTrue(Complex.NaN.atan().isNaN()); - } - - @Test - public void testCos() { - Complex z = new Complex(3, 4); - Complex expected = new Complex(-27.03495, -3.851153); - TestUtils.assertEquals(expected, z.cos(), 1.0e-5); - } - - @Test - public void testCosNaN() { - Assert.assertTrue(Complex.NaN.cos().isNaN()); - } - - @Test - public void testCosInf() { - TestUtils.assertSame(infNegInf, oneInf.cos()); - TestUtils.assertSame(infInf, oneNegInf.cos()); - TestUtils.assertSame(Complex.NaN, infOne.cos()); - TestUtils.assertSame(Complex.NaN, negInfOne.cos()); - TestUtils.assertSame(Complex.NaN, infInf.cos()); - TestUtils.assertSame(Complex.NaN, infNegInf.cos()); - TestUtils.assertSame(Complex.NaN, negInfInf.cos()); - TestUtils.assertSame(Complex.NaN, negInfNegInf.cos()); - } - - @Test - public void testCosh() { - Complex z = new Complex(3, 4); - Complex expected = new Complex(-6.58066, -7.58155); - TestUtils.assertEquals(expected, z.cosh(), 1.0e-5); - } - - @Test - public void testCoshNaN() { - Assert.assertTrue(Complex.NaN.cosh().isNaN()); - } - - @Test - public void testCoshInf() { - TestUtils.assertSame(Complex.NaN, oneInf.cosh()); - TestUtils.assertSame(Complex.NaN, oneNegInf.cosh()); - TestUtils.assertSame(infInf, infOne.cosh()); - TestUtils.assertSame(infNegInf, negInfOne.cosh()); - TestUtils.assertSame(Complex.NaN, infInf.cosh()); - TestUtils.assertSame(Complex.NaN, infNegInf.cosh()); - TestUtils.assertSame(Complex.NaN, negInfInf.cosh()); - TestUtils.assertSame(Complex.NaN, negInfNegInf.cosh()); - } - - @Test - public void testExp() { - final double tol = Math.ulp(1d); - Complex z = new Complex(3, 4); - Complex expected = new Complex(-13.12878, -15.20078); - TestUtils.assertEquals(expected, z.exp(), 1.0e-5); - TestUtils.assertEquals(Complex.ONE, - Complex.ZERO.exp(), tol); - Complex iPi = Complex.I.multiply(new Complex(pi,0)); - TestUtils.assertEquals(Complex.ONE.negate(), - iPi.exp(), tol); - } - - @Test - public void testExpNaN() { - Assert.assertTrue(Complex.NaN.exp().isNaN()); - } - - @Test - public void testExpInf1() { - TestUtils.assertSame(Complex.NaN, oneInf.exp()); - } - - @Test - public void testExpInf2() { - TestUtils.assertSame(Complex.NaN, oneNegInf.exp()); - } - - @Test - public void testExpInf3() { - TestUtils.assertSame(infInf, infOne.exp()); - } - - @Test - @Ignore - public void testJava() {// TODO more debug - System.out.println(">>testJava()"); - // MathTest#testExpSpecialCases() checks the following: - // Assert.assertEquals("exp of -infinity should be 0.0", 0.0, Math.exp(Double.NEGATIVE_INFINITY), Precision.EPSILON); - // Let's check how well Math works: - System.out.println("Math.exp="+Math.exp(Double.NEGATIVE_INFINITY)); - String props[] = { - "java.version", // Java Runtime Environment version - "java.vendor", // Java Runtime Environment vendor - "java.vm.specification.version", // Java Virtual Machine specification version - "java.vm.specification.vendor", // Java Virtual Machine specification vendor - "java.vm.specification.name", // Java Virtual Machine specification name - "java.vm.version", // Java Virtual Machine implementation version - "java.vm.vendor", // Java Virtual Machine implementation vendor - "java.vm.name", // Java Virtual Machine implementation name - "java.specification.version", // Java Runtime Environment specification version - "java.specification.vendor", // Java Runtime Environment specification vendor - "java.specification.name", // Java Runtime Environment specification name - "java.class.version", // Java class format version number - }; - for(String t : props) { - System.out.println(t + "=" + System.getProperty(t)); - } - System.out.println("<<testJava()"); - } - - @Test - public void testExpInf4() { - final Complex exp = negInfOne.exp(); - TestUtils.assertSame(Complex.ZERO, exp); - } - - @Test - public void testExpInf5() { - TestUtils.assertSame(Complex.NaN, infInf.exp()); - } - - @Test - public void testExpInf6() { - TestUtils.assertSame(Complex.NaN, infNegInf.exp()); - } - - @Test - public void testExpInf7() { - TestUtils.assertSame(Complex.NaN, negInfInf.exp()); - } - - @Test - public void testExpInf8() { - TestUtils.assertSame(Complex.NaN, negInfNegInf.exp()); - } - - @Test - public void testLog() { - Complex z = new Complex(3, 4); - Complex expected = new Complex(1.60944, 0.927295); - TestUtils.assertEquals(expected, z.log(), 1.0e-5); - } - - @Test - public void testLogNaN() { - Assert.assertTrue(Complex.NaN.log().isNaN()); - } - - @Test - public void testLogInf() { - final double tol = Math.ulp(1d); - TestUtils.assertEquals(new Complex(inf, pi / 2), - oneInf.log(), tol); - TestUtils.assertEquals(new Complex(inf, -pi / 2), - oneNegInf.log(), tol); - TestUtils.assertEquals(infZero, infOne.log(), tol); - TestUtils.assertEquals(new Complex(inf, pi), - negInfOne.log(), tol); - TestUtils.assertEquals(new Complex(inf, pi / 4), - infInf.log(), tol); - TestUtils.assertEquals(new Complex(inf, -pi / 4), - infNegInf.log(), tol); - TestUtils.assertEquals(new Complex(inf, 3d * pi / 4), - negInfInf.log(), tol); - TestUtils.assertEquals(new Complex(inf, - 3d * pi / 4), - negInfNegInf.log(), tol); - } - - @Test - public void testLogZero() { - TestUtils.assertSame(negInfZero, Complex.ZERO.log()); - } - - @Test - public void testPow() { - Complex x = new Complex(3, 4); - Complex y = new Complex(5, 6); - Complex expected = new Complex(-1.860893, 11.83677); - TestUtils.assertEquals(expected, x.pow(y), 1.0e-5); - } - - @Test - public void testPowNaNBase() { - Complex x = new Complex(3, 4); - Assert.assertTrue(Complex.NaN.pow(x).isNaN()); - } - - @Test - public void testPowNaNExponent() { - Complex x = new Complex(3, 4); - Assert.assertTrue(x.pow(Complex.NaN).isNaN()); - } - - @Test - public void testPowInf() { - TestUtils.assertSame(Complex.NaN,Complex.ONE.pow(oneInf)); - TestUtils.assertSame(Complex.NaN,Complex.ONE.pow(oneNegInf)); - TestUtils.assertSame(Complex.NaN,Complex.ONE.pow(infOne)); - TestUtils.assertSame(Complex.NaN,Complex.ONE.pow(infInf)); - TestUtils.assertSame(Complex.NaN,Complex.ONE.pow(infNegInf)); - TestUtils.assertSame(Complex.NaN,Complex.ONE.pow(negInfInf)); - TestUtils.assertSame(Complex.NaN,Complex.ONE.pow(negInfNegInf)); - TestUtils.assertSame(Complex.NaN,infOne.pow(Complex.ONE)); - TestUtils.assertSame(Complex.NaN,negInfOne.pow(Complex.ONE)); - TestUtils.assertSame(Complex.NaN,infInf.pow(Complex.ONE)); - TestUtils.assertSame(Complex.NaN,infNegInf.pow(Complex.ONE)); - TestUtils.assertSame(Complex.NaN,negInfInf.pow(Complex.ONE)); - TestUtils.assertSame(Complex.NaN,negInfNegInf.pow(Complex.ONE)); - TestUtils.assertSame(Complex.NaN,negInfNegInf.pow(infNegInf)); - TestUtils.assertSame(Complex.NaN,negInfNegInf.pow(negInfNegInf)); - TestUtils.assertSame(Complex.NaN,negInfNegInf.pow(infInf)); - TestUtils.assertSame(Complex.NaN,infInf.pow(infNegInf)); - TestUtils.assertSame(Complex.NaN,infInf.pow(negInfNegInf)); - TestUtils.assertSame(Complex.NaN,infInf.pow(infInf)); - TestUtils.assertSame(Complex.NaN,infNegInf.pow(infNegInf)); - TestUtils.assertSame(Complex.NaN,infNegInf.pow(negInfNegInf)); - TestUtils.assertSame(Complex.NaN,infNegInf.pow(infInf)); - } - - @Test - public void testPowZero() { - final double tol = Math.ulp(1d); - TestUtils.assertEquals(Complex.ZERO, - Complex.ZERO.pow(Complex.ONE), tol); - TestUtils.assertEquals(Complex.ZERO, - Complex.ZERO.pow(new Complex(2, 0)), tol); - TestUtils.assertSame(Complex.NaN, - Complex.ZERO.pow(Complex.ZERO)); - TestUtils.assertSame(Complex.NaN, - Complex.ZERO.pow(Complex.I)); - TestUtils.assertEquals(Complex.ONE, - Complex.ONE.pow(Complex.ZERO), tol); - TestUtils.assertEquals(Complex.ONE, - Complex.I.pow(Complex.ZERO), tol); - TestUtils.assertEquals(Complex.ONE, - new Complex(-1, 3).pow(Complex.ZERO), tol); - } - - @Test - public void testScalarPow() { - Complex x = new Complex(3, 4); - double yDouble = 5.0; - Complex yComplex = new Complex(yDouble); - Assert.assertEquals(x.pow(yComplex), x.pow(yDouble)); - } - - @Test - public void testScalarPowNaNBase() { - Complex x = Complex.NaN; - double yDouble = 5.0; - Complex yComplex = new Complex(yDouble); - Assert.assertEquals(x.pow(yComplex), x.pow(yDouble)); - } - - @Test - public void testScalarPowNaNExponent() { - Complex x = new Complex(3, 4); - double yDouble = Double.NaN; - Complex yComplex = new Complex(yDouble); - Assert.assertEquals(x.pow(yComplex), x.pow(yDouble)); - } - - @Test - public void testScalarPowInf() { - TestUtils.assertSame(Complex.NaN,Complex.ONE.pow(Double.POSITIVE_INFINITY)); - TestUtils.assertSame(Complex.NaN,Complex.ONE.pow(Double.NEGATIVE_INFINITY)); - TestUtils.assertSame(Complex.NaN,infOne.pow(1.0)); - TestUtils.assertSame(Complex.NaN,negInfOne.pow(1.0)); - TestUtils.assertSame(Complex.NaN,infInf.pow(1.0)); - TestUtils.assertSame(Complex.NaN,infNegInf.pow(1.0)); - TestUtils.assertSame(Complex.NaN,negInfInf.pow(10)); - TestUtils.assertSame(Complex.NaN,negInfNegInf.pow(1.0)); - TestUtils.assertSame(Complex.NaN,negInfNegInf.pow(Double.POSITIVE_INFINITY)); - TestUtils.assertSame(Complex.NaN,negInfNegInf.pow(Double.POSITIVE_INFINITY)); - TestUtils.assertSame(Complex.NaN,infInf.pow(Double.POSITIVE_INFINITY)); - TestUtils.assertSame(Complex.NaN,infInf.pow(Double.NEGATIVE_INFINITY)); - TestUtils.assertSame(Complex.NaN,infNegInf.pow(Double.NEGATIVE_INFINITY)); - TestUtils.assertSame(Complex.NaN,infNegInf.pow(Double.POSITIVE_INFINITY)); - } - - @Test - public void testScalarPowZero() { - final double tol = Math.ulp(1d); - TestUtils.assertEquals(Complex.ZERO, Complex.ZERO.pow(1.0), tol); - TestUtils.assertEquals(Complex.ZERO, Complex.ZERO.pow(2.0), tol); - TestUtils.assertSame(Complex.NaN, Complex.ZERO.pow(0.0)); - TestUtils.assertSame(Complex.NaN, Complex.ZERO.pow(-1.0)); - TestUtils.assertEquals(Complex.ONE, Complex.ONE.pow(0.0), tol); - TestUtils.assertEquals(Complex.ONE, Complex.I.pow(0.0), tol); - TestUtils.assertEquals(Complex.ONE, new Complex(-1, 3).pow(0.0), tol); - } - - @Test - public void testSin() { - Complex z = new Complex(3, 4); - Complex expected = new Complex(3.853738, -27.01681); - TestUtils.assertEquals(expected, z.sin(), 1.0e-5); - } - - @Test - public void testSinInf() { - TestUtils.assertSame(infInf, oneInf.sin()); - TestUtils.assertSame(infNegInf, oneNegInf.sin()); - TestUtils.assertSame(Complex.NaN, infOne.sin()); - TestUtils.assertSame(Complex.NaN, negInfOne.sin()); - TestUtils.assertSame(Complex.NaN, infInf.sin()); - TestUtils.assertSame(Complex.NaN, infNegInf.sin()); - TestUtils.assertSame(Complex.NaN, negInfInf.sin()); - TestUtils.assertSame(Complex.NaN, negInfNegInf.sin()); - } - - @Test - public void testSinNaN() { - Assert.assertTrue(Complex.NaN.sin().isNaN()); - } - - @Test - public void testSinh() { - Complex z = new Complex(3, 4); - Complex expected = new Complex(-6.54812, -7.61923); - TestUtils.assertEquals(expected, z.sinh(), 1.0e-5); - } - - @Test - public void testSinhNaN() { - Assert.assertTrue(Complex.NaN.sinh().isNaN()); - } - - @Test - public void testSinhInf() { - TestUtils.assertSame(Complex.NaN, oneInf.sinh()); - TestUtils.assertSame(Complex.NaN, oneNegInf.sinh()); - TestUtils.assertSame(infInf, infOne.sinh()); - TestUtils.assertSame(negInfInf, negInfOne.sinh()); - TestUtils.assertSame(Complex.NaN, infInf.sinh()); - TestUtils.assertSame(Complex.NaN, infNegInf.sinh()); - TestUtils.assertSame(Complex.NaN, negInfInf.sinh()); - TestUtils.assertSame(Complex.NaN, negInfNegInf.sinh()); - } - - @Test - public void testSqrtRealPositive() { - Complex z = new Complex(3, 4); - Complex expected = new Complex(2, 1); - TestUtils.assertEquals(expected, z.sqrt(), 1.0e-5); - } - - @Test - public void testSqrtRealZero() { - Complex z = new Complex(0.0, 4); - Complex expected = new Complex(1.41421, 1.41421); - TestUtils.assertEquals(expected, z.sqrt(), 1.0e-5); - } - - @Test - public void testSqrtRealNegative() { - Complex z = new Complex(-3.0, 4); - Complex expected = new Complex(1, 2); - TestUtils.assertEquals(expected, z.sqrt(), 1.0e-5); - } - - @Test - public void testSqrtImaginaryZero() { - Complex z = new Complex(-3.0, 0.0); - Complex expected = new Complex(0.0, 1.73205); - TestUtils.assertEquals(expected, z.sqrt(), 1.0e-5); - } - - @Test - public void testSqrtImaginaryNegative() { - Complex z = new Complex(-3.0, -4.0); - Complex expected = new Complex(1.0, -2.0); - TestUtils.assertEquals(expected, z.sqrt(), 1.0e-5); - } - - @Test - public void testSqrtPolar() { - final double tol = 1e-12; - double r = 1; - for (int i = 0; i < 5; i++) { - r += i; - double theta = 0; - for (int j = 0; j < 11; j++) { - theta += pi / 12; - Complex z = ComplexUtils.polar2Complex(r, theta); - Complex sqrtz = ComplexUtils.polar2Complex(Math.sqrt(r), theta / 2); - TestUtils.assertEquals(sqrtz, z.sqrt(), tol); - } - } - } - - @Test - public void testSqrtNaN() { - Assert.assertTrue(Complex.NaN.sqrt().isNaN()); - } - - @Test - public void testSqrtInf() { - TestUtils.assertSame(infNaN, oneInf.sqrt()); - TestUtils.assertSame(infNaN, oneNegInf.sqrt()); - TestUtils.assertSame(infZero, infOne.sqrt()); - TestUtils.assertSame(zeroInf, negInfOne.sqrt()); - TestUtils.assertSame(infNaN, infInf.sqrt()); - TestUtils.assertSame(infNaN, infNegInf.sqrt()); - TestUtils.assertSame(nanInf, negInfInf.sqrt()); - TestUtils.assertSame(nanNegInf, negInfNegInf.sqrt()); - } - - @Test - public void testSqrt1z() { - Complex z = new Complex(3, 4); - Complex expected = new Complex(4.08033, -2.94094); - TestUtils.assertEquals(expected, z.sqrt1z(), 1.0e-5); - } - - @Test - public void testSqrt1zNaN() { - Assert.assertTrue(Complex.NaN.sqrt1z().isNaN()); - } - - @Test - public void testTan() { - Complex z = new Complex(3, 4); - Complex expected = new Complex(-0.000187346, 0.999356); - TestUtils.assertEquals(expected, z.tan(), 1.0e-5); - /* Check that no overflow occurs (MATH-722) */ - Complex actual = new Complex(3.0, 1E10).tan(); - expected = new Complex(0, 1); - TestUtils.assertEquals(expected, actual, 1.0e-5); - actual = new Complex(3.0, -1E10).tan(); - expected = new Complex(0, -1); - TestUtils.assertEquals(expected, actual, 1.0e-5); - } - - @Test - public void testTanNaN() { - Assert.assertTrue(Complex.NaN.tan().isNaN()); - } - - @Test - public void testTanInf() { - TestUtils.assertSame(Complex.valueOf(0.0, 1.0), oneInf.tan()); - TestUtils.assertSame(Complex.valueOf(0.0, -1.0), oneNegInf.tan()); - TestUtils.assertSame(Complex.NaN, infOne.tan()); - TestUtils.assertSame(Complex.NaN, negInfOne.tan()); - TestUtils.assertSame(Complex.NaN, infInf.tan()); - TestUtils.assertSame(Complex.NaN, infNegInf.tan()); - TestUtils.assertSame(Complex.NaN, negInfInf.tan()); - TestUtils.assertSame(Complex.NaN, negInfNegInf.tan()); - } - - @Test - public void testTanCritical() { - TestUtils.assertSame(infNaN, new Complex(pi/2, 0).tan()); - TestUtils.assertSame(negInfNaN, new Complex(-pi/2, 0).tan()); - } - - @Test - public void testTanh() { - Complex z = new Complex(3, 4); - Complex expected = new Complex(1.00071, 0.00490826); - TestUtils.assertEquals(expected, z.tanh(), 1.0e-5); - /* Check that no overflow occurs (MATH-722) */ - Complex actual = new Complex(1E10, 3.0).tanh(); - expected = new Complex(1, 0); - TestUtils.assertEquals(expected, actual, 1.0e-5); - actual = new Complex(-1E10, 3.0).tanh(); - expected = new Complex(-1, 0); - TestUtils.assertEquals(expected, actual, 1.0e-5); - } - - @Test - public void testTanhNaN() { - Assert.assertTrue(Complex.NaN.tanh().isNaN()); - } - - @Test - public void testTanhInf() { - TestUtils.assertSame(Complex.NaN, oneInf.tanh()); - TestUtils.assertSame(Complex.NaN, oneNegInf.tanh()); - TestUtils.assertSame(Complex.valueOf(1.0, 0.0), infOne.tanh()); - TestUtils.assertSame(Complex.valueOf(-1.0, 0.0), negInfOne.tanh()); - TestUtils.assertSame(Complex.NaN, infInf.tanh()); - TestUtils.assertSame(Complex.NaN, infNegInf.tanh()); - TestUtils.assertSame(Complex.NaN, negInfInf.tanh()); - TestUtils.assertSame(Complex.NaN, negInfNegInf.tanh()); - } - - @Test - public void testTanhCritical() { - TestUtils.assertSame(nanInf, new Complex(0, pi/2).tanh()); - } - - /** test issue MATH-221 */ - @Test - public void testMath221() { - Assert.assertTrue(Complex.equals(new Complex(0,-1), - new Complex(0,1).multiply(new Complex(-1,0)))); - } - - /** - * Test: computing <b>third roots</b> of z. - * <pre> - * <code> - * <b>z = -2 + 2 * i</b> - * => z_0 = 1 + i - * => z_1 = -1.3660 + 0.3660 * i - * => z_2 = 0.3660 - 1.3660 * i - * </code> - * </pre> - */ - @Test - public void testNthRoot_normal_thirdRoot() { - // The complex number we want to compute all third-roots for. - Complex z = new Complex(-2,2); - // The List holding all third roots - Complex[] thirdRootsOfZ = z.nthRoot(3).toArray(new Complex[0]); - // Returned Collection must not be empty! - Assert.assertEquals(3, thirdRootsOfZ.length); - // test z_0 - Assert.assertEquals(1.0, thirdRootsOfZ[0].getReal(), 1.0e-5); - Assert.assertEquals(1.0, thirdRootsOfZ[0].getImaginary(), 1.0e-5); - // test z_1 - Assert.assertEquals(-1.3660254037844386, thirdRootsOfZ[1].getReal(), 1.0e-5); - Assert.assertEquals(0.36602540378443843, thirdRootsOfZ[1].getImaginary(), 1.0e-5); - // test z_2 - Assert.assertEquals(0.366025403784439, thirdRootsOfZ[2].getReal(), 1.0e-5); - Assert.assertEquals(-1.3660254037844384, thirdRootsOfZ[2].getImaginary(), 1.0e-5); - } - - - /** - * Test: computing <b>fourth roots</b> of z. - * <pre> - * <code> - * <b>z = 5 - 2 * i</b> - * => z_0 = 1.5164 - 0.1446 * i - * => z_1 = 0.1446 + 1.5164 * i - * => z_2 = -1.5164 + 0.1446 * i - * => z_3 = -1.5164 - 0.1446 * i - * </code> - * </pre> - */ - @Test - public void testNthRoot_normal_fourthRoot() { - // The complex number we want to compute all third-roots for. - Complex z = new Complex(5,-2); - // The List holding all fourth roots - Complex[] fourthRootsOfZ = z.nthRoot(4).toArray(new Complex[0]); - // Returned Collection must not be empty! - Assert.assertEquals(4, fourthRootsOfZ.length); - // test z_0 - Assert.assertEquals(1.5164629308487783, fourthRootsOfZ[0].getReal(), 1.0e-5); - Assert.assertEquals(-0.14469266210702247, fourthRootsOfZ[0].getImaginary(), 1.0e-5); - // test z_1 - Assert.assertEquals(0.14469266210702256, fourthRootsOfZ[1].getReal(), 1.0e-5); - Assert.assertEquals(1.5164629308487783, fourthRootsOfZ[1].getImaginary(), 1.0e-5); - // test z_2 - Assert.assertEquals(-1.5164629308487783, fourthRootsOfZ[2].getReal(), 1.0e-5); - Assert.assertEquals(0.14469266210702267, fourthRootsOfZ[2].getImaginary(), 1.0e-5); - // test z_3 - Assert.assertEquals(-0.14469266210702275, fourthRootsOfZ[3].getReal(), 1.0e-5); - Assert.assertEquals(-1.5164629308487783, fourthRootsOfZ[3].getImaginary(), 1.0e-5); - } - - /** - * Test: computing <b>third roots</b> of z. - * <pre> - * <code> - * <b>z = 8</b> - * => z_0 = 2 - * => z_1 = -1 + 1.73205 * i - * => z_2 = -1 - 1.73205 * i - * </code> - * </pre> - */ - @Test - 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); - // The List holding all third roots - Complex[] thirdRootsOfZ = z.nthRoot(3).toArray(new Complex[0]); - // Returned Collection must not be empty! - Assert.assertEquals(3, thirdRootsOfZ.length); - // test z_0 - Assert.assertEquals(2.0, thirdRootsOfZ[0].getReal(), 1.0e-5); - Assert.assertEquals(0.0, thirdRootsOfZ[0].getImaginary(), 1.0e-5); - // test z_1 - Assert.assertEquals(-1.0, thirdRootsOfZ[1].getReal(), 1.0e-5); - Assert.assertEquals(1.7320508075688774, thirdRootsOfZ[1].getImaginary(), 1.0e-5); - // test z_2 - Assert.assertEquals(-1.0, thirdRootsOfZ[2].getReal(), 1.0e-5); - Assert.assertEquals(-1.732050807568877, thirdRootsOfZ[2].getImaginary(), 1.0e-5); - } - - - /** - * Test: computing <b>third roots</b> of z with real part 0. - * <pre> - * <code> - * <b>z = 2 * i</b> - * => z_0 = 1.0911 + 0.6299 * i - * => z_1 = -1.0911 + 0.6299 * i - * => z_2 = -2.3144 - 1.2599 * i - * </code> - * </pre> - */ - @Test - public void testNthRoot_cornercase_thirdRoot_realPartZero() { - // complex number with only imaginary part - Complex z = new Complex(0,2); - // The List holding all third roots - Complex[] thirdRootsOfZ = z.nthRoot(3).toArray(new Complex[0]); - // Returned Collection must not be empty! - Assert.assertEquals(3, thirdRootsOfZ.length); - // test z_0 - Assert.assertEquals(1.0911236359717216, thirdRootsOfZ[0].getReal(), 1.0e-5); - Assert.assertEquals(0.6299605249474365, thirdRootsOfZ[0].getImaginary(), 1.0e-5); - // test z_1 - Assert.assertEquals(-1.0911236359717216, thirdRootsOfZ[1].getReal(), 1.0e-5); - Assert.assertEquals(0.6299605249474365, thirdRootsOfZ[1].getImaginary(), 1.0e-5); - // test z_2 - Assert.assertEquals(-2.3144374213981936E-16, thirdRootsOfZ[2].getReal(), 1.0e-5); - Assert.assertEquals(-1.2599210498948732, thirdRootsOfZ[2].getImaginary(), 1.0e-5); - } - - /** - * Test cornercases with NaN and Infinity. - */ - @Test - public void testNthRoot_cornercase_NAN_Inf() { - // NaN + finite -> NaN - List<Complex> roots = oneNaN.nthRoot(3); - Assert.assertEquals(1,roots.size()); - Assert.assertEquals(Complex.NaN, roots.get(0)); - - roots = nanZero.nthRoot(3); - Assert.assertEquals(1,roots.size()); - Assert.assertEquals(Complex.NaN, roots.get(0)); - - // NaN + infinite -> NaN - roots = nanInf.nthRoot(3); - Assert.assertEquals(1,roots.size()); - Assert.assertEquals(Complex.NaN, roots.get(0)); - - // finite + infinite -> Inf - roots = oneInf.nthRoot(3); - Assert.assertEquals(1,roots.size()); - Assert.assertEquals(Complex.INF, roots.get(0)); - - // infinite + infinite -> Inf - roots = negInfInf.nthRoot(3); - Assert.assertEquals(1,roots.size()); - Assert.assertEquals(Complex.INF, roots.get(0)); - } - - /** - * Test standard values - */ - @Test - public void testGetArgument() { - Complex z = new Complex(1, 0); - Assert.assertEquals(0.0, z.getArgument(), 1.0e-12); - - z = new Complex(1, 1); - Assert.assertEquals(Math.PI/4, z.getArgument(), 1.0e-12); - - z = new Complex(0, 1); - Assert.assertEquals(Math.PI/2, z.getArgument(), 1.0e-12); - - z = new Complex(-1, 1); - Assert.assertEquals(3 * Math.PI/4, z.getArgument(), 1.0e-12); - - z = new Complex(-1, 0); - Assert.assertEquals(Math.PI, z.getArgument(), 1.0e-12); - - z = new Complex(-1, -1); - Assert.assertEquals(-3 * Math.PI/4, z.getArgument(), 1.0e-12); - - z = new Complex(0, -1); - Assert.assertEquals(-Math.PI/2, z.getArgument(), 1.0e-12); - - z = new Complex(1, -1); - Assert.assertEquals(-Math.PI/4, z.getArgument(), 1.0e-12); - - } - - /** - * Verify atan2-style handling of infinite parts - */ - @Test - public void testGetArgumentInf() { - Assert.assertEquals(Math.PI/4, infInf.getArgument(), 1.0e-12); - Assert.assertEquals(Math.PI/2, oneInf.getArgument(), 1.0e-12); - Assert.assertEquals(0.0, infOne.getArgument(), 1.0e-12); - Assert.assertEquals(Math.PI/2, zeroInf.getArgument(), 1.0e-12); - Assert.assertEquals(0.0, infZero.getArgument(), 1.0e-12); - Assert.assertEquals(Math.PI, negInfOne.getArgument(), 1.0e-12); - Assert.assertEquals(-3.0*Math.PI/4, negInfNegInf.getArgument(), 1.0e-12); - Assert.assertEquals(-Math.PI/2, oneNegInf.getArgument(), 1.0e-12); - } - - /** - * Verify that either part NaN results in NaN - */ - @Test - public void testGetArgumentNaN() { - Assert.assertTrue(Double.isNaN(nanZero.getArgument())); - Assert.assertTrue(Double.isNaN(zeroNaN.getArgument())); - Assert.assertTrue(Double.isNaN(Complex.NaN.getArgument())); - } - - @Test - public void testSerial() { - Complex z = new Complex(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); - Assert.assertEquals(infInf, infcmplx); - Assert.assertTrue(infcmplx.isInfinite()); - TestComplex tz = new TestComplex(3.0, 4.0); - Assert.assertEquals(tz, TestUtils.serializeAndRecover(tz)); - TestComplex ntcmplx = (TestComplex)TestUtils.serializeAndRecover(new TestComplex(oneNaN)); - Assert.assertEquals(nanZero, ntcmplx); - Assert.assertTrue(ntcmplx.isNaN()); - TestComplex inftcmplx = (TestComplex)TestUtils.serializeAndRecover(new TestComplex(infInf)); - Assert.assertEquals(infInf, inftcmplx); - Assert.assertTrue(inftcmplx.isInfinite()); - } - - /** - * Class to test extending Complex - */ - public static class TestComplex extends Complex { - - /** - * Serialization identifier. - */ - private static final long serialVersionUID = 3268726724160389237L; - - public TestComplex(double real, double imaginary) { - super(real, imaginary); - } - - public TestComplex(Complex other){ - 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/a752ab8d/src/test/java/org/apache/commons/complex/ComplexUtilsTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/complex/ComplexUtilsTest.java b/src/test/java/org/apache/commons/complex/ComplexUtilsTest.java deleted file mode 100755 index 09ee4c8..0000000 --- a/src/test/java/org/apache/commons/complex/ComplexUtilsTest.java +++ /dev/null @@ -1,475 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.commons.complex; - -import org.apache.commons.complex.Complex; -import org.apache.commons.complex.ComplexUtils; -import org.junit.Assert; -import org.junit.Test; - -/** - */ -public class ComplexUtilsTest { - - private final double inf = Double.POSITIVE_INFINITY; - private final double negInf = Double.NEGATIVE_INFINITY; - private final double nan = Double.NaN; - private final double pi = Math.PI; - - private final Complex negInfInf = new Complex(negInf, inf); - private final Complex infNegInf = new Complex(inf, negInf); - private final Complex infInf = new Complex(inf, inf); - private final Complex negInfNegInf = new Complex(negInf, negInf); - private final Complex infNaN = new Complex(inf, nan); - - private static Complex c[]; // complex array with real values even and imag - // values odd - private static Complex cr[]; // complex array with real values consecutive - private static Complex ci[]; // complex array with imag values consecutive - private static double d[]; // real array with consecutive vals - private static double di[]; // real array with consecutive vals, - // 'interleaved' length - private static float f[]; // real array with consecutive vals - private static float fi[]; // real array with consec vals, interleaved - // length - private static double sr[]; // real component of split array, evens - private static double si[]; // imag component of split array, odds - private static float sfr[]; // real component of split array, float, evens - private static float sfi[]; // imag component of split array, float, odds - static Complex ans1, ans2; // answers to single value extraction methods - static Complex[] ansArrayc1r, ansArrayc1i, ansArrayc2r, ansArrayc2i, ansArrayc3, ansArrayc4; // answers - // to - // range - // extraction - // methods - static double[] ansArrayd1r, ansArrayd2r, ansArrayd1i, ansArrayd2i, ansArraydi1, ansArraydi2; - static float[] ansArrayf1r, ansArrayf2r, ansArrayf1i, ansArrayf2i, ansArrayfi1, ansArrayfi2; - static String msg; // error message for AssertEquals - static Complex[][] c2d, cr2d, ci2d; // for 2d methods - static Complex[][][] c3d, cr3d, ci3d; // for 3d methods - static double[][] d2d, di2d, sr2d, si2d; - static double[][][] d3d, di3d, sr3d, si3d; - static float[][] f2d, fi2d, sfr2d, sfi2d; - static float[][][] f3d, fi3d, sfr3d, sfi3d; - - private static void setArrays() { // initial setup method - c = new Complex[10]; - cr = new Complex[10]; - ci = new Complex[10]; - d = new double[10]; - f = new float[10]; - di = new double[20]; - fi = new float[20]; - sr = new double[10]; - si = new double[10]; - sfr = new float[10]; - sfi = new float[10]; - c2d = new Complex[10][10]; - cr2d = new Complex[10][10]; - ci2d = new Complex[10][10]; - c3d = new Complex[10][10][10]; - cr3d = new Complex[10][10][10]; - ci3d = new Complex[10][10][10]; - d2d = new double[10][10]; - d3d = new double[10][10][10]; - f2d = new float[10][10]; - f3d = new float[10][10][10]; - sr2d = new double[10][10]; - sr3d = new double[10][10][10]; - si2d = new double[10][10]; - si3d = new double[10][10][10]; - sfr2d = new float[10][10]; - sfr3d = new float[10][10][10]; - sfi2d = new float[10][10]; - sfi3d = new float[10][10][10]; - di2d = new double[10][20]; - di3d = new double[10][10][20]; - fi2d = new float[10][20]; - fi3d = new float[10][10][20]; - for (int i = 0; i < 20; i += 2) { - d[i / 2] = i / 2; - f[i / 2] = i / 2; - di[i] = i; - 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); - sr[i / 2] = i; - si[i / 2] = i + 1; - sfr[i / 2] = i; - sfi[i / 2] = i + 1; - } - for (int i = 0; i < 10; i++) { - for (int j = 0; j < 20; j += 2) { - d2d[i][j / 2] = 10 * i + j / 2; - f2d[i][j / 2] = 10 * i + j / 2; - sr2d[i][j / 2] = 10 * i + j; - si2d[i][j / 2] = 10 * i + j + 1; - sfr2d[i][j / 2] = 10 * i + j; - sfi2d[i][j / 2] = 10 * i + j + 1; - di2d[i][j] = 10 * i + j; - 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); - } - } - for (int i = 0; i < 10; i++) { - for (int j = 0; j < 10; j++) { - for (int k = 0; k < 20; k += 2) { - d3d[i][j][k / 2] = 100 * i + 10 * j + k / 2; - f3d[i][j][k / 2] = 100 * i + 10 * j + k / 2; - sr3d[i][j][k / 2] = 100 * i + 10 * j + k; - si3d[i][j][k / 2] = 100 * i + 10 * j + k + 1; - sfr3d[i][j][k / 2] = 100 * i + 10 * j + k; - sfi3d[i][j][k / 2] = 100 * i + 10 * j + k + 1; - di3d[i][j][k] = 100 * i + 10 * j + k; - 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); - } - } - } - 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) }; - ansArrayd1r = new double[] { 6, 8, 10, 12, 14 }; - ansArrayd1i = new double[] { 7, 9, 11, 13, 15 }; - ansArrayd2r = new double[] { 6, 10, 14 }; - ansArrayd2i = new double[] { 7, 11, 15 }; - ansArrayf1r = new float[] { 6, 8, 10, 12, 14 }; - ansArrayf1i = new float[] { 7, 9, 11, 13, 15 }; - ansArrayf2r = new float[] { 6, 10, 14 }; - ansArrayf2i = new float[] { 7, 11, 15 }; - ansArraydi1 = new double[] { 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; - ansArrayfi1 = new float[] { 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; - ansArraydi2 = new double[] { 6, 7, 10, 11, 14, 15 }; - ansArrayfi2 = new float[] { 6, 7, 10, 11, 14, 15 }; - msg = ""; - } - - @Test - public void testPolar2Complex() { - TestUtils.assertEquals(Complex.ONE, ComplexUtils.polar2Complex(1, 0), 10e-12); - TestUtils.assertEquals(Complex.ZERO, ComplexUtils.polar2Complex(0, 1), 10e-12); - TestUtils.assertEquals(Complex.ZERO, ComplexUtils.polar2Complex(0, -1), 10e-12); - TestUtils.assertEquals(Complex.I, ComplexUtils.polar2Complex(1, pi / 2), 10e-12); - TestUtils.assertEquals(Complex.I.negate(), ComplexUtils.polar2Complex(1, -pi / 2), 10e-12); - double r = 0; - for (int i = 0; i < 5; i++) { - r += i; - double theta = 0; - for (int j = 0; j < 20; j++) { - theta += pi / 6; - TestUtils.assertEquals(altPolar(r, theta), ComplexUtils.polar2Complex(r, theta), 10e-12); - } - theta = -2 * pi; - for (int j = 0; j < 20; j++) { - theta -= pi / 6; - TestUtils.assertEquals(altPolar(r, theta), ComplexUtils.polar2Complex(r, theta), 10e-12); - } - } - } - - protected Complex altPolar(double r, double theta) { - return Complex.I.multiply(new Complex(theta, 0)).exp().multiply(new Complex(r, 0)); - } - - @Test(expected = IllegalArgumentException.class) - public void testPolar2ComplexIllegalModulus() { - ComplexUtils.polar2Complex(-1, 0); - } - - @Test - public void testPolar2ComplexNaN() { - TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(nan, 1)); - TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(1, nan)); - TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(nan, nan)); - } - - @Test - public void testPolar2ComplexInf() { - TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(1, inf)); - TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(1, negInf)); - TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(inf, inf)); - TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(inf, negInf)); - TestUtils.assertSame(infInf, ComplexUtils.polar2Complex(inf, pi / 4)); - TestUtils.assertSame(infNaN, ComplexUtils.polar2Complex(inf, 0)); - TestUtils.assertSame(infNegInf, ComplexUtils.polar2Complex(inf, -pi / 4)); - TestUtils.assertSame(negInfInf, ComplexUtils.polar2Complex(inf, 3 * pi / 4)); - TestUtils.assertSame(negInfNegInf, ComplexUtils.polar2Complex(inf, 5 * pi / 4)); - } - - @Test - public void testCExtract() { - final double[] real = new double[] { negInf, -123.45, 0, 1, 234.56, pi, inf }; - final Complex[] complex = ComplexUtils.real2Complex(real); - - for (int i = 0; i < real.length; i++) { - Assert.assertEquals(real[i], complex[i].getReal(), 0d); - } - } - - // EXTRACTION METHODS - - @Test - public void testExtractionMethods() { - setArrays(); - // Extract complex from real double array, index 3 - TestUtils.assertSame(new Complex(3), ComplexUtils.extractComplexFromRealArray(d, 3)); - // Extract complex from real float array, index 3 - TestUtils.assertSame(new Complex(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)); - // Extract complex from interleaved float array, index 3 - TestUtils.assertSame(new Complex(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)); - // Extract interleaved float from complex array, index 3 - TestUtils.assertEquals(msg, new double[] { 6, 7 }, ComplexUtils.extractInterleavedFromComplexArray(c, 3), - Math.ulp(1)); - if (!msg.equals("")) { - throw new RuntimeException(msg); - } - } - // REAL <-> COMPLEX - - @Test - public void testRealToComplex() { - setArrays(); - // Real double to complex, range 3-7, increment 1, entered as ints - // Real double to complex, whole array - TestUtils.assertEquals(msg, cr, ComplexUtils.real2Complex(d),Math.ulp(1.0)); - // Real float to complex, whole array - TestUtils.assertEquals(msg, cr, ComplexUtils.real2Complex(f),Math.ulp(1.0)); - // 2d - for (int i = 0; i < 10; i++) { - // Real double to complex, 2d - TestUtils.assertEquals(msg, cr2d[i], ComplexUtils.real2Complex(d2d[i]),Math.ulp(1.0)); - // Real float to complex, 2d - TestUtils.assertEquals(msg, cr2d[i], ComplexUtils.real2Complex(f2d[i]),Math.ulp(1.0)); - } - // 3d - for (int i = 0; i < 10; i++) { - for (int j = 0; j < 10; j++) { - // Real double to complex, 3d - TestUtils.assertEquals(msg, cr3d[i][j], ComplexUtils.real2Complex(d3d[i][j]),Math.ulp(1.0)); - // Real float to complex, 3d - TestUtils.assertEquals(msg, cr3d[i][j], ComplexUtils.real2Complex(f3d[i][j]),Math.ulp(1.0)); - } - } - if (!msg.equals("")) { - throw new RuntimeException(msg); - } - } - - @Test - public void testComplexToReal() { - setArrays(); - // Real complex to double, whole array - TestUtils.assertEquals(msg, sr, ComplexUtils.complex2Real(c),Math.ulp(1.0)); - // Real complex to float, whole array - TestUtils.assertEquals(msg, sfr, ComplexUtils.complex2RealFloat(c),Math.ulp(1.0f)); - // 2d - for (int i = 0; i < 10; i++) { - // Real complex to double, 2d - TestUtils.assertEquals(msg, sr2d[i], ComplexUtils.complex2Real(c2d[i]),Math.ulp(1.0)); - // Real complex to float, 2d - TestUtils.assertEquals(msg, sfr2d[i], ComplexUtils.complex2RealFloat(c2d[i]),Math.ulp(1.0f)); - } - // 3d - for (int i = 0; i < 10; i++) { - for (int j = 0; j < 10; j++) { - // Real complex to double, 3d - TestUtils.assertEquals(msg, sr3d[i][j], ComplexUtils.complex2Real(c3d[i][j]),Math.ulp(1.0)); - // Real complex to float, 3d - TestUtils.assertEquals(msg, sfr3d[i][j], ComplexUtils.complex2RealFloat(c3d[i][j]),Math.ulp(1.0f)); - } - } - if (!msg.equals("")) { - throw new RuntimeException(msg); - } - } - - // IMAGINARY <-> COMPLEX - - @Test - public void testImaginaryToComplex() { - setArrays(); - // Imaginary double to complex, whole array - TestUtils.assertEquals(msg, ci, ComplexUtils.imaginary2Complex(d),Math.ulp(1.0)); - // Imaginary float to complex, whole array - TestUtils.assertEquals(msg, ci, ComplexUtils.imaginary2Complex(f),Math.ulp(1.0)); - // 2d - for (int i = 0; i < 10; i++) { - // Imaginary double to complex, 2d - TestUtils.assertEquals(msg, ci2d[i], ComplexUtils.imaginary2Complex(d2d[i]),Math.ulp(1.0)); - // Imaginary float to complex, 2d - TestUtils.assertEquals(msg, ci2d[i], ComplexUtils.imaginary2Complex(f2d[i]),Math.ulp(1.0)); - } - // 3d - for (int i = 0; i < 10; i++) { - for (int j = 0; j < 10; j++) { - // Imaginary double to complex, 3d - TestUtils.assertEquals(msg, ci3d[i][j], ComplexUtils.imaginary2Complex(d3d[i][j]),Math.ulp(1.0)); - // Imaginary float to complex, 3d - TestUtils.assertEquals(msg, ci3d[i][j], ComplexUtils.imaginary2Complex(f3d[i][j]),Math.ulp(1.0)); - } - } - if (!msg.equals("")) { - throw new RuntimeException(msg); - } - } - - @Test - public void testComplexToImaginary() { - setArrays(); - // Imaginary complex to double, whole array - TestUtils.assertEquals(msg, si, ComplexUtils.complex2Imaginary(c),Math.ulp(1.0)); - // Imaginary complex to float, whole array - TestUtils.assertEquals(msg, sfi, ComplexUtils.complex2ImaginaryFloat(c),Math.ulp(1.0f)); - // 2d - for (int i = 0; i < 10; i++) { - // Imaginary complex to double, 2d - TestUtils.assertEquals(msg, si2d[i], ComplexUtils.complex2Imaginary(c2d[i]),Math.ulp(1.0)); - // Imaginary complex to float, 2d - TestUtils.assertEquals(msg, sfi2d[i], ComplexUtils.complex2ImaginaryFloat(c2d[i]),Math.ulp(1.0f)); - } - // 3d - for (int i = 0; i < 10; i++) { - for (int j = 0; j < 10; j++) { - // Imaginary complex to double, 3d - TestUtils.assertEquals(msg, si3d[i][j], ComplexUtils.complex2Imaginary(c3d[i][j]),Math.ulp(1.0)); - // Imaginary complex to float, 3d - TestUtils.assertEquals(msg, sfi3d[i][j], ComplexUtils.complex2ImaginaryFloat(c3d[i][j]),Math.ulp(1.0f)); - } - } - if (!msg.equals("")) { - throw new RuntimeException(msg); - } - } - - // INTERLEAVED <-> COMPLEX - - @Test - public void testInterleavedToComplex() { - setArrays(); - // Interleaved double to complex, whole array - TestUtils.assertEquals(msg, c, ComplexUtils.interleaved2Complex(di),Math.ulp(1.0)); - // Interleaved float to complex, whole array - TestUtils.assertEquals(msg, c, ComplexUtils.interleaved2Complex(fi),Math.ulp(1.0)); - // 2d - for (int i = 0; i < 10; i++) { - // Interleaved double to complex, 2d - TestUtils.assertEquals(msg, c2d[i], ComplexUtils.interleaved2Complex(di2d[i]),Math.ulp(1.0)); - // Interleaved float to complex, 2d - TestUtils.assertEquals(msg, c2d[i], ComplexUtils.interleaved2Complex(fi2d[i]),Math.ulp(1.0)); - } - // 3d - for (int i = 0; i < 10; i++) { - for (int j = 0; j < 10; j++) { - // Interleaved double to complex, 3d - TestUtils.assertEquals(msg, c3d[i][j], ComplexUtils.interleaved2Complex(di3d[i][j]),Math.ulp(1.0)); - // Interleaved float to complex, 3d - TestUtils.assertEquals(msg, c3d[i][j], ComplexUtils.interleaved2Complex(fi3d[i][j]),Math.ulp(1.0)); - } - } - if (!msg.equals("")) { - throw new RuntimeException(msg); - } - } - - @Test - public void testComplexToInterleaved() { - setArrays(); - TestUtils.assertEquals(msg, di, ComplexUtils.complex2Interleaved(c),Math.ulp(1.0)); - // Interleaved complex to float, whole array - TestUtils.assertEquals(msg, fi, ComplexUtils.complex2InterleavedFloat(c),Math.ulp(1.0f)); - // 2d - for (int i = 0; i < 10; i++) { - // Interleaved complex to double, 2d - TestUtils.assertEquals(msg, di2d[i], ComplexUtils.complex2Interleaved(c2d[i]),Math.ulp(1.0)); - // Interleaved complex to float, 2d - TestUtils.assertEquals(msg, fi2d[i], ComplexUtils.complex2InterleavedFloat(c2d[i]),Math.ulp(1.0f)); - } - // 3d - for (int i = 0; i < 10; i++) { - for (int j = 0; j < 10; j++) { - // Interleaved complex to double, 3d - TestUtils.assertEquals(msg, di3d[i][j], ComplexUtils.complex2Interleaved(c3d[i][j]),Math.ulp(1.0)); - // Interleaved complex to float, 3d - TestUtils.assertEquals(msg, fi3d[i][j], ComplexUtils.complex2InterleavedFloat(c3d[i][j]),Math.ulp(1.0f)); - } - } - if (!msg.equals("")) { - throw new RuntimeException(msg); - } - } - - // SPLIT TO COMPLEX - @Test - public void testSplit2Complex() { - setArrays(); - // Split double to complex, whole array - TestUtils.assertEquals(msg, c, ComplexUtils.split2Complex(sr, si),Math.ulp(1.0)); - - // 2d - for (int i = 0; i < 10; i++) { - // Split double to complex, 2d - TestUtils.assertEquals(msg, c2d[i], ComplexUtils.split2Complex(sr2d[i], si2d[i]),Math.ulp(1.0)); - } - // 3d - for (int i = 0; i < 10; i++) { - for (int j = 0; j < 10; j++) { - // Split double to complex, 3d - TestUtils.assertEquals(msg, c3d[i][j], ComplexUtils.split2Complex(sr3d[i][j], si3d[i][j]),Math.ulp(1.0)); - } - } - if (!msg.equals("")) { - throw new RuntimeException(msg); - } - } - - // INITIALIZATION METHODS - - @Test - public void testInitialize() { - Complex[] c = new Complex[10]; - ComplexUtils.initialize(c); - for (Complex cc : c) { - TestUtils.assertEquals(new Complex(0, 0), cc, Math.ulp(0)); - } - } -}
