首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用sympy.simplify实现python的速度性能

用sympy.simplify实现python的速度性能
EN

Stack Overflow用户
提问于 2022-09-09 08:42:29
回答 1查看 60关注 0票数 1

作为微分方程项目的一部分,我有一个函数,用数学表达式(渐近类或int)替换变量'y‘,并尽可能简化结果,以便以后在我的程序中使用。

下面是与我的描述相对应的代码:

代码语言:javascript
复制
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)。

提前谢谢你

EN

回答 1

Stack Overflow用户

发布于 2022-09-09 20:14:08

如果您只是检查某个特定表达式是否是解决方案,请考虑函数checkodesol。如果您真的只想要一个简化的版本,那么最好只使用subs来查看输入和输出示例,而这并不是您想要的。也许您只需要在结果上使用expand_mul (或_mexpand),而不是更一般的simplifyexpand

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73659519

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档