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