-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathComplexMath.hx
162 lines (133 loc) · 3.63 KB
/
ComplexMath.hx
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
package deep.math;
/**
* ...
* @author deep <system.grand@gmail.com>
*/
class ComplexMath
{
@op("+", true) inline static public function add(a:Complex, b:Complex):Complex
{
return new Complex(a.re + b.re, a.im + b.im);
}
@op("+", true) inline static public function addFloat(a:Complex, b:Float):Complex
{
return new Complex(a.re + b, a.im);
}
@op("-", true) inline static public function sub(a:Complex, b:Complex):Complex
{
return new Complex(a.re - b.re, a.im - b.im);
}
@op("-x") inline static public function neg(a:Complex):Complex
{
return new Complex( -a.re, -a.im);
}
@op("-", false) inline static public function subFloat(a:Complex, b:Float):Complex
{
return new Complex(a.re - b, a.im);
}
@op("*", true) inline static public function mult(a:Complex, b:Complex):Complex
{
return new Complex(a.re * b.re - a.im * b.im, a.re * b.im + a.im * b.re);
}
@op("*", true) inline static public function multFloat(a:Complex, b:Float):Complex
{
return new Complex(a.re * b, a.im * b);
}
@op("/", true) inline static public function div(a:Complex, b:Complex):Complex
{
var div = 1 / abs2(b);
return new Complex((a.re * b.re + a.im * b.im) * div, (a.re * b.im + a.im * b.re) * div);
}
@op("/", false) inline static public function divFloat(a:Complex, b:Float):Complex
{
return new Complex(a.re / b, a.im / b);
}
@op("+=", true) inline static public function iadd(a:Complex, b:Complex):Complex
{
a.re += b.re;
a.im += b.im;
return a;
}
@op("+=", false) inline static public function iaddFloat(a:Complex, b:Float):Complex
{
a.re += b;
return a;
}
@op("-=", true) inline static public function isub(a:Complex, b:Complex):Complex
{
a.re -= b.re;
a.im -= b.im;
return a;
}
@op("-=", false) inline static public function isubFloat(a:Complex, b:Float):Complex
{
a.re -= b;
return a;
}
@op("*=", true) inline static public function imult(a:Complex, b:Complex):Complex
{
var are = a.re;
var bre = b.re;
a.re = are * bre - a.im * b.im;
a.im = are * b.im + a.im * bre;
return a;
}
@op("*=", false) inline static public function imultFloat(a:Complex, b:Float):Complex
{
a.re *= b;
a.im *= b;
return a;
}
@op("/=", true) inline static public function idiv(a:Complex, b:Complex):Complex
{
var are = a.re;
var bre = b.re;
var div = 1 / abs2(b);
a.re = (are * bre + a.im * b.im) * div;
a.im = (are * b.im + a.im * bre) * div;
return a;
}
@op("/=", false) inline static public function idivFloat(a:Complex, b:Float):Complex
{
a.re /= b;
a.im /= b;
return a;
}
@op("==", true) public static function eq(a:Complex, b:Complex):Bool
{
return a.re == b.re && a.im == b.im;
}
@op("==", true) public static function eqFloat(a:Complex, b:Float):Bool
{
return a.re == b && a.im == 0;
}
@op("!=", true) public static function notEq(a:Complex, b:Complex):Bool
{
return a.re != b.re || a.im != b.im;
}
@op("!=", true) public static function notEqFloat(a:Complex, b:Float):Bool
{
return a.re != b || a.im != 0;
}
@op("<<=") public static function clone(a:Complex, b:Complex):Complex
{
a.re = b.re;
a.im = b.im;
return a;
}
inline static public function sqr(c:Complex):Complex
{
var tim = 2 * c.re * c.im;
c.re = c.re * c.re - c.im * c.im;
c.im = tim;
return c;
}
inline static public function abs(c:Complex):Float
{
return Math.sqrt(c.re * c.re + c.im * c.im);
}
inline static public function abs2(c:Complex):Float
{
return c.re * c.re + c.im * c.im;
}
}