1 /* ****************************************************************************
3 * Copyright (c) Microsoft Corporation.
5 * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
6 * copy of the license can be found in the License.html file at the root of this distribution. If
7 * you cannot locate the Apache License, Version 2.0, please send an email to
8 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
9 * by the terms of the Apache License, Version 2.0.
11 * You must not remove this notice, or any other, from this software.
14 * ***************************************************************************/
17 using Microsoft.Scripting.Utils;
20 using BigInt = System.Numerics.BigInteger;
23 namespace Microsoft.Scripting.Math {
25 /// Implementation of the complex number data type.
28 public struct Complex64 {
29 public static readonly Complex64 Zero = new Complex64(0.0, 0.0);
30 public static readonly Complex64 One = new Complex64(1.0, 0.0);
31 public static readonly Complex64 ImaginaryOne = new Complex64(0.0, 1.0);
33 private readonly double real, imag;
35 public static Complex64 MakeImaginary(double imag) {
36 return new Complex64(0.0, imag);
39 public static Complex64 MakeReal(double real) {
40 return new Complex64(real, 0.0);
43 public static Complex64 Make(double real, double imag) {
44 return new Complex64(real, imag);
47 public Complex64(double real)
51 public Complex64(double real, double imag) {
58 return real == 0.0 && imag == 0.0;
74 public Complex64 Conjugate() {
75 return new Complex64(real, -imag);
79 public override string ToString() {
80 if (real == 0.0) return imag.ToString(System.Globalization.CultureInfo.InvariantCulture.NumberFormat) + "j";
81 else if (imag < 0.0) return string.Format(System.Globalization.CultureInfo.InvariantCulture.NumberFormat, "({0}{1}j)", real, imag);
82 else return string.Format(System.Globalization.CultureInfo.InvariantCulture.NumberFormat, "({0}+{1}j)", real, imag);
85 public static implicit operator Complex64(bool b) {
86 return b ? One : Zero;
89 public static implicit operator Complex64(int i) {
94 public static implicit operator Complex64(uint i) {
98 public static implicit operator Complex64(short i) {
102 [CLSCompliant(false)]
103 public static implicit operator Complex64(ushort i) {
107 public static implicit operator Complex64(long l) {
110 [CLSCompliant(false)]
111 public static implicit operator Complex64(ulong i) {
115 [CLSCompliant(false)]
116 public static implicit operator Complex64(sbyte i) {
120 public static implicit operator Complex64(byte i) {
124 public static implicit operator Complex64(float f) {
128 public static implicit operator Complex64(double d) {
132 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations")] // TODO: fix
133 public static implicit operator Complex64(BigInteger i) {
134 ContractUtils.RequiresNotNull(i, "i");
136 // throws an overflow exception if we can't handle the value.
137 return MakeReal((double)i);
141 public static implicit operator Complex64(BigInt i) {
142 // throws an overflow exception if we can't handle the value.
143 return MakeReal((double)i);
147 public static bool operator ==(Complex64 x, Complex64 y) {
148 return x.real == y.real && x.imag == y.imag;
151 public static bool operator !=(Complex64 x, Complex64 y) {
152 return x.real != y.real || x.imag != y.imag;
155 public static Complex64 Add(Complex64 x, Complex64 y) {
159 public static Complex64 operator +(Complex64 x, Complex64 y) {
160 return new Complex64(x.real + y.real, x.imag + y.imag);
163 public static Complex64 Subtract(Complex64 x, Complex64 y) {
167 public static Complex64 operator -(Complex64 x, Complex64 y) {
168 return new Complex64(x.real - y.real, x.imag - y.imag);
171 public static Complex64 Multiply(Complex64 x, Complex64 y) {
175 public static Complex64 operator *(Complex64 x, Complex64 y) {
176 return new Complex64(x.real * y.real - x.imag * y.imag, x.real * y.imag + x.imag * y.real);
179 public static Complex64 Divide(Complex64 x, Complex64 y) {
183 public static Complex64 operator /(Complex64 a, Complex64 b) {
185 throw new DivideByZeroException("complex division by zero");
188 double real, imag, den, r;
190 if (System.Math.Abs(b.real) >= System.Math.Abs(b.imag)) {
192 den = b.real + r * b.imag;
193 real = (a.real + a.imag * r) / den;
194 imag = (a.imag - a.real * r) / den;
197 den = b.imag + r * b.real;
198 real = (a.real * r + a.imag) / den;
199 imag = (a.imag * r - a.real) / den;
202 return new Complex64(real, imag);
205 public static Complex64 Negate(Complex64 x) {
209 public static Complex64 operator -(Complex64 x) {
210 return new Complex64(-x.real, -x.imag);
213 public static Complex64 Plus(Complex64 x) {
217 public static Complex64 operator +(Complex64 x) {
221 [Obsolete("Deprecated - consider using MS.Scripting.Utils.MathUtils.Hypot")]
222 public static double Hypot(double x, double y) {
223 return MathUtils.Hypot(x, y);
226 public double Abs() {
227 return MathUtils.Hypot(real, imag);
230 public Complex64 Power(Complex64 y) {
235 if (power == c && power >= 0 && d == .0) {
236 Complex64 result = One;
237 if (power == 0) return result;
238 Complex64 factor = this;
240 if ((power & 1) != 0) {
241 result = result * factor;
243 factor = factor * factor;
248 return y.IsZero ? One : Zero;
252 double powers = a * a + b * b;
253 double arg = System.Math.Atan2(b, a);
254 double mul = System.Math.Pow(powers, c / 2) * System.Math.Exp(-d * arg);
255 double common = c * arg + .5 * d * System.Math.Log(powers);
256 return new Complex64(mul * System.Math.Cos(common), mul * System.Math.Sin(common));
260 public override int GetHashCode() {
261 // The Object.GetHashCode function needs to be consistent with the Object.Equals function.
262 // Languages that build on top of this may have a more flexible equality function and
263 // so may not be able to use this hash function directly.
264 // For example, Python allows that c=Complex64(1.5, 0), f = 1.5f, c==f.
265 // so then the hash(f) == hash(c). Since the python (and other languages) can define an arbitrary
266 // hash(float) function, the language may need to define a matching hash(complex) function for
267 // the cases where the float and complex numbers overlap.
268 return (int)real + (int)imag * 1000003;
271 public override bool Equals(object obj) {
272 if (!(obj is Complex64)) return false;
273 return this == ((Complex64)obj);