    1   /*
    2    * Copyright 2003-2005 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16
    17  package org.apache.commons.math.complex;
    18
    19  import org.apache.commons.math.TestUtils;
    20
    21  import junit.framework.TestCase;
    22
    23  /***
    24   * @version $Revision: 349309 $ $Date: 2005-11-27 13:55:08 -0700 (Sun, 27 Nov 2005) $
    25   */
    26  public class ComplexUtilsTest extends TestCase {
    27
    28      private double inf = Double.POSITIVE_INFINITY;
    29      private double negInf = Double.NEGATIVE_INFINITY;
    30      private double nan = Double.NaN;
    31      private double pi = Math.PI;
    32
    33      private Complex oneInf = new Complex(1, inf);
    34      private Complex oneNegInf = new Complex(1, negInf);
    35      private Complex infOne = new Complex(inf, 1);
    36      private Complex negInfOne = new Complex(negInf, 1);
    37      private Complex negInfInf = new Complex(negInf, inf);
    38      private Complex infNegInf = new Complex(inf, negInf);
    39      private Complex infInf = new Complex(inf, inf);
    40      private Complex negInfNegInf = new Complex(negInf, negInf);
    41      private Complex oneNaN = new Complex(1, nan);
    42      private Complex infNaN = new Complex(inf, nan);
    43      private Complex negInfNaN = new Complex(negInf, nan);
    44      private Complex nanInf = new Complex(nan, inf);
    45      private Complex nanNegInf = new Complex(nan, negInf);
    46      private Complex zeroNaN = new Complex(0, nan);
    47      private Complex nanZero = new Complex(nan, 0);
    48      private Complex infZero = new Complex(inf, 0);
    49      private Complex zeroInf = new Complex(0, inf);
    50      private Complex zeroNegInf = new Complex(0, negInf);
    51      private Complex negInfZero = new Complex(negInf, 0);
    52
    53      private ComplexFormat fmt = new ComplexFormat();
    54
    55      public void testAcos() {
    56          Complex z = new Complex(3, 4);
    57          Complex expected = new Complex(0.936812, -2.30551);
    58          TestUtils.assertEquals(expected, ComplexUtils.acos(z), 1.0e-5);
    59          TestUtils.assertEquals(new Complex(Math.acos(0), 0),
    60                  ComplexUtils.acos(Complex.ZERO), 1.0e-12);
    61      }
    62
    63      public void testAcosInf() {
    64          TestUtils.assertSame(Complex.NaN, ComplexUtils.acos(oneInf));
    65          TestUtils.assertSame(Complex.NaN, ComplexUtils.acos(oneNegInf));
    66          TestUtils.assertSame(Complex.NaN, ComplexUtils.acos(infOne));
    67          TestUtils.assertSame(Complex.NaN, ComplexUtils.acos(negInfOne));
    68          TestUtils.assertSame(Complex.NaN, ComplexUtils.acos(infInf));
    69          TestUtils.assertSame(Complex.NaN, ComplexUtils.acos(infNegInf));
    70          TestUtils.assertSame(Complex.NaN, ComplexUtils.acos(negInfInf));
    71          TestUtils.assertSame(Complex.NaN, ComplexUtils.acos(negInfNegInf));
    72      }
    73
    74      public void testAcosNaN() {
    75          assertTrue(ComplexUtils.acos(Complex.NaN).isNaN());
    76      }
    77
    78      public void testAcosNull() {
    79          try {
    80              Complex z = ComplexUtils.acos(null);
    81              fail("Expecting NullPointerException");
    82          } catch (NullPointerException ex) {
    83              // expected
    84          }
    85      }
    86
    87      public void testAsin() {
    88          Complex z = new Complex(3, 4);
    89          Complex expected = new Complex(0.633984, 2.30551);
    90          TestUtils.assertEquals(expected, ComplexUtils.asin(z), 1.0e-5);
    91      }
    92
    93      public void testAsinNaN() {
    94          assertTrue(ComplexUtils.asin(Complex.NaN).isNaN());
    95      }
    96
    97      public void testAsinInf() {
    98          TestUtils.assertSame(Complex.NaN, ComplexUtils.asin(oneInf));
    99          TestUtils.assertSame(Complex.NaN, ComplexUtils.asin(oneNegInf));
    100         TestUtils.assertSame(Complex.NaN, ComplexUtils.asin(infOne));
    101         TestUtils.assertSame(Complex.NaN, ComplexUtils.asin(negInfOne));
    102         TestUtils.assertSame(Complex.NaN, ComplexUtils.asin(infInf));
    103         TestUtils.assertSame(Complex.NaN, ComplexUtils.asin(infNegInf));
    104         TestUtils.assertSame(Complex.NaN, ComplexUtils.asin(negInfInf));
    105         TestUtils.assertSame(Complex.NaN, ComplexUtils.asin(negInfNegInf));
    106     }
    107
    108     public void testAsinNull() {
    109         try {
    110             Complex z = ComplexUtils.asin(null);
    111             fail("Expecting NullPointerException");
    112         } catch (NullPointerException ex) {
    113             // expected
    114         }
    115     }
    116
    117     public void testAtan() {
    118         Complex z = new Complex(3, 4);
    119         Complex expected = new Complex(1.44831, 0.158997);
    120         TestUtils.assertEquals(expected, ComplexUtils.atan(z), 1.0e-5);
    121     }
    122
    123     public void testAtanInf() {
    124         TestUtils.assertSame(Complex.NaN, ComplexUtils.atan(oneInf));
    125         TestUtils.assertSame(Complex.NaN, ComplexUtils.atan(oneNegInf));
    126         TestUtils.assertSame(Complex.NaN, ComplexUtils.atan(infOne));
    127         TestUtils.assertSame(Complex.NaN, ComplexUtils.atan(negInfOne));
    128         TestUtils.assertSame(Complex.NaN, ComplexUtils.atan(infInf));
    129         TestUtils.assertSame(Complex.NaN, ComplexUtils.atan(infNegInf));
    130         TestUtils.assertSame(Complex.NaN, ComplexUtils.atan(negInfInf));
    131         TestUtils.assertSame(Complex.NaN, ComplexUtils.atan(negInfNegInf));
    132     }
    133
    134     public void testAtanNaN() {
    135         assertTrue(ComplexUtils.atan(Complex.NaN).isNaN());
    136         assertTrue(ComplexUtils.atan(Complex.I).isNaN());
    137     }
    138
    139     public void testAtanNull() {
    140         try {
    141             Complex z = ComplexUtils.atan(null);
    142             fail("Expecting NullPointerException");
    143         } catch (NullPointerException ex) {
    144             // expected
    145         }
    146     }
    147
    148     public void testCos() {
    149         Complex z = new Complex(3, 4);
    150         Complex expected = new Complex(-27.03495, -3.851153);
    151         TestUtils.assertEquals(expected, ComplexUtils.cos(z), 1.0e-5);
    152     }
    153
    154     public void testCosNaN() {
    155         assertTrue(ComplexUtils.cos(Complex.NaN).isNaN());
    156     }
    157
    158     public void testCosInf() {
    159         TestUtils.assertSame(infNegInf, ComplexUtils.cos(oneInf));
    160         TestUtils.assertSame(infInf, ComplexUtils.cos(oneNegInf));
    161         TestUtils.assertSame(Complex.NaN, ComplexUtils.cos(infOne));
    162         TestUtils.assertSame(Complex.NaN, ComplexUtils.cos(negInfOne));
    163         TestUtils.assertSame(Complex.NaN, ComplexUtils.cos(infInf));
    164         TestUtils.assertSame(Complex.NaN, ComplexUtils.cos(infNegInf));
    165         TestUtils.assertSame(Complex.NaN, ComplexUtils.cos(negInfInf));
    166         TestUtils.assertSame(Complex.NaN, ComplexUtils.cos(negInfNegInf));
    167     }
    168
    169     public void testCosNull() {
    170         try {
    171             Complex z = ComplexUtils.cos(null);
    172             fail("Expecting NullPointerException");
    173         } catch (NullPointerException ex) {
    174             // expected
    175         }
    176     }
    177
    178     public void testCosh() {
    179         Complex z = new Complex(3, 4);
    180         Complex expected = new Complex(-6.58066, -7.58155);
    181         TestUtils.assertEquals(expected, ComplexUtils.cosh(z), 1.0e-5);
    182     }
    183
    184     public void testCoshNaN() {
    185         assertTrue(ComplexUtils.cosh(Complex.NaN).isNaN());
    186     }
    187
    188     public void testCoshInf() {
    189         TestUtils.assertSame(Complex.NaN, ComplexUtils.cosh(oneInf));
    190         TestUtils.assertSame(Complex.NaN, ComplexUtils.cosh(oneNegInf));
    191         TestUtils.assertSame(infInf, ComplexUtils.cosh(infOne));
    192         TestUtils.assertSame(infNegInf, ComplexUtils.cosh(negInfOne));
    193         TestUtils.assertSame(Complex.NaN, ComplexUtils.cosh(infInf));
    194         TestUtils.assertSame(Complex.NaN, ComplexUtils.cosh(infNegInf));
    195         TestUtils.assertSame(Complex.NaN, ComplexUtils.cosh(negInfInf));
    196         TestUtils.assertSame(Complex.NaN, ComplexUtils.cosh(negInfNegInf));
    197     }
    198
    199     public void testCoshNull() {
    200         try {
    201             Complex z = ComplexUtils.cosh(null);
    202             fail("Expecting NullPointerException");
    203         } catch (NullPointerException ex) {
    204             // expected
    205         }
    206     }
    207
    208     public void testExp() {
    209         Complex z = new Complex(3, 4);
    210         Complex expected = new Complex(-13.12878, -15.20078);
    211         TestUtils.assertEquals(expected, ComplexUtils.exp(z), 1.0e-5);
    212         TestUtils.assertEquals(Complex.ONE,
    213                 ComplexUtils.exp(Complex.ZERO), 10e-12);
    214         Complex iPi = Complex.I.multiply(new Complex(pi,0));
    215         TestUtils.assertEquals(Complex.ONE.negate(),
    216                 ComplexUtils.exp(iPi), 10e-12);
    217     }
    218
    219     public void testExpNaN() {
    220         assertTrue(ComplexUtils.exp(Complex.NaN).isNaN());
    221     }
    222
    223     public void testExpInf() {
    224         TestUtils.assertSame(Complex.NaN, ComplexUtils.exp(oneInf));
    225         TestUtils.assertSame(Complex.NaN, ComplexUtils.exp(oneNegInf));
    226         TestUtils.assertSame(infInf, ComplexUtils.exp(infOne));
    227         TestUtils.assertSame(Complex.ZERO, ComplexUtils.exp(negInfOne));
    228         TestUtils.assertSame(Complex.NaN, ComplexUtils.exp(infInf));
    229         TestUtils.assertSame(Complex.NaN, ComplexUtils.exp(infNegInf));
    230         TestUtils.assertSame(Complex.NaN, ComplexUtils.exp(negInfInf));
    231         TestUtils.assertSame(Complex.NaN, ComplexUtils.exp(negInfNegInf));
    232     }
    233
    234     public void testExpNull() {
    235         try {
    236             Complex z = ComplexUtils.exp(null);
    237             fail("Expecting NullPointerException");
    238         } catch (NullPointerException ex) {
    239             // expected
    240         }
    241     }
    242
    243     public void testLog() {
    244         Complex z = new Complex(3, 4);
    245         Complex expected = new Complex(1.60944, 0.927295);
    246         TestUtils.assertEquals(expected, ComplexUtils.log(z), 1.0e-5);
    247     }
    248
    249     public void testLogNaN() {
    250         assertTrue(ComplexUtils.log(Complex.NaN).isNaN());
    251     }
    252
    253     public void testLogInf() {
    254         TestUtils.assertEquals(new Complex(inf, pi / 2),
    255                 ComplexUtils.log(oneInf), 10e-12);
    256         TestUtils.assertEquals(new Complex(inf, -pi / 2),
    257                 ComplexUtils.log(oneNegInf), 10e-12);
    258         TestUtils.assertEquals(infZero, ComplexUtils.log(infOne), 10e-12);
    259         TestUtils.assertEquals(new Complex(inf, pi),
    260                 ComplexUtils.log(negInfOne), 10e-12);
    261         TestUtils.assertEquals(new Complex(inf, pi / 4),
    262                 ComplexUtils.log(infInf), 10e-12);
    263         TestUtils.assertEquals(new Complex(inf, -pi / 4),
    264                 ComplexUtils.log(infNegInf), 10e-12);
    265         TestUtils.assertEquals(new Complex(inf, 3d * pi / 4),
    266                 ComplexUtils.log(negInfInf), 10e-12);
    267         TestUtils.assertEquals(new Complex(inf, - 3d * pi / 4),
    268                 ComplexUtils.log(negInfNegInf), 10e-12);
    269     }
    270
    271     public void testLogZero() {
    272         TestUtils.assertSame(negInfZero, ComplexUtils.log(Complex.ZERO));
    273     }
    274
    275     public void testlogNull() {
    276         try {
    277             Complex z = ComplexUtils.log(null);
    278             fail("Expecting NullPointerException");
    279         } catch (NullPointerException ex) {
    280             // expected
    281         }
    282     }
    283
    284     public void testPolar2Complex() {
    285         TestUtils.assertEquals(Complex.ONE,
    286                 ComplexUtils.polar2Complex(1, 0), 10e-12);
    287         TestUtils.assertEquals(Complex.ZERO,
    288                 ComplexUtils.polar2Complex(0, 1), 10e-12);
    289         TestUtils.assertEquals(Complex.ZERO,
    290                 ComplexUtils.polar2Complex(0, -1), 10e-12);
    291         TestUtils.assertEquals(Complex.I,
    292                 ComplexUtils.polar2Complex(1, pi/2), 10e-12);
    293         TestUtils.assertEquals(Complex.I.negate(),
    294                 ComplexUtils.polar2Complex(1, -pi/2), 10e-12);
    295         double r = 0;
    296         for (int i = 0; i < 5; i++) {
    297           r += i;
    298           double theta = 0;
    299           for (int j =0; j < 20; j++) {
    300               theta += pi / 6;
    301               TestUtils.assertEquals(altPolar(r, theta),
    302                       ComplexUtils.polar2Complex(r, theta), 10e-12);
    303           }
    304           theta = -2 * pi;
    305           for (int j =0; j < 20; j++) {
    306               theta -= pi / 6;
    307               TestUtils.assertEquals(altPolar(r, theta),
    308                       ComplexUtils.polar2Complex(r, theta), 10e-12);
    309           }
    310         }
    311     }
    312
    313     protected Complex altPolar(double r, double theta) {
    314         return ComplexUtils.exp(Complex.I.multiply
    315                 (new Complex(theta, 0))).multiply(new Complex(r, 0));
    316     }
    317
    318     public void testPolar2ComplexIllegalModulus() {
    319         try {
    320             Complex z = ComplexUtils.polar2Complex(-1, 0);
    321             fail("Expecting IllegalArgumentException");
    322         } catch (IllegalArgumentException ex) {
    323             // expected
    324         }
    325     }
    326
    327     public void testPolar2ComplexNaN() {
    328         TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(nan, 1));
    329         TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(1, nan));
    330         TestUtils.assertSame(Complex.NaN,
    331                 ComplexUtils.polar2Complex(nan, nan));
    332     }
    333
    334     public void testPolar2ComplexInf() {
    335         TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(1, inf));
    336         TestUtils.assertSame(Complex.NaN,
    337                 ComplexUtils.polar2Complex(1, negInf));
    338         TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(inf, inf));
    339         TestUtils.assertSame(Complex.NaN,
    340                 ComplexUtils.polar2Complex(inf, negInf));
    341         TestUtils.assertSame(infInf, ComplexUtils.polar2Complex(inf, pi/4));
    342         TestUtils.assertSame(infNaN, ComplexUtils.polar2Complex(inf, 0));
    343         TestUtils.assertSame(infNegInf, ComplexUtils.polar2Complex(inf, -pi/4));
    344         TestUtils.assertSame(negInfInf, ComplexUtils.polar2Complex(inf, 3*pi/4));
    345         TestUtils.assertSame(negInfNegInf, ComplexUtils.polar2Complex(inf, 5*pi/4));
    346     }
    347
    348     public void testPow() {
    349         Complex x = new Complex(3, 4);
    350         Complex y = new Complex(5, 6);
    351         Complex expected = new Complex(-1.860893, 11.83677);
    352         TestUtils.assertEquals(expected, ComplexUtils.pow(x, y), 1.0e-5);
    353     }
    354
    355     public void testPowNaNBase() {
    356         Complex x = new Complex(3, 4);
    357         assertTrue(ComplexUtils.pow(Complex.NaN, x).isNaN());
    358     }
    359
    360     public void testPowNaNExponent() {
    361         Complex x = new Complex(3, 4);
    362         assertTrue(ComplexUtils.pow(x, Complex.NaN).isNaN());
    363     }
    364
    365    public void testPowInf() {
    366        TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(Complex.ONE, oneInf));
    367        TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(Complex.ONE, oneNegInf));
    368        TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(Complex.ONE, infOne));
    369        TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(Complex.ONE, infInf));
    370        TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(Complex.ONE, infNegInf));
    371        TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(Complex.ONE, negInfInf));
    372        TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(Complex.ONE, negInfNegInf));
    373        TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(infOne, Complex.ONE));
    374        TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(negInfOne, Complex.ONE));
    375        TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(infInf, Complex.ONE));
    376        TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(infNegInf, Complex.ONE));
    377        TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(negInfInf, Complex.ONE));
    378        TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(negInfNegInf, Complex.ONE));
    379        TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(negInfNegInf, infNegInf));
    380        TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(negInfNegInf, negInfNegInf));
    381        TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(negInfNegInf, infInf));
    382        TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(infInf, infNegInf));
    383        TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(infInf, negInfNegInf));
    384        TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(infInf, infInf));
    385        TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(infNegInf, infNegInf));
    386        TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(infNegInf, negInfNegInf));
    387        TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(infNegInf, infInf));
    388    }
    389
    390    public void testPowZero() {
    391        TestUtils.assertSame(Complex.NaN,
    392                ComplexUtils.pow(Complex.ZERO, Complex.ONE));
    393        TestUtils.assertSame(Complex.NaN,
    394                ComplexUtils.pow(Complex.ZERO, Complex.ZERO));
    395        TestUtils.assertSame(Complex.NaN,
    396                ComplexUtils.pow(Complex.ZERO, Complex.I));
    397        TestUtils.assertEquals(Complex.ONE,
    398                ComplexUtils.pow(Complex.ONE, Complex.ZERO), 10e-12);
    399        TestUtils.assertEquals(Complex.ONE,
    400                ComplexUtils.pow(Complex.I, Complex.ZERO), 10e-12);
    401        TestUtils.assertEquals(Complex.ONE,
    402                ComplexUtils.pow(new Complex(-1, 3), Complex.ZERO), 10e-12);
    403    }
    404
    405     public void testpowNull() {
    406         try {
    407             Complex z = ComplexUtils.pow(null, Complex.ONE);
    408             fail("Expecting NullPointerException");
    409         } catch (NullPointerException ex) {
    410             // expected
    411         }
    412         try {
    413             Complex z = ComplexUtils.pow(Complex.ONE, null);
    414             fail("Expecting NullPointerException");
    415         } catch (NullPointerException ex) {
    416             // expected
    417         }
    418     }
    419
    420     public void testSin() {
    421         Complex z = new Complex(3, 4);
    422         Complex expected = new Complex(3.853738, -27.01681);
    423         TestUtils.assertEquals(expected, ComplexUtils.sin(z), 1.0e-5);
    424     }
    425
    426     public void testSinInf() {
    427         TestUtils.assertSame(infInf, ComplexUtils.sin(oneInf));
    428         TestUtils.assertSame(infNegInf, ComplexUtils.sin(oneNegInf));
    429         TestUtils.assertSame(Complex.NaN, ComplexUtils.sin(infOne));
    430         TestUtils.assertSame(Complex.NaN, ComplexUtils.sin(negInfOne));
    431         TestUtils.assertSame(Complex.NaN, ComplexUtils.sin(infInf));
    432         TestUtils.assertSame(Complex.NaN, ComplexUtils.sin(infNegInf));
    433         TestUtils.assertSame(Complex.NaN, ComplexUtils.sin(negInfInf));
    434         TestUtils.assertSame(Complex.NaN, ComplexUtils.sin(negInfNegInf));
    435     }
    436
    437     public void testSinNaN() {
    438         assertTrue(ComplexUtils.sin(Complex.NaN).isNaN());
    439     }
    440
    441     public void testSinNull() {
    442         try {
    443             Complex z = ComplexUtils.sin(null);
    444             fail("Expecting NullPointerException");
    445         } catch (NullPointerException ex) {
    446             // expected
    447         }
    448     }
    449
    450     public void testSinh() {
    451         Complex z = new Complex(3, 4);
    452         Complex expected = new Complex(-6.54812, -7.61923);
    453         TestUtils.assertEquals(expected, ComplexUtils.sinh(z), 1.0e-5);
    454     }
    455
    456     public void testSinhNaN() {
    457         assertTrue(ComplexUtils.sinh(Complex.NaN).isNaN());
    458     }
    459
    460     public void testSinhInf() {
    461         TestUtils.assertSame(Complex.NaN, ComplexUtils.sinh(oneInf));
    462         TestUtils.assertSame(Complex.NaN, ComplexUtils.sinh(oneNegInf));
    463         TestUtils.assertSame(infInf, ComplexUtils.sinh(infOne));
    464         TestUtils.assertSame(negInfInf, ComplexUtils.sinh(negInfOne));
    465         TestUtils.assertSame(Complex.NaN, ComplexUtils.sinh(infInf));
    466         TestUtils.assertSame(Complex.NaN, ComplexUtils.sinh(infNegInf));
    467         TestUtils.assertSame(Complex.NaN, ComplexUtils.sinh(negInfInf));
    468         TestUtils.assertSame(Complex.NaN, ComplexUtils.sinh(negInfNegInf));
    469     }
    470
    471     public void testsinhNull() {
    472         try {
    473             Complex z = ComplexUtils.sinh(null);
    474             fail("Expecting NullPointerException");
    475         } catch (NullPointerException ex) {
    476             // expected
    477         }
    478     }
    479
    480     public void testSqrtRealPositive() {
    481         Complex z = new Complex(3, 4);
    482         Complex expected = new Complex(2, 1);
    483         TestUtils.assertEquals(expected, ComplexUtils.sqrt(z), 1.0e-5);
    484     }
    485
    486     public void testSqrtRealZero() {
    487         Complex z = new Complex(0.0, 4);
    488         Complex expected = new Complex(1.41421, 1.41421);
    489         TestUtils.assertEquals(expected, ComplexUtils.sqrt(z), 1.0e-5);
    490     }
    491
    492     public void testSqrtRealNegative() {
    493         Complex z = new Complex(-3.0, 4);
    494         Complex expected = new Complex(1, 2);
    495         TestUtils.assertEquals(expected, ComplexUtils.sqrt(z), 1.0e-5);
    496     }
    497
    498     public void testSqrtImaginaryZero() {
    499         Complex z = new Complex(-3.0, 0.0);
    500         Complex expected = new Complex(0.0, 1.73205);
    501         TestUtils.assertEquals(expected, ComplexUtils.sqrt(z), 1.0e-5);
    502     }
    503
    504     public void testSqrtImaginaryNegative() {
    505         Complex z = new Complex(-3.0, -4.0);
    506         Complex expected = new Complex(1.0, -2.0);
    507         TestUtils.assertEquals(expected, ComplexUtils.sqrt(z), 1.0e-5);
    508     }
    509
    510     public void testSqrtPolar() {
    511         double r = 1;
    512         for (int i = 0; i < 5; i++) {
    513             r += i;
    514             double theta = 0;
    515             for (int j =0; j < 11; j++) {
    516                 theta += pi /12;
    517                 Complex z = ComplexUtils.polar2Complex(r, theta);
    518                 Complex sqrtz = ComplexUtils.polar2Complex(Math.sqrt(r), theta / 2);
    519                 TestUtils.assertEquals(sqrtz, ComplexUtils.sqrt(z), 10e-12);
    520             }
    521         }
    522     }
    523
    524     public void testSqrtNaN() {
    525         assertTrue(ComplexUtils.sqrt(Complex.NaN).isNaN());
    526     }
    527
    528     public void testSqrtInf() {
    529         TestUtils.assertSame(infNaN, ComplexUtils.sqrt(oneInf));
    530         TestUtils.assertSame(infNaN, ComplexUtils.sqrt(oneNegInf));
    531         TestUtils.assertSame(infZero, ComplexUtils.sqrt(infOne));
    532         TestUtils.assertSame(zeroInf, ComplexUtils.sqrt(negInfOne));
    533         TestUtils.assertSame(infNaN, ComplexUtils.sqrt(infInf));
    534         TestUtils.assertSame(infNaN, ComplexUtils.sqrt(infNegInf));
    535         TestUtils.assertSame(nanInf, ComplexUtils.sqrt(negInfInf));
    536         TestUtils.assertSame(nanNegInf, ComplexUtils.sqrt(negInfNegInf));
    537     }
    538
    539     public void testSqrtNull() {
    540         try {
    541             Complex z = ComplexUtils.sqrt(null);
    542             fail("Expecting NullPointerException");
    543         } catch (NullPointerException ex) {
    544             // expected
    545         }
    546     }
    547
    548     public void testSqrt1z() {
    549         Complex z = new Complex(3, 4);
    550         Complex expected = new Complex(4.08033, -2.94094);
    551         TestUtils.assertEquals(expected, ComplexUtils.sqrt1z(z), 1.0e-5);
    552     }
    553
    554     public void testSqrt1zNaN() {
    555         assertTrue(ComplexUtils.sqrt1z(Complex.NaN).isNaN());
    556     }
    557
    558     public void testSqrt1zNull() {
    559         try {
    560             Complex z = ComplexUtils.sqrt1z(null);
    561             fail("Expecting NullPointerException");
    562         } catch (NullPointerException ex) {
    563             // expected
    564         }
    565     }
    566
    567     public void testTan() {
    568         Complex z = new Complex(3, 4);
    569         Complex expected = new Complex(-0.000187346, 0.999356);
    570         TestUtils.assertEquals(expected, ComplexUtils.tan(z), 1.0e-5);
    571     }
    572
    573     public void testTanNaN() {
    574         assertTrue(ComplexUtils.tan(Complex.NaN).isNaN());
    575     }
    576
    577     public void testTanInf() {
    578         TestUtils.assertSame(zeroNaN, ComplexUtils.tan(oneInf));
    579         TestUtils.assertSame(zeroNaN, ComplexUtils.tan(oneNegInf));
    580         TestUtils.assertSame(Complex.NaN, ComplexUtils.tan(infOne));
    581         TestUtils.assertSame(Complex.NaN, ComplexUtils.tan(negInfOne));
    582         TestUtils.assertSame(Complex.NaN, ComplexUtils.tan(infInf));
    583         TestUtils.assertSame(Complex.NaN, ComplexUtils.tan(infNegInf));
    584         TestUtils.assertSame(Complex.NaN, ComplexUtils.tan(negInfInf));
    585         TestUtils.assertSame(Complex.NaN, ComplexUtils.tan(negInfNegInf));
    586     }
    587
    588    public void testTanCritical() {
    589         TestUtils.assertSame(infNaN, ComplexUtils.tan(new Complex(pi/2, 0)));
    590         TestUtils.assertSame(negInfNaN, ComplexUtils.tan(new Complex(-pi/2, 0)));
    591     }
    592
    593     public void testTanNull() {
    594         try {
    595             Complex z = ComplexUtils.tan(null);
    596             fail("Expecting NullPointerException");
    597         } catch (NullPointerException ex) {
    598             // expected
    599         }
    600     }
    601
    602     public void testTanh() {
    603         Complex z = new Complex(3, 4);
    604         Complex expected = new Complex(1.00071, 0.00490826);
    605         TestUtils.assertEquals(expected, ComplexUtils.tanh(z), 1.0e-5);
    606     }
    607
    608     public void testTanhNaN() {
    609         assertTrue(ComplexUtils.tanh(Complex.NaN).isNaN());
    610     }
    611
    612     public void testTanhInf() {
    613         TestUtils.assertSame(Complex.NaN, ComplexUtils.tanh(oneInf));
    614         TestUtils.assertSame(Complex.NaN, ComplexUtils.tanh(oneNegInf));
    615         TestUtils.assertSame(nanZero, ComplexUtils.tanh(infOne));
    616         TestUtils.assertSame(nanZero, ComplexUtils.tanh(negInfOne));
    617         TestUtils.assertSame(Complex.NaN, ComplexUtils.tanh(infInf));
    618         TestUtils.assertSame(Complex.NaN, ComplexUtils.tanh(infNegInf));
    619         TestUtils.assertSame(Complex.NaN, ComplexUtils.tanh(negInfInf));
    620         TestUtils.assertSame(Complex.NaN, ComplexUtils.tanh(negInfNegInf));
    621     }
    622
    623     public void testTanhCritical() {
    624         TestUtils.assertSame(nanInf, ComplexUtils.tanh(new Complex(0, pi/2)));
    625     }
    626
    627     public void testTanhNull() {
    628         try {
    629             Complex z = ComplexUtils.tanh(null);
    630             fail("Expecting NullPointerException");
    631         } catch (NullPointerException ex) {
    632             // expected
    633         }
    634     }
    635 }