/*
 * 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.math.complex;

import org.apache.commons.math.TestUtils;

import junit.framework.TestCase;

/**
 * @version $Revision$ $Date$
 */
public class ComplexUtilsTest extends TestCase {

	private double inf = Double.POSITIVE_INFINITY;
	private double negInf = Double.NEGATIVE_INFINITY;
	private double nan = Double.NaN;
	private double pi = Math.PI;

	private Complex negInfInf = new Complex(negInf, inf);
	private Complex infNegInf = new Complex(inf, negInf);
	private Complex infInf = new Complex(inf, inf);
	private Complex negInfNegInf = new Complex(negInf, negInf);
	private Complex infNaN = new Complex(inf, nan);

	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));
	}

	public void testPolar2ComplexIllegalModulus() {
		try {
			ComplexUtils.polar2Complex(-1, 0);
			fail("Expecting IllegalArgumentException");
		} catch (IllegalArgumentException ex) {
			// expected
		}
	}

	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));
	}

	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));
	}

}
