I have an expression with several variables, let's say something like below:
import numpy as np
import sympy as sym
from sympy import Symbol, Add, re, lambdify
x = sym.Symbol('x')
y = sym.Symbol('y')
z = sym.Symbol('z')
F = x+ y +z
I have three lists for the variables like below:
x = [3, 2 ,3]
y = [4, 5 , 6]
z = [7, 10 ,3]
I want to evaluate my function for the each element of my variables. I know I can define something like below:
f_dis = lambdify([x, y, z], x + y + z, 'numpy')
d = f_dis(3, 4, 7)
print ( "f_dis =", d)
which give me 14 as the desired result. But how can I pass the x, y, and z as three lists (instead of writing the elements separately) and get a result like below:
[14, 17, 12]
It seems using lambdify is a more efficient way to evaluate a function, based on this note: https://www.sympy.org/scipy-2017-codegen-tutorial/notebooks/22-lambdify.html
Thanks.
import sympy as sp
x = sp.Symbol('x')
y = sp.Symbol('y')
z = sp.Symbol('z')
X = [3, 2 ,3]
Y = [4, 5 , 6]
Z = [7, 10 ,3]
values = list(zip(X, Y, Z))
f_dis = sp.lambdify([x, y, z], x + y + z, 'numpy')
ans = [f_dis(*value) for value in values]
for d in ans:
print ( "f_dis =", d)
this will give you:
f_dis = 14
f_dis = 17
f_dis = 12