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.MathRuntimeException;
021
022 /**
023 * Static implementations of common
024 * {@link org.apache.commons.math.complex.Complex} utilities functions.
025 *
026 * @version $Revision: 811685 $ $Date: 2009-09-05 13:36:48 -0400 (Sat, 05 Sep 2009) $
027 */
028 public class ComplexUtils {
029
030 /**
031 * Default constructor.
032 */
033 private ComplexUtils() {
034 super();
035 }
036
037 /**
038 * Creates a complex number from the given polar representation.
039 * <p>
040 * The value returned is <code>r·e<sup>i·theta</sup></code>,
041 * computed as <code>r·cos(theta) + r·sin(theta)i</code></p>
042 * <p>
043 * If either <code>r</code> or <code>theta</code> is NaN, or
044 * <code>theta</code> is infinite, {@link Complex#NaN} is returned.</p>
045 * <p>
046 * If <code>r</code> is infinite and <code>theta</code> is finite,
047 * infinite or NaN values may be returned in parts of the result, following
048 * the rules for double arithmetic.<pre>
049 * Examples:
050 * <code>
051 * polar2Complex(INFINITY, π/4) = INFINITY + INFINITY i
052 * polar2Complex(INFINITY, 0) = INFINITY + NaN i
053 * polar2Complex(INFINITY, -π/4) = INFINITY - INFINITY i
054 * polar2Complex(INFINITY, 5π/4) = -INFINITY - INFINITY i </code></pre></p>
055 *
056 * @param r the modulus of the complex number to create
057 * @param theta the argument of the complex number to create
058 * @return <code>r·e<sup>i·theta</sup></code>
059 * @throws IllegalArgumentException if r is negative
060 * @since 1.1
061 */
062 public static Complex polar2Complex(double r, double theta) {
063 if (r < 0) {
064 throw MathRuntimeException.createIllegalArgumentException(
065 "negative complex module {0}", r);
066 }
067 return new Complex(r * Math.cos(theta), r * Math.sin(theta));
068 }
069
070 }