ComplexUtils.java

  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. package org.apache.commons.math3.complex;

  18. import org.apache.commons.math3.exception.MathIllegalArgumentException;
  19. import org.apache.commons.math3.exception.util.LocalizedFormats;
  20. import org.apache.commons.math3.util.FastMath;

  21. /**
  22.  * Static implementations of common
  23.  * {@link org.apache.commons.math3.complex.Complex} utilities functions.
  24.  *
  25.  */
  26. public class ComplexUtils {

  27.     /**
  28.      * Default constructor.
  29.      */
  30.     private ComplexUtils() {}

  31.     /**
  32.      * Creates a complex number from the given polar representation.
  33.      * <p>
  34.      * The value returned is <code>r&middot;e<sup>i&middot;theta</sup></code>,
  35.      * computed as <code>r&middot;cos(theta) + r&middot;sin(theta)i</code></p>
  36.      * <p>
  37.      * If either <code>r</code> or <code>theta</code> is NaN, or
  38.      * <code>theta</code> is infinite, {@link Complex#NaN} is returned.</p>
  39.      * <p>
  40.      * If <code>r</code> is infinite and <code>theta</code> is finite,
  41.      * infinite or NaN values may be returned in parts of the result, following
  42.      * the rules for double arithmetic.<pre>
  43.      * Examples:
  44.      * <code>
  45.      * polar2Complex(INFINITY, &pi;/4) = INFINITY + INFINITY i
  46.      * polar2Complex(INFINITY, 0) = INFINITY + NaN i
  47.      * polar2Complex(INFINITY, -&pi;/4) = INFINITY - INFINITY i
  48.      * polar2Complex(INFINITY, 5&pi;/4) = -INFINITY - INFINITY i </code></pre></p>
  49.      *
  50.      * @param r the modulus of the complex number to create
  51.      * @param theta  the argument of the complex number to create
  52.      * @return <code>r&middot;e<sup>i&middot;theta</sup></code>
  53.      * @throws MathIllegalArgumentException if {@code r} is negative.
  54.      * @since 1.1
  55.      */
  56.     public static Complex polar2Complex(double r, double theta) throws MathIllegalArgumentException {
  57.         if (r < 0) {
  58.             throw new MathIllegalArgumentException(
  59.                   LocalizedFormats.NEGATIVE_COMPLEX_MODULE, r);
  60.         }
  61.         return new Complex(r * FastMath.cos(theta), r * FastMath.sin(theta));
  62.     }

  63.     /**
  64.      * Convert an array of primitive doubles to an array of {@code Complex} objects.
  65.      *
  66.      * @param real Array of numbers to be converted to their {@code Complex}
  67.      * equivalent.
  68.      * @return an array of {@code Complex} objects.
  69.      *
  70.      * @since 3.1
  71.      */
  72.     public static Complex[] convertToComplex(double[] real) {
  73.         final Complex c[] = new Complex[real.length];
  74.         for (int i = 0; i < real.length; i++) {
  75.             c[i] = new Complex(real[i], 0);
  76.         }

  77.         return c;
  78.     }
  79. }