forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCmplx.cs
More file actions
176 lines (158 loc) · 5.15 KB
/
Cmplx.cs
File metadata and controls
176 lines (158 loc) · 5.15 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System;
using System.Runtime.CompilerServices;
namespace IronPythonTest {
public class Cmplx {
private double _r;
private double _i;
public Cmplx()
: this(0, 0) {
}
public Cmplx(double r)
: this(r, 0) {
}
public Cmplx(double r, double i) {
this._r = r;
this._i = i;
}
public override int GetHashCode() {
return _r.GetHashCode() ^ _i.GetHashCode();
}
public override bool Equals(object obj) {
if (obj is Cmplx) {
Cmplx o = (Cmplx)obj;
return o._r == _r && o._i == _i;
} else if (obj is IConvertible) {
double o = ((IConvertible)obj).ToDouble(null);
return o == _r && _i == 0;
}
return false;
}
public override string ToString() {
return String.Format("({0} + {1}i)", _r, _i);
}
public double Real {
get {
return _r;
}
}
public double Imag {
get {
return _i;
}
}
public static Cmplx operator *(double x, Cmplx y) {
return new Cmplx(x * y._r, x * y._i);
}
public static Cmplx operator *(Cmplx x, double y) {
return new Cmplx(x._r * y, x._i * y);
}
public static Cmplx operator *(Cmplx x, Cmplx y) {
return new Cmplx(x._r * y._r - x._i * y._i, x._r * y._i + x._i * y._r);
}
public static Cmplx operator /(double x, Cmplx y) {
return new Cmplx(x) / y;
}
public static Cmplx operator /(Cmplx x, double y) {
return new Cmplx(x._r / y, x._i / y);
}
public static Cmplx operator /(Cmplx x, Cmplx y) {
double div = y._r * y._r + y._i * y._i;
return new Cmplx((x._r * y._r + x._i * y._i) / div, (x._i * y._r - x._r * y._i) / div);
}
public static Cmplx operator +(double x, Cmplx y) {
return new Cmplx(x + y._r, y._i);
}
public static Cmplx operator +(Cmplx x, double y) {
return new Cmplx(x._r + y, x._i);
}
public static Cmplx operator +(Cmplx x, Cmplx y) {
return new Cmplx(x._r + y._r, x._i + y._i);
}
public static Cmplx operator -(double x, Cmplx y) {
return new Cmplx(x - y._r, -y._i);
}
public static Cmplx operator -(Cmplx x, double y) {
return new Cmplx(x._r - y, x._i);
}
public static Cmplx operator -(Cmplx x, Cmplx y) {
return new Cmplx(x._r - y._r, x._i - y._i);
}
public static Cmplx operator -(Cmplx x) {
return new Cmplx(-x._r, -x._i);
}
[SpecialName]
public static Cmplx op_MultiplicationAssignment(Cmplx x, double y) {
x._r *= y;
x._i *= y;
return x;
}
[SpecialName]
public static Cmplx op_MultiplicationAssignment(Cmplx x, Cmplx y) {
double r = x._r * y._r - x._i * y._i;
double i = x._r * y._i + x._i * y._r;
x._r = r;
x._i = i;
return x;
}
[SpecialName]
public static Cmplx op_SubtractionAssignment(Cmplx x, double y) {
x._r -= y;
return x;
}
[SpecialName]
public static Cmplx op_SubtractionAssignment(Cmplx x, Cmplx y) {
x._r -= y._r;
x._i -= y._i;
return x;
}
[SpecialName]
public static Cmplx op_AdditionAssignment(Cmplx x, double y) {
x._r += y;
return x;
}
[SpecialName]
public static Cmplx op_AdditionAssignment(Cmplx x, Cmplx y) {
x._r += y._r;
x._i += y._i;
return x;
}
[SpecialName]
public static Cmplx op_DivisionAssignment(Cmplx x, double y) {
x._r /= y;
x._i /= y;
return x;
}
[SpecialName]
public static Cmplx op_DivisionAssignment(Cmplx x, Cmplx y) {
double div = y._r * y._r + y._i * y._i;
double r = (x._r * y._r + x._i * y._i) / div;
double i = (x._i * y._r - x._r * y._i) / div;
x._r = r;
x._i = i;
return x;
}
}
public class Cmplx2 {
private double _r;
private double _i;
public Cmplx2()
: this(0, 0) {
}
public Cmplx2(double r)
: this(r, 0) {
}
public Cmplx2(double r, double i) {
this._r = r;
this._i = i;
}
public static Cmplx2 operator +(Cmplx y, Cmplx2 x) {
return new Cmplx2(x._r + y.Real, x._i + y.Imag);
}
public static Cmplx2 operator +(Cmplx2 x, Cmplx y) {
return new Cmplx2(x._r + y.Real, x._i + y.Imag);
}
}
}