Search code examples
pythonscipytypeerrorbessel-functions

Python TypeError: ufunc 'k0' using Scipy.special.k0


Hoping to get some guidance with my code, I'm quite new to python and I'm having difficulties with the modified bessel function k0 in my code. The actual code is split into two parts

1) The first section uses sympy to solve t and return the value of x0 at t=0.

2) The second part uses this value x0 to solve the t for different values of x.

It is at the second step that I'm having difficulites the scipy.special.k0 functions does not seem to work for the stated solved x0 value and I'm not sure why

    from __future__ import division 
    import sympy as sy
    from sympy import besselk, log, nsolve
    import math
    import numpy as np
    import scipy.special as sp
    Tc = 9.2
    Tb = 5.2
    tb = Tb / Tc
    print 'Value of tb is:'+' '+ str(tb)
    L = 100*10**-9
    W = 100*10**-9
    n = 3*10**-6
    r1 = W / 2
    print 'value of r1 is:'+' '+ str(r1)
    x1 = r1 /n
    print 'value of x1 is'+ ' '+ str(x1)
    S = math.sqrt(3 / (1+ tb + tb**2))
    print 'value of S is:'+' ' + str(S)
    B = (math.pi / 2)*(1+(L/W))
    print 'value of B is:'+' ' + str(B)
    E = [x /10 for x in range (1, 2, 1)]
    x0 = sy.symbols('x0')
    i = (S*x0)*(1-tb**3)*besselk(0, x0) /  3*(log(x0/x1)+B) * besselk(0, S*x0)
    t = - i*( (log(x0/x1)*B)**2 - (log(x0/x1)+B)**2 )
    X0 = nsolve(t, E)
    G = sp.k0(X0)
    print 'value of x0 is:' +' '+ str(X0)

When ever I run the code it results in the following error

TypeError: ufunc 'k0' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Any help is greatly appreciated and thank you in advance.


Solution

  • X0 is not a regular floating point value; it is an mpf-type from mpmath (which is included as part of sympy). scipy.special.k0 doesn't know what that is. Change this:

    G = sp.k0(X0)
    

    to this:

    G = sp.k0(float(X0))