K i?ddlmZmZddlmZmZmZddlmZm Z ddl m Z ddl m Z ddlmZddlmZddlmZdd lmZmZmZdd lmZdd lmZdd lmZdd lmZm Z ddl!m"Z"m#Z#dZ$ddZ%ddZ&y))SPow) Derivative AppliedUndefdiff)EqualityEq)Dummy)sympify) BooleanAtom)exp)Order)simplifyposify besselsimp)trigsimp) sqrtdenest)solve) _preprocess ode_order)iterable is_sequencec||i}|jtD]T}|j|k(r|j|j||</|j ||ij d||<V|j |S)aa When replacing the func with something else, we usually want the derivative evaluated, so this function helps in making that happen. Examples ======== >>> from sympy import Derivative, symbols, Function >>> from sympy.solvers.ode.subscheck import sub_func_doit >>> x, z = symbols('x, z') >>> y = Function('y') >>> sub_func_doit(3*Derivative(y(x), x) - 1, y(x), x) 2 >>> sub_func_doit(x*Derivative(y(x), x) - y(x)**2 + y(x), y(x), ... 1/(x*(z + 1/x))) x*(-1/(x**2*(z + 1/x)) + 1/(x**3*(z + 1/x)**2)) + 1/(x*(z + 1/x)) ...- 1/(x**2*(z + 1/x)**2) F)deep)atomsrexprrvariable_countxreplacedoit)eqfuncnewrepsds a/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/solvers/ode/subscheck.py sub_func_doitr&s* +D XXj !? 66T>chh 0 01DGjj$-222>DG ? ;;t Nc  t|rt|||St|ts t |d}| t |j \}}t|trt|jdk7rtd|zt|tr,t!||Dcgc]}t#||||c}St|ts t ||}n|j$|k(r |j&}|dk(r t)||}|j |k(xr|j$j+| } |rK| sIt-||} | r;| D cgc]} t || } } t| dk(r| d} t#|| |dS|jd} |j+t.r |j |k(sJ|j$j1}|j$j3}|j4}t|t6sJ|j8}|t/| |zk(sJ|j |j$z j;||j=j?}t/| ||z z}||z}|j1|k(sJ|j3}|dk(|fSd }d}|rh|dk(r|j |j$z }|j |k(r#tA|||j$}tC|}n|dz }YtE|jGt8}|r|j?d }nd}|dz }n|dk(rttEtItK|j | |tK|j$| |z tI|j z tI|j$z}|dz }nY|d k(rR|j |k(r*|j$j+|sd|j$i}n;|j$|k(r*|j j+|sd|j i}ni}|j |j$z }tMd|dzD]a}|dk(r@|jK| } t-||jK| |}|stN |d||<H||dz jK| ||<c|d kDr|j |j$} }tM|d d D]}|dk(rd|vrntA||jK| |||}tA| |jK| |||} t || }tE|}t|tPtRfr|s~tTjVx}} |j }|j$} tI|| z jYd}t[d }|j;||}t]|\}}tE|j_|j_||i}|dz }nn|rh|sd |fS|d ur)tOdta|zdzta|zdzd|fS#t$rt|tr|n|gDcgc]}|jtncc}w}}tj|}t|dk7r td|j}YwxYwcc}wcc} w#tN$r |dz }Y5wxYw)a Substitutes ``sol`` into ``ode`` and checks that the result is ``0``. This works when ``func`` is one function, like `f(x)` or a list of functions like `[f(x), g(x)]` when `ode` is a system of ODEs. ``sol`` can be a single solution or a list of solutions. Each solution may be an :py:class:`~sympy.core.relational.Equality` that the solution satisfies, e.g. ``Eq(f(x), C1), Eq(f(x) + C1, 0)``; or simply an :py:class:`~sympy.core.expr.Expr`, e.g. ``f(x) - C1``. In most cases it will not be necessary to explicitly identify the function, but if the function cannot be inferred from the original equation it can be supplied through the ``func`` argument. If a sequence of solutions is passed, the same sort of container will be used to return the result for each solution. It tries the following methods, in order, until it finds zero equivalence: 1. Substitute the solution for `f` in the original equation. This only works if ``ode`` is solved for `f`. It will attempt to solve it first unless ``solve_for_func == False``. 2. Take `n` derivatives of the solution, where `n` is the order of ``ode``, and check to see if that is equal to the solution. This only works on exact ODEs. 3. Take the 1st, 2nd, ..., `n`\th derivatives of the solution, each time solving for the derivative of `f` of that order (this will always be possible because `f` is a linear operator). Then back substitute each derivative into ``ode`` in reverse order. This function returns a tuple. The first item in the tuple is ``True`` if the substitution results in ``0``, and ``False`` otherwise. The second item in the tuple is what the substitution results in. It should always be ``0`` if the first item is ``True``. Sometimes this function will return ``False`` even when an expression is identically equal to ``0``. This happens when :py:meth:`~sympy.simplify.simplify.simplify` does not reduce the expression to ``0``. If an expression returned by this function vanishes identically, then ``sol`` really is a solution to the ``ode``. If this function seems to hang, it is probably because of a hard simplification. To use this function to test, test the first item of the tuple. Examples ======== >>> from sympy import (Eq, Function, checkodesol, symbols, ... Derivative, exp) >>> x, C1, C2 = symbols('x,C1,C2') >>> f, g = symbols('f g', cls=Function) >>> checkodesol(f(x).diff(x), Eq(f(x), C1)) (True, 0) >>> assert checkodesol(f(x).diff(x), C1)[0] >>> assert not checkodesol(f(x).diff(x), x)[0] >>> checkodesol(f(x).diff(x, 2), x**2) (False, 2) >>> eqs = [Eq(Derivative(f(x), x), f(x)), Eq(Derivative(g(x), x), g(x))] >>> sol = [Eq(f(x), C1*exp(x)), Eq(g(x), C2*exp(x))] >>> checkodesol(eqs, sol) (True, [0, 0]) )r!rz0must pass func arg to checkodesol for this case./func must be a function of one variable, not %s)ordersolve_for_funcautoFTforcer!zUnable to test if z is a solution to .)1rchecksysodesol isinstancerr rlhs ValueErrorrsetrrunionlenpopargstype checkodesolrhsreversedrhasrrgetOremoveOrrr subsrexpandr&rrrewriterrrangeNotImplementedErrorboolr rZeroas_numer_denomr rrstr) odesolr!r+r,_sfuncsisolvedr>teqsxOtermsolrhsOexprsorderodesubsneworderresidualtestnumode_diffssdiffsolsdssdfr5 ode_or_boolnum_funcr#s r%r=r=0sB}c3T22 c8 $aj | !#''*GAt dL )S^q-@ = DF F3tCycfg^_+c1E.Yghh c8 $sm Dll #t$ WW_ 6SWW[[%6!6FfC (+,12dA;,C,3x1}!fsCu$& & ! A wwu~ww$ " %%%%ai((((77377?((v6;;=DDFVe^,-H$||~)))??$A x(( AG a<ww(Hww$!(D#'':qM1 !))C.)BIIDI) qLG \cggq%04E3JJK!"$,SWW$567A qLG \ww$sww{{4'8sww<DT):sww<''CGG#C1eai( : 6!B-#B !Q8""55 # '*!f #+1q5/"6"6q"9HQK% :*{77CGGSub"-.AAv!8"3'TYYq!_hqkJC'TYYq!_hqkJC"$S#,K"*;"7K!+k/BC&().C#)oo)oo.$c 99;A>?f hhtU+"3K TSM**40995$-H1  Q T ay d!"6S"A #!#&s8#,.1#23 3qzO "3,3%:qQWW\*::E:CEKK'E5zQ FHH99;D h-t/1 s<Y&[%[*$[/ [":ZA["!["/\\c d}||}tt|D]8}t||ts||j||j z ||<:|{g}|D]`}|j t}tj|Dcgc]}|j tc}}|j|btt|}tdDs4t|Dchc]}|jc}dk7rtdz|D].} t| j tdk7s%tdt|t|D chc]} | jc} k7r tdi} |D]} t| j td}| j |k(r | j } | j|k(xr| j j#| } | st%| |} | st&| j } | | |<g} |D]m}|D]}t)||| |}t+|}|dk7r1|j-d }|dk7rt/|j+}nd}| j1|ott| dk(rtt| ddk(rd| fSd | fScc}wcc}wcc} w) aw Substitutes corresponding ``sols`` for each functions into each ``eqs`` and checks that the result of substitutions for each equation is ``0``. The equations and solutions passed can be any iterable. This only works when each ``sols`` have one function only, like `x(t)` or `y(t)`. For each function, ``sols`` can have a single solution or a list of solutions. In most cases it will not be necessary to explicitly identify the function, but if the function cannot be inferred from the original equation it can be supplied through the ``func`` argument. When a sequence of equations is passed, the same sequence is used to return the result for each equation with each function substituted with corresponding solutions. It tries the following method to find zero equivalence for each equation: Substitute the solutions for functions, like `x(t)` and `y(t)` into the original equations containing those functions. This function returns a tuple. The first item in the tuple is ``True`` if the substitution results for each equation is ``0``, and ``False`` otherwise. The second item in the tuple is what the substitution results in. Each element of the ``list`` should always be ``0`` corresponding to each equation if the first item is ``True``. Note that sometimes this function may return ``False``, but with an expression that is identically equal to ``0``, instead of returning ``True``. This is because :py:meth:`~sympy.simplify.simplify.simplify` cannot reduce the expression to ``0``. If an expression returned by each function vanishes identically, then ``sols`` really is a solution to ``eqs``. If this function seems to hang, it is probably because of a difficult simplification. Examples ======== >>> from sympy import Eq, diff, symbols, sin, cos, exp, sqrt, S, Function >>> from sympy.solvers.ode.subscheck import checksysodesol >>> C1, C2 = symbols('C1:3') >>> t = symbols('t') >>> x, y = symbols('x, y', cls=Function) >>> eq = (Eq(diff(x(t),t), x(t) + y(t) + 17), Eq(diff(y(t),t), -2*x(t) + y(t) + 12)) >>> sol = [Eq(x(t), (C1*sin(sqrt(2)*t) + C2*cos(sqrt(2)*t))*exp(t) - S(5)/3), ... Eq(y(t), (sqrt(2)*C1*cos(sqrt(2)*t) - sqrt(2)*C2*sin(sqrt(2)*t))*exp(t) - S(46)/3)] >>> checksysodesol(eq, sol) (True, [0, 0]) >>> eq = (Eq(diff(x(t),t),x(t)*y(t)**4), Eq(diff(y(t),t),y(t)**3)) >>> sol = [Eq(x(t), C1*exp(-1/(4*(C2 + t)))), Eq(y(t), -sqrt(2)*sqrt(-1/(C2 + t))/2), ... Eq(x(t), C1*exp(-1/(4*(C2 + t)))), Eq(y(t), sqrt(2)*sqrt(-1/(C2 + t))/2)] >>> checksysodesol(eq, sol) (True, [0, 0]) c`tttt|r |S|gSN)listmapr r)r s r%_sympifyz checksysodesol.._sympifyWs'Cx|>??">??r'c3pK|].}t|txrt|jdk(0yw)r)N)r4rr9r;).0r!s r% z!checksysodesol..ds,X$z$ -E#dii.A2EEXs46r)r*z'solutions should have one function onlyzCnumber of solutions provided does not match the number of equationsrTr.F)rFr9r4rr5r>rrr7r8rextendriallr;r6r?r@rrGr&rrDrappend)rTsolsr!rkrQrPr derivsr$rMdictsolrRr>checkeqr_s r%r3r3#sh@ 3-C 3s8_- c!fh 'VZZ#a&**,CF- | BXXj)F35;; G1!6 GHD LL  SZ  XRWX X 5 )4TYY )*A-JTQRRH syy& '1 ,FG GH 5zST2c#''233^__G CIIl+,Q/ 77d?,,CD:T):%:T"C))''C  G  8Dr47B 8 b\ 7&BQw^,,.Br  3w<A$s7|"4Q"71"<gwO!H * 3sK 3K$K )Nr-Trh)' sympy.corerrsympy.core.functionrrrsympy.core.relationalrr sympy.core.symbolr sympy.core.sympifyr sympy.logic.boolalgr sympy.functionsr sympy.seriesrsympy.simplify.simplifyrrrsympy.simplify.trigsimprsympy.simplify.sqrtdenestr sympy.solversrsympy.solvers.deutilsrrsympy.utilities.iterablesrrr&r=r3r'r%rsG@@.#&+@@,08;