source: src/main/java/agents/anac/y2019/harddealer/math3/complex/ComplexUtils.java

Last change on this file was 204, checked in by Katsuhide Fujita, 5 years ago

Fixed errors of ANAC2019 agents

  • Property svn:executable set to *
File size: 3.3 KB
Line 
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
18package agents.anac.y2019.harddealer.math3.complex;
19
20import agents.anac.y2019.harddealer.math3.exception.MathIllegalArgumentException;
21import agents.anac.y2019.harddealer.math3.exception.util.LocalizedFormats;
22import agents.anac.y2019.harddealer.math3.util.FastMath;
23
24/**
25 * Static implementations of common
26 * {@link agents.anac.y2019.harddealer.math3.complex.Complex} utilities functions.
27 *
28 */
29public class ComplexUtils {
30
31 /**
32 * Default constructor.
33 */
34 private ComplexUtils() {}
35
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></p>
41 * <p>
42 * If either <code>r</code> or <code>theta</code> is NaN, or
43 * <code>theta</code> is infinite, {@link Complex#NaN} is returned.</p>
44 * <p>
45 * If <code>r</code> is infinite and <code>theta</code> is finite,
46 * infinite or NaN values may be returned in parts of the result, following
47 * the rules for double arithmetic.<pre>
48 * Examples:
49 * <code>
50 * polar2Complex(INFINITY, &pi;/4) = INFINITY + INFINITY i
51 * polar2Complex(INFINITY, 0) = INFINITY + NaN i
52 * polar2Complex(INFINITY, -&pi;/4) = INFINITY - INFINITY i
53 * polar2Complex(INFINITY, 5&pi;/4) = -INFINITY - INFINITY i </code></pre></p>
54 *
55 * @param r the modulus of the complex number to create
56 * @param theta the argument of the complex number to create
57 * @return <code>r&middot;e<sup>i&middot;theta</sup></code>
58 * @throws MathIllegalArgumentException if {@code r} is negative.
59 * @since 1.1
60 */
61 public static Complex polar2Complex(double r, double theta) throws MathIllegalArgumentException {
62 if (r < 0) {
63 throw new MathIllegalArgumentException(
64 LocalizedFormats.NEGATIVE_COMPLEX_MODULE, r);
65 }
66 return new Complex(r * FastMath.cos(theta), r * FastMath.sin(theta));
67 }
68
69 /**
70 * Convert an array of primitive doubles to an array of {@code Complex} objects.
71 *
72 * @param real Array of numbers to be converted to their {@code Complex}
73 * equivalent.
74 * @return an array of {@code Complex} objects.
75 *
76 * @since 3.1
77 */
78 public static Complex[] convertToComplex(double[] real) {
79 final Complex c[] = new Complex[real.length];
80 for (int i = 0; i < real.length; i++) {
81 c[i] = new Complex(real[i], 0);
82 }
83
84 return c;
85 }
86}
Note: See TracBrowser for help on using the repository browser.