aboutsummaryrefslogtreecommitdiff
path: root/src/mtx_spherical_harmonics/sph_radial.c
blob: 32e86bbc66f8b25b95f1c13bb8d51cc732276c2f (plain)
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
/*
 * Recursive computation of (arbitrary degree) spherical Bessel/Neumann/Hankel functions, 
 * according to Gumerov and Duraiswami,
 * "The Fast Multipole Methods for the Helmholtz Equation in Three Dimensions",
 * Elsevier, 2005.
 *
 * Implementation by Franz Zotter, Institute of Electronic Music and Acoustics
 * (IEM), University of Music and Dramatic Arts (KUG), Graz, Austria
 * http://iem.at/Members/zotter, 2007.
 *
 * This code is published under the Gnu General Public License, see
 * "LICENSE.txt"
 */

#include <math.h>

#include "sph_radial.h"

#define EPS 1e-10

static void radialRecurrence (double x, double *y, int n);

// the two recurrences for higher n:
// by now no numeric stabilization for the bessel function is performed

static void radialRecurrence (double x, double *y, int n) {
   int k;
   for (k=1;k<n;k++) {
      y[k+1] = -y[k-1] + y[k]/x * (2*k+1);
   }
}

static void radialDiffRecurrence (double x, double *y1, double *yd, int n) {
   int k;
   for (k=0;k<n;k++) {
      yd[k] = y1[k]/x * n - y1[k+1];
   }
}

void sphBessel (double x, double *y, int n) { //TODO: small values!
   if (y==0) 
      return;
   if (n>=0) 
      y[0] = (x<EPS)?1.0:sin(x)/x;
   if (n>=1) 
      y[1] = -cos(x)/x + y[0]/x;
   radialRecurrence (x,y,n);
}

void sphNeumann (double x, double *y, int n) {
   if (y==0) 
      return;
   if (n>=0) 
      y[0] = -cos(x)/x;
   if (n>=1) 
      y[1] = ((x<EPS)?1.0:sin(x)/x) - y[0]/x;
   radialRecurrence (x,y,n);
}

void sphBesselDiff (double x, double *y, int n) {
   double *y1;
   if (n<0)
      return;
   if ((y1 = (double*)calloc(n+2,sizeof(double)))==0)
      return;
   sphBessel (x,y1,n+1);
   radialDiffRecurrence (x,y1,y,n);
   free(y1);
}

void sphNeumannDiff (double x, double *y, int n) {
   double *y1;
   if (n<0)
      return;
   if ((y1 = (double*)calloc(n+2,sizeof(double)))==0)
      return;
   sphNeumann (x,y,n+1);
   radialDiffRecurrence (x,y1,y,n);
   free(y1);
}