Evaluate sympy expression from an array of values

后端 未结 4 1138
旧巷少年郎
旧巷少年郎 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条回答
  •  不知归路
    2020-12-01 02:53

    Or you can do it via numpy.vectorize. I am using x, g, and xvals from the question body.

    scalar_func = lambda xx: float(g.evalf(subs={x: xx}))
    vector_func = numpy.vectorize(scalar_func)
    vector_func(xvals) # returns a numpy array [10000.0, 9980.01, 9960.04, ...]
    

提交回复
热议问题