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