@@ -51,6 +51,7 @@ extension Complex: ElementaryFunctions {
51
51
/// Note that naive evaluation of this expression in floating-point would be prone to premature
52
52
/// overflow, since `cos` and `sin` both have magnitude less than 1 for most inputs (i.e.
53
53
/// `exp(x)` may be infinity when `exp(x) cos(y)` would not be.
54
+ @inlinable
54
55
public static func exp( _ z: Complex ) -> Complex {
55
56
guard z. isFinite else { return z }
56
57
// If x < log(greatestFiniteMagnitude), then exp(x) does not overflow.
@@ -65,6 +66,7 @@ extension Complex: ElementaryFunctions {
65
66
return Complex ( . cos( z. y) , . sin( z. y) ) . multiplied ( by: . exp( z. x) )
66
67
}
67
68
69
+ @inlinable
68
70
public static func expMinusOne( _ z: Complex ) -> Complex {
69
71
// exp(x + iy) - 1 = (exp(x) cos(y) - 1) + i exp(x) sin(y)
70
72
// -------- u --------
@@ -134,6 +136,7 @@ extension Complex: ElementaryFunctions {
134
136
// This function and sinh should stay in sync; if you make a
135
137
// modification here, you should almost surely make a parallel
136
138
// modification to sinh below.
139
+ @inlinable
137
140
public static func cosh( _ z: Complex ) -> Complex {
138
141
guard z. isFinite else { return z }
139
142
guard z. x. magnitude < - RealType. log ( . ulpOfOne) else {
@@ -163,6 +166,7 @@ extension Complex: ElementaryFunctions {
163
166
// sinh(x + iy) = sinh(x) cos(y) + i cosh(x) sinh(y)
164
167
//
165
168
// See cosh above for algorithm details.
169
+ @inlinable
166
170
public static func sinh( _ z: Complex ) -> Complex {
167
171
guard z. isFinite else { return z }
168
172
guard z. x. magnitude < - RealType. log ( . ulpOfOne) else {
@@ -178,6 +182,7 @@ extension Complex: ElementaryFunctions {
178
182
}
179
183
180
184
// tanh(z) = sinh(z) / cosh(z)
185
+ @inlinable
181
186
public static func tanh( _ z: Complex ) -> Complex {
182
187
guard z. isFinite else { return z }
183
188
// Note that when |x| is larger than -log(.ulpOfOne),
@@ -201,23 +206,27 @@ extension Complex: ElementaryFunctions {
201
206
}
202
207
203
208
// cos(z) = cosh(iz)
209
+ @inlinable
204
210
public static func cos( _ z: Complex ) -> Complex {
205
211
return cosh ( Complex ( - z. y, z. x) )
206
212
}
207
213
208
214
// sin(z) = -i*sinh(iz)
215
+ @inlinable
209
216
public static func sin( _ z: Complex ) -> Complex {
210
217
let w = sinh ( Complex ( - z. y, z. x) )
211
218
return Complex ( w. y, - w. x)
212
219
}
213
220
214
221
// tan(z) = -i*tanh(iz)
222
+ @inlinable
215
223
public static func tan( _ z: Complex ) -> Complex {
216
224
let w = tanh ( Complex ( - z. y, z. x) )
217
225
return Complex ( w. y, - w. x)
218
226
}
219
227
220
228
// MARK: - log-like functions
229
+ @inlinable
221
230
public static func log( _ z: Complex ) -> Complex {
222
231
// If z is zero or infinite, the phase is undefined, so the result is
223
232
// the single exceptional value.
@@ -317,6 +326,7 @@ extension Complex: ElementaryFunctions {
317
326
return Complex ( . log( onePlus: s) / 2 , θ)
318
327
}
319
328
329
+ @inlinable
320
330
public static func log( onePlus z: Complex ) -> Complex {
321
331
// If either |x| or |y| is bounded away from the origin, we don't need
322
332
// any extra precision, and can just literally compute log(1+z). Note
@@ -348,13 +358,15 @@ extension Complex: ElementaryFunctions {
348
358
return Complex ( . log( onePlus: s) / 2 , θ)
349
359
}
350
360
361
+ @inlinable
351
362
public static func acos( _ z: Complex ) -> Complex {
352
363
Complex (
353
364
2 * RealType. atan2 ( y: sqrt ( 1 - z) . real, x: sqrt ( 1 + z) . real) ,
354
365
RealType . asinh ( ( sqrt ( 1 + z) . conjugate * sqrt( 1 - z) ) . imaginary)
355
366
)
356
367
}
357
368
369
+ @inlinable
358
370
public static func asin( _ z: Complex ) -> Complex {
359
371
Complex (
360
372
RealType . atan2 ( y: z. x, x: ( sqrt ( 1 - z) * sqrt( 1 + z) ) . real) ,
@@ -363,11 +375,13 @@ extension Complex: ElementaryFunctions {
363
375
}
364
376
365
377
// atan(z) = -i*atanh(iz)
378
+ @inlinable
366
379
public static func atan( _ z: Complex ) -> Complex {
367
380
let w = atanh ( Complex ( - z. y, z. x) )
368
381
return Complex ( w. y, - w. x)
369
382
}
370
383
384
+ @inlinable
371
385
public static func acosh( _ z: Complex ) -> Complex {
372
386
Complex (
373
387
RealType . asinh ( ( sqrt ( z- 1 ) . conjugate * sqrt( z+ 1 ) ) . real) ,
@@ -376,11 +390,13 @@ extension Complex: ElementaryFunctions {
376
390
}
377
391
378
392
// asinh(z) = -i*asin(iz)
393
+ @inlinable
379
394
public static func asinh( _ z: Complex ) -> Complex {
380
395
let w = asin ( Complex ( - z. y, z. x) )
381
396
return Complex ( w. y, - w. x)
382
397
}
383
398
399
+ @inlinable
384
400
public static func atanh( _ z: Complex ) -> Complex {
385
401
// TODO: Kahan uses a much more complicated expression here; possibly
386
402
// simply because he didn't have a complex log(1+z) with good
@@ -395,10 +411,12 @@ extension Complex: ElementaryFunctions {
395
411
}
396
412
397
413
// MARK: - pow-like functions
414
+ @inlinable
398
415
public static func pow( _ z: Complex , _ w: Complex ) -> Complex {
399
416
return exp ( w * log( z) )
400
417
}
401
418
419
+ @inlinable
402
420
public static func pow( _ z: Complex , _ n: Int ) -> Complex {
403
421
if z. isZero { return . zero }
404
422
// TODO: this implementation is not quite correct, because n may be
@@ -411,6 +429,7 @@ extension Complex: ElementaryFunctions {
411
429
return exp ( log ( z) . multiplied ( by: RealType ( n) ) )
412
430
}
413
431
432
+ @inlinable
414
433
public static func sqrt( _ z: Complex ) -> Complex {
415
434
let lengthSquared = z. lengthSquared
416
435
if lengthSquared. isNormal {
@@ -439,6 +458,7 @@ extension Complex: ElementaryFunctions {
439
458
return Complex . sqrt ( z. divided ( by: scale) ) . multiplied ( by: . sqrt( scale) )
440
459
}
441
460
461
+ @inlinable
442
462
public static func root( _ z: Complex , _ n: Int ) -> Complex {
443
463
if z. isZero { return . zero }
444
464
// TODO: this implementation is not quite correct, because n may be
0 commit comments