@@ -2,7 +2,6 @@ public struct Complex<T : FloatingPoint> {
22 public var real : T
33 public var imaginary : T
44
5- @differentiable ( where T : Differentiable)
65 public init ( real: T = 0 , imaginary: T = 0 ) {
76 self . real = real
87 self . imaginary = imaginary
@@ -15,43 +14,43 @@ extension Complex : Differentiable where T : Differentiable {
1514}
1615
1716extension Complex {
18-
17+
1918 public static var i : Complex {
2019 return Complex ( real: 0 , imaginary: 1 )
2120 }
2221
23-
22+
2423 public var isFinite : Bool {
2524 return real. isFinite && imaginary. isFinite
2625 }
2726
28-
27+
2928 public var isInfinite : Bool {
3029 return real. isInfinite || imaginary. isInfinite
3130 }
3231
33-
32+
3433 public var isNaN : Bool {
3534 return ( real. isNaN && !imaginary. isInfinite) ||
3635 ( imaginary. isNaN && !real. isInfinite)
3736 }
3837
39-
38+
4039 public var isZero : Bool {
4140 return real. isZero && imaginary. isZero
4241 }
4342}
4443
4544extension Complex : ExpressibleByIntegerLiteral {
46-
45+
4746 public init ( integerLiteral value: Int ) {
4847 self . real = T ( value)
4948 self . imaginary = 0
5049 }
5150}
5251
5352extension Complex : CustomStringConvertible {
54-
53+
5554 public var description : String {
5655 return real. isNaN && real. sign == . minus
5756 ? imaginary. sign == . minus
@@ -64,36 +63,36 @@ extension Complex : CustomStringConvertible {
6463}
6564
6665extension Complex : Equatable {
67-
66+
6867 public static func == ( lhs: Complex , rhs: Complex ) -> Bool {
6968 return lhs. real == rhs. real && lhs. imaginary == rhs. imaginary
7069 }
7170}
7271
7372extension Complex : AdditiveArithmetic {
74-
73+
7574 @differentiable ( vjp: _vjpAdd ( lhs: rhs: ) where T : Differentiable)
7675 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
8079 }
8180
82-
81+
8382 public static func += ( lhs: inout Complex , rhs: Complex ) {
8483 lhs. real += rhs. real
8584 lhs. imaginary += rhs. imaginary
8685 }
8786
88-
87+
8988 @differentiable ( vjp: _vjpSubtract ( lhs: rhs: ) where T : Differentiable)
9089 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
9493 }
9594
96-
95+
9796 public static func -= ( lhs: inout Complex , rhs: Complex ) {
9897 lhs. real -= rhs. real
9998 lhs. imaginary -= rhs. imaginary
@@ -107,7 +106,7 @@ extension Complex : Numeric {
107106 self . imaginary = 0
108107 }
109108
110-
109+
111110 @differentiable ( vjp: _vjpMultiply ( lhs: rhs: ) where T : Differentiable)
112111 public static func * ( lhs: Complex , rhs: Complex ) -> Complex {
113112 var a = lhs. real, b = lhs. imaginary, c = rhs. real, d = rhs. imaginary
@@ -149,12 +148,12 @@ extension Complex : Numeric {
149148 return Complex ( real: x, imaginary: y)
150149 }
151150
152-
151+
153152 public static func *= ( lhs: inout Complex , rhs: Complex ) {
154153 lhs = lhs * rhs
155154 }
156155
157-
156+
158157 public var magnitude : T {
159158 var x = abs ( real)
160159 var y = abs ( imaginary)
@@ -168,21 +167,21 @@ extension Complex : Numeric {
168167}
169168
170169extension Complex : SignedNumeric {
171-
170+
172171 @differentiable ( vjp: _vjpNegate where T : Differentiable)
173172 public static prefix func - ( operand: Complex ) -> Complex {
174173 return Complex ( real: - operand. real, imaginary: - operand. imaginary)
175174 }
176175
177-
176+
178177 public mutating func negate( ) {
179178 real. negate ( )
180179 imaginary. negate ( )
181180 }
182181}
183182
184183extension Complex {
185-
184+
186185 @differentiable ( vjp: _vjpDivide ( lhs: rhs: ) where T : Differentiable)
187186 public static func / ( lhs: Complex , rhs: Complex ) -> Complex {
188187 var a = lhs. real, b = lhs. imaginary, c = rhs. real, d = rhs. imaginary
@@ -218,51 +217,51 @@ extension Complex {
218217 return Complex ( real: x, imaginary: y)
219218 }
220219
221-
220+
222221 public static func /= ( lhs: inout Complex , rhs: Complex ) {
223222 lhs = lhs / rhs
224223 }
225224}
226225
227226extension Complex {
228-
227+
229228 @differentiable ( vjp: _vjpComplexConjugate where T : Differentiable)
230229 public func complexConjugate( ) -> Complex {
231230 return Complex ( real: real, imaginary: - imaginary)
232231 }
233232}
234233
235-
234+
236235public func abs< T> ( _ z: Complex < T > ) -> Complex < T > {
237236 return Complex ( real: z. magnitude)
238237}
239238
240239extension Complex {
241-
240+
242241 @differentiable ( vjp: _vjpAdding ( real: ) where T : Differentiable, T . TangentVector == T)
243242 public func adding( real: T ) -> Complex {
244243 var c = self
245244 c. real += real
246245 return c
247246 }
248247
249-
248+
250249 @differentiable ( vjp: _vjpSubtracting ( real: ) where T : Differentiable, T . TangentVector == T)
251250 public func subtracting( real: T ) -> Complex {
252251 var c = self
253252 c. real -= real
254253 return c
255254 }
256255
257-
256+
258257 @differentiable ( vjp: _vjpAdding ( imaginary: ) where T : Differentiable, T . TangentVector == T)
259258 public func adding( imaginary: T ) -> Complex {
260259 var c = self
261260 c. imaginary += imaginary
262261 return c
263262 }
264-
265-
263+
264+
266265 @differentiable ( vjp: _vjpSubtracting ( imaginary: ) where T : Differentiable, T . TangentVector == T)
267266 public func subtracting( imaginary: T ) -> Complex {
268267 var c = self
@@ -273,25 +272,25 @@ extension Complex {
273272
274273extension Complex where T : Differentiable {
275274 @usableFromInline
276- static func _vjpAdd( lhs: Complex , rhs: Complex )
275+ static func _vjpAdd( lhs: Complex , rhs: Complex )
277276 -> ( Complex , ( Complex ) -> ( Complex , Complex ) ) {
278277 return ( lhs + rhs, { v in ( v, v) } )
279278 }
280279
281280 @usableFromInline
282- static func _vjpSubtract( lhs: Complex , rhs: Complex )
281+ static func _vjpSubtract( lhs: Complex , rhs: Complex )
283282 -> ( Complex , ( Complex ) -> ( Complex , Complex ) ) {
284283 return ( lhs - rhs, { v in ( v, - v) } )
285284 }
286285
287286 @usableFromInline
288- static func _vjpMultiply( lhs: Complex , rhs: Complex )
287+ static func _vjpMultiply( lhs: Complex , rhs: Complex )
289288 -> ( Complex , ( Complex ) -> ( Complex , Complex ) ) {
290289 return ( lhs * rhs, { v in ( rhs * v, lhs * v) } )
291290 }
292291
293292 @usableFromInline
294- static func _vjpDivide( lhs: Complex , rhs: Complex )
293+ static func _vjpDivide( lhs: Complex , rhs: Complex )
295294 -> ( Complex , ( Complex ) -> ( Complex , Complex ) ) {
296295 return ( lhs / rhs, { v in ( v / rhs, - lhs / ( rhs * rhs) * v) } )
297296 }
0 commit comments