    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 junit.framework.TestCase;
    20
    21  /***
    22   * @version $Revision: 331126 $ $Date: 2005-11-06 11:06:46 -0700 (Sun, 06 Nov 2005) $
    23   */
    24  public class ComplexTest extends TestCase {
    25
    26      private double inf = Double.POSITIVE_INFINITY;
    27      private double neginf = Double.NEGATIVE_INFINITY;
    28      private double nan = Double.NaN;
    29      private Complex oneInf = new Complex(1, inf);
    30      private Complex oneNegInf = new Complex(1, neginf);
    31      private Complex infOne = new Complex(inf, 1);
    32      private Complex negInfInf = new Complex(neginf, inf);
    33      private Complex negInfNegInf = new Complex(neginf, neginf);
    34      private Complex oneNaN = new Complex(1, nan);
    35
    36      public void testConstructor() {
    37          Complex z = new Complex(3.0, 4.0);
    38          assertEquals(3.0, z.getReal(), 1.0e-5);
    39          assertEquals(4.0, z.getImaginary(), 1.0e-5);
    40      }
    41
    42      public void testConstructorNaN() {
    43          Complex z = new Complex(3.0, Double.NaN);
    44          assertTrue(z.isNaN());
    45
    46          z = new Complex(nan, 4.0);
    47          assertTrue(z.isNaN());
    48
    49          z = new Complex(3.0, 4.0);
    50          assertFalse(z.isNaN());
    51      }
    52
    53      public void testAbs() {
    54          Complex z = new Complex(3.0, 4.0);
    55          assertEquals(5.0, z.abs(), 1.0e-5);
    56      }
    57
    58      public void testAbsNaN() {
    59          assertTrue(Double.isNaN(Complex.NaN.abs()));
    60          Complex z = new Complex(inf, nan);
    61          assertTrue(Double.isNaN(z.abs()));
    62      }
    63
    64      public void testAbsInfinite() {
    65          Complex z = new Complex(inf, 0);
    66          assertEquals(inf, z.abs(), 0);
    67          z = new Complex(0, neginf);
    68          assertEquals(inf, z.abs(), 0);
    69          z = new Complex(inf, neginf);
    70          assertEquals(inf, z.abs(), 0);
    71      }
    72
    73      public void testAdd() {
    74          Complex x = new Complex(3.0, 4.0);
    75          Complex y = new Complex(5.0, 6.0);
    76          Complex z = x.add(y);
    77          assertEquals(8.0, z.getReal(), 1.0e-5);
    78          assertEquals(10.0, z.getImaginary(), 1.0e-5);
    79      }
    80
    81      public void testAddNaN() {
    82          Complex x = new Complex(3.0, 4.0);
    83          Complex z = x.add(Complex.NaN);
    84          assertTrue(z.isNaN());
    85          z = new Complex(1, nan);
    86          Complex w = x.add(z);
    87          assertEquals(w.real, 4.0, 0);
    88          assertTrue(Double.isNaN(w.imaginary));
    89      }
    90
    91      public void testAddInfinite() {
    92          Complex x = new Complex(1, 1);
    93          Complex z = new Complex(inf, 0);
    94          Complex w = x.add(z);
    95          assertEquals(w.imaginary, 1, 0);
    96          assertEquals(inf, w.real, 0);
    97
    98          x = new Complex(neginf, 0);
    99          assertTrue(Double.isNaN(x.add(z).real));
    100     }
    101
    102     public void testConjugate() {
    103         Complex x = new Complex(3.0, 4.0);
    104         Complex z = x.conjugate();
    105         assertEquals(3.0, z.getReal(), 1.0e-5);
    106         assertEquals(-4.0, z.getImaginary(), 1.0e-5);
    107     }
    108
    109     public void testConjugateNaN() {
    110         Complex z = Complex.NaN.conjugate();
    111         assertTrue(z.isNaN());
    112     }
    113
    114     public void testConjugateInfiinite() {
    115         Complex z = new Complex(0, inf);
    116         assertEquals(neginf, z.conjugate().imaginary, 0);
    117         z = new Complex(0, neginf);
    118         assertEquals(inf, z.conjugate().imaginary, 0);
    119     }
    120
    121     public void testDivide() {
    122         Complex x = new Complex(3.0, 4.0);
    123         Complex y = new Complex(5.0, 6.0);
    124         Complex z = x.divide(y);
    125         assertEquals(39.0 / 61.0, z.getReal(), 1.0e-5);
    126         assertEquals(2.0 / 61.0, z.getImaginary(), 1.0e-5);
    127     }
    128
    129     public void testDivideInfinite() {
    130         Complex x = new Complex(3, 4);
    131         Complex w = new Complex(neginf, inf);
    132         assertTrue(x.divide(w).equals(Complex.ZERO));
    133
    134         Complex z = w.divide(x);
    135         assertTrue(Double.isNaN(z.real));
    136         assertEquals(inf, z.imaginary, 0);
    137
    138         w = new Complex(inf, inf);
    139         z = w.divide(x);
    140         assertTrue(Double.isNaN(z.imaginary));
    141         assertEquals(inf, z.real, 0);
    142
    143         w = new Complex(1, inf);
    144         z = w.divide(w);
    145         assertTrue(Double.isNaN(z.real));
    146         assertTrue(Double.isNaN(z.imaginary));
    147     }
    148
    149     public void testDivideNaN() {
    150         Complex x = new Complex(3.0, 4.0);
    151         Complex z = x.divide(Complex.NaN);
    152         assertTrue(z.isNaN());
    153     }
    154
    155     public void testDivideNaNInf() {
    156        Complex z = oneInf.divide(Complex.ONE);
    157        assertTrue(Double.isNaN(z.real));
    158        assertEquals(inf, z.imaginary, 0);
    159
    160        z = negInfNegInf.divide(oneNaN);
    161        assertTrue(Double.isNaN(z.real));
    162        assertTrue(Double.isNaN(z.imaginary));
    163
    164        z = negInfInf.divide(Complex.ONE);
    165        assertTrue(Double.isNaN(z.real));
    166        assertTrue(Double.isNaN(z.imaginary));
    167     }
    168
    169     public void testMultiply() {
    170         Complex x = new Complex(3.0, 4.0);
    171         Complex y = new Complex(5.0, 6.0);
    172         Complex z = x.multiply(y);
    173         assertEquals(-9.0, z.getReal(), 1.0e-5);
    174         assertEquals(38.0, z.getImaginary(), 1.0e-5);
    175     }
    176
    177     public void testMultiplyNaN() {
    178         Complex x = new Complex(3.0, 4.0);
    179         Complex z = x.multiply(Complex.NaN);
    180         assertTrue(z.isNaN());
    181     }
    182
    183     public void testMultiplyNaNInf() {
    184         Complex z = new Complex(1,1);
    185         Complex w = z.multiply(infOne);
    186         assertEquals(w.real, inf, 0);
    187         assertEquals(w.imaginary, inf, 0);
    188
    189         w = oneInf.multiply(oneNegInf);
    190         assertEquals(w.real, inf, 0);
    191         assertTrue(Double.isNaN(w.imaginary));
    192
    193         w = negInfNegInf.multiply(oneNaN);
    194         assertTrue(Double.isNaN(w.real));
    195         assertTrue(Double.isNaN(w.imaginary));
    196     }
    197
    198     public void testNegate() {
    199         Complex x = new Complex(3.0, 4.0);
    200         Complex z = x.negate();
    201         assertEquals(-3.0, z.getReal(), 1.0e-5);
    202         assertEquals(-4.0, z.getImaginary(), 1.0e-5);
    203     }
    204
    205     public void testNegateNaN() {
    206         Complex z = Complex.NaN.negate();
    207         assertTrue(z.isNaN());
    208     }
    209
    210     public void testSubtract() {
    211         Complex x = new Complex(3.0, 4.0);
    212         Complex y = new Complex(5.0, 6.0);
    213         Complex z = x.subtract(y);
    214         assertEquals(-2.0, z.getReal(), 1.0e-5);
    215         assertEquals(-2.0, z.getImaginary(), 1.0e-5);
    216     }
    217
    218     public void testSubtractNaN() {
    219         Complex x = new Complex(3.0, 4.0);
    220         Complex z = x.subtract(Complex.NaN);
    221         assertTrue(z.isNaN());
    222     }
    223
    224     public void testEqualsNull() {
    225         Complex x = new Complex(3.0, 4.0);
    226         assertFalse(x.equals(null));
    227     }
    228
    229     public void testEqualsClass() {
    230         Complex x = new Complex(3.0, 4.0);
    231         assertFalse(x.equals(this));
    232     }
    233
    234     public void testEqualsSame() {
    235         Complex x = new Complex(3.0, 4.0);
    236         assertTrue(x.equals(x));
    237     }
    238
    239     public void testEqualsTrue() {
    240         Complex x = new Complex(3.0, 4.0);
    241         Complex y = new Complex(3.0, 4.0);
    242         assertTrue(x.equals(y));
    243     }
    244
    245     public void testEqualsRealDifference() {
    246         Complex x = new Complex(0.0, 0.0);
    247         Complex y = new Complex(0.0 + Double.MIN_VALUE, 0.0);
    248         assertFalse(x.equals(y));
    249     }
    250
    251     public void testEqualsImaginaryDifference() {
    252         Complex x = new Complex(0.0, 0.0);
    253         Complex y = new Complex(0.0, 0.0 + Double.MIN_VALUE);
    254         assertFalse(x.equals(y));
    255     }
    256
    257     public void testEqualsNaN() {
    258         Complex realNaN = new Complex(Double.NaN, 0.0);
    259         Complex imaginaryNaN = new Complex(0.0, Double.NaN);
    260         Complex complexNaN = Complex.NaN;
    261         assertTrue(realNaN.equals(imaginaryNaN));
    262         assertTrue(imaginaryNaN.equals(complexNaN));
    263         assertTrue(realNaN.equals(complexNaN));
    264     }
    265
    266     public void testHashCode() {
    267         Complex x = new Complex(0.0, 0.0);
    268         Complex y = new Complex(0.0, 0.0 + Double.MIN_VALUE);
    269         assertFalse(x.hashCode()==y.hashCode());
    270         y = new Complex(0.0 + Double.MIN_VALUE, 0.0);
    271         assertFalse(x.hashCode()==y.hashCode());
    272         Complex realNaN = new Complex(Double.NaN, 0.0);
    273         Complex imaginaryNaN = new Complex(0.0, Double.NaN);
    274         assertEquals(realNaN.hashCode(), imaginaryNaN.hashCode());
    275         assertEquals(imaginaryNaN.hashCode(), Complex.NaN.hashCode());
    276     }
    277 }