1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package org.apache.commons.math.complex; 19 20import org.apache.commons.math.MathRuntimeException; 21import org.apache.commons.math.exception.util.LocalizedFormats; 22import org.apache.commons.math.util.FastMath; 23 24/** 25 * Static implementations of common 26 * {@link org.apache.commons.math.complex.Complex} utilities functions. 27 * 28 * @version $Revision: 990655 $ $Date: 2010-08-29 23:49:40 +0200 (dim. 29 août 2010) $ 29 */ 30public class ComplexUtils { 31 32 /** 33 * Default constructor. 34 */ 35 private ComplexUtils() { 36 super(); 37 } 38 39 /** 40 * Creates a complex number from the given polar representation. 41 * <p> 42 * The value returned is <code>r·e<sup>i·theta</sup></code>, 43 * computed as <code>r·cos(theta) + r·sin(theta)i</code></p> 44 * <p> 45 * If either <code>r</code> or <code>theta</code> is NaN, or 46 * <code>theta</code> is infinite, {@link Complex#NaN} is returned.</p> 47 * <p> 48 * If <code>r</code> is infinite and <code>theta</code> is finite, 49 * infinite or NaN values may be returned in parts of the result, following 50 * the rules for double arithmetic.<pre> 51 * Examples: 52 * <code> 53 * polar2Complex(INFINITY, π/4) = INFINITY + INFINITY i 54 * polar2Complex(INFINITY, 0) = INFINITY + NaN i 55 * polar2Complex(INFINITY, -π/4) = INFINITY - INFINITY i 56 * polar2Complex(INFINITY, 5π/4) = -INFINITY - INFINITY i </code></pre></p> 57 * 58 * @param r the modulus of the complex number to create 59 * @param theta the argument of the complex number to create 60 * @return <code>r·e<sup>i·theta</sup></code> 61 * @throws IllegalArgumentException if r is negative 62 * @since 1.1 63 */ 64 public static Complex polar2Complex(double r, double theta) { 65 if (r < 0) { 66 throw MathRuntimeException.createIllegalArgumentException( 67 LocalizedFormats.NEGATIVE_COMPLEX_MODULE, r); 68 } 69 return new Complex(r * FastMath.cos(theta), r * FastMath.sin(theta)); 70 } 71 72} 73