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.math3.complex;
019    
020    import org.apache.commons.math3.exception.MathIllegalArgumentException;
021    import org.apache.commons.math3.exception.util.LocalizedFormats;
022    import org.apache.commons.math3.util.FastMath;
023    
024    /**
025     * Static implementations of common
026     * {@link org.apache.commons.math3.complex.Complex} utilities functions.
027     *
028     * @version $Id: ComplexUtils.java 1416643 2012-12-03 19:37:14Z tn $
029     */
030    public class ComplexUtils {
031    
032        /**
033         * Default constructor.
034         */
035        private ComplexUtils() {}
036    
037        /**
038         * Creates a complex number from the given polar representation.
039         * <p>
040         * The value returned is <code>r&middot;e<sup>i&middot;theta</sup></code>,
041         * computed as <code>r&middot;cos(theta) + r&middot;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, &pi;/4) = INFINITY + INFINITY i
052         * polar2Complex(INFINITY, 0) = INFINITY + NaN i
053         * polar2Complex(INFINITY, -&pi;/4) = INFINITY - INFINITY i
054         * polar2Complex(INFINITY, 5&pi;/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&middot;e<sup>i&middot;theta</sup></code>
059         * @throws MathIllegalArgumentException if {@code r} is negative.
060         * @since 1.1
061         */
062        public static Complex polar2Complex(double r, double theta) throws MathIllegalArgumentException {
063            if (r < 0) {
064                throw new MathIllegalArgumentException(
065                      LocalizedFormats.NEGATIVE_COMPLEX_MODULE, r);
066            }
067            return new Complex(r * FastMath.cos(theta), r * FastMath.sin(theta));
068        }
069    
070        /**
071         * Convert an array of primitive doubles to an array of {@code Complex} objects.
072         *
073         * @param real Array of numbers to be converted to their {@code Complex}
074         * equivalent.
075         * @return an array of {@code Complex} objects.
076         *
077         * @since 3.1
078         */
079        public static Complex[] convertToComplex(double[] real) {
080            final Complex c[] = new Complex[real.length];
081            for (int i = 0; i < real.length; i++) {
082                c[i] = new Complex(real[i], 0);
083            }
084    
085            return c;
086        }
087    }