简体   繁体   English

在Sympy中找到微分方程的阶

[英]Find the order of a differential equation in Sympy

I have a differential equation defined in Sympy (0.7.6.1), for example: 我有一个在Sympy(0.7.6.1)中定义的微分方程,例如:

>>> y = Function('y')
>>> x = Symbol('x')
>>> y0 = y(x)
>>> y1 = y0.diff(x)
>>> y2 = y1.diff(x)
>>> eq = y2 - 2*y1 + y0
>>> eq
                      2      
         d           d       
y(x) - 2*--(y(x)) + ---(y(x))
         dx           2          
                    dx   

I would like Sympy to tell me the order of the equation. 我希望Sympy告诉我等式的顺序。 I found nothing in the documentation. 我在文档中什么都没找到。 I faintly hoped that degree might work, but it doesn't: 我微弱地希望该degree可以工作,但事实并非如此:

>>> degree(eq)
1
>>> degree(eq, y)
0

Context: For pedagogical purpose, I've written a solver for constant-coefficient linear homogeneous ODEs. 上下文:出于教学目的,我编写了一个常数系数线性齐次ODE的求解器。 It finds the characteristic equation and solves it with Sympy's roots . 它找到特征方程并用Sympy的roots求解。 Currently I have to pass the ODE order by hand next to the ODE itself, which is inconvenient. 目前,我必须在ODE本身旁边手动传递ODE订单,这很不方便。

You can use ode_order . 您可以使用ode_order This gives you the order of the differential equation with respect to a function: 这为您提供了相对于函数的微分方程的阶数:

>>> ode_order(eq, y)
2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM