SymPy:用给定的变量评估给定的表达式

时间:2021-06-14 20:21:46

I have a sympy expression involving two variables a, b. I would now like to evaluate this expression for specific values of a and b. Using a lambda like

我有一个包含两个变量a,b的情感表达式。我现在想要评估这个表达式的特定值a和b。使用像lambda一样

import sympy


def get_expression(a, b):
    # Complex function with a simple result. I have no control here.
    return a*b + 2

a = sympy.Symbol('a')
b = sympy.Symbol('b')
z = get_expression(a, b)

f = lambda a, b: z
print(f(1, 1))

only gives

只给出

a*b + 2

though.

虽然。

Any hints?

任何提示?

1 个解决方案

#1


1  

Turns out that lambdify is what I need:

事实证明lambdify是我需要的:

f = sympy.lambdify([a, b], z)
print(f(1, 1))

#1


1  

Turns out that lambdify is what I need:

事实证明lambdify是我需要的:

f = sympy.lambdify([a, b], z)
print(f(1, 1))