@@ -2,7 +2,7 @@ public struct Complex<T : FloatingPoint> {
2
2
public var real : T
3
3
public var imaginary : T
4
4
5
- @ differentiable ( where T : Differentiable )
5
+ // TODO: Make differentiable, crashing right now
6
6
public init ( real: T = 0 , imaginary: T = 0 ) {
7
7
self . real = real
8
8
self . imaginary = imaginary
@@ -15,43 +15,43 @@ extension Complex : Differentiable where T : Differentiable {
15
15
}
16
16
17
17
extension Complex {
18
-
18
+
19
19
public static var i : Complex {
20
20
return Complex ( real: 0 , imaginary: 1 )
21
21
}
22
22
23
-
23
+
24
24
public var isFinite : Bool {
25
25
return real. isFinite && imaginary. isFinite
26
26
}
27
27
28
-
28
+
29
29
public var isInfinite : Bool {
30
30
return real. isInfinite || imaginary. isInfinite
31
31
}
32
32
33
-
33
+
34
34
public var isNaN : Bool {
35
35
return ( real. isNaN && !imaginary. isInfinite) ||
36
36
( imaginary. isNaN && !real. isInfinite)
37
37
}
38
38
39
-
39
+
40
40
public var isZero : Bool {
41
41
return real. isZero && imaginary. isZero
42
42
}
43
43
}
44
44
45
45
extension Complex : ExpressibleByIntegerLiteral {
46
-
46
+
47
47
public init ( integerLiteral value: Int ) {
48
48
self . real = T ( value)
49
49
self . imaginary = 0
50
50
}
51
51
}
52
52
53
53
extension Complex : CustomStringConvertible {
54
-
54
+
55
55
public var description : String {
56
56
return real. isNaN && real. sign == . minus
57
57
? imaginary. sign == . minus
@@ -64,36 +64,36 @@ extension Complex : CustomStringConvertible {
64
64
}
65
65
66
66
extension Complex : Equatable {
67
-
67
+
68
68
public static func == ( lhs: Complex , rhs: Complex ) -> Bool {
69
69
return lhs. real == rhs. real && lhs. imaginary == rhs. imaginary
70
70
}
71
71
}
72
72
73
73
extension Complex : AdditiveArithmetic {
74
-
74
+
75
75
@differentiable ( vjp: _vjpAdd ( lhs: rhs: ) where T : Differentiable)
76
76
public static func + ( lhs: Complex , rhs: Complex ) -> Complex {
77
- var lhs = lhs
78
- lhs += rhs
79
- return lhs
77
+ var temp = lhs
78
+ temp += rhs
79
+ return temp
80
80
}
81
81
82
-
82
+
83
83
public static func += ( lhs: inout Complex , rhs: Complex ) {
84
84
lhs. real += rhs. real
85
85
lhs. imaginary += rhs. imaginary
86
86
}
87
87
88
-
88
+
89
89
@differentiable ( vjp: _vjpSubtract ( lhs: rhs: ) where T : Differentiable)
90
90
public static func - ( lhs: Complex , rhs: Complex ) -> Complex {
91
- var lhs = lhs
92
- lhs -= rhs
93
- return lhs
91
+ var temp = lhs
92
+ temp -= rhs
93
+ return temp
94
94
}
95
95
96
-
96
+
97
97
public static func -= ( lhs: inout Complex , rhs: Complex ) {
98
98
lhs. real -= rhs. real
99
99
lhs. imaginary -= rhs. imaginary
@@ -107,7 +107,7 @@ extension Complex : Numeric {
107
107
self . imaginary = 0
108
108
}
109
109
110
-
110
+
111
111
@differentiable ( vjp: _vjpMultiply ( lhs: rhs: ) where T : Differentiable)
112
112
public static func * ( lhs: Complex , rhs: Complex ) -> Complex {
113
113
var a = lhs. real, b = lhs. imaginary, c = rhs. real, d = rhs. imaginary
@@ -149,12 +149,12 @@ extension Complex : Numeric {
149
149
return Complex ( real: x, imaginary: y)
150
150
}
151
151
152
-
152
+
153
153
public static func *= ( lhs: inout Complex , rhs: Complex ) {
154
154
lhs = lhs * rhs
155
155
}
156
156
157
-
157
+
158
158
public var magnitude : T {
159
159
var x = abs ( real)
160
160
var y = abs ( imaginary)
@@ -168,21 +168,21 @@ extension Complex : Numeric {
168
168
}
169
169
170
170
extension Complex : SignedNumeric {
171
-
171
+
172
172
@differentiable ( vjp: _vjpNegate where T : Differentiable)
173
173
public static prefix func - ( operand: Complex ) -> Complex {
174
174
return Complex ( real: - operand. real, imaginary: - operand. imaginary)
175
175
}
176
176
177
-
177
+
178
178
public mutating func negate( ) {
179
179
real. negate ( )
180
180
imaginary. negate ( )
181
181
}
182
182
}
183
183
184
184
extension Complex {
185
-
185
+
186
186
@differentiable ( vjp: _vjpDivide ( lhs: rhs: ) where T : Differentiable)
187
187
public static func / ( lhs: Complex , rhs: Complex ) -> Complex {
188
188
var a = lhs. real, b = lhs. imaginary, c = rhs. real, d = rhs. imaginary
@@ -218,51 +218,51 @@ extension Complex {
218
218
return Complex ( real: x, imaginary: y)
219
219
}
220
220
221
-
221
+
222
222
public static func /= ( lhs: inout Complex , rhs: Complex ) {
223
223
lhs = lhs / rhs
224
224
}
225
225
}
226
226
227
227
extension Complex {
228
-
228
+
229
229
@differentiable ( vjp: _vjpComplexConjugate where T : Differentiable)
230
230
public func complexConjugate( ) -> Complex {
231
231
return Complex ( real: real, imaginary: - imaginary)
232
232
}
233
233
}
234
234
235
-
235
+
236
236
public func abs< T> ( _ z: Complex < T > ) -> Complex < T > {
237
237
return Complex ( real: z. magnitude)
238
238
}
239
239
240
240
extension Complex {
241
-
241
+
242
242
@differentiable ( vjp: _vjpAdding ( real: ) where T : Differentiable, T . TangentVector == T)
243
243
public func adding( real: T ) -> Complex {
244
244
var c = self
245
245
c. real += real
246
246
return c
247
247
}
248
248
249
-
249
+
250
250
@differentiable ( vjp: _vjpSubtracting ( real: ) where T : Differentiable, T . TangentVector == T)
251
251
public func subtracting( real: T ) -> Complex {
252
252
var c = self
253
253
c. real -= real
254
254
return c
255
255
}
256
256
257
-
257
+
258
258
@differentiable ( vjp: _vjpAdding ( imaginary: ) where T : Differentiable, T . TangentVector == T)
259
259
public func adding( imaginary: T ) -> Complex {
260
260
var c = self
261
261
c. imaginary += imaginary
262
262
return c
263
263
}
264
-
265
-
264
+
265
+
266
266
@differentiable ( vjp: _vjpSubtracting ( imaginary: ) where T : Differentiable, T . TangentVector == T)
267
267
public func subtracting( imaginary: T ) -> Complex {
268
268
var c = self
@@ -273,25 +273,25 @@ extension Complex {
273
273
274
274
extension Complex where T : Differentiable {
275
275
@usableFromInline
276
- static func _vjpAdd( lhs: Complex , rhs: Complex )
276
+ static func _vjpAdd( lhs: Complex , rhs: Complex )
277
277
-> ( Complex , ( Complex ) -> ( Complex , Complex ) ) {
278
278
return ( lhs + rhs, { v in ( v, v) } )
279
279
}
280
280
281
281
@usableFromInline
282
- static func _vjpSubtract( lhs: Complex , rhs: Complex )
282
+ static func _vjpSubtract( lhs: Complex , rhs: Complex )
283
283
-> ( Complex , ( Complex ) -> ( Complex , Complex ) ) {
284
284
return ( lhs - rhs, { v in ( v, - v) } )
285
285
}
286
286
287
287
@usableFromInline
288
- static func _vjpMultiply( lhs: Complex , rhs: Complex )
288
+ static func _vjpMultiply( lhs: Complex , rhs: Complex )
289
289
-> ( Complex , ( Complex ) -> ( Complex , Complex ) ) {
290
290
return ( lhs * rhs, { v in ( rhs * v, lhs * v) } )
291
291
}
292
292
293
293
@usableFromInline
294
- static func _vjpDivide( lhs: Complex , rhs: Complex )
294
+ static func _vjpDivide( lhs: Complex , rhs: Complex )
295
295
-> ( Complex , ( Complex ) -> ( Complex , Complex ) ) {
296
296
return ( lhs / rhs, { v in ( v / rhs, - lhs / ( rhs * rhs) * v) } )
297
297
}
0 commit comments