This repository has been archived by the owner on Nov 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 572
/
Complex.cs
139 lines (115 loc) · 4.28 KB
/
Complex.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
namespace Xamarin.FormsBook.Toolkit
{
// Mostly a subset of System.Numerics.Complex.
public struct Complex : IEquatable<Complex>, IFormattable
{
bool gotMagnitude, gotMagnitudeSquared;
double magnitude, magnitudeSquared;
public Complex(double real, double imaginary)
: this()
{
Real = real;
Imaginary = imaginary;
}
public double Real { private set; get; }
public double Imaginary { private set; get; }
// MagnitudeSquare and Magnitude calculated on demand and saved.
public double MagnitudeSquared
{
get
{
if (gotMagnitudeSquared)
{
return magnitudeSquared;
}
magnitudeSquared = Real * Real + Imaginary * Imaginary;
gotMagnitudeSquared = true;
return magnitudeSquared;
}
}
public double Magnitude
{
get
{
if (gotMagnitude)
{
return magnitude;
}
magnitude = Math.Sqrt(MagnitudeSquared);
gotMagnitude = true;
return magnitude;
}
}
public static Complex operator +(Complex left, Complex right)
{
return new Complex(left.Real + right.Real, left.Imaginary + right.Imaginary);
}
public static Complex operator -(Complex left, Complex right)
{
return new Complex(left.Real - right.Real, left.Imaginary - right.Imaginary);
}
public static Complex operator *(Complex left, Complex right)
{
return new Complex(left.Real * right.Real - left.Imaginary * right.Imaginary,
left.Real * right.Imaginary + left.Imaginary * right.Real);
}
public static bool operator ==(Complex left, Complex right)
{
return left.Real == right.Real && left.Imaginary == right.Imaginary;
}
public static bool operator !=(Complex left, Complex right)
{
return !(left == right);
}
public static implicit operator Complex(double value)
{
return new Complex(value, 0);
}
public static implicit operator Complex(int value)
{
return new Complex(value, 0);
}
public override int GetHashCode()
{
return Real.GetHashCode() + Imaginary.GetHashCode();
}
public override bool Equals(Object value)
{
return Real.Equals(((Complex)value).Real) &&
Imaginary.Equals(((Complex)value).Imaginary);
}
public bool Equals(Complex value)
{
return Real.Equals(value) && Imaginary.Equals(value);
}
public override string ToString()
{
return String.Format("{0} {1} {2}i", Real,
RealImaginaryConnector(Imaginary),
Math.Abs(Imaginary));
}
public string ToString(string format)
{
return String.Format("{0} {1} {2}i", Real.ToString(format),
RealImaginaryConnector(Imaginary),
Math.Abs(Imaginary).ToString(format));
}
public string ToString(IFormatProvider formatProvider)
{
return String.Format("{0} {1} {2}i", Real.ToString(formatProvider),
RealImaginaryConnector(Imaginary),
Math.Abs(Imaginary).ToString(formatProvider));
}
public string ToString(string format, IFormatProvider formatProvider)
{
return String.Format("{0} {1} {2}i", Real.ToString(format, formatProvider),
RealImaginaryConnector(Imaginary),
Math.Abs(Imaginary).ToString(format, formatProvider));
}
string RealImaginaryConnector(double value)
{
return Math.Sign(value) > 0 ? "+" : "\u2013";
}
}
}