/
bessel.cpp
8880 lines (8432 loc) · 251 KB
/
bessel.cpp
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
//
// Dale Roberts <dale.o.roberts@gmail.com>
//
#define _USE_MATH_DEFINES
#include <cmath>
#include <complex>
#include <algorithm>
#include <float.h>
#include <limits.h>
#include <iostream>
#include "bessel.h"
#define fmax2 std::max
#define fmin2 std::min
#define imax2 std::max
#define imin2 std::min
#ifndef PI
#define PI M_PI
#endif
#ifndef M_LOG10_2
#define M_LOG10_2 0.301029995663981195213738894724 /* log10(2) */
#endif
typedef std::complex<double> Complex;
/* Table of constant values */
static int c__0 = 0;
static int c__1 = 1;
static int c__2 = 2;
static double c_b168 = .5;
static double c_b169 = 0.;
Complex besseli(const Complex& z, const double& nu) {
double zr = z.real(), zi = z.imag(), fnu = nu, cyr = 0.0, cyi = 0.0;
int kode = 1, n = 1, nz = 0, ierr = 0;
Complex r;
if (fnu < 0) {
//I(-nu,z) = I(nu,z) + (2/pi)*sin(pi*nu)*K(nu,z)
fnu = -fnu;
zbesi_(&zr, &zi, &fnu, &kode, &n, &cyr, &cyi, &nz, &ierr);
r = Complex(cyr, cyi);
zbesk_(&zr, &zi, &fnu, &kode, &n, &cyr, &cyi, &nz, &ierr);
r = r + 2/PI*sin(PI*fnu)*Complex(cyr, cyi);
} else {
zbesi_(&zr, &zi, &fnu, &kode, &n, &cyr, &cyi, &nz, &ierr);
r = Complex(cyr, cyi);
}
if (ierr > 1) {
std::cout << "besseli error: " << ierr << " z:" << z << " nu:" << nu << std::endl;
}
return r;
}
Complex besseli(const double& z, const double& nu) {
double zr = z, zi = 0.0, fnu = nu, cyr = 0.0, cyi = 0.0;
int kode = 1, n = 1, nz = 0, ierr = 0;
Complex r;
if (fnu < 0) {
//I(-nu,z) = I(nu,z) + (2/pi)*sin(pi*nu)*K(nu,z)
fnu = -fnu;
zbesi_(&zr, &zi, &fnu, &kode, &n, &cyr, &cyi, &nz, &ierr);
r = Complex(cyr, cyi);
zbesk_(&zr, &zi, &fnu, &kode, &n, &cyr, &cyi, &nz, &ierr);
r = r + 2/PI*sin(PI*fnu)*Complex(cyr, cyi);
} else {
zbesi_(&zr, &zi, &fnu, &kode, &n, &cyr, &cyi, &nz, &ierr);
r = Complex(cyr, cyi);
}
if (ierr > 1) {
std::cout << "besseli error: " << ierr << " z:" << z << " nu:" << nu << std::endl;
}
return r;
}
double fsign(double x, double y)
{
return ((y >= 0) ? std::abs(x) : -std::abs(x));
}
/* Subroutine */ int
zbesh_(double *zr, double *zi, double *fnu,
int *kode, int *m, int *n,
double *cyr, double *cyi, int *nz, int *ierr)
{
/***begin prologue zbesh
***date written 830501 (yymmdd)
***revision date 890801, 930101 (yymmdd)
***category no. b5k
***keywords h-bessel functions,bessel functions of complex argument,
bessel functions of third kind,hankel functions
***author amos, donald e., sandia national laboratories
***purpose to compute the h-bessel functions of a complex argument
***description
***a double precision routine***
on kode=1, zbesh computes an n member sequence of complex
hankel (bessel) functions cy(j)=H(m,fnu+j-1,z) for kinds m=1
or 2, float, nonnegative orders fnu+j-1, j=1,...,n, and complex
z != cmplx(0.,0.) in the cut plane -pi < arg(z) <= pi.
on kode=2, zbesh returns the scaled hankel functions
cy(j) = exp(-mm*z*i)*H(m,fnu+j-1,z) mm=3-2*m, i**2=-1.
which removes the exponential behavior in both the upper and
lower half planes. definitions and notation are found in the
nbs handbook of mathematical functions (ref. 1).
input zr,zi,fnu are double precision
zr,zi - z=cmplx(zr,zi), z != cmplx(0,0),
-pt < arg(z) <= pi
fnu - order of initial h function, fnu >= 0
kode - a parameter to indicate the scaling option
kode= 1 returns
cy(j)=H(m,fnu+j-1,z), j=1,...,n
= 2 returns
cy(j)=H(m,fnu+j-1,z)*exp(-i*z*(3-2m))
j=1,...,n , i**2=-1
m - kind of hankel function, m=1 or 2
n - number of members in the sequence, n >= 1
output cyr,cyi are double precision
cyr,cyi- double precision vectors whose first n components
contain float and imaginary parts for the sequence
cy(j)=H(m,fnu+j-1,z) or
cy(j)=H(m,fnu+j-1,z)*exp(-i*z*(3-2m)) j=1,...,n
depending on kode, i**2=-1.
nz - number of components set to zero due to underflow,
nz= 0 , normal return
nz > 0 , first nz components of cy set to zero due
to underflow, cy(j)=cmplx(0,0)
j=1,...,nz when y > 0.0 and m=1 or
y < 0.0 and m=2. for the complmentary
half planes, nz states only the number
of underflows.
ierr - error flag
ierr=0, normal return - computation completed
ierr=1, input error - no computation
ierr=2, overflow - no computation, fnu too
large or cabs(z) too small or both
ierr=3, cabs(z) or fnu+n-1 large - computation done
but losses of signifcance by argument
reduction produce less than half of machine
accuracy
ierr=4, cabs(z) or fnu+n-1 too large - no computa-
tion because of complete losses of signifi-
cance by argument reduction
ierr=5, error - no computation,
algorithm termination condition not met
***long description
the computation is carried out by the relation
H(m,fnu,z)=(1/mp)*exp(-mp*fnu)*K(fnu,z*exp(-mp))
mp=mm*hpi*i, mm=3-2*m, hpi=pi/2, i**2=-1
for m=1 or 2 where the k bessel function is computed for the
right half plane re(z) >= 0.0. the k function is continued
to the left half plane by the relation
K(fnu,z*exp(mp)) = exp(-mp*fnu)*K(fnu,z)-mp*I(fnu,z)
mp=mr*pi*i, mr=+1 or -1, re(z) > 0, i**2=-1
where I(fnu,z) is the i bessel function.
exponential decay of H(m,fnu,z) occurs in the upper half z
plane for m=1 and the lower half z plane for m=2. exponential
growth occurs in the complementary half planes. scaling
by exp(-mm*z*i) removes the exponential behavior in the
whole z plane for z to infinity.
for negative orders,the formulae
H(1,-fnu,z) = H(1,fnu,z)*cexp( pi*fnu*i)
H(2,-fnu,z) = H(2,fnu,z)*cexp(-pi*fnu*i)
i**2=-1
can be used.
in most complex variable computation, one must evaluate ele-
mentary functions. when the magnitude of z or fnu+n-1 is
large, losses of significance by argument reduction occur.
consequently, if either one exceeds u1=sqrt(0.5/ur), then
losses exceeding half precision are likely and an error flag
ierr=3 is triggered where ur=dmax1(DBL_EPSILON,1.0d-18) is
double precision unit roundoff limited to 18 digits precision.
if either is larger than u2=0.5/ur, then all significance is
lost and ierr=4. in order to use the int function, arguments
must be further restricted not to exceed the largest machine
int, u3=i1mach(9). thus, the magnitude of z and fnu+n-1 is
restricted by min(u2,u3). on 32 bit machines, u1,u2, and u3
are approximately 2.0e+3, 4.2e+6, 2.1e+9 in single precision
arithmetic and 1.3e+8, 1.8e+16, 2.1e+9 in double precision
arithmetic respectively. this makes u2 and u3 limiting in
their respective arithmetics. this means that one can expect
to retain, in the worst cases on 32 bit machines, no digits
in single and only 7 digits in double precision arithmetic.
similar considerations hold for other machines.
the approximate relative error in the magnitude of a complex
bessel function can be expressed by p*10**s where p=max(unit
roundoff,1.0d-18) is the nominal precision and 10**s repre-
sents the increase in error due to argument reduction in the
elementary functions. here, s=max(1,abs(log10(cabs(z))),
abs(log10(fnu))) approximately (i.e. s=max(1,abs(exponent of
cabs(z),abs(exponent of fnu)) ). however, the phase angle may
have only absolute accuracy. this is most likely to occur when
one component (in absolute value) is larger than the other by
several orders of magnitude. if one component is 10**k larger
than the other, then one can expect only max(abs(log10(p))-k,
0) significant digits; or, stated another way, when k exceeds
the exponent of p, no significant digits remain in the smaller
component. however, the phase angle retains absolute accuracy
because, in complex arithmetic with precision p, the smaller
component will not (as a rule) decrease below p times the
magnitude of the larger component. in these extreme cases,
the principal phase angle is on the order of +p, -p, pi/2-p,
or -pi/2+p.
***references handbook of mathematical functions by m. abramowitz
and i. a. stegun, nbs ams series 55, u.s. dept. of
commerce, 1955.
computation of bessel functions of complex argument
by d. e. amos, sand83-0083, may, 1983.
computation of bessel functions of complex argument
and large order by d. e. amos, sand83-0643, may, 1983
a subroutine package for bessel functions of a complex
argument and nonnegative order by d. e. amos, sand85-
1018, may, 1985
a portable package for bessel functions of a complex
argument and nonnegative order by d. e. amos, acm
trans. math. software, vol. 12, no. 3, september 1986,
pp 265-273.
***routines called zacon,zbknu,zbunk,zuoik,zabs,i1mach,d1mach
***end prologue zbesh
*/
/* Initialized data */
double hpi = 1.57079632679489662;
/* Local variables */
int i__, k, k1, k2;
double aa, bb, fn;
int mm;
double az;
int ir, nn;
double rl;
int mr, nw;
double dig, arg, aln, fmm, r1m5, ufl, sgn;
int nuf, inu;
double tol, sti, zni, zti, str, znr, alim, elim;
double atol, rhpi;
int inuh;
double fnul, rtol, ascle, csgni;
double csgnr;
/* complex cy,z,zn,zt,csgn
Parameter adjustments */
--cyi;
--cyr;
/* Function Body
***first executable statement zbesh */
*ierr = 0;
*nz = 0;
if (*zr == 0. && *zi == 0.) {
*ierr = 1;
}
if (*fnu < 0.) {
*ierr = 1;
}
if (*m < 1 || *m > 2) {
*ierr = 1;
}
if (*kode < 1 || *kode > 2) {
*ierr = 1;
}
if (*n < 1) {
*ierr = 1;
}
if (*ierr != 0) {
return 0;
}
nn = *n;
/* -----------------------------------------------------------------------
set parameters related to machine constants.
tol is the approximate unit roundoff limited to 1.0e-18.
elim is the approximate exponential over- and underflow limit.
exp(-elim) < exp(-alim)=exp(-elim)/tol and
exp(elim) > exp(alim)=exp(elim)*tol are intervals near
underflow and overflow limits where scaled arithmetic is done.
rl is the lower boundary of the asymptotic expansion for large z.
dig = number of base 10 digits in tol = 10**(-dig).
fnul is the lower boundary of the asymptotic series for large fnu
----------------------------------------------------------------------- */
tol = fmax2(DBL_EPSILON, 1e-18);
k1 = DBL_MIN_EXP;
k2 = DBL_MAX_EXP;
r1m5 = M_LOG10_2;
k = fmin2(std::abs(k1), std::abs(k2));
elim = ((double) ((float) k) * r1m5 - 3.) * 2.303;
k1 = DBL_MANT_DIG - 1;
aa = r1m5 * (double) ((float) k1);
dig = fmin2(aa,18.);
aa *= 2.303;
alim = elim + fmax2(-aa, -41.45);
fnul = (dig - 3.) * 6. + 10.;
rl = dig * 1.2 + 3.;
fn = *fnu + (double) ((float) (nn - 1));
mm = 3 - *m - *m;
fmm = (double) ((float) mm);
znr = fmm * *zi;
zni = -fmm * *zr;
/* -----------------------------------------------------------------------
test for proper range
----------------------------------------------------------------------- */
az = zabs_(zr, zi);
aa = .5 / tol;
bb = (double) ((float) INT_MAX) * .5;
aa = fmin2(aa,bb);
if (az > aa) {
goto L260;
}
if (fn > aa) {
goto L260;
}
aa = sqrt(aa);
if (az > aa) {
*ierr = 3;
}
if (fn > aa) {
*ierr = 3;
}
/* -----------------------------------------------------------------------
overflow test on the last member of the sequence
----------------------------------------------------------------------- */
ufl = DBL_MIN * 1e3;
if (az < ufl) {
goto L230;
}
if (*fnu > fnul) {
goto L90;
}
if (fn <= 1.) {
goto L70;
}
if (fn > 2.) {
goto L60;
}
if (az > tol) {
goto L70;
}
arg = az * .5;
aln = -fn * log(arg);
if (aln > elim) {
goto L230;
}
goto L70;
L60:
zuoik_(&znr, &zni, fnu, kode, &c__2, &nn, &cyr[1], &cyi[1], &nuf, &tol, &
elim, &alim);
if (nuf < 0) {
goto L230;
}
*nz += nuf;
nn -= nuf;
/* -----------------------------------------------------------------------
here nn=n or nn=0 since nuf=0,nn, or -1 on return from cuoik
if nuf=nn, then cy(i)=czero for all i
----------------------------------------------------------------------- */
if (nn == 0) {
goto L140;
}
L70:
if (znr < 0. || (znr == 0. && zni < 0. && *m == 2)) {
goto L80;
}
/* -----------------------------------------------------------------------
right half plane computation, xn >= 0. .and. (xn != 0. .or.
yn >= 0. .or. m=1)
----------------------------------------------------------------------- */
zbknu_(&znr, &zni, fnu, kode, &nn, &cyr[1], &cyi[1], nz, &tol, &elim, &
alim);
goto L110;
/* -----------------------------------------------------------------------
left half plane computation
----------------------------------------------------------------------- */
L80:
mr = -mm;
zacon_(&znr, &zni, fnu, kode, &mr, &nn, &cyr[1], &cyi[1], &nw, &rl, &fnul,
&tol, &elim, &alim);
if (nw < 0) {
goto L240;
}
*nz = nw;
goto L110;
L90:
/* -----------------------------------------------------------------------
uniform asymptotic expansions for fnu > fnul
----------------------------------------------------------------------- */
mr = 0;
if (znr >= 0. && (znr != 0. || zni >= 0. || *m != 2)) {
goto L100;
}
mr = -mm;
if (znr != 0. || zni >= 0.) {
goto L100;
}
znr = -znr;
zni = -zni;
L100:
zbunk_(&znr, &zni, fnu, kode, &mr, &nn, &cyr[1], &cyi[1], &nw, &tol, &
elim, &alim);
if (nw < 0) {
goto L240;
}
*nz += nw;
L110:
/* -----------------------------------------------------------------------
H(m,fnu,z) = -fmm*(i/hpi)*(zt**fnu)*K(fnu,-z*zt)
zt=exp(-fmm*hpi*i) = cmplx(0.0,-fmm), fmm=3-2*m, m=1,2
----------------------------------------------------------------------- */
sgn = fsign(hpi, -fmm);
/* -----------------------------------------------------------------------
calculate exp(fnu*hpi*i) to minimize losses of significance
when fnu is large
----------------------------------------------------------------------- */
inu = (int) ((float) (*fnu));
inuh = inu / 2;
ir = inu - (inuh << 1);
arg = (*fnu - (double) ((float) (inu - ir))) * sgn;
rhpi = 1. / sgn;
/* zni = rhpi*dcos(arg)
znr = -rhpi*dsin(arg) */
csgni = rhpi * cos(arg);
csgnr = -rhpi * sin(arg);
if (inuh % 2 == 0) {
goto L120;
}
/* znr = -znr
zni = -zni */
csgnr = -csgnr;
csgni = -csgni;
L120:
zti = -fmm;
rtol = 1. / tol;
ascle = ufl * rtol;
for (i__ = 1; i__ <= nn; ++i__) {
/*
str = cyr(i)*znr - cyi(i)*zni
cyi(i) = cyr(i)*zni + cyi(i)*znr
cyr(i) = str
str = -zni*zti
zni = znr*zti
znr = str
*/
aa = cyr[i__];
bb = cyi[i__];
atol = 1.;
if (fmax2(std::abs(aa), std::abs(bb)) > ascle) {
goto L135;
}
aa *= rtol;
bb *= rtol;
atol = tol;
L135:
str = aa * csgnr - bb * csgni;
sti = aa * csgni + bb * csgnr;
cyr[i__] = str * atol;
cyi[i__] = sti * atol;
str = -csgni * zti;
csgni = csgnr * zti;
csgnr = str;
}
return 0;
L140:
if (znr < 0.) {
goto L230;
}
return 0;
L230:
*nz = 0;
*ierr = 2;
return 0;
L240:
if (nw == -1) {
goto L230;
}
*nz = 0;
*ierr = 5;
return 0;
L260:
*nz = 0;
*ierr = 4;
return 0;
} /* zbesh_ */
/* Subroutine */ int
zbesi_(double *zr, double *zi, double *fnu,
int *kode, int *n,
double *cyr, double *cyi, int *nz, int *ierr)
{
/***begin prologue zbesi
***date written 830501 (yymmdd)
***revision date 890801, 930101 (yymmdd)
***category no. b5k
***keywords i-bessel function,complex bessel function,
modified bessel function of the first kind
***author amos, donald e., sandia national laboratories
***purpose to compute i-bessel functions of complex argument
***description
***a double precision routine***
on kode=1, zbesi computes an n member sequence of complex
bessel functions cy(j) = I(fnu+j-1,z) for float, nonnegative
orders fnu+j-1, j=1,...,n and complex z in the cut plane
-pi < arg(z) <= pi. on kode=2, zbesi returns the scaled
functions
cy(j) = exp(-abs(x)) * I(fnu+j-1,z) j = 1,...,n , x=float(z)
with the exponential growth removed in both the left and
right half planes for z to infinity. definitions and notation
are found in the nbs handbook of mathematical functions
(ref. 1).
input zr,zi,fnu are double precision
zr,zi - z=cmplx(zr,zi), -pi < arg(z) <= pi
fnu - order of initial I function, fnu >= 0
kode - a parameter to indicate the scaling option
kode= 1 returns cy(j) = I(fnu+j-1,z), j=1,...,n
= 2 returns cy(j) = I(fnu+j-1,z)*exp(-abs(x)), j=1,...,n
n - number of members of the sequence, n >= 1
output cyr,cyi are double precision
cyr,cyi- double precision vectors whose first n components
contain float and imaginary parts for the sequence
cy(j) = I(fnu+j-1,z) or
cy(j) = I(fnu+j-1,z)*exp(-abs(x)) j=1,...,n
depending on kode, x=float(z)
nz - number of components set to zero due to underflow,
nz= 0 , normal return
nz > 0 , last nz components of cy set to zero
to underflow, cy(j) = cmplx(0,0)
j = n-nz+1,...,n
ierr - error flag
ierr=0, normal return - computation completed
ierr=1, input error - no computation
ierr=2, overflow - no computation, float(z) too
large on kode=1
ierr=3, cabs(z) or fnu+n-1 large - computation done
but losses of signifcance by argument
reduction produce less than half of machine
accuracy
ierr=4, cabs(z) or fnu+n-1 too large - no computa-
tion because of complete losses of signifi-
cance by argument reduction
ierr=5, error - no computation,
algorithm termination condition not met
***long description
the computation is carried out by the power series for
small cabs(z), the asymptotic expansion for large cabs(z),
the miller algorithm normalized by the wronskian and a
neumann series for imtermediate magnitudes, and the
uniform asymptotic expansions for I(fnu,z) and J(fnu,z)
for large orders. backward recurrence is used to generate
sequences or reduce orders when necessary.
the calculations above are done in the right half plane and
continued into the left half plane by the formula
I(fnu,z*exp(m*pi)) = exp(m*pi*fnu)*I(fnu,z) float(z) > 0.0
m = +i or -i, i**2=-1
for negative orders,the formula
I(-fnu,z) = I(fnu,z) + (2/pi)*sin(pi*fnu)*K(fnu,z)
can be used. however,for large orders close to integers, the
the function changes radically. when fnu is a large positive
int,the magnitude of I(-fnu,z)=I(fnu,z) is a large
negative power of ten. but when fnu is not an int,
K(fnu,z) dominates in magnitude with a large positive power of
ten and the most that the second term can be reduced is by
unit roundoff from the coefficient. thus, wide changes can
occur within unit roundoff of a large int for fnu. here,
large means fnu > cabs(z).
in most complex variable computation, one must evaluate ele-
mentary functions. when the magnitude of z or fnu+n-1 is
large, losses of significance by argument reduction occur.
consequently, if either one exceeds u1=sqrt(0.5/ur), then
losses exceeding half precision are likely and an error flag
ierr=3 is triggered where ur=dmax1(d1mach(4),1.0d-18) is
double precision unit roundoff limited to 18 digits precision.
if either is larger than u2=0.5/ur, then all significance is
lost and ierr=4. in order to use the int function, arguments
must be further restricted not to exceed the largest machine
int, u3=i1mach(9). thus, the magnitude of z and fnu+n-1 is
restricted by min(u2,u3). on 32 bit machines, u1,u2, and u3
are approximately 2.0e+3, 4.2e+6, 2.1e+9 in single precision
arithmetic and 1.3e+8, 1.8e+16, 2.1e+9 in double precision
arithmetic respectively. this makes u2 and u3 limiting in
their respective arithmetics. this means that one can expect
to retain, in the worst cases on 32 bit machines, no digits
in single and only 7 digits in double precision arithmetic.
similar considerations hold for other machines.
the approximate relative error in the magnitude of a complex
bessel function can be expressed by p*10**s where p=max(unit
roundoff,1.0e-18) is the nominal precision and 10**s repre-
sents the increase in error due to argument reduction in the
elementary functions. here, s=max(1,abs(log10(cabs(z))),
abs(log10(fnu))) approximately (i.e. s=max(1,abs(exponent of
cabs(z),abs(exponent of fnu)) ). however, the phase angle may
have only absolute accuracy. this is most likely to occur when
one component (in absolute value) is larger than the other by
several orders of magnitude. if one component is 10**k larger
than the other, then one can expect only max(abs(log10(p))-k,
0) significant digits; or, stated another way, when k exceeds
the exponent of p, no significant digits remain in the smaller
component. however, the phase angle retains absolute accuracy
because, in complex arithmetic with precision p, the smaller
component will not (as a rule) decrease below p times the
magnitude of the larger component. in these extreme cases,
the principal phase angle is on the order of +p, -p, pi/2-p,
or -pi/2+p.
***references handbook of mathematical functions by m. abramowitz
and i. a. stegun, nbs ams series 55, u.s. dept. of
commerce, 1955.
computation of bessel functions of complex argument
by d. e. amos, sand83-0083, may, 1983.
computation of bessel functions of complex argument
and large order by d. e. amos, sand83-0643, may, 1983
a subroutine package for bessel functions of a complex
argument and nonnegative order by d. e. amos, sand85-
1018, may, 1985
a portable package for bessel functions of a complex
argument and nonnegative order by d. e. amos, acm
trans. math. software, vol. 12, no. 3, september 1986,
pp 265-273.
***routines called zbinu,zabs,i1mach,d1mach
***end prologue zbesi
*/
/* Initialized data */
double pi = 3.14159265358979324;
double coner = 1.;
double conei = 0.;
/* Local variables */
int i__, k, k1, k2;
double aa, bb, fn, az;
int nn;
double rl, dig, arg, r1m5;
int inu;
double tol, sti, zni, str, znr, alim, elim;
double atol, fnul, rtol, ascle, csgni, csgnr;
/* complex cone,csgn,cw,cy,czero,z,zn
Parameter adjustments */
--cyi;
--cyr;
/* Function Body
***first executable statement zbesi */
*ierr = 0;
*nz = 0;
if (*fnu < 0.) {
*ierr = 1;
}
if (*kode < 1 || *kode > 2) {
*ierr = 1;
}
if (*n < 1) {
*ierr = 1;
}
if (*ierr != 0) {
return 0;
}
/* -----------------------------------------------------------------------
set parameters related to machine constants.
tol is the approximate unit roundoff limited to 1.0e-18.
elim is the approximate exponential over- and underflow limit.
exp(-elim) < exp(-alim)=exp(-elim)/tol and
exp(elim) > exp(alim)=exp(elim)*tol are intervals near
underflow and overflow limits where scaled arithmetic is done.
rl is the lower boundary of the asymptotic expansion for large z.
dig = number of base 10 digits in tol = 10**(-dig).
fnul is the lower boundary of the asymptotic series for large fnu.
----------------------------------------------------------------------- */
tol = fmax2(DBL_EPSILON, 1e-18);
k1 = DBL_MIN_EXP;
k2 = DBL_MAX_EXP;
r1m5 = M_LOG10_2;
k = fmin2(std::abs(k1), std::abs(k2));
elim = ((double) ((float) k) * r1m5 - 3.) * 2.303;
k1 = DBL_MANT_DIG - 1;
aa = r1m5 * (double) ((float) k1);
dig = fmin2(aa,18.);
aa *= 2.303;
alim = elim + fmax2(-aa, -41.45);
rl = dig * 1.2 + 3.;
fnul = (dig - 3.) * 6. + 10.;
/* -----------------------------------------------------------------------------
test for proper range
----------------------------------------------------------------------- */
az = zabs_(zr, zi);
fn = *fnu + (double) ((float) (*n - 1));
aa = .5 / tol;
bb = (double) ((float) INT_MAX) * .5;
aa = fmin2(aa,bb);
if (az > aa) {
goto L260;
}
if (fn > aa) {
goto L260;
}
aa = sqrt(aa);
if (az > aa) {
*ierr = 3;
}
if (fn > aa) {
*ierr = 3;
}
znr = *zr;
zni = *zi;
csgnr = coner;
csgni = conei;
if (*zr >= 0.) {
goto L40;
}
znr = -(*zr);
zni = -(*zi);
/* -----------------------------------------------------------------------
calculate csgn=exp(fnu*pi*i) to minimize losses of significance
when fnu is large
----------------------------------------------------------------------- */
inu = (int) ((float) (*fnu));
arg = (*fnu - (double) ((float) inu)) * pi;
if (*zi < 0.) {
arg = -arg;
}
csgnr = cos(arg);
csgni = sin(arg);
if (inu % 2 == 0) {
goto L40;
}
csgnr = -csgnr;
csgni = -csgni;
L40:
zbinu_(&znr, &zni, fnu, kode, n, &cyr[1], &cyi[1], nz, &rl, &fnul, &tol, &
elim, &alim);
if (*nz < 0) {
goto L120;
}
if (*zr >= 0.) {
return 0;
}
/* -----------------------------------------------------------------------
analytic continuation to the left half plane
----------------------------------------------------------------------- */
nn = *n - *nz;
if (nn == 0) {
return 0;
}
rtol = 1. / tol;
ascle = DBL_MIN * rtol * 1e3;
for (i__ = 1; i__ <= nn; ++i__) { /* f2c-clean: s {i__1} {nn}
str = cyr(i)*csgnr - cyi(i)*csgni
cyi(i) = cyr(i)*csgni + cyi(i)*csgnr
cyr(i) = str */
aa = cyr[i__];
bb = cyi[i__];
atol = 1.;
if (fmax2(std::abs(aa), std::abs(bb)) > ascle) {
goto L55;
}
aa *= rtol;
bb *= rtol;
atol = tol;
L55:
str = aa * csgnr - bb * csgni;
sti = aa * csgni + bb * csgnr;
cyr[i__] = str * atol;
cyi[i__] = sti * atol;
csgnr = -csgnr;
csgni = -csgni;
}
return 0;
L120:
if (*nz == -2) {
goto L130;
}
*nz = 0;
*ierr = 2;
return 0;
L130:
*nz = 0;
*ierr = 5;
return 0;
L260:
*nz = 0;
*ierr = 4;
return 0;
} /* zbesi_ */
/* Subroutine */ int
zbesj_(double *zr, double *zi, double *fnu, int *kode, int *n,
double *cyr, double *cyi, int *nz, int *ierr)
{
/***begin prologue zbesj
***date written 830501 (yymmdd)
***revision date 890801, 930101 (yymmdd)
***category no. b5k
***keywords j-bessel function,bessel function of complex argument,
bessel function of first kind
***author amos, donald e., sandia national laboratories
***purpose to compute the j-bessel function of a complex argument
***description
***a double precision routine***
on kode=1, zbesj computes an n member sequence of complex
bessel functions cy(j) = J(fnu+j-1,z) for float, nonnegative
orders fnu+j-1, j=1,...,n and complex z in the cut plane
-pi < arg(z) <= pi. on kode=2, zbesj returns the scaled
functions
cy(j)=exp(-abs(y))*J(fnu+j-1,z) j = 1,...,n , y=aimag(z)
which remove the exponential growth in both the upper and
lower half planes for z to infinity. definitions and notation
are found in the nbs handbook of mathematical functions
(ref. 1).
input zr,zi,fnu are double precision
zr,zi - z=cmplx(zr,zi), -pi < arg(z) <= pi
fnu - order of initial j function, fnu >= 0
kode - a parameter to indicate the scaling option
kode= 1 returns
cy(j)=J(fnu+j-1,z), j=1,...,n
= 2 returns
cy(j)=J(fnu+j-1,z)exp(-abs(y)), j=1,...,n
n - number of members of the sequence, n >= 1
output cyr,cyi are double precision
cyr,cyi- double precision vectors whose first n components
contain float and imaginary parts for the sequence
cy(j)=J(fnu+j-1,z) or
cy(j)=J(fnu+j-1,z)exp(-abs(y)) j=1,...,n
depending on kode, y=aimag(z).
nz - number of components set to zero due to underflow,
nz= 0 , normal return
nz > 0 , last nz components of cy set zero due
to underflow, cy(j)=cmplx(0,0),
j = n-nz+1,...,n
ierr - error flag
ierr=0, normal return - computation completed
ierr=1, input error - no computation
ierr=2, overflow - no computation, aimag(z)
too large on kode=1
ierr=3, cabs(z) or fnu+n-1 large - computation done
but losses of signifcance by argument
reduction produce less than half of machine
accuracy
ierr=4, cabs(z) or fnu+n-1 too large - no computa-
tion because of complete losses of signifi-
cance by argument reduction
ierr=5, error - no computation,
algorithm termination condition not met
***long description
the computation is carried out by the formula
J(fnu,z)=exp( fnu*pi*i/2)*I(fnu,-i*z) aimag(z) >= 0.0
J(fnu,z)=exp(-fnu*pi*i/2)*I(fnu, i*z) aimag(z) < 0.0
where i**2 = -1 and I(fnu,z) is the i bessel function.
for negative orders,the formula
J(-fnu,z) = J(fnu,z)*cos(pi*fnu) - Y(fnu,z)*sin(pi*fnu)
can be used. however,for large orders close to integers, the
the function changes radically. when fnu is a large positive
int,the magnitude of J(-fnu,z)=J(fnu,z)*cos(pi*fnu) is a
large negative power of ten. but when fnu is not an int,
Y(fnu,z) dominates in magnitude with a large positive power of
ten and the most that the second term can be reduced is by
unit roundoff from the coefficient. thus, wide changes can
occur within unit roundoff of a large int for fnu. here,
large means fnu > cabs(z).
in most complex variable computation, one must evaluate ele-
mentary functions. when the magnitude of z or fnu+n-1 is
large, losses of significance by argument reduction occur.
consequently, if either one exceeds u1=sqrt(0.5/ur), then
losses exceeding half precision are likely and an error flag
ierr=3 is triggered where ur=dmax1(d1mach(4),1.0d-18) is
double precision unit roundoff limited to 18 digits precision.
if either is larger than u2=0.5/ur, then all significance is
lost and ierr=4. in order to use the int function, arguments
must be further restricted not to exceed the largest machine
int, u3=i1mach(9). thus, the magnitude of z and fnu+n-1 is
restricted by min(u2,u3). on 32 bit machines, u1,u2, and u3
are approximately 2.0e+3, 4.2e+6, 2.1e+9 in single precision
arithmetic and 1.3e+8, 1.8e+16, 2.1e+9 in double precision
arithmetic respectively. this makes u2 and u3 limiting in
their respective arithmetics. this means that one can expect
to retain, in the worst cases on 32 bit machines, no digits
in single and only 7 digits in double precision arithmetic.
similar considerations hold for other machines.
the approximate relative error in the magnitude of a complex
bessel function can be expressed by p*10**s where p=max(unit
roundoff,1.0e-18) is the nominal precision and 10**s repre-
sents the increase in error due to argument reduction in the
elementary functions. here, s=max(1,abs(log10(cabs(z))),
abs(log10(fnu))) approximately (i.e. s=max(1,abs(exponent of
cabs(z),abs(exponent of fnu)) ). however, the phase angle may
have only absolute accuracy. this is most likely to occur when
one component (in absolute value) is larger than the other by
several orders of magnitude. if one component is 10**k larger
than the other, then one can expect only max(abs(log10(p))-k,
0) significant digits; or, stated another way, when k exceeds
the exponent of p, no significant digits remain in the smaller
component. however, the phase angle retains absolute accuracy
because, in complex arithmetic with precision p, the smaller
component will not (as a rule) decrease below p times the
magnitude of the larger component. in these extreme cases,
the principal phase angle is on the order of +p, -p, pi/2-p,
or -pi/2+p.
***references handbook of mathematical functions by m. abramowitz
and i. a. stegun, nbs ams series 55, u.s. dept. of
commerce, 1955.
computation of bessel functions of complex argument
by d. e. amos, sand83-0083, may, 1983.
computation of bessel functions of complex argument
and large order by d. e. amos, sand83-0643, may, 1983
a subroutine package for bessel functions of a complex
argument and nonnegative order by d. e. amos, sand85-
1018, may, 1985
a portable package for bessel functions of a complex
argument and nonnegative order by d. e. amos, acm
trans. math. software, vol. 12, no. 3, september 1986,
pp 265-273.
***routines called zbinu,zabs,i1mach,d1mach
***end prologue zbesj */
/* Initialized data */
double hpi = 1.57079632679489662;
/* Local variables */
int i__, k, k1, k2;
double aa, bb, fn;
int nl;
double az;
int ir;
double rl, dig, cii, arg, r1m5;
int inu;
double tol, sti, zni, str, znr, alim, elim;
double atol;
int inuh;
double fnul, rtol, ascle, csgni, csgnr;