More than one module for lambdify in sympy

三世轮回 提交于 2019-12-23 10:28:28

问题


I am trying to make lambdify understand to expect more than one type of input using the modules keyword argument. According to the source code of lambdify (http://docs.sympy.org/dev/_modules/sympy/utilities/lambdify.html), this can be done by using lists of the arguments, but i am not able to do so.

import sympy
from sympy import lambdify
x,y=sympy.symbols('x y')
from sympy.parsing.sympy_parser import parse_expr
func=lambdify(x,parse_expr(exp(x)),modules=["numpy","sympy"])

func(array([3,4]))

gives

array([ 20.08553692,  54.59815003])

but when i try

func(y)

i get an

Attribute error:exp

What am i doing wrong here? Shouldn't func accept both numpy and sympy types? Any help appreciated!!


回答1:


The modules don't dispatch or anything like that. The way that lambdify works is that it creates

lambda x: exp(x)

where exp comes from the namespace of the module(s) you chose. lambdify(x, exp(x), ['numpy', 'sympy']) is roughly equivalent to

from sympy import *
from numpy import *
# Various name replacements for differences in numpy naming conventions, like
# asin = arcsin
return lambda x: exp(x)

If you want to provide a custom function that dispatches, you can use something like Saullo Castro's example. You can also use this with lambdify by providing a dict, like

import numpy as np
import sympy

def myexp(x):
    if isinstance(x, np.ndarray):
        return np.exp(x)
    else:
        return sympy.exp(x)

func = lambdify(x, exp(x), [{'exp': myexp}, 'numpy'])

This gives

>>> func(np.array([1, 2]))
array([ 2.71828183,  7.3890561 ])
>>> func(sympy.Symbol('y'))
exp(y)



回答2:


The documentation says that the modules argument will give more priority to the modules appearing first, which in this case is "numpy". Thefore, if the two modules have the same function it will always take the first one.

A good approach would be:

import numpy as np

def func(x):
    if isinstance(x, np.ndarray):
        return np.exp(x)
    else:
        return sympy.exp(x)


来源:https://stackoverflow.com/questions/25495375/more-than-one-module-for-lambdify-in-sympy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!