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.  *      https://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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */

  21. package org.hipparchus.complex;

  22. import org.hipparchus.CalculusFieldElement;
  23. import org.hipparchus.exception.LocalizedCoreFormats;
  24. import org.hipparchus.exception.MathIllegalArgumentException;
  25. import org.hipparchus.util.FastMath;
  26. import org.hipparchus.util.FieldSinCos;
  27. import org.hipparchus.util.SinCos;

  28. /**
  29.  * Static implementations of common {@link Complex} utilities functions.
  30.  */
  31. public class ComplexUtils {

  32.     /**
  33.      * Default constructor.
  34.      */
  35.     private ComplexUtils() {}

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

  72.     /**
  73.      * Creates a complex number from the given polar representation.
  74.      * <p>
  75.      * The value returned is <code>r&middot;e<sup>i&middot;theta</sup></code>,
  76.      * computed as <code>r&middot;cos(theta) + r&middot;sin(theta)i</code>
  77.      * </p>
  78.      * <p>
  79.      * If either <code>r</code> or <code>theta</code> is NaN, or
  80.      * <code>theta</code> is infinite, {@link Complex#NaN} is returned.
  81.      * </p>
  82.      * <p>
  83.      * If <code>r</code> is infinite and <code>theta</code> is finite,
  84.      * infinite or NaN values may be returned in parts of the result, following
  85.      * the rules for double arithmetic.
  86.      * </p>
  87.      * <pre>
  88.      * Examples:
  89.      * <code>
  90.      * polar2Complex(INFINITY, &pi;/4) = INFINITY + INFINITY i
  91.      * polar2Complex(INFINITY, 0) = INFINITY + NaN i
  92.      * polar2Complex(INFINITY, -&pi;/4) = INFINITY - INFINITY i
  93.      * polar2Complex(INFINITY, 5&pi;/4) = -INFINITY - INFINITY i </code>
  94.      * </pre>
  95.      *
  96.      * @param r the modulus of the complex number to create
  97.      * @param theta  the argument of the complex number to create
  98.      * @param <T> type of the field elements
  99.      * @return <code>r&middot;e<sup>i&middot;theta</sup></code>
  100.      * @throws MathIllegalArgumentException if {@code r} is negative.
  101.      * @since 2.0
  102.      */
  103.     public static <T extends CalculusFieldElement<T>> FieldComplex<T> polar2Complex(T r, T theta) throws MathIllegalArgumentException {
  104.         if (r.getReal() < 0) {
  105.             throw new MathIllegalArgumentException(
  106.                   LocalizedCoreFormats.NEGATIVE_COMPLEX_MODULE, r);
  107.         }
  108.         final FieldSinCos<T> sc = FastMath.sinCos(theta);
  109.         return new FieldComplex<>(r.multiply(sc.cos()), r.multiply(sc.sin()));
  110.     }

  111.     /**
  112.      * Convert an array of primitive doubles to an array of {@code Complex} objects.
  113.      *
  114.      * @param real Array of numbers to be converted to their {@code Complex} equivalent.
  115.      * @return an array of {@code Complex} objects.
  116.      */
  117.     public static Complex[] convertToComplex(double[] real) {
  118.         final Complex[] c = new Complex[real.length];
  119.         for (int i = 0; i < real.length; i++) {
  120.             c[i] = new Complex(real[i], 0);
  121.         }

  122.         return c;
  123.     }

  124. }