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
018package org.apache.commons.math3.complex;
019
020import org.apache.commons.math3.exception.MathIllegalArgumentException;
021import org.apache.commons.math3.exception.util.LocalizedFormats;
022import 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 */
029public class ComplexUtils {
030
031    /**
032     * Default constructor.
033     */
034    private ComplexUtils() {}
035
036    /**
037     * Creates a complex number from the given polar representation.
038     * <p>
039     * The value returned is <code>r&middot;e<sup>i&middot;theta</sup></code>,
040     * computed as <code>r&middot;cos(theta) + r&middot;sin(theta)i</code></p>
041     * <p>
042     * If either <code>r</code> or <code>theta</code> is NaN, or
043     * <code>theta</code> is infinite, {@link Complex#NaN} is returned.</p>
044     * <p>
045     * If <code>r</code> is infinite and <code>theta</code> is finite,
046     * infinite or NaN values may be returned in parts of the result, following
047     * the rules for double arithmetic.<pre>
048     * Examples:
049     * <code>
050     * polar2Complex(INFINITY, &pi;/4) = INFINITY + INFINITY i
051     * polar2Complex(INFINITY, 0) = INFINITY + NaN i
052     * polar2Complex(INFINITY, -&pi;/4) = INFINITY - INFINITY i
053     * polar2Complex(INFINITY, 5&pi;/4) = -INFINITY - INFINITY i </code></pre></p>
054     *
055     * @param r the modulus of the complex number to create
056     * @param theta  the argument of the complex number to create
057     * @return <code>r&middot;e<sup>i&middot;theta</sup></code>
058     * @throws MathIllegalArgumentException if {@code r} is negative.
059     * @since 1.1
060     */
061    public static Complex polar2Complex(double r, double theta) throws MathIllegalArgumentException {
062        if (r < 0) {
063            throw new MathIllegalArgumentException(
064                  LocalizedFormats.NEGATIVE_COMPLEX_MODULE, r);
065        }
066        return new Complex(r * FastMath.cos(theta), r * FastMath.sin(theta));
067    }
068
069    /**
070     * Convert an array of primitive doubles to an array of {@code Complex} objects.
071     *
072     * @param real Array of numbers to be converted to their {@code Complex}
073     * equivalent.
074     * @return an array of {@code Complex} objects.
075     *
076     * @since 3.1
077     */
078    public static Complex[] convertToComplex(double[] real) {
079        final Complex c[] = new Complex[real.length];
080        for (int i = 0; i < real.length; i++) {
081            c[i] = new Complex(real[i], 0);
082        }
083
084        return c;
085    }
086}