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 /*
19 * This is not the original file distributed by the Apache Software Foundation
20 * It has been modified by the Hipparchus project
21 */
22
23 package org.hipparchus.complex;
24
25 import org.hipparchus.CalculusFieldElement;
26 import org.hipparchus.exception.LocalizedCoreFormats;
27 import org.hipparchus.exception.MathIllegalArgumentException;
28 import org.hipparchus.util.FastMath;
29 import org.hipparchus.util.FieldSinCos;
30 import org.hipparchus.util.SinCos;
31
32 /**
33 * Static implementations of common {@link Complex} utilities functions.
34 */
35 public class ComplexUtils {
36
37 /**
38 * Default constructor.
39 */
40 private ComplexUtils() {}
41
42 /**
43 * Creates a complex number from the given polar representation.
44 * <p>
45 * The value returned is <code>r·e<sup>i·theta</sup></code>,
46 * computed as <code>r·cos(theta) + r·sin(theta)i</code>
47 * </p>
48 * <p>
49 * If either <code>r</code> or <code>theta</code> is NaN, or
50 * <code>theta</code> is infinite, {@link Complex#NaN} is returned.
51 * </p>
52 * <p>
53 * If <code>r</code> is infinite and <code>theta</code> is finite,
54 * infinite or NaN values may be returned in parts of the result, following
55 * the rules for double arithmetic.</p>
56 * <pre>
57 * Examples:
58 * <code>
59 * polar2Complex(INFINITY, π/4) = INFINITY + INFINITY i
60 * polar2Complex(INFINITY, 0) = INFINITY + NaN i
61 * polar2Complex(INFINITY, -π/4) = INFINITY - INFINITY i
62 * polar2Complex(INFINITY, 5π/4) = -INFINITY - INFINITY i </code>
63 * </pre>
64 *
65 * @param r the modulus of the complex number to create
66 * @param theta the argument of the complex number to create
67 * @return <code>r·e<sup>i·theta</sup></code>
68 * @throws MathIllegalArgumentException if {@code r} is negative.
69 */
70 public static Complex polar2Complex(double r, double theta) throws MathIllegalArgumentException {
71 if (r < 0) {
72 throw new MathIllegalArgumentException(
73 LocalizedCoreFormats.NEGATIVE_COMPLEX_MODULE, r);
74 }
75 final SinCos sc = FastMath.sinCos(theta);
76 return new Complex(r * sc.cos(), r * sc.sin());
77 }
78
79 /**
80 * Creates a complex number from the given polar representation.
81 * <p>
82 * The value returned is <code>r·e<sup>i·theta</sup></code>,
83 * computed as <code>r·cos(theta) + r·sin(theta)i</code>
84 * </p>
85 * <p>
86 * If either <code>r</code> or <code>theta</code> is NaN, or
87 * <code>theta</code> is infinite, {@link Complex#NaN} is returned.
88 * </p>
89 * <p>
90 * If <code>r</code> is infinite and <code>theta</code> is finite,
91 * infinite or NaN values may be returned in parts of the result, following
92 * the rules for double arithmetic.
93 * </p>
94 * <pre>
95 * Examples:
96 * <code>
97 * polar2Complex(INFINITY, π/4) = INFINITY + INFINITY i
98 * polar2Complex(INFINITY, 0) = INFINITY + NaN i
99 * polar2Complex(INFINITY, -π/4) = INFINITY - INFINITY i
100 * polar2Complex(INFINITY, 5π/4) = -INFINITY - INFINITY i </code>
101 * </pre>
102 *
103 * @param r the modulus of the complex number to create
104 * @param theta the argument of the complex number to create
105 * @param <T> type of the field elements
106 * @return <code>r·e<sup>i·theta</sup></code>
107 * @throws MathIllegalArgumentException if {@code r} is negative.
108 * @since 2.0
109 */
110 public static <T extends CalculusFieldElement<T>> FieldComplex<T> polar2Complex(T r, T theta) throws MathIllegalArgumentException {
111 if (r.getReal() < 0) {
112 throw new MathIllegalArgumentException(
113 LocalizedCoreFormats.NEGATIVE_COMPLEX_MODULE, r);
114 }
115 final FieldSinCos<T> sc = FastMath.sinCos(theta);
116 return new FieldComplex<>(r.multiply(sc.cos()), r.multiply(sc.sin()));
117 }
118
119 /**
120 * Convert an array of primitive doubles to an array of {@code Complex} objects.
121 *
122 * @param real Array of numbers to be converted to their {@code Complex} equivalent.
123 * @return an array of {@code Complex} objects.
124 */
125 public static Complex[] convertToComplex(double[] real) {
126 final Complex[] c = new Complex[real.length];
127 for (int i = 0; i < real.length; i++) {
128 c[i] = new Complex(real[i], 0);
129 }
130
131 return c;
132 }
133
134 }