Ask Your Question
1

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

asked 2020-03-30 18:20:18 +0100

Galedon gravatar image

updated 2024-06-07 08:06:52 +0100

FrédéricC gravatar image

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[100] I get a real number, not an array.

Any ideas what could it be?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2020-04-01 04:27:42 +0100

Juanjo gravatar image

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]).

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-03-30 18:20:18 +0100

Seen: 204 times

Last updated: Apr 01 '20