On Mon, Nov 23, 2015 at 1:38 PM, Emile59 <[email protected]> wrote: > Hello, > > I am currently developping a python program to tranform a symbolic > expression computed by `sympy` into a `numpy` array containing all the > numerical values. I instantiate the symbolic expression with the > `sympy.lambdify` function. > > Some of the symbolic expressions contain Bessel functions, and I pass > `scipy.special.jv/jy` etc. as parameters in the `lambdify` function. Here is > the single code (appart from the program) : > > m = Symbol('m') > expr= > -1.06048319593874*(-(3.14159265358979*m*besselj(27.9937791601866*m, > 4.46681007624482*sqrt(-I)) - > 0.501286290793831*sqrt(-I)*besselj(27.9937791601866*m + 1, > 4.46681007624482*sqrt(-I)))*bessely(27.9937791601866*m, > 6.81776274795262*sqrt(-I))/(3.14159265358979*m*bessely(27.9937791601866*m, > 4.46681007624482*sqrt(-I)) - > 0.501286290793831*sqrt(-I)*bessely(27.9937791601866*m + 1, > 4.46681007624482*sqrt(-I))) + besselj(27.9937791601866*m, > 6.81776274795262*sqrt(-I)))*sin(0.942966379693359*m)*cos(1.5707963267949*m)/m > nm = 5 > vm = arange(nm) +1 > bessel = {'besselj':jv,'besselk':kv,'besseli':iv,'bessely':yv} > libraries = [bessel, "numpy"] > result = lambdify(m, expr, modules=libraries)(vm) > > In[1] : result > Out[1]: > array([ -7.51212638e-030 -3.22606326e-030j, > 4.81143544e-046 +1.04405860e-046j, > 1.97977798e-097 +3.02047228e-098j, > 3.84986092e-124 +4.73598141e-125j, > 1.12934434e-181 +1.21145535e-182j]) > > > > The result is as I expected : a 5 rows 1-d array with each integer value of > the symbol `m`. > > The problem is when I try to implement it in the program. Here is the > implementation : > > expr = list_symbolic_expr[index] > vcm = arange(Dim_col[0]) +1 #Dim_col = list of integer range values to > instantiate > m = Symbol(str(Var_col[0])) #Var_col = list of symbolic integer > parameters > print expr, m > smat = lambdify(m, expr, modules=libraries)(vcm) > > > > Here is the error when using `scipy.special` bessel functions : > > In [2]: run Get_System_Num > Out [2]: > -1.06048319593874*(-(3.14159265358979*m*besselj(27.9937791601866*m, > 4.46681007624482*sqrt(-I)) - > 0.501286290793831*sqrt(-I)*besselj(27.9937791601866*m + 1, > 4.46681007624482*sqrt(-I)))*bessely(27.9937791601866*m, > 6.81776274795262*sqrt(-I))/(3.14159265358979*m*bessely(27.9937791601866*m, > 4.46681007624482*sqrt(-I)) - > 0.501286290793831*sqrt(-I)*bessely(27.9937791601866*m + 1, > 4.46681007624482*sqrt(-I))) + besselj(27.9937791601866*m, > 6.81776274795262*sqrt(-I)))*sin(0.942966379693359*m)*cos(1.5707963267949*m)/m > m > File "Numeric\Get_System_Num.py", line 183, in get_Mat > smat = lambdify(m, expr, modules=libraries)(vcm) > > File "<string>", line 1, in <lambda> > > TypeError: ufunc 'jv' not supported for the input types, and the inputs > could not be safely coerced to any supported types according to the casting > rule ''safe''
The only thing that I can see that would be different here is the size of the input array. What is Dim_col[0]? Note that for me, anything >= 8 gives nan in the result (due to precision loss). It looks like the default behavior of the "safe" casting has changed in recent versions of numpy (https://docs.scipy.org/doc/numpy/reference/generated/numpy.can_cast.html), so the version of numpy you are using is important. What versions of numpy and scipy are you using? > > > > Here is the error when using `sympy.special` bessel functions : > > > File "Numeric\Get_System_Num.py", line 183, in get_Mat > smat = lambdify(m, expr, modules=libraries)(vcm) > > File "<string>", line 1, in <lambda> > > File > "C:\Users\Emile\Anaconda2\lib\site-packages\sympy\core\function.py", line > 375, in __new__ > result = super(Function, cls).__new__(cls, *args, **options) > > File > "C:\Users\Emile\Anaconda2\lib\site-packages\sympy\core\function.py", line > 199, in __new__ > evaluated = cls.eval(*args) > > File > "C:\Users\Emile\Anaconda2\lib\site-packages\sympy\functions\special\bessel.py", > line 161, in eval > if nu.is_integer: > > AttributeError: 'numpy.ndarray' object has no attribute 'is_integer' No, this won't work. You can't use NumPy arrays as input to SymPy functions. SymPy functions only work with SymPy expressions. If you want to use a NumPy array you have to lambdify the expression first to use the numpy and scipy functions. > > > When I try to lambdify this simple expression : > `17469169.5935065*sin(0.942966379693359*m)*cos(1.5707963267949*m)/m`, with > "numpy" as module argument, it gives : `'float' object has no attribute > 'sin'` I can't reproduce this. What is the input to the lambdified function? > > I have also tried the packaged module 'numexpr' without success, as it stops > on this other expression : `0.058**(46.6321243523316*k)`, saying : > > File > "C:\Users\Emile\Anaconda2\lib\site-packages\numexpr\necompiler.py", line > 756, in evaluate > signature = [(name, getType(arg)) for (name, arg) in zip(names, > arguments)] > > File > "C:\Users\Emile\Anaconda2\lib\site-packages\numexpr\necompiler.py", line > 654, in getType > raise ValueError("unknown type %s" % a.dtype.name) > > ValueError: unknown type object I also can't reproduce this. Aaron Meurer > > > > It seems that `lambdify` cannot interprete properly the input value in the > bessel function in the program code (wether it is with scipy or sympy bessel > functions), as the input value seems to be an array object, while it is not > a problem in the single code. Yet I do not see the difference between them. > There is also a problem about defining the modules, even if numpy and bessel > functions are correctly specified. > So I really don't know the nature of this problem, and I can't even track it > as the exception raised comes from inside sympy and I don't know how to put > flags in sympy functions. > Thank you for reading this, and I hope someone may help me on this topic. > Please tell me if more information are needed to illustrate this problem. > > > -- > You received this message because you are subscribed to the Google Groups > "sympy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/sympy. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sympy/470d0442-aaa1-4a79-b4b2-4fabbc011235%40googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sympy. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAKgW%3D6%2BtjrUbV_OJhDFWn6uHFgD93LBSKPb6mbkd5i46rLDKoQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
