[英]Integrate custom function with sympy
Is it possible to integrate custom function using sympy? 是否可以使用sympy集成自定义功能?
I want something like this: 我想要这样的东西:
def func(x, y):
return something_with(x, y)
res = integrate(func, (y, x-2, 3), (x, 1, 2))
Is it possible? 可能吗?
You can integrate some functions with sympy's integrate . 您可以使用sympy的Integrated集成一些功能。 Suppose you had the function (one variable to make it simpler) 假设您具有函数(一个变量使其更简单)
def my_func(x):
return x**2 + x + 1
Then you could integrate it with 然后,您可以将其与
x = Symbol('x')
integrate(my_func(x), (x, a, b))
Although only limited mathematical functions are likely to work. 尽管只有有限的数学函数可能起作用。
If you only care about the numerical answer, you could try scipy 's integrate.quad()
(or scipy.integrate.dblquad()
) which will do numerical integration, and can tackle a broader set of functions within a small error. 如果只关心数值答案,则可以尝试scipy的integrate.quad()
(或scipy.integrate.dblquad()
),该方法可以进行数值积分,并且可以在一个很小的误差内处理更广泛的功能。
scipy.integrate.quad(my_func, a, b)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.