source: sasview/src/sas/models/c_extension/cephes/jn.c @ 573e7034

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 573e7034 was 79492222, checked in by krzywon, 10 years ago

Changed the file and folder names to remove all SANS references.

  • Property mode set to 100644
File size: 2.0 KB
Line 
1/*                                                      jn.c
2 *
3 *      Bessel function of integer order
4 *
5 *
6 *
7 * SYNOPSIS:
8 *
9 * int n;
10 * double x, y, jn();
11 *
12 * y = jn( n, x );
13 *
14 *
15 *
16 * DESCRIPTION:
17 *
18 * Returns Bessel function of order n, where n is a
19 * (possibly negative) integer.
20 *
21 * The ratio of jn(x) to j0(x) is computed by backward
22 * recurrence.  First the ratio jn/jn-1 is found by a
23 * continued fraction expansion.  Then the recurrence
24 * relating successive orders is applied until j0 or j1 is
25 * reached.
26 *
27 * If n = 0 or 1 the routine for j0 or j1 is called
28 * directly.
29 *
30 *
31 *
32 * ACCURACY:
33 *
34 *                      Absolute error:
35 * arithmetic   range      # trials      peak         rms
36 *    DEC       0, 30        5500       6.9e-17     9.3e-18
37 *    IEEE      0, 30        5000       4.4e-16     7.9e-17
38 *
39 *
40 * Not suitable for large n or x. Use jv() instead.
41 *
42 */
43
44/*                                                      jn.c
45Cephes Math Library Release 2.8:  June, 2000
46Copyright 1984, 1987, 2000 by Stephen L. Moshier
47*/
48#include "mconf.h"
49#ifdef ANSIPROT
50extern double fabs ( double );
51extern double j0 ( double );
52extern double j1 ( double );
53#else
54double fabs(), j0(), j1();
55#endif
56extern double MACHEP;
57
58double jn( n, x )
59int n;
60double x;
61{
62double pkm2, pkm1, pk, xk, r, ans;
63int k, sign;
64
65if( n < 0 )
66        {
67        n = -n;
68        if( (n & 1) == 0 )      /* -1**n */
69                sign = 1;
70        else
71                sign = -1;
72        }
73else
74        sign = 1;
75
76if( x < 0.0 )
77        {
78        if( n & 1 )
79                sign = -sign;
80        x = -x;
81        }
82
83if( n == 0 )
84        return( sign * j0(x) );
85if( n == 1 )
86        return( sign * j1(x) );
87if( n == 2 )
88        return( sign * (2.0 * j1(x) / x  -  j0(x)) );
89
90if( x < MACHEP )
91        return( 0.0 );
92
93/* continued fraction */
94#ifdef DEC
95k = 56;
96#else
97k = 53;
98#endif
99
100pk = 2 * (n + k);
101ans = pk;
102xk = x * x;
103
104do
105        {
106        pk -= 2.0;
107        ans = pk - (xk/ans);
108        }
109while( --k > 0 );
110ans = x/ans;
111
112/* backward recurrence */
113
114pk = 1.0;
115pkm1 = 1.0/ans;
116k = n-1;
117r = 2 * k;
118
119do
120        {
121        pkm2 = (pkm1 * r  -  pk * x) / x;
122        pk = pkm1;
123        pkm1 = pkm2;
124        r -= 2.0;
125        }
126while( --k > 0 );
127
128if( fabs(pk) > fabs(pkm1) )
129        ans = j1(x)/pk;
130else
131        ans = j0(x)/pkm1;
132return( sign * ans );
133}
Note: See TracBrowser for help on using the repository browser.