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