0
votes

im writing a programm for a fitting routine and am currently optimizing the code for faster calculations. The weakes point is a part, where i have to calculate a big amount of bessel functions, which takes around 0.7 s. In my case q has 177 entries, th 100 and R 400.

  Js = zeros(numel(th),numel(q)); tR=sin(th')*R;
  for k = 1:numel(q)
  Js(:,k) = sum(tn.*besselj(0,q(k)*tR),2);
  end

I also tried to make a 3D-Matrix, but it takes slightly longer to calculate.

[Q,T,RR]=meshgrid(q,sin(th),R);
Js1 = besselj(0,Q.*T.*RR);

So, i'm wondering, is there a way to calculate these besselfunctions faster? thanks in advance, kuy

1
I don't think there are. You're limited by the use of the built-in besselj. - rayryeng
Have you tried using bsxfun? - flawr
What are the sizes of the inputs? How many dimensions do each of the inputs have? Which ones are vectors and again are they row or column vectors and which ones are higher dim matrices? Telling us the count of entries doesn't give us that info. - Divakar
@Divakar the matrix, i need to calculate besselfunctions from is of the size 177x100x400 (as an order of magnitude - it will vary a bit), so the vectors are all onedimensional. - kuy
@flawr, i'm not sure how to use bsxfun in this case, do you mean the same approach as rahnema1 (in the answer) - kuy

1 Answers

0
votes

Wolfram shows a special case of Bessel function where the first argument of the function is 0. So we can pre-compute some variables like sgn_cum and km2 to simplify the computation:

n = 10;
k = 0 : n;
sgn_cum = ((-1 * 0.25) .^ k ./ cumprod([1 1:n]).^2).';
km2 = 2*k;

Given a column vector z we can obtain Bessel function as:

bsxfun(@power,z,km2 ) * sgn_cum

Example:

z= (1:5).';
result = bsxfun(@power,z,km2 ) * sgn_cum;

We can decrease n to speed up the computation but with the cost of lower precision.