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
|
// kbessel.cc: implementation of K-Bessel function for arbitrary real
//////////////////////////////////////////////////////////////////////////
//
// Copyright 1990-2012 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>
using namespace std;
#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)
{
double x,y,p1,p2,zf,zz,s,t,q,r,u,v,e,f,c,d,ak;
double nu2,w;
long k,k2,n2,n,ex;
if(debug) {
cout << "\nCalled kbessel("<<nu<<","<<gx<<").\n";
}
k=0;x=gx;
nu2=-4*nu*nu;
n=(long)(32*LOG2+PI*nu/2);
n2=(n<<1);
if (x<n)
{
if(debug) {
cout << "In the x<n case.\n";
}
zf=sqrt(PI/n2);
zz=1.0/(n2<<2);
s=1.0;t=0.0;k2=2*n2+1;
for (k=n2;k>0;--k)
{
k2-=2;
p1=k2*k2+nu2;
ak=-p1*zz/k;
s=1+ak*s;
t=k2+ak*t;
}
u=s*zf; t/=2.0;
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
{
p1=5.0/q;
if (expo(p1)>= -1) p1=0.5;
p2=1.0-r/q; if (p1>p2) p1=p2;
c=-p1; k=1; d=1; e=u; f=v;
if(debug) {
cout << "...outer loop: p1 = "<<p1<<", expo(p1) = "<<expo(p1)<<endl;
}
do
{
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
{
if(debug) {
cout << "In the x>=n case.\n";
}
p2=2*x;
zf=sqrt(PI/p2);
zz=1.0/(4*p2);
s=1.0; k2=2*n2+1;
for (k=n2;k>0;--k)
{
k2-=2;
p1=k2*k2+nu2;
ak=(p1*zz)/k;
s=1.0-(ak*s);
}
y=s*zf;
}
y/=exp(x);
if(debug) {
cout << "kbessel returns " << y << endl;
}
return y;
}
|