BGC Tools
Data Structures | Static Public Member Functions | Static Private Member Functions
BGC.Mathematics.Bessel Class Reference

Handles Calculation of the Modified Bessel Function of the First Kind More...

Data Structures

class  BesselCache
 
class  DoubleBesselCache
 

Static Public Member Functions

static float Bessi (int order, float argument, BesselCache cache=null)
 Returns the modified Bessel function I_(x) for integer order >= 0 and real argument Caches all return values for the last argument used. Flushes cache when a new argument is used. More...
 
static double Bessi (int order, double argument, DoubleBesselCache cache=null)
 Returns the modified Bessel function I_(x) for integer order >= 0 and real argument Caches all return values for the last argument used. Flushes cache when a new argument is used. More...
 

Static Private Member Functions

static double Bessi0 (double x)
 Evaluate modified Bessel function In(x) and order n=0. More...
 
static double Bessi1 (double x)
 Evaluate modified Bessel function In(x) and order n=1. More...
 

Detailed Description

Handles Calculation of the Modified Bessel Function of the First Kind

Definition at line 9 of file Bessel.cs.

Member Function Documentation

◆ Bessi() [1/2]

static float BGC.Mathematics.Bessel.Bessi ( int  order,
float  argument,
BesselCache  cache = null 
)
inlinestatic

Returns the modified Bessel function I_(x) for integer order >= 0 and real argument Caches all return values for the last argument used. Flushes cache when a new argument is used.

Adapted from: "Numerical Recipes in C", Second Edition, Press, WH et al., Cambridge University Press, page 237

Parameters
ordernon-negative integer order
argumentreal-valued argument
Returns

Definition at line 69 of file Bessel.cs.

Referenced by BGC.Audio.Synthesis.STMAudioClip.GetExpBandAmplitude().

73  {
74  if (cache != null)
75  {
76  if (argument != cache.LastArgument)
77  {
78  cache.ClearCache(argument);
79  cache[0] = Bessi0(argument);
80  }
81 
82  if (cache.Contains(order))
83  {
84  return (float)cache[order];
85  }
86  }
87 
88  if (order < 0)
89  {
90  throw new ArgumentOutOfRangeException($"Bessel order must be greater than or equal to 0. Received: {order}");
91  }
92 
93  //Cast argument up to a double
94  double x = argument;
95 
96  double besselValue;
97 
98  if (order == 0)
99  {
100  besselValue = Bessi0(x);
101  }
102  else if (order == 1)
103  {
104  besselValue = Bessi1(x);
105  }
106  else if (x == 0.0)
107  {
108  return 0f;
109  }
110  else
111  {
112  const double ACC = 40.0;
113  const double BIGNO = 1.0e10;
114  const double BIGNI = 1.0e-10;
115 
116  double bim;
117  double bi = 1.0;
118  double tox = 2.0 / Math.Abs(x);
119  double bip = 0.0;
120  double ans = 0.0;
121 
122  for (int j = 2 * (order + (int)Math.Sqrt(ACC * order)); j > 0; j--)
123  {
124  bim = bip + j * tox * bi;
125  bip = bi;
126  bi = bim;
127  if (Math.Abs(bi) > BIGNO)
128  {
129  ans *= BIGNI;
130  bi *= BIGNI;
131  bip *= BIGNI;
132  }
133 
134  if (j == order)
135  {
136  ans = bip;
137  }
138  }
139 
140  //Normalize with the 0th order term
141  if (cache != null)
142  {
143  ans *= cache[0] / bi;
144  }
145  else
146  {
147  ans *= Bessi0(argument) / bi;
148  }
149  besselValue = x < 0.0 && order % 2 == 1 ? -ans : ans;
150  }
151 
152  if (cache != null)
153  {
154  cache[order] = besselValue;
155  }
156 
157  return (float)besselValue;
158  }
static double Bessi1(double x)
Evaluate modified Bessel function In(x) and order n=1.
Definition: Bessel.cs:305
static double Bessi0(double x)
Evaluate modified Bessel function In(x) and order n=0.
Definition: Bessel.cs:272
Here is the caller graph for this function:

◆ Bessi() [2/2]

static double BGC.Mathematics.Bessel.Bessi ( int  order,
double  argument,
DoubleBesselCache  cache = null 
)
inlinestatic

Returns the modified Bessel function I_(x) for integer order >= 0 and real argument Caches all return values for the last argument used. Flushes cache when a new argument is used.

Adapted from: "Numerical Recipes in C", Second Edition, Press, WH et al., Cambridge University Press, page 237

Parameters
ordernon-negative integer order
argumentreal-valued argument
Returns

Definition at line 172 of file Bessel.cs.

176  {
177  if (cache != null)
178  {
179  if (argument != cache.LastArgument)
180  {
181  cache.ClearCache(argument);
182  cache[0] = Bessi0(argument);
183  }
184 
185  if (cache.Contains(order))
186  {
187  return cache[order];
188  }
189  }
190 
191  if (order < 0)
192  {
193  throw new ArgumentOutOfRangeException($"Bessel order must be greater than or equal to 0. Received: {order}");
194  }
195 
196  //Cast argument up to a double
197  double x = argument;
198 
199  double besselValue;
200 
201  if (order == 0)
202  {
203  besselValue = Bessi0(x);
204  }
205  else if (order == 1)
206  {
207  besselValue = Bessi1(x);
208  }
209  else if (x == 0.0)
210  {
211  return 0.0;
212  }
213  else
214  {
215  const double ACC = 40.0;
216  const double BIGNO = 1.0e10;
217  const double BIGNI = 1.0e-10;
218 
219  double bim;
220  double bi = 1.0;
221  double tox = 2.0 / Math.Abs(x);
222  double bip = 0.0;
223  double ans = 0.0;
224 
225  for (int j = 2 * (order + (int)Math.Sqrt(ACC * order)); j > 0; j--)
226  {
227  bim = bip + j * tox * bi;
228  bip = bi;
229  bi = bim;
230  if (Math.Abs(bi) > BIGNO)
231  {
232  ans *= BIGNI;
233  bi *= BIGNI;
234  bip *= BIGNI;
235  }
236 
237  if (j == order)
238  {
239  ans = bip;
240  }
241  }
242 
243  //Normalize with the 0th order term
244  if (cache != null)
245  {
246  ans *= cache[0] / bi;
247  }
248  else
249  {
250  ans *= Bessi0(argument) / bi;
251  }
252  besselValue = x < 0.0 && order % 2 == 1 ? -ans : ans;
253  }
254 
255  if (cache != null)
256  {
257  cache[order] = besselValue;
258  }
259 
260  return besselValue;
261  }
static double Bessi1(double x)
Evaluate modified Bessel function In(x) and order n=1.
Definition: Bessel.cs:305
static double Bessi0(double x)
Evaluate modified Bessel function In(x) and order n=0.
Definition: Bessel.cs:272

◆ Bessi0()

static double BGC.Mathematics.Bessel.Bessi0 ( double  x)
inlinestaticprivate

Evaluate modified Bessel function In(x) and order n=0.

Adapted from: "Numerical Recipes in C", Second Edition, Press, WH et al., Cambridge University Press, page 237

Parameters
xModified Bessel Function Argument
Returns
Modified Bessel Function of order n=0 at Argument

Definition at line 272 of file Bessel.cs.

273  {
274  double ax, ans;
275  double y;
276 
277  if ((ax = Math.Abs(x)) < 3.75)
278  {
279  y = x / 3.75;
280  y *= y;
281  ans = 1.0 + y * (3.5156229 + y * (3.0899424 + y * (1.2067492
282  + y * (0.2659732 + y * (0.360768e-1 + y * 0.45813e-2)))));
283  }
284  else
285  {
286  y = 3.75 / ax;
287  ans = (Math.Exp(ax) / Math.Sqrt(ax)) * (0.39894228 + y * (0.1328592e-1
288  + y * (0.225319e-2 + y * (-0.157565e-2 + y * (0.916281e-2
289  + y * (-0.2057706e-1 + y * (0.2635537e-1 + y * (-0.1647633e-1
290  + y * 0.392377e-2))))))));
291  }
292 
293  return ans;
294  }

◆ Bessi1()

static double BGC.Mathematics.Bessel.Bessi1 ( double  x)
inlinestaticprivate

Evaluate modified Bessel function In(x) and order n=1.

Adapted from: "Numerical Recipes in C", Second Edition, Press, WH et al., Cambridge University Press, page 237

Parameters
xModified Bessel Function Argument
Returns
Modified Bessel Function of order n=1 at Argument

Definition at line 305 of file Bessel.cs.

306  {
307  double ax, ans;
308  double y;
309 
310  if ((ax = Math.Abs(x)) < 3.75)
311  {
312  y = x / 3.75;
313  y *= y;
314  ans = ax * (0.5 + y * (0.87890594 + y * (0.51498869 + y * (0.15084934
315  + y * (0.2658733e-1 + y * (0.301532e-2 + y * 0.32411e-3))))));
316  }
317  else
318  {
319  y = 3.75 / ax;
320  ans = 0.2282967e-1 + y * (-0.2895312e-1 + y * (0.1787654e-1
321  - y * 0.420059e-2));
322  ans = 0.39894228 + y * (-0.3988024e-1 + y * (-0.362018e-2
323  + y * (0.163801e-2 + y * (-0.1031555e-1 + y * ans))));
324  ans *= (Math.Exp(ax) / Math.Sqrt(ax));
325  }
326 
327  return x < 0.0 ? -ans : ans;
328  }

The documentation for this class was generated from the following file: