Evaluate sympy expression from an array of values

后端 未结 4 1135
旧巷少年郎
旧巷少年郎 2020-12-01 02:06

I\'m experimenting with sympy and I\'ve hit upon an issue I can\'t work out.

Using scipy I can write an expression and evaluate it for an array of x values as follow

4条回答
  •  猫巷女王i
    2020-12-01 02:54

    While the accepted answer makes it clear that the OP was looking for numerical evaluation, I'll still point out that one can also have symbolic evaluation by using symarray:

    import sympy
    xs = sympy.symarray('x', 10)
    f = lambda x: x**2
    f(xs)
    

    yields

    array([x_0**2, x_1**2, x_2**2, x_3**2, x_4**2, x_5**2, x_6**2, x_7**2,
           x_8**2, x_9**2], dtype=object)
    

    Note that this also uses a numpy array internally, but one filled with sympy.Expressions.

提交回复
热议问题