File: kn.cpp

package info (click to toggle)
libitpp 4.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 7,520 kB
  • ctags: 6,341
  • sloc: cpp: 51,608; sh: 9,248; makefile: 636; fortran: 8
file content (276 lines) | stat: -rw-r--r-- 5,987 bytes parent folder | download
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
/*!
 * \file
 * \brief Implementation of modified Bessel functions of third kind
 * \author Tony Ottosson
 *
 * -------------------------------------------------------------------------
 *
 * IT++ - C++ library of mathematical, signal processing, speech processing,
 *        and communications classes and functions
 *
 * Copyright (C) 1995-2008  (see AUTHORS file for a list of contributors)
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * -------------------------------------------------------------------------
 *
 * This is slightly modified routine from the Cephes library:
 * http://www.netlib.org/cephes/
 */

#include <itpp/base/bessel/bessel_internal.h>
#include <itpp/base/itassert.h>


/*
 * Modified Bessel function, third kind, integer order
 *
 * double x, y, kn();
 * int n;
 *
 * y = kn( n, x );
 *
 * DESCRIPTION:
 *
 * Returns modified Bessel function of the third kind
 * of order n of the argument.
 *
 * The range is partitioned into the two intervals [0,9.55] and
 * (9.55, infinity).  An ascending power series is used in the
 * low range, and an asymptotic expansion in the high range.
 *
 * ACCURACY:
 *
 *                      Relative error:
 * arithmetic   domain     # trials      peak         rms
 *    IEEE      0,30        90000       1.8e-8      3.0e-10
 *
 *  Error is high only near the crossover point x = 9.55
 * between the two expansions used.
 */


/*
  Cephes Math Library Release 2.8:  June, 2000
  Copyright 1984, 1987, 1988, 2000 by Stephen L. Moshier
*/


/*
Algorithm for Kn.
                       n-1
                   -n   -  (n-k-1)!    2   k
K (x)  =  0.5 (x/2)     >  -------- (-x /4)
 n                      -     k!
                       k=0

                    inf.                                   2   k
       n         n   -                                   (x /4)
 + (-1)  0.5(x/2)    >  {p(k+1) + p(n+k+1) - 2log(x/2)} ---------
                     -                                  k! (n+k)!
                    k=0

where  p(m) is the psi function: p(1) = -EUL and

                      m-1
                       -
      p(m)  =  -EUL +  >  1/k
                       -
                      k=1

For large x,
                                         2        2     2
                                      u-1     (u-1 )(u-3 )
K (z)  =  sqrt(pi/2z) exp(-z) { 1 + ------- + ------------ + ...}
 v                                        1            2
                                    1! (8z)     2! (8z)
asymptotically, where

           2
    u = 4 v .

*/


#define EUL 5.772156649015328606065e-1
#define MAXFAC 31

#define MACHEP 1.11022302462515654042E-16   /* 2**-53 */
#define MAXLOG 7.08396418532264106224E2     /* log 2**1022 */
#define MINLOG -7.08396418532264106224E2    /* log 2**-1022 */
#define MAXNUM 1.79769313486231570815E308    /* 2**1024*(1-MACHEP) */
#define PI 3.14159265358979323846       /* pi */


double kn(int nn, double x)
{
  double k, kf, nk1f, nkf, zn, t, s, z0, z;
  double ans, fn, pn, pk, zmn, tlg, tox;
  int i, n;

  if( nn < 0 )
    n = -nn;
  else
    n = nn;

  if( n > MAXFAC )
    {
    overf:
      it_warning("kn(): overflow range error");
      return( MAXNUM );
    }

  if( x <= 0.0 )
    {
      if( x < 0.0 )
	it_warning("kn(): argument domain error");
      else
	it_warning("kn(): function singularity");
      return( MAXNUM );
    }


  if( x > 9.55 )
    goto asymp;

  ans = 0.0;
  z0 = 0.25 * x * x;
  fn = 1.0;
  pn = 0.0;
  zmn = 1.0;
  tox = 2.0/x;

  if( n > 0 )
    {
      /* compute factorial of n and psi(n) */
      pn = -EUL;
      k = 1.0;
      for( i=1; i<n; i++ )
	{
	  pn += 1.0/k;
	  k += 1.0;
	  fn *= k;
	}

      zmn = tox;

      if( n == 1 )
	{
	  ans = 1.0/x;
	}
      else
	{
	  nk1f = fn/n;
	  kf = 1.0;
	  s = nk1f;
	  z = -z0;
	  zn = 1.0;
	  for( i=1; i<n; i++ )
	    {
	      nk1f = nk1f/(n-i);
	      kf = kf * i;
	      zn *= z;
	      t = nk1f * zn / kf;
	      s += t;
	      if( (MAXNUM - fabs(t)) < fabs(s) )
		goto overf;
	      if( (tox > 1.0) && ((MAXNUM/tox) < zmn) )
		goto overf;
	      zmn *= tox;
	    }
	  s *= 0.5;
	  t = fabs(s);
	  if( (zmn > 1.0) && ((MAXNUM/zmn) < t) )
	    goto overf;
	  if( (t > 1.0) && ((MAXNUM/t) < zmn) )
	    goto overf;
	  ans = s * zmn;
	}
    }


  tlg = 2.0 * log( 0.5 * x );
  pk = -EUL;
  if( n == 0 )
    {
      pn = pk;
      t = 1.0;
    }
  else
    {
      pn = pn + 1.0/n;
      t = 1.0/fn;
    }
  s = (pk+pn-tlg)*t;
  k = 1.0;
  do
    {
      t *= z0 / (k * (k+n));
      pk += 1.0/k;
      pn += 1.0/(k+n);
      s += (pk+pn-tlg)*t;
      k += 1.0;
    }
  while( fabs(t/s) > MACHEP );

  s = 0.5 * s / zmn;
  if( n & 1 )
    s = -s;
  ans += s;

  return(ans);



  /* Asymptotic expansion for Kn(x) */
  /* Converges to 1.4e-17 for x > 18.4 */

 asymp:

  if( x > MAXLOG )
    {
      it_warning("kn(): underflow range error");
      return(0.0);
    }
  k = n;
  pn = 4.0 * k * k;
  pk = 1.0;
  z0 = 8.0 * x;
  fn = 1.0;
  t = 1.0;
  s = t;
  nkf = MAXNUM;
  i = 0;
  do
    {
      z = pn - pk * pk;
      t = t * z /(fn * z0);
      nk1f = fabs(t);
      if( (i >= n) && (nk1f > nkf) )
	{
	  goto adone;
	}
      nkf = nk1f;
      s += t;
      fn += 1.0;
      pk += 2.0;
      i += 1;
    }
  while( fabs(t/s) > MACHEP );

 adone:
  ans = exp(-x) * sqrt( PI/(2.0*x) ) * s;
  return(ans);
}