vnl_bessel.cxx
Go to the documentation of this file.
1 // This is core/vnl/vnl_bessel.cxx
2 #include <algorithm>
3 #include "vnl_bessel.h"
4 
5 //:
6 // \file
7 // \brief Bessel functions of the first kind
8 // \author Tim Cootes
9 
10 //: Compute Bessel functions of first kind up to order n_max.
11 // On exit, J[i] = J_i(x) for i=0..n_max
12 //
13 // Uses recurrence relation: J_(n-1)(x)+J_(n+1)=(2n/x)J_n(x)
14 // Thus J_n(x) = (2(n+1)/x)J_(n+1)(x) - J_(n+2)(x)
15 // Start with arbitrary guess for high n and work backwards.
16 // Normalise suitably.
17 void vnl_bessel(unsigned n_max, double x, vnl_vector<double>& J)
18 {
19  if (x==0.0)
20  {
21  J.set_size(1+n_max);
22  J.fill(0.0);
23  J[0]=1.0;
24  return;
25  }
26  int nhi = 2*((std::max(int(n_max),int(x))+15)/2+1);
27  vnl_vector<double> j(nhi+1);
28  j[nhi]=0.0;
29  j[nhi-1]=1.0;
30  for (int m=nhi-2; m>=0; --m)
31  j[m]=2*(m+1)*j[m+1]/x - j[m+2];
32 
33  // Normalise and return first (1+n_max) values
34  double sum=j[0];
35  for (int m=2;m<=nhi;m+=2) sum+=2*j[m];
36 
37  J.set_size(1+n_max);
38  for (unsigned int m=0; m<=n_max; ++m) J[m]=j[m]/sum;
39 }
40 
41 //: Returns J_0(x), the value of the Bessel function of order 0 at x.
42 // Bessel function of the first kind of order zero.
43 //
44 // Uses recurrence relation: J_(n-1)(x)+J_(n+1)=(2n/x)J_n(x)
45 double vnl_bessel0(double x)
46 {
47  if (x==0) return 1.0;
48  int nhi = 2*((int(x)+15)/2); // Even
49  double j3=0.0;
50  double j2=1.0;
51  double j0=j2,j1;
52  double even_sum=j2;
53  for (int i=nhi;i>=0;i-=2)
54  {
55  // j0 is i-th term, j1 is i+1-th etc
56  j1=2*(i+2)*j2/x - j3;
57  j0=2*(i+1)*j1/x - j2;
58  even_sum+=j0;
59  j3=j1;
60  j2=j0;
61  }
62  return j0/(2*even_sum-j0);
63 }
64 
65 //: Returns J_n(x), the value of the Bessel function of order n at x.
66 // Bessel function of the first kind of order zero.
67 //
68 // Uses recurrence relation: J_(n-1)(x)+J_(n+1)=(2n/x)J_n(x)
69 double vnl_bessel(unsigned n, double x)
70 {
71  if (x==0)
72  {
73  if (n==0) return 1.0;
74  else return 0.0;
75  }
76 
77  int nhi = 2*((std::max(int(n),int(x))+15)/2+1);
78  double j3=0.0;
79  double j2=1.0;
80  double j0=j2,j1;
81  double even_sum=j2;
82  double jn=j0;
83  for (int i=nhi; i>=0; i-=2)
84  {
85  // j0 is i-th term, j1 is i+1-th etc
86  j1=2*(i+2)*j2/x - j3;
87  j0=2*(i+1)*j1/x - j2;
88  even_sum+=j0;
89  j3=j1;
90  j2=j0;
91  if ((unsigned int)i==n) jn=j0;
92  else if ((unsigned int)i+1==n) jn=j1;
93  }
94  return jn/(2*even_sum-j0);
95 }
bool set_size(size_t n)
Resize to n elements.
Definition: vnl_vector.hxx:250
#define m
Definition: vnl_vector.h:43
void vnl_bessel(unsigned n_max, double x, vnl_vector< double > &J)
Compute Bessel functions of first kind up to order n_max.
Definition: vnl_bessel.cxx:17
vnl_vector & fill(T const &v)
Set all values to v.
Definition: vnl_vector.hxx:314
double vnl_bessel0(double x)
Returns J_0(x), the value of the Bessel function of order 0 at x.
Definition: vnl_bessel.cxx:45
vnl_decnum max(vnl_decnum const &x, vnl_decnum const &y)
Definition: vnl_decnum.h:412
Bessel functions of the first kind.