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
|
// kbessel.cc: implementation of K-Bessel function for arbitrary real
//////////////////////////////////////////////////////////////////////////
//
// Copyright 1990-2023 John Cremona
//
// This file is part of the eclib package.
//
// eclib is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2 of the License, or (at your
// option) any later version.
//
// eclib is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License
// along with eclib; if not, write to the Free Software Foundation,
// Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
//
//////////////////////////////////////////////////////////////////////////
// parameter nu. Adapted from the PARI function kbessel.
#include <iostream> // for debugging only
#include <cmath>
#include <eclib/templates.h>
#include <eclib/kbessel.h>
#ifndef M_LN2
#define M_LN2 0.69314718055994530942
#endif
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define LOG2 M_LN2
#define PI M_PI
#define expo(x) ((x)==0?-1000:(long)floor(log(fabs((x)))/LOG2))
#define lbin -64 // = bit-precision of doubles (or should be)
double kbessel(double nu, double gx, int debug)
{
if(debug) {
cout << "\nCalled kbessel("<<nu<<","<<gx<<").\n";
}
double x=gx, nu2=-4*nu*nu, y;
long n=(long)(32*LOG2+PI*nu/2);
long n2=(n<<1);
if (x<n)
{
if(debug) {
cout << "In the x<n case.\n";
}
double
zf=sqrt(PI/n2),
zz=1.0/(n2<<2),
s=1.0,
t=0.0;
long k2=2*n2+1, ex;
for (long k=n2;k>0;--k)
{
k2-=2;
double p1=k2*k2+nu2;
double ak=-p1*zz/k;
s=1+ak*s;
t=k2+ak*t;
}
t/=2.0;
double u=s*zf;
double
v=-(t*zf+u*nu)/n2,
q=n2,
r=x+x;
if(debug) {
cout << "Finished k loop. lbin = "<<lbin<<endl;
cout << "t,u,v,q,r = "<<t<<", "<<u<<", "<<v<<", "<<q<<", "<<r<<endl;
}
do
{
double p1=5.0/q;
if (expo(p1)>= -1) p1=0.5;
double p2=1.0-r/q;
if (p1>p2) p1=p2;
double
c=-p1,
d=1,
e=u,
f=v;
long k=1;
if(debug) {
cout << "...outer loop: p1 = "<<p1<<", expo(p1) = "<<expo(p1)<<endl;
}
do
{
double w=(((k-0.5)*u)+((q-k)*v));
w+=nu*(u-(v+v));
u=q*v/k;
v=w/k;
d*=c;
e+=d*u;
f+=(p1=(d*v));
k++;
ex=expo(p1)-expo(f);
if(debug) {
cout << "......inner loop: f = "<<f<<", expo(f) = "<<expo(f)<<endl;
cout << " p1= "<<p1<<", expo(p1) = "<<expo(p1)<<", ex = "<<ex<<endl;
}
}
while(ex>lbin);
if(debug) {
cout << "...finished inner loop -- ex = "<<ex<<endl;
}
p1=u;u=e;e=p1;p1=v;v=f;f=p1;
q*=(1+c);
p1=q-r; ex=expo(p1);
if(debug) {
cout << "...outer loop -- ex = "<<ex<<endl;
cout << "u,q,r,p1 = " << u<<", "<<q<<", "<<r<<", "<<r<< endl;
}
}
while(ex>lbin);
y=u*pow((x/n),nu);
}
else // x>=n
{
if(debug) {
cout << "In the x>=n case.\n";
}
double p2=2*x;
double
zf=sqrt(PI/p2),
zz=1.0/(4*p2),
s=1.0;
long k2=2*n2+1;
for (long k=n2;k>0;--k)
{
k2-=2;
double p1=k2*k2+nu2;
double ak=(p1*zz)/k;
s=1.0-(ak*s);
}
y=s*zf;
}
y/=exp(x);
if(debug) {
cout << "kbessel returns " << y << endl;
}
return y;
}
|