-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMathBase.Complex.pas
More file actions
743 lines (651 loc) · 18.8 KB
/
Copy pathMathBase.Complex.pas
File metadata and controls
743 lines (651 loc) · 18.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
unit MathBase.Complex;
{-----------------------------------------------------------------------------
MathBase.Complex
Portable double-precision complex arithmetic for mathlib-fp.
The functions in this unit use principal values where a complex function is
multivalued. TComplex is a value type: arithmetic never mutates an operand.
-----------------------------------------------------------------------------}
{$mode objfpc}{$H+}{$J-}
{$modeswitch advancedrecords}
interface
uses
SysUtils, Math;
type
{ Single-precision complex scalar. Operations stay in Single precision;
callers must opt in to an explicit conversion when crossing precisions. }
TSingleComplex = record
Re: Single;
Im: Single;
class function Create(const ARe, AIm: Single): TSingleComplex; static;
class function Zero: TSingleComplex; static;
class function One: TSingleComplex; static;
class operator +(const A, B: TSingleComplex): TSingleComplex;
class operator -(const A, B: TSingleComplex): TSingleComplex;
class operator -(const A: TSingleComplex): TSingleComplex;
class operator *(const A, B: TSingleComplex): TSingleComplex;
class operator /(const A, B: TSingleComplex): TSingleComplex;
class operator =(const A, B: TSingleComplex): Boolean;
class operator <>(const A, B: TSingleComplex): Boolean;
function Conjugate: TSingleComplex;
function SqrMagnitude: Single;
function Magnitude: Single;
function IsFinite: Boolean;
end;
TSingleComplexArray = array of TSingleComplex;
TComplex = record
Re: Double;
Im: Double;
class function Create(const ARe, AIm: Double): TComplex; static;
class function FromPolar(const Radius, Angle: Double): TComplex; static;
class function Zero: TComplex; static;
class function One: TComplex; static;
class function ImaginaryUnit: TComplex; static;
class operator +(const A, B: TComplex): TComplex;
class operator +(const A: TComplex; const B: Double): TComplex;
class operator +(const A: Double; const B: TComplex): TComplex;
class operator -(const A, B: TComplex): TComplex;
class operator -(const A: TComplex; const B: Double): TComplex;
class operator -(const A: Double; const B: TComplex): TComplex;
class operator -(const A: TComplex): TComplex;
class operator *(const A, B: TComplex): TComplex;
class operator *(const A: TComplex; const B: Double): TComplex;
class operator *(const A: Double; const B: TComplex): TComplex;
class operator /(const A, B: TComplex): TComplex;
class operator /(const A: TComplex; const B: Double): TComplex;
class operator /(const A: Double; const B: TComplex): TComplex;
class operator =(const A, B: TComplex): Boolean;
class operator <>(const A, B: TComplex): Boolean;
function Conjugate: TComplex;
function SqrMagnitude: Double;
function Magnitude: Double;
function Argument: Double;
function IsFinite: Boolean;
end;
TComplexArray = array of TComplex;
function ToComplex(const Z: TSingleComplex): TComplex;
function ToSingleComplex(const Z: TComplex): TSingleComplex;
function CExp(const Z: TComplex): TComplex;
function CLog(const Z: TComplex): TComplex;
function CSqrt(const Z: TComplex): TComplex;
function CPow(const Base, Exponent: TComplex): TComplex;
function CPow(const Base: TComplex; const Exponent: Double): TComplex;
function CSin(const Z: TComplex): TComplex;
function CCos(const Z: TComplex): TComplex;
function CTan(const Z: TComplex): TComplex;
function CSinh(const Z: TComplex): TComplex;
function CCosh(const Z: TComplex): TComplex;
function CTanh(const Z: TComplex): TComplex;
function CAsin(const Z: TComplex): TComplex;
function CAcos(const Z: TComplex): TComplex;
function CAtan(const Z: TComplex): TComplex;
function CAsinh(const Z: TComplex): TComplex;
function CAcosh(const Z: TComplex): TComplex;
function CAtanh(const Z: TComplex): TComplex;
implementation
class function TSingleComplex.Create(const ARe, AIm: Single): TSingleComplex;
begin
Result.Re := ARe;
Result.Im := AIm;
end;
class function TSingleComplex.Zero: TSingleComplex;
begin
Result := Create(0.0, 0.0);
end;
class function TSingleComplex.One: TSingleComplex;
begin
Result := Create(1.0, 0.0);
end;
class operator TSingleComplex.+(const A, B: TSingleComplex): TSingleComplex;
begin
Result := Create(A.Re + B.Re, A.Im + B.Im);
end;
class operator TSingleComplex.-(const A, B: TSingleComplex): TSingleComplex;
begin
Result := Create(A.Re - B.Re, A.Im - B.Im);
end;
class operator TSingleComplex.-(const A: TSingleComplex): TSingleComplex;
begin
Result := Create(-A.Re, -A.Im);
end;
class operator TSingleComplex.*(const A, B: TSingleComplex): TSingleComplex;
begin
Result := Create(A.Re * B.Re - A.Im * B.Im,
A.Re * B.Im + A.Im * B.Re);
end;
class operator TSingleComplex./(const A, B: TSingleComplex): TSingleComplex;
var
Scale, BR, BI, AR, AI, Denominator: Double;
begin
if (B.Re = 0.0) and (B.Im = 0.0) then
Exit(Create(NaN, NaN));
if IsNan(A.Re) or IsNan(A.Im) or IsNan(B.Re) or IsNan(B.Im) then
Exit(Create(NaN, NaN));
if IsInfinite(B.Re) or IsInfinite(B.Im) then
begin
if A.IsFinite then
Exit(Zero)
else
Exit(Create(NaN, NaN));
end;
Scale := Max(Abs(B.Re), Abs(B.Im));
BR := B.Re / Scale;
BI := B.Im / Scale;
AR := A.Re / Scale;
AI := A.Im / Scale;
Denominator := BR * BR + BI * BI;
Result := Create((AR * BR + AI * BI) / Denominator,
(AI * BR - AR * BI) / Denominator);
end;
class operator TSingleComplex.=(const A, B: TSingleComplex): Boolean;
begin
Result := (A.Re = B.Re) and (A.Im = B.Im);
end;
class operator TSingleComplex.<>(const A, B: TSingleComplex): Boolean;
begin
Result := not (A = B);
end;
function TSingleComplex.Conjugate: TSingleComplex;
begin
Result := Create(Re, -Im);
end;
function TSingleComplex.SqrMagnitude: Single;
begin
Result := Re * Re + Im * Im;
end;
function TSingleComplex.Magnitude: Single;
var
X, Y, Ratio: Single;
begin
X := Abs(Re);
Y := Abs(Im);
if IsInfinite(X) or IsInfinite(Y) then
Exit(Infinity);
if IsNan(X) or IsNan(Y) then
Exit(NaN);
if X < Y then
begin
Ratio := X;
X := Y;
Y := Ratio;
end;
if X = 0.0 then
Exit(0.0);
Ratio := Y / X;
Result := X * Sqrt(1.0 + Ratio * Ratio);
end;
function TSingleComplex.IsFinite: Boolean;
begin
Result := not IsNan(Re) and not IsInfinite(Re) and
not IsNan(Im) and not IsInfinite(Im);
end;
function ToComplex(const Z: TSingleComplex): TComplex;
begin
Result := TComplex.Create(Z.Re, Z.Im);
end;
function ToSingleComplex(const Z: TComplex): TSingleComplex;
begin
if Z.IsFinite and
((Abs(Z.Re) > MaxSingle) or (Abs(Z.Im) > MaxSingle)) then
raise ERangeError.Create(
'ToSingleComplex: component is outside the finite Single range.');
Result := TSingleComplex.Create(Z.Re, Z.Im);
end;
function ComplexMagnitude(const ARe, AIm: Double): Double;
var
X, Y, Temp: Double;
begin
X := Abs(ARe);
Y := Abs(AIm);
{ Match hypot-style IEEE-754 behavior and avoid Inf / Inf invalid
operations in the scaled calculation below. }
if IsInfinite(X) or IsInfinite(Y) then
Exit(Infinity);
if IsNan(X) or IsNan(Y) then
Exit(NaN);
if X < Y then
begin
Temp := X;
X := Y;
Y := Temp;
end;
if X = 0.0 then
Exit(0.0);
Result := X * Sqrt(1.0 + Sqr(Y / X));
end;
function Log1PAccurate(const X: Double): Double;
var
Y: Double;
begin
Y := 1.0 + X;
if Y = 1.0 then
Exit(X);
Result := Ln(Y) * X / (Y - 1.0);
end;
function ComplexLogMagnitude(const ARe, AIm: Double): Double;
var
X, Y, Temp, Ratio: Double;
begin
X := Abs(ARe);
Y := Abs(AIm);
if IsInfinite(X) or IsInfinite(Y) then
Exit(Infinity);
if IsNan(X) or IsNan(Y) then
Exit(NaN);
if X < Y then
begin
Temp := X;
X := Y;
Y := Temp;
end;
if X = 0.0 then
Exit(-Infinity);
Ratio := Y / X;
Result := Ln(X) + 0.5 * Log1PAccurate(Ratio * Ratio);
end;
function RealArcSinhStable(const X: Double): Double;
var
AbsoluteX, Value: Double;
begin
if IsNan(X) or IsInfinite(X) then
Exit(X);
AbsoluteX := Abs(X);
if AbsoluteX < 1.0E-8 then
Exit(X)
else if AbsoluteX > 1.0E150 then
Value := Ln(AbsoluteX) + Ln(2.0)
else
Value := Log1PAccurate(AbsoluteX + AbsoluteX * AbsoluteX /
(1.0 + Sqrt(1.0 + AbsoluteX * AbsoluteX)));
if X < 0.0 then
Result := -Value
else
Result := Value;
end;
function RealArcCoshStable(const X: Double): Double;
begin
if IsNan(X) or (X < 1.0) then
Exit(NaN);
if IsInfinite(X) then
Exit(Infinity);
if X = 1.0 then
Exit(0.0);
if X > 1.0E150 then
Exit(Ln(X) + Ln(2.0));
Result := Log1PAccurate((X - 1.0) +
Sqrt((X - 1.0) * (X + 1.0)));
end;
function IsNegativeZero(const Value: Double): Boolean;
var
Bits: QWord;
begin
Bits := 0;
Move(Value, Bits, SizeOf(Value));
Result := (Value = 0.0) and ((Bits and QWord($8000000000000000)) <> 0);
end;
function ComplexNaN: TComplex;
begin
Result := TComplex.Create(NaN, NaN);
end;
function ComplexLog1P(const Z: TComplex): TComplex;
var
OnePlusReal, RadiusSquaredMinusOne: Double;
begin
if IsNan(Z.Re) or IsNan(Z.Im) then
Exit(ComplexNaN);
OnePlusReal := 1.0 + Z.Re;
if (Abs(Z.Re) < 0.5) and (Abs(Z.Im) < 0.5) then
begin
{ |1+z|^2 = 1 + 2 Re(z) + |z|^2. log1p retains the
first-order term when 1+Re(z) itself rounds to one. }
RadiusSquaredMinusOne := 2.0 * Z.Re + Z.Re * Z.Re + Z.Im * Z.Im;
Result.Re := 0.5 * Log1PAccurate(RadiusSquaredMinusOne);
end
else
Result.Re := Ln(ComplexMagnitude(OnePlusReal, Z.Im));
Result.Im := ArcTan2(Z.Im, OnePlusReal);
end;
class function TComplex.Create(const ARe, AIm: Double): TComplex;
begin
Result.Re := ARe;
Result.Im := AIm;
end;
class function TComplex.FromPolar(const Radius, Angle: Double): TComplex;
begin
Result.Re := Radius * Cos(Angle);
Result.Im := Radius * Sin(Angle);
end;
class function TComplex.Zero: TComplex;
begin
Result := Create(0.0, 0.0);
end;
class function TComplex.One: TComplex;
begin
Result := Create(1.0, 0.0);
end;
class function TComplex.ImaginaryUnit: TComplex;
begin
Result := Create(0.0, 1.0);
end;
class operator TComplex.+(const A, B: TComplex): TComplex;
begin
Result := Create(A.Re + B.Re, A.Im + B.Im);
end;
class operator TComplex.+(const A: TComplex; const B: Double): TComplex;
begin
Result := Create(A.Re + B, A.Im);
end;
class operator TComplex.+(const A: Double; const B: TComplex): TComplex;
begin
Result := Create(A + B.Re, B.Im);
end;
class operator TComplex.-(const A, B: TComplex): TComplex;
begin
Result := Create(A.Re - B.Re, A.Im - B.Im);
end;
class operator TComplex.-(const A: TComplex; const B: Double): TComplex;
begin
Result := Create(A.Re - B, A.Im);
end;
class operator TComplex.-(const A: Double; const B: TComplex): TComplex;
begin
Result := Create(A - B.Re, -B.Im);
end;
class operator TComplex.-(const A: TComplex): TComplex;
begin
Result := Create(-A.Re, -A.Im);
end;
class operator TComplex.*(const A, B: TComplex): TComplex;
begin
Result := Create(A.Re * B.Re - A.Im * B.Im,
A.Re * B.Im + A.Im * B.Re);
end;
class operator TComplex.*(const A: TComplex; const B: Double): TComplex;
begin
Result := Create(A.Re * B, A.Im * B);
end;
class operator TComplex.*(const A: Double; const B: TComplex): TComplex;
begin
Result := Create(A * B.Re, A * B.Im);
end;
class operator TComplex./(const A, B: TComplex): TComplex;
var
Scale, BR, BI, AR, AI, Denominator: Double;
begin
if IsNan(A.Re) or IsNan(A.Im) or IsNan(B.Re) or IsNan(B.Im) then
Exit(ComplexNaN);
if IsInfinite(B.Re) or IsInfinite(B.Im) then
begin
if A.IsFinite then
Exit(Create(0.0, 0.0))
else
Exit(ComplexNaN);
end;
Scale := Max(Abs(B.Re), Abs(B.Im));
if Scale = 0.0 then
begin
Result := Create(A.Re / Scale, A.Im / Scale);
Exit;
end;
BR := B.Re / Scale;
BI := B.Im / Scale;
AR := A.Re / Scale;
AI := A.Im / Scale;
Denominator := BR * BR + BI * BI;
Result := Create((AR * BR + AI * BI) / Denominator,
(AI * BR - AR * BI) / Denominator);
end;
class operator TComplex./(const A: TComplex; const B: Double): TComplex;
begin
Result := Create(A.Re / B, A.Im / B);
end;
class operator TComplex./(const A: Double; const B: TComplex): TComplex;
begin
Result := Create(A, 0.0) / B;
end;
class operator TComplex.=(const A, B: TComplex): Boolean;
begin
Result := (A.Re = B.Re) and (A.Im = B.Im);
end;
class operator TComplex.<>(const A, B: TComplex): Boolean;
begin
Result := not (A = B);
end;
function TComplex.Conjugate: TComplex;
begin
Result := Create(Re, -Im);
end;
function TComplex.SqrMagnitude: Double;
begin
Result := Re * Re + Im * Im;
end;
function TComplex.Magnitude: Double;
begin
Result := ComplexMagnitude(Re, Im);
end;
function TComplex.Argument: Double;
begin
if (Im = 0.0) and (Re < 0.0) then
begin
if IsNegativeZero(Im) then
Result := -Pi
else
Result := Pi;
end
else
Result := ArcTan2(Im, Re);
end;
function TComplex.IsFinite: Boolean;
begin
Result := not IsNan(Re) and not IsInfinite(Re) and
not IsNan(Im) and not IsInfinite(Im);
end;
function CExp(const Z: TComplex): TComplex;
var
Scale: Double;
begin
if IsNan(Z.Re) or IsNan(Z.Im) or IsInfinite(Z.Im) then
Exit(ComplexNaN);
if IsInfinite(Z.Re) and (Z.Re > 0.0) and (Z.Im = 0.0) then
Exit(TComplex.Create(Infinity, Z.Im));
Scale := Exp(Z.Re);
Result := TComplex.Create(Scale * Cos(Z.Im), Scale * Sin(Z.Im));
end;
function CLog(const Z: TComplex): TComplex;
begin
Result := TComplex.Create(ComplexLogMagnitude(Z.Re, Z.Im), Z.Argument);
end;
function CSqrt(const Z: TComplex): TComplex;
var
M, T: Double;
begin
if IsInfinite(Z.Im) then
Exit(TComplex.Create(Infinity, Z.Im));
if IsInfinite(Z.Re) then
begin
if Z.Re > 0.0 then
Exit(TComplex.Create(Infinity, Z.Im * 0.0));
if (Z.Im < 0.0) or IsNegativeZero(Z.Im) then
Exit(TComplex.Create(0.0, -Infinity))
else
Exit(TComplex.Create(0.0, Infinity));
end;
if IsNan(Z.Re) or IsNan(Z.Im) then
Exit(ComplexNaN);
if Z.Im = 0.0 then
begin
if Z.Re >= 0.0 then
Exit(TComplex.Create(Sqrt(Z.Re), Z.Im))
else if IsNegativeZero(Z.Im) then
Exit(TComplex.Create(0.0, -Sqrt(-Z.Re)))
else
Exit(TComplex.Create(0.0, Sqrt(-Z.Re)));
end;
M := Z.Magnitude;
if Z.Re >= 0.0 then
begin
T := Sqrt(0.5 * (M + Z.Re));
Result := TComplex.Create(T, Z.Im / (2.0 * T));
end
else
begin
T := Sqrt(0.5 * (M - Z.Re));
Result := TComplex.Create(Abs(Z.Im) / (2.0 * T), Sign(Z.Im) * T);
end;
end;
function CPow(const Base, Exponent: TComplex): TComplex;
begin
Result := CExp(Exponent * CLog(Base));
end;
function CPow(const Base: TComplex; const Exponent: Double): TComplex;
begin
Result := CPow(Base, TComplex.Create(Exponent, 0.0));
end;
function StableSinh(const X: Double): Double; inline;
var
X2: Double;
begin
{ Some RTL targets evaluate sinh as a subtraction of exponentials. The
short odd series preserves the imaginary perturbations used by explicit
complex-step differentiation. }
if Abs(X) < 1E-4 then
begin
X2 := X * X;
Result := X * (1.0 + X2 * (1.0 / 6.0 +
X2 * (1.0 / 120.0 + X2 / 5040.0)));
end
else
Result := Sinh(X);
end;
function CSin(const Z: TComplex): TComplex;
begin
Result := TComplex.Create(Sin(Z.Re) * Cosh(Z.Im),
Cos(Z.Re) * StableSinh(Z.Im));
end;
function CCos(const Z: TComplex): TComplex;
begin
Result := TComplex.Create(Cos(Z.Re) * Cosh(Z.Im),
-Sin(Z.Re) * StableSinh(Z.Im));
end;
function CTan(const Z: TComplex): TComplex;
begin
Result := CSin(Z) / CCos(Z);
end;
function CSinh(const Z: TComplex): TComplex;
begin
Result := TComplex.Create(StableSinh(Z.Re) * Cos(Z.Im),
Cosh(Z.Re) * Sin(Z.Im));
end;
function CCosh(const Z: TComplex): TComplex;
begin
Result := TComplex.Create(Cosh(Z.Re) * Cos(Z.Im),
StableSinh(Z.Re) * Sin(Z.Im));
end;
function CTanh(const Z: TComplex): TComplex;
begin
Result := CSinh(Z) / CCosh(Z);
end;
function CAsin(const Z: TComplex): TComplex;
var
IUnit: TComplex;
begin
IUnit := TComplex.ImaginaryUnit;
Result := -IUnit * CAsinh(IUnit * Z);
end;
function CAcos(const Z: TComplex): TComplex;
begin
Result := TComplex.Create(Pi / 2.0, 0.0) - CAsin(Z);
end;
function CAtan(const Z: TComplex): TComplex;
var
IUnit: TComplex;
begin
IUnit := TComplex.ImaginaryUnit;
Result := -IUnit * CAtanh(IUnit * Z);
end;
function CAsinh(const Z: TComplex): TComplex;
const
LargeThreshold = 1.0E150;
var
A, AbsB, B, Cosine, MaxComponent, RMinus, RPlus, RealPart: Double;
begin
if IsNan(Z.Re) or IsNan(Z.Im) then
Exit(ComplexNaN);
MaxComponent := Max(Abs(Z.Re), Abs(Z.Im));
if MaxComponent >= LargeThreshold then
begin
{ asinh(z) ~ log(2z). Reflect through the origin on the left
half-plane so the principal branch and signed-zero side are kept. }
if (Z.Re < 0.0) or IsNegativeZero(Z.Re) then
Exit(-(CLog(-Z) + Ln(2.0)))
else
Exit(CLog(Z) + Ln(2.0));
end;
if Z.Re = 0.0 then
begin
{ The imaginary axis outside [-i,i] is the asinh branch cut. Handle it
explicitly so signed zero selects the requested side. }
A := Abs(Z.Im);
if A <= 1.0 then
Exit(TComplex.Create(Z.Re, ArcSin(Z.Im)));
RealPart := RealArcCoshStable(A);
if IsNegativeZero(Z.Re) then
RealPart := -RealPart;
if Z.Im < 0.0 then
Exit(TComplex.Create(RealPart, -Pi / 2.0))
else
Exit(TComplex.Create(RealPart, Pi / 2.0));
end;
{ Let A = (|z+i| + |z-i|)/2 = cosh(Re(asinh(z))) and
B = Im(z)/A = sin(Im(asinh(z))). This component form avoids z*z,
whose equal large components can cancel differently across targets. }
RPlus := ComplexMagnitude(Z.Re, Z.Im + 1.0);
RMinus := ComplexMagnitude(Z.Re, Z.Im - 1.0);
A := 0.5 * RPlus + 0.5 * RMinus;
if A < 1.0 then
A := 1.0;
B := Z.Im / A;
if B > 1.0 then
B := 1.0
else if B < -1.0 then
B := -1.0;
AbsB := Abs(B);
Cosine := Sqrt(Max(0.0, (1.0 - AbsB) * (1.0 + AbsB)));
if Cosine = 0.0 then
begin
RealPart := RealArcCoshStable(A);
if Z.Re < 0.0 then
RealPart := -RealPart;
end
else
RealPart := RealArcSinhStable(Z.Re / Cosine);
Result := TComplex.Create(RealPart, ArcSin(B));
end;
function CAcosh(const Z: TComplex): TComplex;
begin
if IsNan(Z.Re) or IsNan(Z.Im) then
Exit(ComplexNaN);
{ This equivalent principal-value formula avoids multiplying two square
roots, which can overflow even when acosh(z) is representable. }
Result := 2.0 * CLog(CSqrt(0.5 * (Z + TComplex.One)) +
CSqrt(0.5 * (Z - TComplex.One)));
end;
function CAtanh(const Z: TComplex): TComplex;
const
AsymptoticThreshold = 1.0E8;
var
BranchImaginary: Double;
begin
if IsNan(Z.Re) or IsNan(Z.Im) then
Exit(ComplexNaN);
if Max(Abs(Z.Re), Abs(Z.Im)) >= AsymptoticThreshold then
begin
if (Z.Im < 0.0) or IsNegativeZero(Z.Im) then
BranchImaginary := -Pi / 2.0
else
BranchImaginary := Pi / 2.0;
Result := TComplex.One / Z + TComplex.Create(0.0, BranchImaginary);
Exit;
end;
Result := 0.5 * (ComplexLog1P(Z) - ComplexLog1P(-Z));
end;
end.