# The bessel_K function does not accept an element of a numpy array

I had a problem with classical sage ploting of Bessel K function in classical way, so I tried to manage through python matplotlib.pyplot and numpy library.

My code looks like this (for some reasons the formating put lines together, co I had to add a free line every time - just here, there are not present in original code):

import numpy as np
import matplotlib.pyplot as plt

def P(l,m,x):
return (-1)^m*gamma(l+m+1)*(1-x^2)^(m/2)/2^m/gamma(l-m+1)/gamma(m+1)*hypergeometric([m-l,m+l+1],[1+m],(1-x)/2)

N=1000
l1=-0.01996
romin=0
romax=1

a=sqrt(3).n()
R=1
m=0

roDD=R*a/sqrt(1+a^2).n()
nu=a*i/2
xDD=a*sqrt(abs(l1*(l1+1)))
ccD=1/sqrt(1+a^2)
A=P(l1,m,ccD)/bessel_K(nu,xDD)

f2=np.zeros(N)
ro2=np.linspace(roDD,romax,N)

for i in range(N):
f2[i]=A*bessel_K(nu,(1/R)*sqrt(1+a^2)*sqrt(abs(l1*(l1+1)))*ro2[i])


But it gives me the following error message:

NotImplementedError: The Function bessel_K does not support numpy arrays as arguments

If I delete " *ro2[i] ", everything works just fine. If I print for example r2 I get a real number, not an array.

Any ideas what could it be?

edit retag close merge delete

Sort by » oldest newest most voted The error message seems to be quite clear. The function bessel_K does not recognize ro2[i], which is an element of a numpy array. As a workaround, you can convert ro2[i] to a Sage data type. For example, you can replace it by RR(ro2[i]).

more