    1   /*
    2    * Copyright 2003-2004 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: 1.1 $ $Date: 2004/07/12 00:27:09 $
    25   */
    26  public class ComplexUtilsTest extends TestCase {
    27
    28      public void testAcos() {
    29          Complex z = new Complex(3, 4);
    30          Complex expected = new Complex(0.936812, -2.30551);
    31          TestUtils.assertEquals(expected, ComplexUtils.acos(z), 1.0e-5);
    32      }
    33
    34      public void testAcosNaN() {
    35          assertTrue(ComplexUtils.acos(Complex.NaN).isNaN());
    36      }
    37
    38      public void testAsin() {
    39          Complex z = new Complex(3, 4);
    40          Complex expected = new Complex(0.633984, 2.30551);
    41          TestUtils.assertEquals(expected, ComplexUtils.asin(z), 1.0e-5);
    42      }
    43
    44      public void testAsinNaN() {
    45          assertTrue(ComplexUtils.asin(Complex.NaN).isNaN());
    46      }
    47
    48      public void testAtan() {
    49          Complex z = new Complex(3, 4);
    50          Complex expected = new Complex(1.44831, 0.158997);
    51          TestUtils.assertEquals(expected, ComplexUtils.atan(z), 1.0e-5);
    52      }
    53
    54      public void testAtanNaN() {
    55          assertTrue(ComplexUtils.atan(Complex.NaN).isNaN());
    56      }
    57
    58      public void testCos() {
    59          Complex z = new Complex(3, 4);
    60          Complex expected = new Complex(-27.03495, -3.851153);
    61          TestUtils.assertEquals(expected, ComplexUtils.cos(z), 1.0e-5);
    62      }
    63
    64      public void testCosh() {
    65          Complex z = new Complex(3, 4);
    66          Complex expected = new Complex(-6.58066, -7.58155);
    67          TestUtils.assertEquals(expected, ComplexUtils.cosh(z), 1.0e-5);
    68      }
    69
    70      public void testCoshNaN() {
    71          assertTrue(ComplexUtils.cosh(Complex.NaN).isNaN());
    72      }
    73
    74      public void testCosNaN() {
    75          assertTrue(ComplexUtils.cos(Complex.NaN).isNaN());
    76      }
    77
    78      public void testExp() {
    79          Complex z = new Complex(3, 4);
    80          Complex expected = new Complex(-13.12878, -15.20078);
    81          TestUtils.assertEquals(expected, ComplexUtils.exp(z), 1.0e-5);
    82      }
    83
    84      public void testExpNaN() {
    85          assertTrue(ComplexUtils.exp(Complex.NaN).isNaN());
    86      }
    87
    88      public void testLog() {
    89          Complex z = new Complex(3, 4);
    90          Complex expected = new Complex(1.60944, 0.927295);
    91          TestUtils.assertEquals(expected, ComplexUtils.log(z), 1.0e-5);
    92      }
    93
    94      public void testLogNaN() {
    95          assertTrue(ComplexUtils.log(Complex.NaN).isNaN());
    96      }
    97
    98      public void testPow() {
    99          Complex x = new Complex(3, 4);
    100         Complex y = new Complex(5, 6);
    101         Complex expected = new Complex(-1.860893, 11.83677);
    102         TestUtils.assertEquals(expected, ComplexUtils.pow(x, y), 1.0e-5);
    103     }
    104
    105     public void testPowNaNBase() {
    106         Complex x = new Complex(3, 4);
    107         assertTrue(ComplexUtils.pow(Complex.NaN, x).isNaN());
    108     }
    109
    110     public void testPowNaNExponent() {
    111         Complex x = new Complex(3, 4);
    112         assertTrue(ComplexUtils.pow(x, Complex.NaN).isNaN());
    113     }
    114
    115     public void testSin() {
    116         Complex z = new Complex(3, 4);
    117         Complex expected = new Complex(3.853738, -27.01681);
    118         TestUtils.assertEquals(expected, ComplexUtils.sin(z), 1.0e-5);
    119     }
    120
    121     public void testSinh() {
    122         Complex z = new Complex(3, 4);
    123         Complex expected = new Complex(-6.54812, -7.61923);
    124         TestUtils.assertEquals(expected, ComplexUtils.sinh(z), 1.0e-5);
    125     }
    126
    127     public void testSinhNaN() {
    128         assertTrue(ComplexUtils.sinh(Complex.NaN).isNaN());
    129     }
    130
    131     public void testSinNaN() {
    132         assertTrue(ComplexUtils.sin(Complex.NaN).isNaN());
    133     }
    134
    135     public void testSqrtRealPositive() {
    136         Complex z = new Complex(3, 4);
    137         Complex expected = new Complex(2, 1);
    138         TestUtils.assertEquals(expected, ComplexUtils.sqrt(z), 1.0e-5);
    139     }
    140
    141     public void testSqrtRealZero() {
    142         Complex z = new Complex(0.0, 4);
    143         Complex expected = new Complex(1.41421, 1.41421);
    144         TestUtils.assertEquals(expected, ComplexUtils.sqrt(z), 1.0e-5);
    145     }
    146
    147     public void testSqrtRealNegative() {
    148         Complex z = new Complex(-3.0, 4);
    149         Complex expected = new Complex(1, 2);
    150         TestUtils.assertEquals(expected, ComplexUtils.sqrt(z), 1.0e-5);
    151     }
    152
    153     public void testSqrtImaginaryZero() {
    154         Complex z = new Complex(-3.0, 0.0);
    155         Complex expected = new Complex(0.0, 1.73205);
    156         TestUtils.assertEquals(expected, ComplexUtils.sqrt(z), 1.0e-5);
    157     }
    158
    159     public void testSqrtImaginaryNegative() {
    160         Complex z = new Complex(-3.0, -4.0);
    161         Complex expected = new Complex(1.0, -2.0);
    162         TestUtils.assertEquals(expected, ComplexUtils.sqrt(z), 1.0e-5);
    163     }
    164
    165     public void testSqrt1z() {
    166         Complex z = new Complex(3, 4);
    167         Complex expected = new Complex(4.08033, -2.94094);
    168         TestUtils.assertEquals(expected, ComplexUtils.sqrt1z(z), 1.0e-5);
    169     }
    170
    171     public void testSqrt1zNaN() {
    172         assertTrue(ComplexUtils.sqrt1z(Complex.NaN).isNaN());
    173     }
    174
    175     public void testSqrtNaN() {
    176         assertTrue(ComplexUtils.sqrt(Complex.NaN).isNaN());
    177     }
    178
    179     public void testTan() {
    180         Complex z = new Complex(3, 4);
    181         Complex expected = new Complex(-0.000187346, 0.999356);
    182         TestUtils.assertEquals(expected, ComplexUtils.tan(z), 1.0e-5);
    183     }
    184
    185     public void testTanh() {
    186         Complex z = new Complex(3, 4);
    187         Complex expected = new Complex(1.00071, 0.00490826);
    188         TestUtils.assertEquals(expected, ComplexUtils.tanh(z), 1.0e-5);
    189     }
    190
    191     public void testTanhNaN() {
    192         assertTrue(ComplexUtils.tanh(Complex.NaN).isNaN());
    193     }
    194
    195     public void testTanNaN() {
    196         assertTrue(ComplexUtils.tan(Complex.NaN).isNaN());
    197     }
    198 }