作为微分方程项目的一部分,我有一个函数,用数学表达式(渐近类或int)替换变量'y‘,并尽可能简化结果,以便以后在我的程序中使用。
下面是与我的描述相对应的代码:
def get_ode_with_proposed_equations(self, ode, undefined_equation, proposed_equation):
"""This function replaces an undefined equation in the differential
equation with the corresponding proposed solution. However, the
algorithm can be very slow at times which can make you think that
the algorithm is frozen.
Parameter:
ode (sympy): ode is for Ordinary Differential Equation. It is
the differential equation that we want to solve.
undefined_equation (sympy symbol): An equation symbolized by "y"
followed by the corresponding index. i.e "y3"
proposed_equation (sympy expression): A sympy mathematical equation.
i.e exp(x) + sin(x)
Return:
sympy expression: The Ordinary Differential Equation but with the
proposed equation instead of the undefined equation.
"""
try:
return round(simplify(ode.subs(undefined_equation, proposed_equation).doit()))
except TypeError:
return simplify(ode.subs(undefined_equation, proposed_equation).doit())这个障碍是没有预料到的速度问题,因为有时我的程序会完全冻结。我使用多处理池来优化这个函数。这有助于一些表达,但该算法可以冻结时间的时间。我还试图找到一种简化的替代方法,因为这种方法非常缓慢,但没有定论。因此,我想知道是否有办法提高这个函数的性能,因为我必须简化结果(例如,我必须得到0而不是x -x +(1/2)*x**2 - (1/2)*x**2)。
提前谢谢你
发布于 2022-09-09 20:14:08
如果您只是检查某个特定表达式是否是解决方案,请考虑函数checkodesol。如果您真的只想要一个简化的版本,那么最好只使用subs来查看输入和输出示例,而这并不是您想要的。也许您只需要在结果上使用expand_mul (或_mexpand),而不是更一般的simplify或expand。
https://stackoverflow.com/questions/73659519
复制相似问题