Re: (very) slow recursive function



"Ralf Schaa" <schaa@xxxxxxxxxxxxxxxx> wrote in message
news:1162260932.671131.148230@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

For whom is interested: here is a program that calculates the modified
Bessel function of the first kind with fractional order. Works fine and
is recursive but really slow for order>20.

similar explanations as before.
But note also that you calculate four exponentials where
only two are necessary.

pure recursive function Inu(z,nu) result(I_nu)

! Computes the modified Besselfunction of
! order 1/2, 3/2, ... , n/2 with complex argument
! using a recursion relationship together with
! explicit formulas for I_1/2 and I_3/2.
!
! Works fine for nu < 20.

implicit none

complex(dc), intent(in) :: z ! complex argument
real(dp), intent(in) :: nu ! fractional order
complex(dc) :: I_nu ! function result

complex(dc) :: sinh_z ! sinh for complex arguments
complex(dc) :: cosh_z ! cosh for complex arguments

sinh_z = (exp(z)-exp(-z))/2.0d0
cosh_z = (exp(z)+exp(-z))/2.0d0

! mind divisions with 'z' -> z cannot be 0 !
if(nu == 0.5d0) then
I_nu = sqrt(2.0d0/PI)*sinh_z/sqrt(z)
elseif(nu == 1.5d0) then
I_nu = (2.0d0*cosh_z - 2.0d0*sinh_z/z)/sqrt(2.0d0*PI*z)
else
I_nu = Inu(z,nu-2.0d0) - 2.0d0*(nu-1.0d0)*Inu(z,nu-1.0d0)/z
endif
end function Inu


.