001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.commons.math.complex; 019 020 import org.apache.commons.math.TestUtils; 021 022 import junit.framework.TestCase; 023 024 /** 025 * @version $Revision: 670469 $ $Date: 2008-06-23 04:01:38 -0400 (Mon, 23 Jun 2008) $ 026 */ 027 public class ComplexUtilsTest extends TestCase { 028 029 private double inf = Double.POSITIVE_INFINITY; 030 private double negInf = Double.NEGATIVE_INFINITY; 031 private double nan = Double.NaN; 032 private double pi = Math.PI; 033 034 private Complex negInfInf = new Complex(negInf, inf); 035 private Complex infNegInf = new Complex(inf, negInf); 036 private Complex infInf = new Complex(inf, inf); 037 private Complex negInfNegInf = new Complex(negInf, negInf); 038 private Complex infNaN = new Complex(inf, nan); 039 040 public void testPolar2Complex() { 041 TestUtils.assertEquals(Complex.ONE, 042 ComplexUtils.polar2Complex(1, 0), 10e-12); 043 TestUtils.assertEquals(Complex.ZERO, 044 ComplexUtils.polar2Complex(0, 1), 10e-12); 045 TestUtils.assertEquals(Complex.ZERO, 046 ComplexUtils.polar2Complex(0, -1), 10e-12); 047 TestUtils.assertEquals(Complex.I, 048 ComplexUtils.polar2Complex(1, pi/2), 10e-12); 049 TestUtils.assertEquals(Complex.I.negate(), 050 ComplexUtils.polar2Complex(1, -pi/2), 10e-12); 051 double r = 0; 052 for (int i = 0; i < 5; i++) { 053 r += i; 054 double theta = 0; 055 for (int j =0; j < 20; j++) { 056 theta += pi / 6; 057 TestUtils.assertEquals(altPolar(r, theta), 058 ComplexUtils.polar2Complex(r, theta), 10e-12); 059 } 060 theta = -2 * pi; 061 for (int j =0; j < 20; j++) { 062 theta -= pi / 6; 063 TestUtils.assertEquals(altPolar(r, theta), 064 ComplexUtils.polar2Complex(r, theta), 10e-12); 065 } 066 } 067 } 068 069 protected Complex altPolar(double r, double theta) { 070 return Complex.I.multiply(new Complex(theta, 0)).exp().multiply(new Complex(r, 0)); 071 } 072 073 public void testPolar2ComplexIllegalModulus() { 074 try { 075 ComplexUtils.polar2Complex(-1, 0); 076 fail("Expecting IllegalArgumentException"); 077 } catch (IllegalArgumentException ex) { 078 // expected 079 } 080 } 081 082 public void testPolar2ComplexNaN() { 083 TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(nan, 1)); 084 TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(1, nan)); 085 TestUtils.assertSame(Complex.NaN, 086 ComplexUtils.polar2Complex(nan, nan)); 087 } 088 089 public void testPolar2ComplexInf() { 090 TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(1, inf)); 091 TestUtils.assertSame(Complex.NaN, 092 ComplexUtils.polar2Complex(1, negInf)); 093 TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(inf, inf)); 094 TestUtils.assertSame(Complex.NaN, 095 ComplexUtils.polar2Complex(inf, negInf)); 096 TestUtils.assertSame(infInf, ComplexUtils.polar2Complex(inf, pi/4)); 097 TestUtils.assertSame(infNaN, ComplexUtils.polar2Complex(inf, 0)); 098 TestUtils.assertSame(infNegInf, ComplexUtils.polar2Complex(inf, -pi/4)); 099 TestUtils.assertSame(negInfInf, ComplexUtils.polar2Complex(inf, 3*pi/4)); 100 TestUtils.assertSame(negInfNegInf, ComplexUtils.polar2Complex(inf, 5*pi/4)); 101 } 102 103 }