This repository was archived by the owner on Aug 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmp_cmplx.pas
2491 lines (2153 loc) · 67.2 KB
/
mp_cmplx.pas
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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit mp_cmplx;
{Multi precision complex floating point arithmetic routines}
interface
{$i STD.INC}
{$ifdef BIT16}
{$N+}
{$endif}
uses
BTypes, mp_types;
{$i mp_conf.inc}
(*************************************************************************
DESCRIPTION : Multi precision complex floating point arithmetic routines
REQUIREMENTS : BP7, D1-D7/D9-D10/D12/D17-D18, FPC, VP
EXTERNAL DATA : (mp_types)
MEMORY USAGE : heap
DISPLAY MODE : ---
REFERENCES : [41] [HMF]: M. Abramowitz, I.A. Stegun. Handbook of Mathematical
Functions. New York, 1970, http://www.math.sfu.ca/~cbm/aands/
[42] W. Kahan, "Branch Cuts for Complex Elementary Functions, or Much Ado
About Nothing's Sign Bit", in The State of Art in Numerical Analysis,
ed. by A. Iserles and M.J.D. Powell, 1987, pp. 165-211.
Available as http://people.freebsd.org/~das/kahan86branch.pdf
Version Date Author Modification
------- -------- ------- ------------------------------------------
0.00.10 15.12.13 W.Ehrhardt Basic type definitions, mpc_init[*]
0.00.11 16.12.13 we mpc_clear[*]
0.00.12 02.01.14 we mpc_set0/1/i/dbl/ext/mpf
0.00.13 02.01.14 we mpc_setp_mpf
0.00.14 08.02.14 we mpc_add*, mpc_sub*, mpc_chs
0.00.15 09.02.14 we mpc_abs/2, mpc_mul*, mpc_div*
0.00.16 10.02.14 we mpc_checksum, mpc_copy/p, mpc_is0/1/i
0.00.16 10.02.14 we mpc_checksum, mpc_copy/p, mpc_is0/1/i
0.00.17 11.02.14 we mpc_sqrt, mpc_sqr
0.00.18 11.02.14 we mpc_arg, mpc_ln
0.00.19 11.02.14 we mpc_cis, mpc_exp
0.00.20 12.02.14 we mpc_sin/cos/sincos
0.00.21 12.02.14 we mpc_sinh/cosh/sincosh
0.00.21 13.02.14 we mpc_coth, mpc_tanh
0.00.22 13.02.14 we mpc_pow, mpc_cot, mpc_tan
0.00.23 14.02.14 we mpc_arctan/h
0.00.24 15.02.14 we mpc_arccos/h, mpc_arcsin/h
0.00.25 16.02.14 we mpc_nroot/1
0.00.26 18.02.14 we mpc_is1a, improved mpc_coth
1.27.00 19.02.14 we mp_complex types moved to unit mp_types
1.28.00 31.03.14 we mpc_csc/h, mpc_sec/h
1.28.01 01.04.14 we mpc_arccot/c
1.28.02 01.04.14 we mpc_arccoth/c
1.28.03 02.04.14 we mpc_arcsec/h
1.28.04 02.04.14 we mpc_arccsc/h
1.28.05 03.04.14 we fix mpc_tanh: use s_mpf_ldx(a.re)
1.31.00 24.11.14 we mpc_agm1
1.31.01 25.11.14 we mpc_agm
1.33.00 09.10.16 we mpc_exp2, mpc_exp10
1.33.01 13.10.16 we fix arg check in mpc_agm1
1.33.02 14.10.16 we mpc_coth without vers()
1.33.03 14.10.16 we mpc_ln1p
1.33.04 15.10.16 we mpc_expm1
1.38.00 26.06.18 we mpc_??_ext changed to mpc_??_dbl
1.39.00 12.11.18 we mpc_log10
1.39.01 13.11.18 we mpc_is_ia
1.39.02 13.11.18 we fix mpc_ln1p for small imaginary a
1.39.03 14.11.18 we fix mpc_expm1 for small imaginary a
1.39.04 14.11.18 we fix mpc_arctanh for real |a| > 1
1.39.05 16.11.18 we mpc_arctanh: adjust sign on the branch cut
**************************************************************************)
(*-------------------------------------------------------------------------
(C) Copyright 2013-2018 Wolfgang Ehrhardt
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from
the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in
a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
----------------------------------------------------------------------------*)
procedure mpc_abs(const a: mp_complex; var b: mp_float);
{-Calculate the absolute value, b = |a|}
procedure mpc_abs2(const a: mp_complex; var b: mp_float);
{-Calculate the squared absolute value, b = |a|^2}
procedure mpc_add(const a,b: mp_complex; var c: mp_complex);
{-Calculate c = a+b}
procedure mpc_add_dbl(const a: mp_complex; b: double; var c: mp_complex);
{-Calculate c = a+b}
procedure mpc_add_mpf(const a: mp_complex; const b: mp_float; var c: mp_complex);
{-Calculate c = a+b}
procedure mpc_agm(const x,y: mp_complex; var w: mp_complex);
{-Calculate the 'optimal' arithmetic-geometric mean w = AGM(x,y)}
procedure mpc_agm1(const z: mp_complex; var w: mp_complex);
{-Calculate the 'optimal' arithmetic-geometric mean w = AGM(1,z)}
procedure mpc_arccos(const a: mp_complex; var b: mp_complex);
{-Calculate the principal value of the complex inverse circular cosine b = arccos(a)}
procedure mpc_arccosh(const a: mp_complex; var b: mp_complex);
{-Calculate the principal value of the complex inverse hyperbolic cosine b = arccosh(a)}
procedure mpc_arccot(const a: mp_complex; var b: mp_complex);
{-Calculate the principal value of the complex inverse circular cotangent b = arccot(a) = arctan(1/a)}
procedure mpc_arccotc(const a: mp_complex; var b: mp_complex);
{-Calculate the principal value of the complex inverse circular cotangent b = arccotc(a) = Pi/2 - arctan(a)}
procedure mpc_arccoth(const a: mp_complex; var b: mp_complex);
{-Calculate the principal value of the complex inverse hyperbolic cotangent b = arccoth(a) = arctanh(1/a)}
procedure mpc_arccothc(const a: mp_complex; var b: mp_complex);
{-Calculate the principal value of the complex inverse hyperbolic cotangent b = arccothc(a) = arctanh(a) + i*Pi/2}
procedure mpc_arccsc(const a: mp_complex; var b: mp_complex);
{-Calculate the principal value of the complex inverse circular cosecant b = arccsc(a) = arcsin(1/a)}
procedure mpc_arccsch(const a: mp_complex; var b: mp_complex);
{-Calculate the principal value of the complex inverse hyperbolic cosecant b = arccsch(a) = arcsinh(1/a)}
procedure mpc_arcsec(const a: mp_complex; var b: mp_complex);
{-Calculate the principal value of the complex inverse circular secant b = arcssec(a) = arccos(1/a)}
procedure mpc_arcsech(const a: mp_complex; var b: mp_complex);
{-Calculate the principal value of the complex inverse hyperbolic secant b = arcssech(a) = arccosh(1/a)}
procedure mpc_arcsin(const a: mp_complex; var b: mp_complex);
{-Calculate the principal value of the complex inverse circular sine b = arcsin(a)}
procedure mpc_arcsinh(const a: mp_complex; var b: mp_complex);
{-Calculate the principal value of the complex inverse hyperbolic sine b = arcsinh(a)}
procedure mpc_arctan(const a: mp_complex; var b: mp_complex);
{-Calculate the principal value of the complex inverse circular tangent w = arctan(z)}
procedure mpc_arctanh(const a: mp_complex; var b: mp_complex);
{-Calculate the principal value of the complex inverse hyperbolic tangent b = arctanh(a)}
procedure mpc_arg(const a: mp_complex; var b: mp_float);
{-Calculate the principle value of the argument: b = arg(a) = arctan2(a.im, a.re)}
function mpc_checksum(const a: mp_complex): longint;
{-Return a checksum for a, -1 if mp_error<>MP_OKAY, -2 if not initialized}
procedure mpc_chs(const a: mp_complex; var b: mp_complex);
{-Change sign, b = -a}
procedure mpc_cis(const x: mp_float; var a: mp_complex);
{-Calculate a = exp(i*x) = cos(x) + i*sin(x)}
procedure mpc_clear(var a: mp_complex);
{-Clear an mp_complex}
procedure mpc_clear2(var a,b: mp_complex);
{-Clear 2 mp_complex}
procedure mpc_clear3(var a,b,c: mp_complex);
{-Clear 3 mp_complex}
procedure mpc_clear4(var a,b,c,d: mp_complex);
{-Clear 4 mp_complex}
procedure mpc_clear5(var a,b,c,d,e: mp_complex);
{-Clear 5 mp_complex}
procedure mpc_conj(const a: mp_complex; var b: mp_complex);
{-Return the complex conjugate b = a.re - i*a.im}
procedure mpc_copy(const a: mp_complex; var b: mp_complex);
{-Copy a to b including bitprecs}
procedure mpc_copyp(const a: mp_complex; var b: mp_complex);
{-Copy a to b, preserve b's bitprecs}
procedure mpc_cos(const a: mp_complex; var b: mp_complex);
{-Calculate b = cos(a)}
procedure mpc_cosh(const a: mp_complex; var b: mp_complex);
{-Calculate b = cosh(a)}
procedure mpc_cot(const a: mp_complex; var b: mp_complex);
{-Calculate b = cot(a)}
procedure mpc_coth(const a: mp_complex; var b: mp_complex);
{-Calculate b = coth(a)}
procedure mpc_csc(const a: mp_complex; var b: mp_complex);
{-Calculate the complex circular cosecant b = csc(a) = 1/sin(a)}
procedure mpc_csch(const a: mp_complex; var b: mp_complex);
{-Calculate the complex hyperbolic cosecant b = csch(a) = 1/sinh(a)}
procedure mpc_div(const a,b: mp_complex; var c: mp_complex);
{-Calculate c = a/b}
procedure mpc_div_dbl(const a: mp_complex; b: double; var c: mp_complex);
{-Calculate c = a/b}
procedure mpc_div_mpf(const a: mp_complex; const b: mp_float; var c: mp_complex);
{-Calculate c = a/b}
procedure mpc_exch(var a,b: mp_complex);
{-Exchange two mp_complexes (including bitprec)}
procedure mpc_exp(const a: mp_complex; var b: mp_complex);
{-Calculate b = exp(a)}
procedure mpc_exp2(const a: mp_complex; var b: mp_complex);
{-Calculate b = 2^a = exp(a*ln(2))}
procedure mpc_exp10(const a: mp_complex; var b: mp_complex);
{-Calculate b = 10^a = exp(a*ln(10))}
procedure mpc_expm1(const a: mp_complex; var b: mp_complex);
{-Calculate b = exp(a)-1}
procedure mpc_init(var a: mp_complex);
{-Initialize an mp_complex with default precision}
procedure mpc_init2(var a,b: mp_complex);
{-Initialize two mp_complexes with default precision}
procedure mpc_init3(var a,b,c: mp_complex);
{-Initialize 3 mp_complexes with default precision}
procedure mpc_init4(var a,b,c,d: mp_complex);
{-Initialize 4 mp_complexes with default precision}
procedure mpc_init5(var a,b,c,d,e: mp_complex);
{-Initialize 5 mp_complexes with default precision}
procedure mpc_initp(var a: mp_complex; prec: longint);
{-Initialize an mp_complex with bit precision prec}
procedure mpc_initp2(var a,b: mp_complex; prec: longint);
{-Initialize two mp_complexes with bit precision prec}
procedure mpc_initp3(var a,b,c: mp_complex; prec: longint);
{-Initialize 3 mp_complexes with bit precision prec}
procedure mpc_initp4(var a,b,c,d: mp_complex; prec: longint);
{-Initialize 4 mp_complexes with bit precision prec}
procedure mpc_initp5(var a,b,c,d,e: mp_complex; prec: longint);
{-Initialize 5 mp_complexes with bit precision prec}
procedure mpc_initp_multi_p(var pv: array of pmp_complex; prec: longint);
{-Initialize with bit precision prec a list of mp_complexes given as a pointer}
{ vector; on error the already initialized mp_complexes will be cleared}
procedure mpc_inv(const a: mp_complex; var b: mp_complex);
{-Calculate b = 1/a}
function mpc_is0(const a: mp_complex): boolean;
{-Return true if a=0}
function mpc_is1(const a: mp_complex): boolean;
{-Return true if a=1}
function mpc_is1a(const a: mp_complex): boolean;
{-Return true if a = +1 or -1}
function mpc_is_i(const a: mp_complex): boolean;
{-Return true if a=I}
function mpc_is_ia(const a: mp_complex): boolean;
{-Return true if a=I or a= -I}
procedure mpc_ln(const a: mp_complex; var b: mp_complex);
{-Calculate the natural logarithm b = ln(a); principal branch ln(|a|) + i*arg(a), accurate near |a|=1}
procedure mpc_ln1p(const a: mp_complex; var b: mp_complex);
{-Calculate the natural logarithm b = ln(1+a)}
procedure mpc_log10(const a: mp_complex; var b: mp_complex);
{-Calculate the principal branch of the base 10 logarithm of a, b=ln(z)/ln(10)}
procedure mpc_mul(const a,b: mp_complex; var c: mp_complex);
{-Calculate c = a*b}
procedure mpc_mul_2k(const a: mp_complex; k: longint; var b: mp_complex);
{-Calculate b = a*2^k}
procedure mpc_mul_dbl(const a: mp_complex; b: double; var c: mp_complex);
{-Calculate c = a*b}
procedure mpc_mul_mpf(const a: mp_complex; const b: mp_float; var c: mp_complex);
{-Calculate c = a*b}
function mpc_not_init(const a: mp_complex): boolean;
{-Sanity check if a is initialized, does not catch all cases!}
procedure mpc_pow(const a,b: mp_complex; var c: mp_complex);
{-Calculate the principal value of the complex power c = a^b = exp(b*ln(a))}
procedure mpc_nroot(const a: mp_complex; n: longint; var b: mp_complex);
{-Calculate the nth principal root b = a^(1/n) = exp(ln(a)/n)}
procedure mpc_nroot1(n: longint; var a: mp_complex);
{-Calculate the principal nth root of unity a = exp(2*Pi*i/n)}
procedure mpc_sec(const a: mp_complex; var b: mp_complex);
{-Calculate the complex circular secant b = sec(a) = 1/cos(a)}
procedure mpc_sech(const a: mp_complex; var b: mp_complex);
{-Calculate the complex hyperbolic secant b = sech(a) = 1/cosh(a)}
procedure mpc_set0(var a: mp_complex);
{-Set a=0}
procedure mpc_set1(var a: mp_complex);
{-Set a=1}
procedure mpc_seti(var a: mp_complex);
{-Set a=i}
procedure mpc_set_dbl(var a: mp_complex; x,y: double);
{-Set a = x + iy with x,y double, error if x,y are NAN or INF}
{$ifndef EXT64}
procedure mpc_set_ext(var a: mp_complex; x,y: extended);
{-Set a = x + iy with x,y extended, error if x,y are NAN or INF}
{$endif}
procedure mpc_set_mpf(var a: mp_complex; const x,y: mp_float);
{-Set a = x + iy}
procedure mpc_setp_mpf(var a: mp_complex; const x,y: mp_float);
{-Set a = x + iy, bitprecs of a are preserved}
procedure mpc_sin(const a: mp_complex; var b: mp_complex);
{-Calculate b = sin(a)}
procedure mpc_sincos(const a: mp_complex; var s,c: mp_complex);
{-Calculate s = sin(a), c = cos(a)}
procedure mpc_sinh(const a: mp_complex; var b: mp_complex);
{-Calculate b = sinh(a)}
procedure mpc_sinhcosh(const a: mp_complex; var s,c: mp_complex);
{-Calculate s = sinh(a), c = cosh(a)}
procedure mpc_sqr(const a: mp_complex; var b: mp_complex);
{-Calculate b = a*a}
procedure mpc_sqrt(const a: mp_complex; var b: mp_complex);
{-Calculate the principal square root b = sqrt(a)}
procedure mpc_sub(const a,b: mp_complex; var c: mp_complex);
{-Calculate c = a-b}
procedure mpc_sub_dbl(const a: mp_complex; b: double; var c: mp_complex);
{-Calculate c = a-b}
procedure mpc_sub_mpf(const a: mp_complex; const b: mp_float; var c: mp_complex);
{-Calculate c = a-b}
procedure mpc_tan(const a: mp_complex; var b: mp_complex);
{-Calculate b = tan(a)}
procedure mpc_tanh(const a: mp_complex; var b: mp_complex);
{-Calculate b = tanh(a)}
procedure mpc_xdivc(a: double; const b: mp_complex; var c: mp_complex);
{-Calculate c = a/b}
procedure s_mpc_mul(const a,b: mp_complex; bc: boolean; var c: mp_complex);
{-Calculate c = a*b or c=a*conj(b) if bc}
implementation
uses
mp_base, mp_real;
{---------------------------------------------------------------------------}
procedure mpc_abs(const a: mp_complex; var b: mp_float);
{-Calculate the absolute value, b = |a|}
begin
mpf_hypot(a.re, a.im, b);
end;
{---------------------------------------------------------------------------}
procedure mpc_abs2(const a: mp_complex; var b: mp_float);
{-Calculate the squared absolute value, b = |a|^2}
var
x: mp_float;
begin
{$ifdef MPC_ArgCheck}
_CheckBitPrec(b);
{$endif}
mpf_initp(x,b.bitprec);
if mp_error=MP_OKAY then begin
mpf_sqr(a.re,x);
mpf_sqr(a.im,b);
mpf_add(x,b,b);
mpf_clear(x);
end;
end;
{---------------------------------------------------------------------------}
procedure mpc_add(const a,b: mp_complex; var c: mp_complex);
{-Calculate c = a+b}
begin
mpf_add(a.re, b.re, c.re);
mpf_add(a.im, b.im, c.im);
end;
{---------------------------------------------------------------------------}
procedure mpc_add_dbl(const a: mp_complex; b: double; var c: mp_complex);
{-Calculate c = a+b}
begin
mpf_add_dbl(a.re, b, c.re);
mpf_copy(a.im, c.im);
end;
{---------------------------------------------------------------------------}
procedure mpc_add_mpf(const a: mp_complex; const b: mp_float; var c: mp_complex);
{-Calculate c = a+b}
begin
mpf_add(a.re, b, c.re);
mpf_copy(a.im, c.im);
end;
{---------------------------------------------------------------------------}
procedure mpc_agm1(const z: mp_complex; var w: mp_complex);
{-Calculate the 'optimal' arithmetic-geometric mean w = AGM(1,z)}
var
a,b,u: mp_complex;
lim,cnt,prec: longint;
r: integer;
begin
if mp_error<>MP_OKAY then exit;
{$ifdef MPC_ArgCheck}
if mpc_not_init(z) or mpc_not_init(w) then begin
{$ifdef MPC_HaltOnArgCheck}
{$ifdef MPC_UseExceptions}
raise MPXNotInit.Create('mpc_agm1');
{$else}
RunError(MP_RTE_NOTINIT);
{$endif}
{$else}
set_mp_error(MP_NOTINIT);
exit;
{$endif}
end;
{$endif}
if mpf_is0(z.im) and (mpf_is0(z.re) or (mpf_is1a(z.re) and s_mpf_is_neg(z.re))) then begin
mpc_set0(w);
exit;
end;
prec := w.re.bitprec+8;
lim := prec div 2;
mpc_initp3(a,b,u,prec);
if mp_error<>MP_OKAY then exit;
{Compute suitable starting values a, b for optimal AGM, see Pari/GP }
{source code and the discussion in the pari-dev thread 'Complex AGM'}
{http://pari.math.u-bordeaux.fr/archives/pari-dev-1202/msg00045.html}
{or http://comments.gmane.org/gmane.comp.mathematics.pari.devel/3543}
if s_mpf_is_neg(z.re) then begin
if s_mpf_is_neg(z.im) then begin
{a := +I*a}
r := -1;
mpf_mul_2k(z.im,-1, a.re);
s_mpf_chs(a.re);
mpf_add_dbl(z.re, 1.0, a.im);
mpf_mul_2k(a.im,-1, a.im);
end
else begin
{a := -I*a}
r := 1;
mpf_mul_2k(z.im,-1, a.re);
mpf_add_dbl(z.re, 1.0, a.im);
mpf_mul_dbl(a.im, -0.5, a.im);
end;
mpc_chs(z,b);
mpc_sqrt(b,b);
end
else begin
{no rotation}
r := 0;
mpc_mul_dbl(z, 0.5, a);
mpf_add_dbl(a.re, 0.5, a.re);
mpc_sqrt(z,b);
end;
{Here a.re >= 0 and b.re >= 0, do standard AGM iteration}
for cnt:=0 to lim do begin
{u = a*b}
mpc_mul(a,b,u);
{a = (a+b)/2}
mpc_add(a,b,a);
mpc_sqrt(u,b);
{b = sqrt(a*b)}
mpc_mul_2k(a,-1,a);
{check difference between arith and geo part}
mpc_sub(b,a,u);
if mpc_is0(u) then break
else begin
mpc_abs(u,u.re);
mpc_abs(a,u.im);
if lim < s_mpf_ldx(u.im)-s_mpf_ldx(u.re) then break;
end;
end;
{iteration difference is < prec/2, do final step to make it < prec}
mpc_add(a,b,a);
mpc_mul_2k(a,-1,a);
{undo rotation}
case r of
1: begin
{w=a*I}
mpf_copyp(a.im, w.re);
mpf_copyp(a.re, w.im);
s_mpf_chs(w.re);
end;
-1: begin
{w=-a*I}
mpf_copyp(a.im, w.re);
mpf_copyp(a.re, w.im);
s_mpf_chs(w.im);
end;
else begin
{w=a}
mpf_copyp(a.re, w.re);
mpf_copyp(a.im, w.im);
end;
end; {case}
mpc_clear3(a,b,u);
end;
{---------------------------------------------------------------------------}
procedure mpc_agm(const x,y: mp_complex; var w: mp_complex);
{-Calculate the 'optimal' arithmetic-geometric mean w = AGM(x,y)}
var
u,v: mp_complex;
begin
if mp_error<>MP_OKAY then exit;
{$ifdef MPC_ArgCheck}
_CheckBitPrec(w.re);
{$endif}
if mpc_is0(x) or mpc_is0(y) then begin
mpc_set0(w);
exit;
end;
mpc_initp2(u,v,w.re.bitprec+32);
if mp_error=MP_OKAY then begin
mpc_abs(x,u.re);
mpc_abs(y,u.im);
if mpf_is_gt(u.re,u.im) then begin
{|x| > |y|}
mpc_div(y,x,v);
mpc_copyp(x,u);
end
else begin
{|x| <= |y|}
mpc_div(x,y,v);
mpc_copyp(y,u);
end;
mpc_agm1(v,v);
mpc_mul(u,v,w);
mpc_clear2(u,v);
end;
end;
{---------------------------------------------------------------------------}
procedure mpc_arccos(const a: mp_complex; var b: mp_complex);
{-Calculate the principal value of the complex inverse circular cosine b = arccos(a)}
var
zm,zp: mp_complex;
x,y: mp_float;
prec: longint;
begin
if mp_error<>MP_OKAY then exit;
{Ref: Kahan[42], procedure CACOS}
{$ifdef MPC_ArgCheck}
_CheckBitPrec(b.re);
{$endif}
prec := b.re.bitprec+8;
mpc_initp2(zm,zp, prec);
if mp_error=MP_OKAY then begin
mpf_initp2(x,y,prec);
if mp_error=MP_OKAY then begin
{zp = sqrt(1+a)}
mpc_add_dbl(a,1.0,zp);
mpc_sqrt(zp,zp);
{zm = sqrt(1-a)}
mpc_chs(a,zm);
s_mpf_inc1(zm.re);
mpc_sqrt(zm,zm);
{b.re = 2.0*arctan2(re(sqrt(1-a), re(sqrt(1+a)))) }
mpf_arctan2(zm.re, zp.re, x);
mpf_mul_2k(x,1,b.re);
{b.im = arcsinh(im(sqrt(1+conj(a))*sqrt(1-a))) }
{use im(sqrt(1+conj(a))) = -im(sqrt(1+a)) }
mpf_mul(zp.re, zm.im, y);
mpf_mul(zp.im, zm.re, x);
mpf_sub(y,x,y);
mpf_arcsinh(y,b.im);
mpf_clear2(x,y);
end;
mpc_clear2(zm,zp);
end;
end;
{---------------------------------------------------------------------------}
procedure mpc_arccosh(const a: mp_complex; var b: mp_complex);
{-Calculate the principal value of the complex inverse hyperbolic cosine b = arccosh(a)}
var
zm,zp: mp_complex;
begin
if mp_error<>MP_OKAY then exit;
{Ref: Kahan[42], procedure CACOSH}
{$ifdef MPC_ArgCheck}
_CheckBitPrec(b.re);
{$endif}
mpc_initp2(zm,zp, b.re.bitprec+8);
if mp_error=MP_OKAY then begin
{zp = sqrt(a+1)}
mpc_add_dbl(a,1.0,zp);
mpc_sqrt(zp,zp);
{zm = sqrt(a-1)}
mpc_sub_dbl(a,1.0,zm);
mpc_sqrt(zm,zm);
{b.im = 2.0*arctan2(im(sqrt(a-1.0), re(sqrt(a+1.0)))) }
mpf_arctan2(zm.im, zp.re, b.im);
s_mpf_incexp(b.im,1);
{b.re = arcsinh(re(sqrt(conj(a)-1.0)*sqrt(a+1.0))) }
{use im(sqrt(conj(a)-1)) = -im(sqrt(a-1)) }
mpf_mul(zp.re, zm.re, zm.re);
mpf_mul(zp.im, zm.im, zm.im);
mpf_add(zm.re, zm.im, zm.re);
mpf_arcsinh(zm.re,b.re);
mpc_clear2(zm,zp);
end;
end;
{---------------------------------------------------------------------------}
procedure mpc_arcsin(const a: mp_complex; var b: mp_complex);
{-Calculate the principal value of the complex inverse circular sine b = arcsin(a)}
var
zm,zp: mp_complex;
x,y: mp_float;
prec: longint;
begin
if mp_error<>MP_OKAY then exit;
{Ref: Kahan[42], procedure CASIN}
{$ifdef MPC_ArgCheck}
_CheckBitPrec(b.re);
{$endif}
prec := b.re.bitprec+8;
mpc_initp2(zm,zp, prec);
if mp_error=MP_OKAY then begin
mpf_initp2(x,y,prec);
if mp_error=MP_OKAY then begin
{zp = sqrt(1+a)}
mpc_add_dbl(a,1.0,zp);
mpc_sqrt(zp,zp);
{zm = sqrt(1-a)}
mpc_chs(a,zm);
s_mpf_inc1(zm.re);
mpc_sqrt(zm,zm);
{y = im(sqrt(1-conj(a))*sqrt(1+a))}
mpf_mul(zm.re, zp.im, y);
mpf_mul(zp.re, zm.im, x);
mpf_sub(y,x,y);
{x = re(sqrt(1-a)*sqrt(1+a))}
mpf_mul(zp.re, zm.re, x);
mpf_mul(zp.im, zm.im, zm.im);
mpf_sub(x,zm.im,x);
{b.re = arctan2(re(a), re(sqrt(1-a)*sqrt(1+a))) }
{b.im = arcsinh(im(sqrt(1-conj(a))*sqrt(1+a))) }
mpf_arctan2(a.re, x, b.re);
mpf_arcsinh(y, b.im);
mpf_clear2(x,y);
end;
mpc_clear2(zm,zp);
end;
end;
{---------------------------------------------------------------------------}
procedure mpc_arcsinh(const a: mp_complex; var b: mp_complex);
{-Calculate the principal value of the complex inverse hyperbolic sine b = arcsinh(a)}
begin
{arcsinh(a) = -i*arcsin(i*a)}
mpc_copyp(a,b);
mpf_exch(b.re,b.im);
s_mpf_chs(b.re);
mpc_arcsin(b,b);
mpf_exch(b.re,b.im);
s_mpf_chs(b.im);
end;
{---------------------------------------------------------------------------}
procedure mpc_arctan(const a: mp_complex; var b: mp_complex);
{-Calculate the principal value of the complex inverse circular tangent w = arctan(z)}
begin
{Ref HMF[41], 4.4.22: arctan(z) = -i*arctanh(iz)}
mpc_copyp(a,b);
mpf_exch(b.re,b.im);
s_mpf_chs(b.re);
mpc_arctanh(b,b);
mpf_exch(b.re,b.im);
s_mpf_chs(b.im);
end;
{---------------------------------------------------------------------------}
procedure mpc_arctanh(const a: mp_complex; var b: mp_complex);
{-Calculate the principal value of the complex inverse hyperbolic tangent b = arctanh(a)}
var
x,x1,y,u,v: mp_float;
begin
if mp_error<>MP_OKAY then exit;
if s_mpf_is0(a.re) then begin
{arctanh(iy)=i*arctan(y);}
mpf_set0(b.re);
mpf_arctan(a.im, b.im);
exit;
end;
{$ifdef MPC_ArgCheck}
_CheckBitPrec(b.re);
{$endif}
mpf_initp5(x,x1,y,u,v, b.re.bitprec+8);
if mp_error=MP_OKAY then begin
{Ref: Kahan[42], procedure CATANH, normal case with rho=0}
mpf_abs(a.re, x);
mpf_abs(a.im, y);
{v = y^2}
mpf_sqr(y,v);
{x1 = 1-x}
mpf_chs(x,x1);
s_mpf_inc1(x1);
{u := ln1p(4.0*x/(sqr(x1) + v));}
mpf_sqr(x1,u);
mpf_add(u,v,u);
mpf_div(x,u,u);
s_mpf_incexp(u,2);
mpf_ln1p(u,u);
{v := arctan2(2.0*y, x1*(1.0+x) - v)}
s_mpf_incexp(y,1);
s_mpf_inc1(x);
mpf_mul(x,x1,x);
mpf_sub(x,v,x);
if mpf_is0(y) and (mpf_cmp_dbl(a.re,1) > 0) then begin
{Adjust sign on the branch cut}
mpf_set_pi(v);
mpf_chs(v,v);
end
else mpf_arctan2(y,x,v);
{b.re := copysign(0.25,a.re)*u;}
{b.im := copysign(0.5 ,a.im)*v;}
if s_mpf_is_neg(a.re) then s_mpf_chs(u);
if s_mpf_is_neg(a.im) then s_mpf_chs(v);
mpf_mul_2k(u,-2,b.re);
mpf_mul_2k(v,-1,b.im);
mpf_clear5(x,x1,y,u,v);
end;
end;
{---------------------------------------------------------------------------}
procedure mpc_arccoth(const a: mp_complex; var b: mp_complex);
{-Calculate the principal value of the complex inverse hyperbolic cotangent b = arccoth(a) = arctanh(1/a)}
var
z: mp_complex;
lr,prec: longint;
begin
if mp_error<>MP_OKAY then exit;
{$ifdef MPC_ArgCheck}
if mpc_not_init(a) or mpc_not_init(b) then begin
{$ifdef MPC_HaltOnArgCheck}
{$ifdef MPC_UseExceptions}
raise MPXNotInit.Create('mpc_arccoth');
{$else}
RunError(MP_RTE_NOTINIT);
{$endif}
{$else}
set_mp_error(MP_NOTINIT);
exit;
{$endif}
end;
{$endif}
prec := b.re.bitprec+8;
if s_mpf_ldx(a.im) < 0 then begin
{|a.im| < 0.5}
lr := s_mpf_ldx(a.re);
if (lr >=0 ) and (lr <= 1) then begin
{0.5 <= |a.re| < 2: add some more precision near poles at +- 1}
inc(prec,8);
end;
end;
mpc_initp(z, prec);
if mp_error=MP_OKAY then begin
mpc_inv(a,z);
mpc_arctanh(z,z);
mpc_copyp(z,b);
mpc_clear(z);
end;
end;
{---------------------------------------------------------------------------}
procedure mpc_arccothc(const a: mp_complex; var b: mp_complex);
{-Calculate the principal value of the complex inverse hyperbolic cotangent b = arccothc(a) = arctanh(a) + i*Pi/2}
var
z: mp_complex;
prec: longint;
begin
if mp_error<>MP_OKAY then exit;
if s_mpf_is_neg(a.im) then begin
{if z.im < 0 then arccothc(a) = arccoth(a)}
mpc_arccoth(a,b);
end
else begin
{$ifdef MPC_ArgCheck}
_CheckBitPrec(b.re);
{$endif}
prec := b.re.bitprec+8;
mpc_initp(z, prec);
if mp_error=MP_OKAY then begin
mpc_arctanh(a,z);
mpf_copyp(z.re,b.re);
mpf_set_pi2k(z.re,-1);
mpf_add(z.re, z.im, z.im);
mpf_copyp(z.im,b.im);
mpc_clear(z);
end;
end;
end;
{---------------------------------------------------------------------------}
procedure mpc_arccot(const a: mp_complex; var b: mp_complex);
{-Calculate the principal value of the complex inverse circular cotangent b = arccot(a) = arctan(1/a)}
begin
{arccot(z) = i*arccoth(i*z))}
mpc_copyp(a,b);
mpf_exch(b.re,b.im);
s_mpf_chs(b.re);
mpc_arccoth(b,b);
mpf_exch(b.re,b.im);
s_mpf_chs(b.re);
end;
{---------------------------------------------------------------------------}
procedure mpc_arccotc(const a: mp_complex; var b: mp_complex);
{-Calculate the principal value of the complex inverse circular cotangent b = arccotc(a) = Pi/2 - arctan(a)}
var
z: mp_complex;
prec: longint;
begin
if mp_error<>MP_OKAY then exit;
if s_mpf_is_gt0(a.re) then begin
{if z.re < 0 then arccotc(a) = arccot(a)}
mpc_arccot(a,b);
end
else begin
{$ifdef MPC_ArgCheck}
_CheckBitPrec(b.re);
{$endif}
prec := b.re.bitprec+8;
mpc_initp(z, prec);
if mp_error=MP_OKAY then begin
mpc_arctan(a,z);
mpf_chs(z.im,b.im);
mpf_set_pi2k(z.im,-1);
mpf_sub(z.im, z.re, z.re);
mpf_copyp(z.re,b.re);
mpc_clear(z);
end;
end;
end;
{---------------------------------------------------------------------------}
procedure mpc_arccsc(const a: mp_complex; var b: mp_complex);
{-Calculate the principal value of the complex inverse circular cosecant b = arccsc(a) = arcsin(1/a)}
var
z: mp_complex;
lr,prec: longint;
begin
if mp_error<>MP_OKAY then exit;
{$ifdef MPC_ArgCheck}
if mpc_not_init(a) or mpc_not_init(b) then begin
{$ifdef MPC_HaltOnArgCheck}
{$ifdef MPC_UseExceptions}
raise MPXNotInit.Create('mpc_arccsc');
{$else}
RunError(MP_RTE_NOTINIT);
{$endif}
{$else}
set_mp_error(MP_NOTINIT);
exit;
{$endif}
end;
{$endif}
prec := b.re.bitprec+8;
if s_mpf_ldx(a.im) < 0 then begin
{|a.im| < 0.5}
lr := s_mpf_ldx(a.re);
if (lr >=0 ) and (lr <= 1) then begin
{0.5 <= |a.re| < 2: add some more precision near z = +- 1}
inc(prec,8);
end;
end;
mpc_initp(z, prec);
if mp_error=MP_OKAY then begin
mpc_inv(a,z);
mpc_arcsin(z,z);
mpc_copyp(z,b);
mpc_clear(z);
end;
end;
{---------------------------------------------------------------------------}
procedure mpc_arccsch(const a: mp_complex; var b: mp_complex);
{-Calculate the principal value of the complex inverse hyperbolic cosecant b = arccsch(a) = arcsinh(1/a)}
begin
if mp_error<>MP_OKAY then exit;
{Use arccsch(z) = i*arccsc(i*z)}
mpc_copyp(a,b);
mpf_exch(b.re,b.im);
s_mpf_chs(b.re);
mpc_arccsc(b,b);
mpf_exch(b.re,b.im);
s_mpf_chs(b.re);
end;
{---------------------------------------------------------------------------}
procedure mpc_arcsec(const a: mp_complex; var b: mp_complex);
{-Calculate the principal value of the complex inverse circular secant b = arcssec(a) = arccos(1/a)}
var
z: mp_complex;
lr,prec: longint;
begin
if mp_error<>MP_OKAY then exit;
{$ifdef MPC_ArgCheck}
if mpc_not_init(a) or mpc_not_init(b) then begin
{$ifdef MPC_HaltOnArgCheck}