1
1
// A major part of the Complex number implementation comes from
2
2
// [@xwu](https://github.com/xwu)'s project, [NumericAnnex](https://github.com/xwu/NumericAnnex).
3
3
4
- public struct Complex < T : FloatingPoint > {
5
- public var real : T
6
- public var imaginary : T
4
+ struct Complex < T : FloatingPoint > {
5
+ var real : T
6
+ var imaginary : T
7
7
8
8
@differentiable ( vjp: _vjpInit where T : Differentiable, T . TangentVector == T)
9
- public init ( real: T = 0 , imaginary: T = 0 ) {
9
+ init ( real: T = 0 , imaginary: T = 0 ) {
10
10
self . real = real
11
11
self . imaginary = imaginary
12
12
}
13
13
}
14
14
15
15
extension Complex : Differentiable where T : Differentiable {
16
- public typealias TangentVector = Complex
17
- public typealias AllDifferentiableVariables = Complex
16
+ typealias TangentVector = Complex
17
+ typealias AllDifferentiableVariables = Complex
18
18
}
19
19
20
20
extension Complex {
21
- public static var i : Complex {
21
+ static var i : Complex {
22
22
return Complex ( real: 0 , imaginary: 1 )
23
23
}
24
24
25
- public var isFinite : Bool {
25
+ var isFinite : Bool {
26
26
return real. isFinite && imaginary. isFinite
27
27
}
28
28
29
- public var isInfinite : Bool {
29
+ var isInfinite : Bool {
30
30
return real. isInfinite || imaginary. isInfinite
31
31
}
32
32
33
- public var isNaN : Bool {
33
+ var isNaN : Bool {
34
34
return ( real. isNaN && !imaginary. isInfinite) ||
35
35
( imaginary. isNaN && !real. isInfinite)
36
36
}
37
37
38
- public var isZero : Bool {
38
+ var isZero : Bool {
39
39
return real. isZero && imaginary. isZero
40
40
}
41
41
}
42
42
43
43
extension Complex : ExpressibleByIntegerLiteral {
44
- public init ( integerLiteral value: Int ) {
44
+ init ( integerLiteral value: Int ) {
45
45
self . real = T ( value)
46
46
self . imaginary = 0
47
47
}
48
48
}
49
49
50
50
extension Complex : CustomStringConvertible {
51
- public var description : String {
51
+ var description : String {
52
52
return real. isNaN && real. sign == . minus
53
53
? imaginary. sign == . minus
54
54
? " - \( - real) - \( - imaginary) i "
@@ -60,46 +60,46 @@ extension Complex : CustomStringConvertible {
60
60
}
61
61
62
62
extension Complex : Equatable {
63
- public static func == ( lhs: Complex , rhs: Complex ) -> Bool {
63
+ c static func == ( lhs: Complex , rhs: Complex ) -> Bool {
64
64
return lhs. real == rhs. real && lhs. imaginary == rhs. imaginary
65
65
}
66
66
}
67
67
68
68
extension Complex : AdditiveArithmetic {
69
69
@differentiable ( vjp: _vjpAdd ( lhs: rhs: ) where T : Differentiable)
70
- public static func + ( lhs: Complex , rhs: Complex ) -> Complex {
70
+ static func + ( lhs: Complex , rhs: Complex ) -> Complex {
71
71
var temp = lhs
72
72
temp += rhs
73
73
return temp
74
74
}
75
75
76
- public static func += ( lhs: inout Complex , rhs: Complex ) {
76
+ static func += ( lhs: inout Complex , rhs: Complex ) {
77
77
lhs. real += rhs. real
78
78
lhs. imaginary += rhs. imaginary
79
79
}
80
80
81
81
@differentiable ( vjp: _vjpSubtract ( lhs: rhs: ) where T : Differentiable)
82
- public static func - ( lhs: Complex , rhs: Complex ) -> Complex {
82
+ static func - ( lhs: Complex , rhs: Complex ) -> Complex {
83
83
var temp = lhs
84
84
temp -= rhs
85
85
return temp
86
86
}
87
87
88
- public static func -= ( lhs: inout Complex , rhs: Complex ) {
88
+ static func -= ( lhs: inout Complex , rhs: Complex ) {
89
89
lhs. real -= rhs. real
90
90
lhs. imaginary -= rhs. imaginary
91
91
}
92
92
}
93
93
94
94
extension Complex : Numeric {
95
- public init ? < U> ( exactly source: U ) where U : BinaryInteger {
95
+ init ? < U> ( exactly source: U ) where U : BinaryInteger {
96
96
guard let t = T ( exactly: source) else { return nil }
97
97
self . real = t
98
98
self . imaginary = 0
99
99
}
100
100
101
101
@differentiable ( vjp: _vjpMultiply ( lhs: rhs: ) where T : Differentiable)
102
- public static func * ( lhs: Complex , rhs: Complex ) -> Complex {
102
+ static func * ( lhs: Complex , rhs: Complex ) -> Complex {
103
103
var a = lhs. real, b = lhs. imaginary, c = rhs. real, d = rhs. imaginary
104
104
let ac = a * c, bd = b * d, ad = a * d, bc = b * c
105
105
let x = ac - bd
@@ -139,11 +139,11 @@ extension Complex : Numeric {
139
139
return Complex ( real: x, imaginary: y)
140
140
}
141
141
142
- public static func *= ( lhs: inout Complex , rhs: Complex ) {
142
+ static func *= ( lhs: inout Complex , rhs: Complex ) {
143
143
lhs = lhs * rhs
144
144
}
145
145
146
- public var magnitude : T {
146
+ var magnitude : T {
147
147
var x = abs ( real)
148
148
var y = abs ( imaginary)
149
149
if x. isInfinite { return x }
@@ -157,19 +157,19 @@ extension Complex : Numeric {
157
157
158
158
extension Complex : SignedNumeric {
159
159
@differentiable ( vjp: _vjpNegate where T : Differentiable)
160
- public static prefix func - ( operand: Complex ) -> Complex {
160
+ static prefix func - ( operand: Complex ) -> Complex {
161
161
return Complex ( real: - operand. real, imaginary: - operand. imaginary)
162
162
}
163
163
164
- public mutating func negate( ) {
164
+ mutating func negate( ) {
165
165
real. negate ( )
166
166
imaginary. negate ( )
167
167
}
168
168
}
169
169
170
170
extension Complex {
171
171
@differentiable ( vjp: _vjpDivide ( lhs: rhs: ) where T : Differentiable)
172
- public static func / ( lhs: Complex , rhs: Complex ) -> Complex {
172
+ static func / ( lhs: Complex , rhs: Complex ) -> Complex {
173
173
var a = lhs. real, b = lhs. imaginary, c = rhs. real, d = rhs. imaginary
174
174
var x : T
175
175
var y : T
@@ -204,27 +204,27 @@ extension Complex {
204
204
}
205
205
206
206
207
- public static func /= ( lhs: inout Complex , rhs: Complex ) {
207
+ static func /= ( lhs: inout Complex , rhs: Complex ) {
208
208
lhs = lhs / rhs
209
209
}
210
210
}
211
211
212
212
extension Complex {
213
213
@differentiable ( vjp: _vjpComplexConjugate where T : Differentiable)
214
- public func complexConjugate( ) -> Complex {
214
+ func complexConjugate( ) -> Complex {
215
215
return Complex ( real: real, imaginary: - imaginary)
216
216
}
217
217
}
218
218
219
- public func abs< T> ( _ z: Complex < T > ) -> Complex < T > {
219
+ func abs< T> ( _ z: Complex < T > ) -> Complex < T > {
220
220
return Complex ( real: z. magnitude)
221
221
}
222
222
223
223
extension Complex {
224
224
@differentiable ( vjp: _vjpAdding ( real: )
225
225
where T : Differentiable,
226
226
T . TangentVector == T)
227
- public func adding( real: T ) -> Complex {
227
+ func adding( real: T ) -> Complex {
228
228
var c = self
229
229
c. real += real
230
230
return c
@@ -233,7 +233,7 @@ extension Complex {
233
233
@differentiable ( vjp: _vjpSubtracting ( real: )
234
234
where T : Differentiable,
235
235
T . TangentVector == T)
236
- public func subtracting( real: T ) -> Complex {
236
+ func subtracting( real: T ) -> Complex {
237
237
var c = self
238
238
c. real -= real
239
239
return c
@@ -242,7 +242,7 @@ extension Complex {
242
242
@differentiable ( vjp: _vjpAdding ( imaginary: )
243
243
where T : Differentiable,
244
244
T . TangentVector == T)
245
- public func adding( imaginary: T ) -> Complex {
245
+ func adding( imaginary: T ) -> Complex {
246
246
var c = self
247
247
c. imaginary += imaginary
248
248
return c
@@ -251,7 +251,7 @@ extension Complex {
251
251
@differentiable ( vjp: _vjpSubtracting ( imaginary: )
252
252
where T : Differentiable,
253
253
T . TangentVector == T)
254
- public func subtracting( imaginary: T ) -> Complex {
254
+ func subtracting( imaginary: T ) -> Complex {
255
255
var c = self
256
256
c. imaginary -= imaginary
257
257
return c
0 commit comments