简体   繁体   中英

Find the order of a differential equation in Sympy

I have a differential equation defined in Sympy (0.7.6.1), for example:

>>> 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. I found nothing in the documentation. I faintly hoped that degree might work, but it doesn't:

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

Context: For pedagogical purpose, I've written a solver for constant-coefficient linear homogeneous ODEs. It finds the characteristic equation and solves it with Sympy's roots . Currently I have to pass the ODE order by hand next to the ODE itself, which is inconvenient.

You can use ode_order . This gives you the order of the differential equation with respect to a function:

>>> ode_order(eq, y)
2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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