K i ddlmZddlmZmZddlmZmZddlm Z m Z m Z m Z ddl mZddlmZddlmZdd lmZmZmZmZmZmZmZdd lmZdd lmZmZdd l m!Z!m"Z"m#Z#dd l$m%Z%ddl&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/ddl0m1Z1ddl2m3Z3ddl4m5Z5m6Z6m7Z7ddl8m9Z9m:Z:m;Z;mm?Z?ddl@mAZAddlBmCZCddlDmEZEmFZFddlGmHZHddlImJZJddlKmLZLmMZMmNZNmOZOddlPmQZQmRZRmSZSmTZTmUZUmVZVmWZWddlXmYZYGddeZZ[GddZ\Gd d!Z]Gd"d#e]Z^Gd$d%e]Z_Gd&d'e^Z`Gd(d)e^ZaGd*d+e^ZbGd,d-e^ZcGd.d/e]ZdGd0d1e^ZeGd2d3e^ZfGd4d5e^ZgGd6d7e^ZhGd8d9e^ZiGd:d;eiZjGd<d=e^ZkGd>d?e^ZlGd@dAelekZmGdBdCemZnGdDdEe]ZoGdFdGe]ZpGdHdIe]ZqGdJdKe]ZrGdLdMe]ZsGdNdOe]ZtGdPdQe]ZuGdRdSe]ZvGdTdUe]ZwGdVdWe]ZxGdXdYe]ZyidZedd[eqd\etd]esd^evd_eid`e`daead-ecdbeedcefddemdeeldfekdgebdhendiejereuehexewepeoege_eydj Zzddkl{m|Z|m}Z}m~Z~mZyl)m) annotations)ClassVarIterator) match_riccati solve_riccati)AddSPowRational)cached_property) factor_terms)Expr) AppliedUndef DerivativediffFunctionexpandSubs_mexpand)zoo)EqualityEq)SymbolDummyWild)Mul) exptanlogsqrtbesseljbesselycbrtairyaiairybi)Integral)Poly)cancelfactordegree)collectsimplify separatevars logcombineposify)fraction)numbered_symbols)solve) ode_order _preprocess) _lin_eq2dict)PolyNonlinearError)equivalence_hypergeometricmatch_2nd_2F1_hypergeometricget_sol_2F1_hypergeometricmatch_2nd_hypergeometric)!_get_euler_characteristic_eq_sols!_get_const_characteristic_eq_sols _solve_undetermined_coefficients_solve_variation_of_parameters _test_term _undetermined_coefficients_match_get_simplified_sol)_ode_lie_groupceZdZdZy) ODEMatchErrorzFRaised if a SingleODESolver is asked to solve an ODE it does not matchN)__name__ __module__ __qualname____doc__^/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/solvers/ode/single.pyrErE%sPrKrEceZdZUdZded<ded<ded<ded <ded <ded <d Zdd ZeddZeddZ eddZ eddZ ddZ dddZ dddZedZdZy )SingleODEProblemaRepresents an ordinary differential equation (ODE) This class is used internally in the by dsolve and related functions/classes so that properties of an ODE can be computed efficiently. Examples ======== This class is used internally by dsolve. To instantiate an instance directly first define an ODE problem: >>> from sympy import Function, Symbol >>> x = Symbol('x') >>> f = Function('f') >>> eq = f(x).diff(x, 2) Now you can create a SingleODEProblem instance and query its properties: >>> from sympy.solvers.ode.single import SingleODEProblem >>> problem = SingleODEProblem(f(x).diff(x), f(x), x) >>> problem.eq Derivative(f(x), x) >>> problem.func f(x) >>> problem.sym x reqrfuncrsymint_order _eq_expanded_eq_preprocessedNc t|tsJt|tsJt|tsJt|tsJ||_||_||_||_||_ yN) isinstancerrrboolrOrPrQprepparams)selfrOrPrQrZkwargss rL__init__zSingleODEProblem.__init__Qse"d###$ ---#v&&&$%%%   rKcBt|j|jSrW)r4rOrPr\s rLorderzSingleODEProblem.order\s$)),,rKc"|jSrW)_get_eq_preprocessedr`s rLeq_preprocessedz SingleODEProblem.eq_preprocessed`s((**rKc6td|jg}td|jg}d}|jjr|jj |jj |j|j}|dvrg|j||j|zz}|rD||r?|j||z}t|jjDcgc]}||z  c}}|t|j}|Scc}w)Naexcludec1)rr) rrPrQrOis_Addcoeffrramatchr argsr)r\rfri reduced_eq deriv_coefrdenargs rLeq_high_order_freez#SingleODEProblem.eq_high_order_freeds tyyk * $ + 77>>tyy~~dhh 'KLJ'$$Qtyy"}_52))QrU*C!$$'',,&G3s3w&G!HJ  J'Hs- Dc,t|jSrW)rrdr`s rL eq_expandedzSingleODEProblem.eq_expandedusd**++rKc|jr:t|j|j\}}||jk7rt|S|j}|SrW)rZr5rOrP ValueError)r\ process_eq process_funcs rLrcz%SingleODEProblem._get_eq_preprocessedysM 99'2477DII'F $J tyy(  JrKcv|j||}t|Dcgc] }t|}}|Scc}w)zV Returns a list of constants that do not occur in eq already. )iter_numbered_constantsrangenext)r\numstartprefixncsiCss rLget_numbered_constantsz'SingleODEProblem.get_numbered_constantss; **5&9!&s ,Ad3i , , -s6c |jj}|jjt}|r/||Dchc] }t t |j "c}z}t|||Scc}w)z[ Returns an iterator of constants that do not occur in eq already. )rrrh)rO free_symbolsatomsrrstrrPr2)r\rratom_setfunc_setfs rLr{z(SingleODEProblem.iter_numbered_constantss_ 77''77==*  h?AFF ,? ?HeFHMM@s%A5ctd}|j}|jj|j|j }||vS)Nu)rrQrOsubsrPr)r\rxsymss rL is_autonomouszSingleODEProblem.is_autonomouss< #J HHww||DIIq)66}rKc j}jd}t|dzDchc]}t||||}} t ||\}} |j s"tfd| jDryt|dzDcic]9}|| j||j||tj;} }|| d<| Scc}w#t $rYywxYwcc}w)a Matches a differential equation to the linear form: .. math:: a_n(x) y^{(n)} + \cdots + a_1(x)y' + a_0(x) y + B(x) = 0 Returns a dict of order:coeff terms, where order is the order of the derivative on each term, and coeff is the coefficient of that derivative. The key ``-1`` holds the function `B(x)`. Returns ``None`` if the ODE is not linear. This function assumes that ``func`` has already been checked to be good. Examples ======== >>> from sympy import Function, cos, sin >>> from sympy.abc import x >>> from sympy.solvers.ode.single import SingleODEProblem >>> f = Function('f') >>> eq = f(x).diff(x, 3) + 2*f(x).diff(x) + \ ... x*f(x).diff(x, 2) + cos(x)*f(x).diff(x) + x - f(x) - \ ... sin(x) >>> obj = SingleODEProblem(eq, f(x), x) >>> obj.get_linear_coefficients(eq, f(x), 3) {-1: x - sin(x), 0: -1, 1: cos(x) + 2, 2: x, 3: 1} >>> eq = f(x).diff(x, 3) + 2*f(x).diff(x) + \ ... x*f(x).diff(x, 2) + cos(x)*f(x).diff(x) + x - f(x) - \ ... sin(f(x)) >>> obj = SingleODEProblem(eq, f(x), x) >>> obj.get_linear_coefficients(eq, f(x), 3) == None True rrNc3@K|]}|jywrW)has).0crPs rL z;SingleODEProblem.get_linear_coefficients..sHd Hs) rPrmr|rr6r7ranyvaluesgetrr Zero) r\rOrPrarrrsymsetrhs lhs_termstermss ` rLget_linear_coefficientsz(SingleODEProblem.get_linear_coefficientssB II IIaL27a.AQ*QqT1a(AA )"f5NC 774=CHY5E5E5GHHDI%PQ'NSqIMM!A$))Aq/166::SSb  B"   TsC$ C)>C8) C54C5)T)returnrR)rr)rrC)rz list[Symbol])rr)rzIterator[Symbol])rFrGrHrI__annotations___eq_high_order_freer^r rardrsrurcrr{rrrJrKrLrNrN*s< H  K K --++ ,, N -rKrNc~eZdZUdZded<ded<ded<dZd ed <dZd ed <d ZddZddddZ ddZ ddddZ y)SingleODESolvera Base class for Single ODE solvers. Subclasses should implement the _matches and _get_general_solution methods. This class is not intended to be instantiated directly but its subclasses are as part of dsolve. Examples ======== You can use a subclass of SingleODEProblem to solve a particular type of ODE. We first define a particular ODE problem: >>> from sympy import Function, Symbol >>> x = Symbol('x') >>> f = Function('f') >>> eq = f(x).diff(x, 2) Now we solve this problem using the NthAlgebraic solver which is a subclass of SingleODESolver: >>> from sympy.solvers.ode.single import NthAlgebraic, SingleODEProblem >>> problem = SingleODEProblem(eq, f(x), x) >>> solver = NthAlgebraic(problem) >>> solver.get_general_solution() [Eq(f(x), _C*x + _C)] The normal way to solve an ODE is to use dsolve (which would use NthAlgebraic and other solvers internally). When using dsolve a number of other things are done such as evaluating integrals, simplifying the solution and renumbering the constants: >>> from sympy import dsolve >>> dsolve(eq, hint='nth_algebraic') Eq(f(x), C1 + C2*x) z ClassVar[str]hintzClassVar[bool] has_integralrN ode_problemNz bool | None_matchedz list | Nonerac||_yrW)r)r\rs rLr^zSingleODESolver.__init__ s &rKc|j5|jj|jvrd|_|jS|j|j|_|jS)NF)rarr_matchesr`s rLmatcheszSingleODESolver.matchessU :: !d&6&6&<&B'rKrc6eZdZdZdZdZdZd dZdZdZ y) SinglePatternODESolverz4Superclass for ODE solvers based on pattern matchingc|j}|jj}|j}|j}|j |||SrW)rrPrQra_wilds)r\probrrras rLwildszSinglePatternODESolver.wilds)s? IINN HH {{1a''rKc|j}|jDcgc]"}|j|tj$c}Scc}wrW) _wilds_matchrrr r)r\rlws rL wilds_matchz"SinglePatternODESolver.wilds_match0s6!!.2jjl; !QVV$;;;s'A c|jj}|jjj}|jj}|jj}||j ||}|dvry|j ||||}|j|jtst||j|z }|j||j |||gt}|j|x|_}||j||Sy)N)rFrP)rrurPrQrar _equationrkrrrr,r)rlr_verify)r\rOrrradfpatternrls rLrzSinglePatternODESolver._matches4s    ) )    ! ! & &      && qTYYq%   ..1q%0}}R $$T*RXXb\)*B ZZ11qt,VZ <$&HHW$55E  <<!% %rKcy)NTrJr\fxs rLrzSinglePatternODESolver._verifyIsrKcd}t|)Nz5Subclasses of SingleODESolver should implement _wildsr)r\rrrars rLrzSinglePatternODESolver._wildsLsE!#&&rKcd}t|)Nz8Subclasses of SingleODESolver should implement _equationr)r\rrrars rLrz SinglePatternODESolver._equationPsH!#&&rKNr) rFrGrHrIrrrrrrrJrKrLrr&s#>(<*''rKrcLeZdZUdZdZdZdZddd dZiZde d<e d Z y ) NthAlgebraica Solves an `n`\th order ordinary differential equation using algebra and integrals. There is no general form for the kind of equation that this can solve. The the equation is solved algebraically treating differentiation as an invertible algebraic function. Examples ======== >>> from sympy import Function, dsolve, Eq >>> from sympy.abc import x >>> f = Function('f') >>> eq = Eq(f(x) * (f(x).diff(x)**2 - 1), 0) >>> dsolve(eq, f(x), hint='nth_algebraic') [Eq(f(x), 0), Eq(f(x), C1 - x), Eq(f(x), C1 + x)] Note that this solver can return algebraic solutions that do not have any integration constants (f(x) = 0 in the above example). nth_algebraicTc  |jj}|jj}|jj}|j | fd} fd}|||} t ||d}|Dcgc]}t|||}}|Dcgc]}t||}}||_ t|dk7S#t $rg}YZwxYwcc}wcc}w)a% Matches any differential equation that nth_algebraic can solve. Uses `sympy.solve` but teaches it how to integrate derivatives. This involves calling `sympy.solve` and does most of the work of finding a solution (apart from evaluating the integrals). c>fd}|jt|S)Nc|d|dd}}|}|D]/\}}t|D]}|k(r |}t||}1|SNrr)r|r) rm differanddiffs toreplacevn_diffxvars rL expand_diffxz.replace..expand_diffxsj#'7DH5 % !ADAq"1XA8(-i(8I(29a(@I AA ! rK)replacer)rOrrrs ` rLrz&NthAlgebraic._matches..replaces !::j,7 7rKc0|jfdS)Nct|SrW)r)ers rLz:NthAlgebraic._matches..unreplace..sz!S/ArK)r)rOrrs `rL unreplacez(NthAlgebraic._matches..unreplaces::e%AB BrKFrr) rrOrPrQ _get_diffxr3rr-r solutionslen) r\rOrPrrrsubs_eqnsolnssolnrs @rLrzNthAlgebraic._matchesos   $$""$ 8 C2s# (D59E=BBD)D#./BB278$$%885zQ# E C8s)C;CC CCrc|jSrW)r)r\rs rLrz"NthAlgebraic._get_general_solutions ~~rKzdict[Symbol, type[Function]] _diffx_storedctjjd}|1Gfddt}tjj |}|S)NceZdZfdZy)&NthAlgebraic._get_diffx..diffxcfdS)Nc4t|tdzS)Nr)r'r)exprrs rLrz@NthAlgebraic._get_diffx..diffx.inverse..ss(;eCj(HrKrJ)r\rs rLinversez.NthAlgebraic._get_diffx..diffx.inverses IHrKN)rFrGrHr)rsrLrrs IrKr)rrrr setdefault)rdiffclsrs` rLrzNthAlgebraic._get_diffxsM,,00d; ? I I#00;;CGGrKNrrY) rFrGrHrIrrrrrr staticmethodrrJrKrLrrUsC, DL.`>B35M/4rKrc@eZdZdZdZdZdgZdZdZd dZ ddd d Z y ) FirstExacta Solves 1st order exact ordinary differential equations. A 1st order differential equation is called exact if it is the total differential of a function. That is, the differential equation .. math:: P(x, y) \,\partial{}x + Q(x, y) \,\partial{}y = 0 is exact if there is some function `F(x, y)` such that `P(x, y) = \partial{}F/\partial{}x` and `Q(x, y) = \partial{}F/\partial{}y`. It can be shown that a necessary and sufficient condition for a first order ODE to be exact is that `\partial{}P/\partial{}y = \partial{}Q/\partial{}x`. Then, the solution will be as given below:: >>> from sympy import Function, Eq, Integral, symbols, pprint >>> x, y, t, x0, y0, C1= symbols('x,y,t,x0,y0,C1') >>> P, Q, F= map(Function, ['P', 'Q', 'F']) >>> pprint(Eq(Eq(F(x, y), Integral(P(t, y), (t, x0, x)) + ... Integral(Q(x0, t), (t, y0, y))), C1)) x y / / | | F(x, y) = | P(t, y) dt + | Q(x0, t) dt = C1 | | / / x0 y0 Where the first partials of `P` and `Q` exist and are continuous in a simply connected region. A note: SymPy currently has no way to represent inert substitution on an expression, so the hint ``1st_exact_Integral`` will return an integral with `dy`. This is supposed to represent the function that you are solving for. Examples ======== >>> from sympy import Function, dsolve, cos, sin >>> from sympy.abc import x >>> f = Function('f') >>> dsolve(cos(f(x)) - (x*sin(f(x)) - f(x)**2)*f(x).diff(x), ... f(x), hint='1st_exact') Eq(x*cos(f(x)) + f(x)**3/3, C1) References ========== - https://en.wikipedia.org/wiki/Exact_differential_equation - M. Tenenbaum & H. Pollard, "Ordinary Differential Equations", Dover 1963, pp. 73 # indirect doctest 1st_exactTrctd||j|g}td||j|g}||fSNPrgQrrr\rrrarrs rLrzFirstExact._wilds? qtyy|n - qtyy|n -!t rKcV|j\}}|||j|zzSrWrrr\rrrarrs rLrzFirstExact._equation(zz|11RWWQZ<rKc|j\}}|jj}td}|j \}}|j ||}|j ||}t |j||j|z }|jryt ||z } t | |z } || jvr| } |} n|| jvr| } |} nytt| | } || z}|| z}|j |||j|<|j |||j|<y)NyTF) rrrQrrrr)ris_zerorrr'r) r\rrrrr mr numeratorfactor_nfactor_mr*integration_variables rLrzFirstExact._verifys<zz|1     #J!1 FF2qM FF2qM166!9qvvay01   ik*Hyjl+H---"'($(///"'($&*>?@F KA KA#$66!R=D  a #$66!R=D  a rKrc |j\}}|jj}|jj}|jj d\}t d}|j ||}|j ||}ttt||t|t||j|z |z|||}|gS)Nrr~r ) rrrPrQrrrrrr'r) r\rr rrrC1r gen_sols rLrz FirstExact._get_general_solution-s!1    " "      77A7> #J FF2qM FF2qMT(1a.$Q!Q)<)B rKrc8eZdZdZdZdZdgZdZdZddd dZ y ) FirstLineara Solves 1st order linear differential equations. These are differential equations of the form .. math:: dy/dx + P(x) y = Q(x)\text{.} These kinds of differential equations can be solved in a general way. The integrating factor `e^{\int P(x) \,dx}` will turn the equation into a separable equation. The general solution is:: >>> from sympy import Function, dsolve, Eq, pprint, diff, sin >>> from sympy.abc import x >>> f, P, Q = map(Function, ['f', 'P', 'Q']) >>> genform = Eq(f(x).diff(x) + P(x)*f(x), Q(x)) >>> pprint(genform) d P(x)*f(x) + --(f(x)) = Q(x) dx >>> pprint(dsolve(genform, f(x), hint='1st_linear_Integral')) / / \ | | | | | / | / | | | | | | | | P(x) dx | - | P(x) dx | | | | | | | / | / f(x) = |C1 + | Q(x)*e dx|*e | | | \ / / Examples ======== >>> f = Function('f') >>> pprint(dsolve(Eq(x*diff(f(x), x) - f(x), x**2*sin(x)), ... f(x), '1st_linear')) f(x) = x*(C1 - cos(x)) References ========== - https://en.wikipedia.org/wiki/Linear_differential_equation#First-order_equation_with_variable_coefficients - M. Tenenbaum & H. Pollard, "Ordinary Differential Equations", Dover 1963, pp. 92 # indirect doctest 1st_linearTrctd||g}td||||j|g}||fSrrrs rLrzFirstLinear._wildsss> qtf % qtQqTYYq\2 3!t rKc\|j\}}|j|||zz|z SrWrrs rLrzFirstLinear._equationxs-zz|1wwqzAbD 1$$rKrc N|j\}}|jj}|jj}|jj d\}t ||t |tt ||z|ztt || z}|gSNrr)rrrPrQrrr'r)r\rrrrrrgensols rLrz!FirstLinear._get_general_solution|s!1    " "    !!88Q8?B"x#hq!n*=(=qAA8Aq>/"#%xrKNr rFrGrHrIrrrarrrrJrKrLrr<s01d DL CE %>BrKrc>eZdZdZdZdZdgZdZdZdZ ddd d Z y ) AlmostLineara Solves an almost-linear differential equation. The general form of an almost linear differential equation is .. math:: a(x) g'(f(x)) f'(x) + b(x) g(f(x)) + c(x) Here `f(x)` is the function to be solved for (the dependent variable). The substitution `g(f(x)) = u(x)` leads to a linear differential equation for `u(x)` of the form `a(x) u' + b(x) u + c(x) = 0`. This can be solved for `u(x)` by the `first_linear` hint and then `f(x)` is found by solving `g(f(x)) = u(x)`. See Also ======== :obj:`sympy.solvers.ode.single.FirstLinear` Examples ======== >>> from sympy import dsolve, Function, pprint, sin, cos >>> from sympy.abc import x >>> f = Function('f') >>> d = f(x).diff(x) >>> eq = x*d + x*f(x) + 1 >>> dsolve(eq, f(x), hint='almost_linear') Eq(f(x), (C1 - Ei(x))*exp(-x)) >>> pprint(dsolve(eq, f(x), hint='almost_linear')) -x f(x) = (C1 - Ei(x))*e >>> example = cos(f(x))*f(x).diff(x) + sin(f(x)) + 1 >>> pprint(example) d sin(f(x)) + cos(f(x))*--(f(x)) + 1 dx >>> pprint(dsolve(example, f(x), hint='almost_linear')) / -x \ / -x \ [f(x) = pi - asin\C1*e - 1/, f(x) = asin\C1*e - 1/] References ========== - Joel Moses, "Symbolic Integration - The Stormy Decade", Communications of the ACM, Volume 14, Number 8, August 1971, pp. 558 almost_linearTrctd||j|g}td||j|g}||fSrrrs rLrzAlmostLinear._wildsrrKcV|j\}}||j|z|zSrWrrs rLrzAlmostLinear._equations(zz|1|arKc|j\}}|jr|j|ntj|f\}}|j |dk7rt |j ||z j|smt|j|dd|_ ||jj |z |_ | |_ t||jz |_ yy)NrFas_AddrT) rrjas_independentr rrr-rrlyaxcxbx)r\rrfbrs rLrzAlmostLinear._verifys!1'(xxq#affa[1 66":?8AFF2JqL#9#=#=b#A"1o44R4FqIDG$'',,r**DGbDG"1o/DGrKrc |jj}|jjd\}t|j|t |j |jz tt |j|jz |z|ztt |j|jz | z}|gSr) rrQrrr)r'r+r*rr,)r\rrrrs rLrz"AlmostLinear._get_general_solutions    !!88Q8?DGGrHdggdggos8DGGTXT[T[O]^C_?`-`bc$ddx33457xrKNrrrJrKrLr!r!s5-\ DL CE  ">BrKr!c8eZdZdZdZdZdgZdZdZddd dZ y) Bernoullia{ Solves Bernoulli differential equations. These are equations of the form .. math:: dy/dx + P(x) y = Q(x) y^n\text{, }n \ne 1`\text{.} The substitution `w = 1/y^{1-n}` will transform an equation of this form into one that is linear (see the docstring of :obj:`~sympy.solvers.ode.single.FirstLinear`). The general solution is:: >>> from sympy import Function, dsolve, Eq, pprint >>> from sympy.abc import x, n >>> f, P, Q = map(Function, ['f', 'P', 'Q']) >>> genform = Eq(f(x).diff(x) + P(x)*f(x), Q(x)*f(x)**n) >>> pprint(genform) d n P(x)*f(x) + --(f(x)) = Q(x)*f (x) dx >>> pprint(dsolve(genform, f(x), hint='Bernoulli_Integral'), num_columns=110) -1 ----- n - 1 // / / \ \ || | | | | || | / | / | / | || | | | | | | | || | -(n - 1)* | P(x) dx | -(n - 1)* | P(x) dx | (n - 1)* | P(x) dx| || | | | | | | | || | / | / | / | f(x) = ||C1 - n* | Q(x)*e dx + | Q(x)*e dx|*e | || | | | | \\ / / / / Note that the equation is separable when `n = 1` (see the docstring of :obj:`~sympy.solvers.ode.single.Separable`). >>> pprint(dsolve(Eq(f(x).diff(x) + P(x)*f(x), Q(x)*f(x)), f(x), ... hint='separable_Integral')) f(x) / | / | 1 | | - dy = C1 + | (-P(x) + Q(x)) dx | y | | / / Examples ======== >>> from sympy import Function, dsolve, Eq, pprint, log >>> from sympy.abc import x >>> f = Function('f') >>> pprint(dsolve(Eq(x*f(x).diff(x) + f(x), log(x)*f(x)**2), ... f(x), hint='Bernoulli')) 1 f(x) = ----------------- C1*x + log(x) + 1 References ========== - https://en.wikipedia.org/wiki/Bernoulli_differential_equation - M. Tenenbaum & H. Pollard, "Ordinary Differential Equations", Dover 1963, pp. 95 # indirect doctest Trctd||g}td||g}td|||||j|g}|||fS)Nrrgrrr)r\rrrarrrs rLrzBernoulli._wilds+sT qtf % qtf % q!A$! ! 5 6!QwrKcj|j\}}}|j|||zz|||zzz SrWr)r\rrrarrrs rLrzBernoulli._equation1s7**,1awwqzAbD 1RU7**rKrc|j\}}}|jj}|jj}|jj d\}|dk(r)t t ||t| |z|z}|gSt |d|z z||dz t|t| t||zztt||z|zz td|z t||zz}|gSr) rrrPrQrrr r'r) r\rrrrrrrrs rLrzBernoulli._get_general_solution5s""$1a    " "      77A7> a4B A26A& & Fx QqS q1u3r(1a./@+A)A!(1a.1*234!666!a%!Q/01F xrKNrrrJrKrLr0r0s1IT DL CE +>BrKr0c,eZdZdZdZdZdZddd dZy) Factorablea Solves equations having a solvable factor. This function is used to solve the equation having factors. Factors may be of type algebraic or ode. It will try to solve each factor independently. Factors will be solved by calling dsolve. We will return the list of solutions. Examples ======== >>> from sympy import Function, dsolve, pprint >>> from sympy.abc import x >>> f = Function('f') >>> eq = (f(x)**2-4)*(f(x).diff(x)+f(x)) >>> pprint(dsolve(eq, f(x))) -x [f(x) = 2, f(x) = -2, f(x) = C1*e ] factorableFc$|jj}|jjj}|jj}||j |}g|_|j ||t}tt|d}tjt|}|Dcgc]+}t|jdk7s|j-}}t|dkDs |dddkDrV|D]8\} } | j||s|j j!| :t|j dkDryt#||}t|dkDr*|D cgc]} || z  c} |_|j |gk7} | S|D]5} | j||s|j j!| 7t|j dkDxrt|dkDScc}wcc} w)NrrrT)rrOrPrQreqsr,r)r1r*r make_argsrrm as_base_exprappendr3)r\eq_origrrrrOfactorsfacrootsbaseexporootrrs rLrzFactorable._matches_s""%%    ! ! & &     qTYYq\ __QqT&_ 1 fRj !! $--r +.5JsSXX9I"JJ u:a<58A;q=# * d88AaD>HHOOD) *488}Qb"  u:a<056d6DHhh7)+GN #AuuQqT{" #488}Q13w<>1!K7sHH H Trc|jjj}|jj}|j}g}|D]H} t |||}t |t r|j|8|j|J|gk(rtdtzdzdz|S#t$rYzwxYw)NThe given ODE  cannot be solved byz the factorable group method) rrPrQr8dsolverXlistextendr;rr)r\rrPreqnssolsrOsols rLrz Factorable._get_general_solutionzs$$))    xx %B %Ra)c4(KK$KK$ % 2:%&6R&@CY&Y0'12 2 '  s B66 CCNrrFrGrHrIrrrrrJrKrLr5r5Gs# ( DL26>BrKr5c8eZdZdZdZdZdgZdZdZddd d Z y ) RiccatiSpeciala| The general Riccati equation has the form .. math:: dy/dx = f(x) y^2 + g(x) y + h(x)\text{.} While it does not have a general solution [1], the "special" form, `dy/dx = a y^2 - b x^c`, does have solutions in many cases [2]. This routine returns a solution for `a(dy/dx) = b y^2 + c y/x + d/x^2` that is obtained by using a suitable change of variables to reduce it to the special form and is valid when neither `a` nor `b` are zero and either `c` or `d` is zero. >>> from sympy.abc import x, a, b, c, d >>> from sympy import dsolve, checkodesol, pprint, Function >>> f = Function('f') >>> y = f(x) >>> genform = a*y.diff(x) - (b*y**2 + c*y/x + d/x**2) >>> sol = dsolve(genform, y, hint="Riccati_special_minus2") >>> pprint(sol, wrap_line=False) / / __________________ \\ | __________________ | / 2 || | / 2 | \/ 4*b*d - (a + c) *log(x)|| -|a + c - \/ 4*b*d - (a + c) *tan|C1 + ----------------------------|| \ \ 2*a // f(x) = ------------------------------------------------------------------------ 2*b*x >>> checkodesol(genform, sol, order=1)[0] True References ========== - https://www.maplesoft.com/support/help/Maple/view.aspx?path=odeadvisor/Riccati - https://eqworld.ipmnet.ru/en/solutions/ode/ode0106.pdf - https://eqworld.ipmnet.ru/en/solutions/ode/ode0123.pdf Riccati_special_minus2Frcjtd|||||j|dg}td|||||j|dg}td|||||j|g}td|||||j|g}||||fS)Nrfrrgr-rdr)r\rrrarfr-rrQs rLrzRiccatiSpecial._wildss q!A$! ! a8 9 q!A$! ! a8 9 q!A$! ! 5 6 q!A$! ! 5 6!QzrKc|j\}}}}||j|z||dzzz||z|z z||dzz zSNrr)r\rrrarfr-rrQs rLrzRiccatiSpecial._equationsNZZ\ 1a|aAg%"Q.1a477rKTrc r|j\}}}}|jj}|jj}|jj d\}t d|z|z||z dzz } t |||z | t| d|zz t|z|zzz d|z|zz } | gS)Nrrr) rrrPrQrr!rrr ) r\rrfr-rrQrrrmurs rLrz$RiccatiSpecial._get_general_solutions%%' 1a    " "      77A7> !A#a%1q51*$ %BQCAaCQ"(<$=!==!AFGxrKNrrrJrKrLrNrNs0$J $DL CE8>BrKrNc>eZdZdZdZdZdgZdZdZdZ dd d d Z y ) RationalRiccatia Gives general solutions to the first order Riccati differential equations that have atleast one rational particular solution. .. math :: y' = b_0(x) + b_1(x) y + b_2(x) y^2 where `b_0`, `b_1` and `b_2` are rational functions of `x` with `b_2 \ne 0` (`b_2 = 0` would make it a Bernoulli equation). Examples ======== >>> from sympy import Symbol, Function, dsolve, checkodesol >>> f = Function('f') >>> x = Symbol('x') >>> eq = -x**4*f(x)**2 + x**3*f(x).diff(x) + x**2*f(x) + 20 >>> sol = dsolve(eq, hint="1st_rational_riccati") >>> sol Eq(f(x), (4*C1 - 5*x**9 - 4)/(x**2*(C1 + x**9 - 1))) >>> checkodesol(eq, sol) (True, 0) References ========== - Riccati ODE: https://en.wikipedia.org/wiki/Riccati_equation - N. Thieu Vo - Rational and Algebraic Solutions of First-Order Algebraic ODEs: Algorithm 11, pp. 78 - https://www3.risc.jku.at/publications/download/risc_5387/PhDThesisThieu.pdf F1st_rational_riccatirctd||||j|g}td||||j|g}td||||j|g}|||fS)Nb0rgb1b2r)r\rrrar[r\r]s rLrzRationalRiccati._wildssq $1qtyy| 4 5 $1qtyy| 4 5 $1qtyy| 4 5B|rKcp|j\}}}|j||z ||zz ||dzzz SrSr)r\rrrar[r\r]s rLrzRationalRiccati._equations;ZZ\ BwwqzBB&BE11rKcL|jj}|jjj}|jj}|jj}|dk7ryt |||\}}|sy|\}}} |j \} } } | || || | ix|_}y)NrFT)rrurPrQrarrr) r\rOrrrarlfuncs_b0_b1_b2r[r\r]s rLrzRationalRiccati._matchess    ) )    ! ! & &      && A:$RA. u S#ZZ\ B%'b#r3$??ErKTrc|j\}}}|jj}|jj}t |||||dS)NT)r)rrrPrQr)r\rr[r\r]rrs rLrz%RationalRiccati._get_general_solutionsM%%' B    " "    RBBt<B=rKrXc>eZdZdZdZdZdgZdZdZdZ ddd d Z y ) "SecondNonlinearAutonomousConserveda< Gives solution for the autonomous second order nonlinear differential equation of the form .. math :: f''(x) = g(f(x)) The solution for this differential equation can be computed by multiplying by `f'(x)` and integrating on both sides, converting it into a first order differential equation. Examples ======== >>> from sympy import Function, symbols, dsolve >>> f, g = symbols('f g', cls=Function) >>> x = symbols('x') >>> eq = f(x).diff(x, 2) - g(f(x)) >>> dsolve(eq, simplify=False) [Eq(Integral(1/sqrt(C1 + 2*Integral(g(_u), _u)), (_u, f(x))), C2 + x), Eq(Integral(1/sqrt(C1 + 2*Integral(g(_u), _u)), (_u, f(x))), C2 - x)] >>> from sympy import exp, log >>> eq = f(x).diff(x, 2) - exp(f(x)) + log(f(x)) >>> dsolve(eq, simplify=False) [Eq(Integral(1/sqrt(-2*_u*log(_u) + 2*_u + C1 + 2*exp(_u)), (_u, f(x))), C2 + x), Eq(Integral(1/sqrt(-2*_u*log(_u) + 2*_u + C1 + 2*exp(_u)), (_u, f(x))), C2 - x)] References ========== - https://eqworld.ipmnet.ru/en/solutions/ode/ode0301.pdf "2nd_nonlinear_autonomous_conservedTrc ~tdd||j|||j|dg}|fS)Nfyrrrgr)r\rrraris rLrz)SecondNonlinearAutonomousConserved._wilds<s6 $AaDIIaL!A$))Aq/ B Cv rKcR|jd}|j|d|zS)Nrrr)r\rrraris rLrz,SecondNonlinearAutonomousConserved._equation@s& ZZ\!_wwq!}r!!rKc.|jjSrW)rrrs rLrz*SecondNonlinearAutonomousConserved._verifyDs---rKrc|jd}|jj}|jj}t d}|j ||}|jj d\}}dt||z|z}tdt|z ||f} t| ||zt| ||z gS)Nrrrrr) rrrPrQrrrr'r!r) r\rgrrrrC2insidelhss rLrz8SecondNonlinearAutonomousConserved._get_general_solutionGs    q !    " "     #J FF2qM!!88Q8?BHQN"R'qf~2w/3QCa11rKNrrrJrKrLrfrfs5 B 0DL CE".>B 2rKrfc>eZdZdZdZdZdgZdZdZdZ ddd dZ y ) Liouvillea Solves 2nd order Liouville differential equations. The general form of a Liouville ODE is .. math:: \frac{d^2 y}{dx^2} + g(y) \left(\! \frac{dy}{dx}\!\right)^2 + h(x) \frac{dy}{dx}\text{.} The general solution is: >>> from sympy import Function, dsolve, Eq, pprint, diff >>> from sympy.abc import x >>> f, g, h = map(Function, ['f', 'g', 'h']) >>> genform = Eq(diff(f(x),x,x) + g(f(x))*diff(f(x),x)**2 + ... h(x)*diff(f(x),x), 0) >>> pprint(genform) 2 2 /d \ d d g(f(x))*|--(f(x))| + h(x)*--(f(x)) + ---(f(x)) = 0 \dx / dx 2 dx >>> pprint(dsolve(genform, f(x), hint='Liouville_Integral')) f(x) / / | | | / | / | | | | | - | h(x) dx | | g(y) dy | | | | | / | / C1 + C2* | e dx + | e dy = 0 | | / / Examples ======== >>> from sympy import Function, dsolve, Eq, pprint >>> from sympy.abc import x >>> f = Function('f') >>> pprint(dsolve(diff(f(x), x, x) + diff(f(x), x)**2/f(x) + ... diff(f(x), x)/x, f(x), hint='Liouville')) ________________ ________________ [f(x) = -\/ C1 + C2*log(x) , f(x) = \/ C1 + C2*log(x) ] References ========== - Goldstein and Braun, "Advanced Methods for the Solution of Differential Equations", pp. 98 - https://www.maplesoft.com/support/help/Maple/view.aspx?path=odeadvisor/Liouville # indirect doctest Trc td||j|||j|dg}td||j|g}td||j|g}|||fS)NrQrrgrkr)r\rrrarQrrus rLrzLiouville._wildssl qtyy|QqTYYq!_= > qtyy|n - qtyy|n -!QwrKc|j\}}}||j|dz||j|dzzz||j|zzSrSr)r\rrrarQrrus rLrzLiouville._equationsO **,1aA2771:q=01RWWQZ<??rKc|j\}}}td|_|jj}t ||z j ||j|_t ||z j ||j|_|j|jjvs||jjvryy)Nr FT) rrr rrQr-rrnhr)r\rrQrrurs rLrzLiouville._verifys""$1as    !A###B/!A###B/ 66TVV(( (A1D1D,DrKrc |j\}}}|jj}|jj}|jj d\}}t t t |j|j|jd|f} t| |t t t |j| |zz|zd} | gS)Nrrr) rrrPrQrr'rrnr rrx) r\rrQrrurrrrorRrs rLrzLiouville._get_general_solutions""$1a    " "    !!88Q8?Bs8DFFDFF34tvvtR6HIS2hsHTVVQ,?+?'@!DDDrI1MyrKNrrrJrKrLrsrsSs67p DL CE @>BrKrscDeZdZdZdZdZdgZdZdZdZ dZ dd d d Z y ) Separableaa Solves separable 1st order differential equations. This is any differential equation that can be written as `P(y) \tfrac{dy}{dx} = Q(x)`. The solution can then just be found by rearranging terms and integrating: `\int P(y) \,dy = \int Q(x) \,dx`. This hint uses :py:meth:`sympy.simplify.simplify.separatevars` as its back end, so if a separable equation is not caught by this solver, it is most likely the fault of that function. :py:meth:`~sympy.simplify.simplify.separatevars` is smart enough to do most expansion and factoring necessary to convert a separable equation `F(x, y)` into the proper form `P(x)\cdot{}Q(y)`. The general solution is:: >>> from sympy import Function, dsolve, Eq, pprint >>> from sympy.abc import x >>> a, b, c, d, f = map(Function, ['a', 'b', 'c', 'd', 'f']) >>> genform = Eq(a(x)*b(f(x))*f(x).diff(x), c(x)*d(f(x))) >>> pprint(genform) d a(x)*b(f(x))*--(f(x)) = c(x)*d(f(x)) dx >>> pprint(dsolve(genform, f(x), hint='separable_Integral')) f(x) / / | | | b(y) | c(x) | ---- dy = C1 + | ---- dx | d(y) | a(x) | | / / Examples ======== >>> from sympy import Function, dsolve, Eq >>> from sympy.abc import x >>> f = Function('f') >>> pprint(dsolve(Eq(f(x)*f(x).diff(x) + x, 3*x*f(x)**2), f(x), ... hint='separable', simplify=False)) / 2 \ 2 log\3*f (x) - 1/ x ---------------- = C1 + -- 6 2 References ========== - M. Tenenbaum & H. Pollard, "Ordinary Differential Equations", Dover 1963, pp. 52 # indirect doctest separableTrctd||j|||j|dg}td||j|g}||fSNrQrrgrrr\rrrarQrs rLrzSeparable._wildsP qtyy|QqTYYq!_= > qtyy|n -!t rKcV|j\}}|||j|zzSrWrr\rrrarQrs rLrzSeparable._equationr rKc|j\}}td|_|jj}t |j ||j}t |j ||j}t |d||jf|_t |d||jf|_t|jxr |jS)Nr T)dictsymbols) rrr rrQr.rm1m2rY)r\rrQrrs rLrzSeparable._verifys!1s     DFF+ , DFF+ ,qta[Aqta[ADGG'((rKc|jj}|jj}|j|j||fSrW)rrPrQrr)r\rrs rL_get_match_objectzSeparable._get_match_objects;    " "    wwB&&rKrc8|j\}}}}|jjd\}t|d||jz||jz |jd|f}t |t|d ||z||z ||z}|gS)Nrrrk)rrrr'r r) r\rrrrrrrRrs rLrzSeparable._get_general_solutions..0 B2  77A7>r'{2dff:-bj8 rS(BwK<1#5 1$yrKNr rFrGrHrIrrrarrrrrrJrKrLr{r{s:5l DL CE   )' >BrKr{c6eZdZdZdZdZdgZdZdZdZ dZ y ) SeparableReduceda7 Solves a differential equation that can be reduced to the separable form. The general form of this equation is .. math:: y' + (y/x) H(x^n y) = 0\text{}. This can be solved by substituting `u(y) = x^n y`. The equation then reduces to the separable form `\frac{u'}{u (\mathrm{power} - H(u))} - \frac{1}{x} = 0`. The general solution is: >>> from sympy import Function, dsolve, pprint >>> from sympy.abc import x, n >>> f, g = map(Function, ['f', 'g']) >>> genform = f(x).diff(x) + (f(x)/x)*g(x**n*f(x)) >>> pprint(genform) / n \ d f(x)*g\x *f(x)/ --(f(x)) + --------------- dx x >>> pprint(dsolve(genform, hint='separable_reduced')) n x *f(x) / | | 1 | ------------ dy = C1 + log(x) | y*(n - g(y)) | / See Also ======== :obj:`sympy.solvers.ode.single.Separable` Examples ======== >>> from sympy import dsolve, Function, pprint >>> from sympy.abc import x >>> f = Function('f') >>> d = f(x).diff(x) >>> eq = (x - x**2*f(x))*d - f(x) >>> dsolve(eq, hint='separable_reduced') [Eq(f(x), (1 - sqrt(C1*x**2 + 1))/x), Eq(f(x), (sqrt(C1*x**2 + 1) + 1)/x)] >>> pprint(dsolve(eq, hint='separable_reduced')) ___________ ___________ / 2 / 2 1 - \/ C1*x + 1 \/ C1*x + 1 + 1 [f(x) = ------------------, f(x) = ------------------] x x References ========== - Joel Moses, "Symbolic Integration - The Stormy Decade", Communications of the ACM, Volume 14, Number 8, August 1971, pp. 558 separable_reducedTrc|D]}|j|st|tr+|jd|k(r|jdcS||k(r|jdcS|j |j |cSyr)rrXr r:_degreerm)r\rrvals rLrzSeparableReduced._degreeRs  5Cwwqzc3'COO,=a,@A,EOO-a01AXOO-a01<<!44 5rKct}|jj}|jj}t d|_t |tr|jt}nOt |tr|jt}n)t |tr|jt}n|h}|D]}|j|s|j||\}}|j|j||j f||j|j||j f|j z } |j| |S)Nr )setrrPrQrr rXr rrr rr(rradd) r\rpowsrrexprsrrrrpows rL_powerszSeparableReduced._powersasu    " "    s dC JJsOE c "JJsOE c "JJsOEFE Cwwqz))!R01llAFF2tvv$6#91=dllAFFSUW[W]W]L^Kacgcici>jj    rKc|j\}}|jj}t||z |z|z }|j\}}t |}t |}|j |}|j|j |t|}t|dk(r|dtk7rtd|_ d|ji|_ |j||dz|z|j}|j||dz|z|j}||z }|j} t| dk(r?| j!|jk(r"|jj|d|dyyy)Nrrt)powerrTF)rrrQr-as_numer_denomrrupdaterGrrrrr2rrpop) r\rr~rqrr*demrtestfrees rLrzSeparableReduced._verifyxsP##%S    !B$s(3,')6((*SSkSk||C  DLL%&Dz t9a?rKc|jj}|jj}|jdj |jd|j }d|j |jd|z zz }|j d|d|z ddi}|j ||dddi}|||||jdz|zfS)Nrrrrrrk)rrPrQrrr )r\rrrycoeffrrs rLrz"SeparableReduced._get_match_objects    " "     GGCL  dggclDFF 3DFFDGGG,q012ffaBqD'1 -fffaGQ /2q!TWWW--b000rKN) rFrGrHrIrrrarrrrrJrKrLrrs/;x DL CE .41rKrcDeZdZdZdZdZdgZdZdZdZ dZ dd d d Z y ) HomogeneousCoeffSubsDepDivIndepac Solves a 1st order differential equation with homogeneous coefficients using the substitution `u_1 = \frac{\text{}}{\text{}}`. This is a differential equation .. math:: P(x, y) + Q(x, y) dy/dx = 0 such that `P` and `Q` are homogeneous and of the same order. A function `F(x, y)` is homogeneous of order `n` if `F(x t, y t) = t^n F(x, y)`. Equivalently, `F(x, y)` can be rewritten as `G(y/x)` or `H(x/y)`. See also the docstring of :py:meth:`~sympy.solvers.ode.homogeneous_order`. If the coefficients `P` and `Q` in the differential equation above are homogeneous functions of the same order, then it can be shown that the substitution `y = u_1 x` (i.e. `u_1 = y/x`) will turn the differential equation into an equation separable in the variables `x` and `u`. If `h(u_1)` is the function that results from making the substitution `u_1 = f(x)/x` on `P(x, f(x))` and `g(u_2)` is the function that results from the substitution on `Q(x, f(x))` in the differential equation `P(x, f(x)) + Q(x, f(x)) f'(x) = 0`, then the general solution is:: >>> from sympy import Function, dsolve, pprint >>> from sympy.abc import x >>> f, g, h = map(Function, ['f', 'g', 'h']) >>> genform = g(f(x)/x) + h(f(x)/x)*f(x).diff(x) >>> pprint(genform) /f(x)\ /f(x)\ d g|----| + h|----|*--(f(x)) \ x / \ x / dx >>> pprint(dsolve(genform, f(x), ... hint='1st_homogeneous_coeff_subs_dep_div_indep_Integral')) f(x) ---- x / | | -h(u1) log(x) = C1 + | ---------------- d(u1) | u1*h(u1) + g(u1) | / Where `u_1 h(u_1) + g(u_1) \ne 0` and `x \ne 0`. See also the docstrings of :obj:`~sympy.solvers.ode.single.HomogeneousCoeffBest` and :obj:`~sympy.solvers.ode.single.HomogeneousCoeffSubsIndepDivDep`. Examples ======== >>> from sympy import Function, dsolve >>> from sympy.abc import x >>> f = Function('f') >>> pprint(dsolve(2*x*f(x) + (x**2 + f(x)**2)*f(x).diff(x), f(x), ... hint='1st_homogeneous_coeff_subs_dep_div_indep', simplify=False)) / 3 \ |3*f(x) f (x)| log|------ + -----| | x 3 | \ x / log(x) = log(C1) - ------------------- 3 References ========== - https://en.wikipedia.org/wiki/Homogeneous_differential_equation - M. Tenenbaum & H. Pollard, "Ordinary Differential Equations", Dover 1963, pp. 59 # indirect doctest (1st_homogeneous_coeff_subs_dep_div_indepTrctd||j|||j|dg}td||j|g}||fSr~rrs rLrz&HomogeneousCoeffSubsDepDivIndep._wildsrrKcV|j\}}|||j|zzSrWrrs rLrz)HomogeneousCoeffSubsDepDivIndep._equationr rKc|j\|_|_td|_|j j }t|jj||j|_t|jj||j|_t|j||j}t|j||j}||k(rm|ktd|_ t|j|j|jzzj|d|j|jidk7ryyyNr rrrTF rrQrrr rrQr.rhomogeneous_orderrr-r\rrorderaorderbs rLrz'HomogeneousCoeffSubsDepDivIndep._verifys))+s    dffkk"dff56dffkk"dff56"4661dff5"4661dff5 V  23ZDF$&&-/55q!TVVTVV6LMNRSSrKc |jj}|jj}td|_d}d}|j |j |||j|j|j||g SNu1r rrPrQrrrQrrr r\rrxargyargs rLrz1HomogeneousCoeffSubsDepDivIndep._get_match_objectf    " "    +AtvvtwwdKKrKrc |j\ }}}}}}}} } |jjd\} t| |||zzz j |d||i|d||z f} t t t|| t| zd} | j ||j ||| z f||| z f||ff}|gSNrrT)force)rrrr'rr/rr r\rrQrrrrrr rrrrRrKrs rLrz5HomogeneousCoeffSubsDepDivIndep._get_general_solution s,0,B,B,D)1b!QAtT  77A7>RRT] !Q2 / r!t CFC#b'M2$?((2q/&&AH 1t8}q"g'NOyrKNrrrJrKrLrrs<KX 6DL CE   L>BrKrcDeZdZdZdZdZdgZdZdZdZ dZ dd d d Z y ) HomogeneousCoeffSubsIndepDivDepa Solves a 1st order differential equation with homogeneous coefficients using the substitution `u_2 = \frac{\text{}}{\text{}}`. This is a differential equation .. math:: P(x, y) + Q(x, y) dy/dx = 0 such that `P` and `Q` are homogeneous and of the same order. A function `F(x, y)` is homogeneous of order `n` if `F(x t, y t) = t^n F(x, y)`. Equivalently, `F(x, y)` can be rewritten as `G(y/x)` or `H(x/y)`. See also the docstring of :py:meth:`~sympy.solvers.ode.homogeneous_order`. If the coefficients `P` and `Q` in the differential equation above are homogeneous functions of the same order, then it can be shown that the substitution `x = u_2 y` (i.e. `u_2 = x/y`) will turn the differential equation into an equation separable in the variables `y` and `u_2`. If `h(u_2)` is the function that results from making the substitution `u_2 = x/f(x)` on `P(x, f(x))` and `g(u_2)` is the function that results from the substitution on `Q(x, f(x))` in the differential equation `P(x, f(x)) + Q(x, f(x)) f'(x) = 0`, then the general solution is: >>> from sympy import Function, dsolve, pprint >>> from sympy.abc import x >>> f, g, h = map(Function, ['f', 'g', 'h']) >>> genform = g(x/f(x)) + h(x/f(x))*f(x).diff(x) >>> pprint(genform) / x \ / x \ d g|----| + h|----|*--(f(x)) \f(x)/ \f(x)/ dx >>> pprint(dsolve(genform, f(x), ... hint='1st_homogeneous_coeff_subs_indep_div_dep_Integral')) x ---- f(x) / | | -g(u1) | ---------------- d(u1) | u1*g(u1) + h(u1) | / f(x) = C1*e Where `u_1 g(u_1) + h(u_1) \ne 0` and `f(x) \ne 0`. See also the docstrings of :obj:`~sympy.solvers.ode.single.HomogeneousCoeffBest` and :obj:`~sympy.solvers.ode.single.HomogeneousCoeffSubsDepDivIndep`. Examples ======== >>> from sympy import Function, pprint, dsolve >>> from sympy.abc import x >>> f = Function('f') >>> pprint(dsolve(2*x*f(x) + (x**2 + f(x)**2)*f(x).diff(x), f(x), ... hint='1st_homogeneous_coeff_subs_indep_div_dep', ... simplify=False)) / 2 \ |3*x | log|----- + 1| | 2 | \f (x) / log(f(x)) = log(C1) - -------------- 3 References ========== - https://en.wikipedia.org/wiki/Homogeneous_differential_equation - M. Tenenbaum & H. Pollard, "Ordinary Differential Equations", Dover 1963, pp. 59 # indirect doctest (1st_homogeneous_coeff_subs_indep_div_depTrctd||j|||j|dg}td||j|g}||fSr~rrs rLrz&HomogeneousCoeffSubsIndepDivDep._wildslrrKcV|j\}}|||j|zzSrWrrs rLrz)HomogeneousCoeffSubsIndepDivDep._equationqr rKc|j\|_|_td|_|j j }t|jj||j|_t|jj||j|_t|j||j}t|j||j}||k(rm|ktd|_ t|j|j|jzzj||j|jdidk7ryyyrrrs rLrz'HomogeneousCoeffSubsIndepDivDep._verifyus))+s    dffkk"dff56dffkk"dff56"4661dff5"4661dff5 V  23ZDF$&&-/55q$&&$&&!6LMNRSSrKc |jj}|jj}td|_d}d}|j |j |||j|j|j||g Srrrs rLrz1HomogeneousCoeffSubsIndepDivDep._get_match_objectrrKrc |j\ }}}}}}}} } |jjd\} tt | |||zzz j |||di|d||z f} t tt|| t| zd} | j ||j ||| z f||| z f||ff}|gSr) rrrr'r-rr/rr rs rLrz5HomogeneousCoeffSubsIndepDivDep._get_general_solutions,0,B,B,D)1b!QAtT  77A7>x!QAX 4 4aQ] CDr4QRSUQUFVWCGS3r7]34@((2q/&&AH 1t8}q"g'NOyrKNrrrJrKrLrrs<N^ 6DL CE   L>BrKrc2eZdZdZdZdZdgZdZddd dZy ) HomogeneousCoeffBesta Returns the best solution to an ODE from the two hints ``1st_homogeneous_coeff_subs_dep_div_indep`` and ``1st_homogeneous_coeff_subs_indep_div_dep``. This is as determined by :py:meth:`~sympy.solvers.ode.ode.ode_sol_simplicity`. See the :obj:`~sympy.solvers.ode.single.HomogeneousCoeffSubsIndepDivDep` and :obj:`~sympy.solvers.ode.single.HomogeneousCoeffSubsDepDivIndep` docstrings for more information on these hints. Note that there is no ``ode_1st_homogeneous_coeff_best_Integral`` hint. Examples ======== >>> from sympy import Function, dsolve, pprint >>> from sympy.abc import x >>> f = Function('f') >>> pprint(dsolve(2*x*f(x) + (x**2 + f(x)**2)*f(x).diff(x), f(x), ... hint='1st_homogeneous_coeff_best', simplify=False)) / 2 \ |3*x | log|----- + 1| | 2 | \f (x) / log(f(x)) = log(C1) - -------------- 3 References ========== - https://en.wikipedia.org/wiki/Homogeneous_differential_equation - M. Tenenbaum & H. Pollard, "Ordinary Differential Equations", Dover 1963, pp. 59 # indirect doctest 1st_homogeneous_coeff_bestFrc^tj||xrtj||SrW)rrrrs rLrzHomogeneousCoeffBest._verifys..66tR@A.66tR@ ArKTrc<tj|}tj|}|jj|rHt |jj g|d}t |jj g|d}t||gfdS)Nrrc*t|t S)N) trysolving)ode_sol_simplicityr-)rrs rLrz.s/A!RX`T`/arK)key)rrrrrPodesimprOmin)r\rsol1sol2rs @rLrz*HomogeneousCoeffBest._get_general_solutions/DDTJ.DDTJ    " " 4++..ffrf;efD4++..ffrf;efDD$<%abbrKNr) rFrGrHrIrrrarrrJrKrLrrs-'P (DL CEA>B crKrc<eZdZdZdZdZdgZdZdZdZ dZ d Z y ) LinearCoefficientsaa Solves a differential equation with linear coefficients. The general form of a differential equation with linear coefficients is .. math:: y' + F\left(\!\frac{a_1 x + b_1 y + c_1}{a_2 x + b_2 y + c_2}\!\right) = 0\text{,} where `a_1`, `b_1`, `c_1`, `a_2`, `b_2`, `c_2` are constants and `a_1 b_2 - a_2 b_1 \ne 0`. This can be solved by substituting: .. math:: x = x' + \frac{b_2 c_1 - b_1 c_2}{a_2 b_1 - a_1 b_2} y = y' + \frac{a_1 c_2 - a_2 c_1}{a_2 b_1 - a_1 b_2}\text{.} This substitution reduces the equation to a homogeneous differential equation. See Also ======== :obj:`sympy.solvers.ode.single.HomogeneousCoeffBest` :obj:`sympy.solvers.ode.single.HomogeneousCoeffSubsIndepDivDep` :obj:`sympy.solvers.ode.single.HomogeneousCoeffSubsDepDivIndep` Examples ======== >>> from sympy import dsolve, Function, pprint >>> from sympy.abc import x >>> f = Function('f') >>> df = f(x).diff(x) >>> eq = (x + f(x) + 1)*df + (f(x) - 6*x + 1) >>> dsolve(eq, hint='linear_coefficients') [Eq(f(x), -x - sqrt(C1 + 7*x**2) - 1), Eq(f(x), -x + sqrt(C1 + 7*x**2) - 1)] >>> pprint(dsolve(eq, hint='linear_coefficients')) ___________ ___________ / 2 / 2 [f(x) = -x - \/ C1 + 7*x - 1, f(x) = -x + \/ C1 + 7*x - 1] References ========== - Joel Moses, "Symbolic Integration - The Stormy Decade", Communications of the ACM, Volume 14, Number 8, August 1971, pp. 558 linear_coefficientsTrctd||j|||j|dg}td||j|g}||fSr~rrs rLrzLinearCoefficients._wilds rrKcV|j\}}|||j|zzSrWrrs rLrzLinearCoefficients._equationr rKcJ|j\|_|_|j\}}|j|jz }|jj }|j ||}|r|\|_|_td}td}td|_ |jjj|j||f||ff} |||jzf|||jzf||j|f||ff} t| j| } tt!| |j||gj#||j|z|z} | r| || |c|_|_t%|j||} t%|j||} | | k(rY| W|jj||j|_|jj||j|_yyyy)Nrrr TF)rrQrrrrQ_linear_coeff_matchrrrr rOrrr-r,rrlr)r\rrfr-Frr[rrdummy_eqrepsrorderdorderes rLrzLinearCoefficients._verifys))+zz|1 FF466M    ))!R0 #) DItyc Ac A3ZDF''**//"''!*a2q'0JKHDII &A M(:Q OaQSWUD d 34H)BGGAJ+;<BB1RWWQZ>> from sympy import Function, sin >>> from sympy.abc import x >>> from sympy.solvers.ode.single import LinearCoefficients >>> f = Function('f') >>> eq = (-25*f(x) - 8*x + 62)/(4*f(x) + 11*x - 11) >>> obj = LinearCoefficients(eq) >>> obj._linear_coeff_match(eq, f(x)) (1/9, 22/9) >>> eq = sin((-5*f(x) - 8*x + 6)/(4*f(x) + x - 1)) >>> obj = LinearCoefficients(eq) >>> obj._linear_coeff_match(eq, f(x)) (19/27, 2/27) >>> eq = sin(f(x)/x) >>> obj = LinearCoefficients(eq) >>> obj._linear_coeff_match(eq, f(x)) rc,t|}|jdd}|jsy|j}|jsy|j}|jsy||z|zz|zk(r|||fSy)z Internal function of _linear_coeff_match that returns Rationals a, b, c if eq is a*x + b*f(x) + c, else None. Tr&rN)rr( is_Rationalrk)rOrrfr-rrs rLabcz3LinearCoefficients._linear_coeff_match..abcVs "B!!!QqT$!7:A== A==1A==QqS1QqT6\A%%!Qw&rKc|jj\}} |}|3|\}}} |}|"|\}}} ||z||zz }|s| r |r |||||| |fSyyyy)a! Internal function of _linear_coeff_match that returns Rationals a1, b1, c1, a2, b2, c2 and a2*b1 - a1*b2 of the expression (a1*x + b1*f(x) + c1)/(a2*x + b2*f(x) + c2) if one of c1 or c2 and a2*b1 - a1*b2 is non-zero, else None. N)togetherr) rrrrQr a1r\ria2r]c2rs rLrlz5LinearCoefficients._linear_coeff_match..matchis<<>002DAqAA} BF=!"JBB22 Aba!2r2r2q88'(b!rKrc34K|]}|k(ywrWrJ)rmirrls rLrz9LinearCoefficients._linear_coeff_match..~s2"eBi2o2sN)rPrmrrr is_Functionrall)r\rrPfir rr\rirr]rdenomrrrrlrs @@@@@rLrz&LinearCoefficients._linear_coeff_match/sJ II IIaL & 9$#'**X"6 >B"''Q, LA bggaj&<&<WWQZ > HBF 1557^ #222,. )BBBErEBrEM5(2b52b5=%*?? ?32 >s C7AC7c |jj}|jj}td|_td}|j |j ||||j|j|j|jg S)Nrr) rrPrQrrrQrr rr)r\rrrs rLrz$LinearCoefficients._get_match_objectsi    " "    + #JAq$''466499diiPPrKN) rFrGrHrIrrrarrrrrrJrKrLrrs80b !DL CE  8Q@fQrKrc,eZdZdZdZdZdZddd dZy) NthOrderReduciblea; Solves ODEs that only involve derivatives of the dependent variable using a substitution of the form `f^n(x) = g(x)`. For example any second order ODE of the form `f''(x) = h(f'(x), x)` can be transformed into a pair of 1st order ODEs `g'(x) = h(g(x), x)` and `f'(x) = g(x)`. Usually the 1st order ODE for `g` is easier to solve. If that gives an explicit solution for `g` then `f` is found simply by integration. Examples ======== >>> from sympy import Function, dsolve, Eq >>> from sympy.abc import x >>> f = Function('f') >>> eq = Eq(x*f(x).diff(x)**2 + f(x).diff(x, 2), 0) >>> dsolve(eq, f(x), hint='nth_order_reducible') ... # doctest: +NORMALIZE_WHITESPACE Eq(f(x), C1 - sqrt(-1/C2)*log(-C2*sqrt(-1/C2) + x) + sqrt(-1/C2)*log(C2*sqrt(-1/C2) + x)) nth_order_reducibleFc~|jj}|jj}|jj} t |j dk(sJ|j tDcgc]8}|j|k(r't |jdk(r|jd:}}|Dcgc] \}}||k(s |}}}t |dkryt||_ t} |j|j||j| j|ryycc}wcc}}w)NrrrFT)rrdrPrQrrmrrrvariable_countrsmallestrrrr) r\rOrPrrQvcrrordsDs rLrzNthOrderReducible._matchess    - -$$     499~"""+-88J+?>avv~#a&6&6"71"<q!>> +daAF++ t9q=D  G 77499Q . 2 6 6t <>+s5=D49 D9D9Trc|jj}|jjj}|jj}|j}|j t Dcgc]}|j}} tj}||vr t|} n%||j||} |j| | |} t| | |} t| ts| g} g} | D];}t|j| || ||}| j|=| Scc}wrW)rrOrPrQrrrnamerrrrrFrXrGr;)r\rrOrrrrfnamesrrnrgeqgsolfsolgsolifsolis rLrz'NthOrderReducible._get_general_solutions"        ! ! & &     MM!#,!78A887<BrKrc,eZdZdZdZdZdZddddZy) SecondHypergeometrica Solves 2nd order linear differential equations. It computes special function solutions which can be expressed using the 2F1, 1F1 or 0F1 hypergeometric functions. .. math:: y'' + A(x) y' + B(x) y = 0\text{,} where `A` and `B` are rational functions. These kinds of differential equations have solution of non-Liouvillian form. Given linear ODE can be obtained from 2F1 given by .. math:: (x^2 - x) y'' + ((a + b + 1) x - c) y' + b a y = 0\text{,} where {a, b, c} are arbitrary constants. Notes ===== The algorithm should find any solution of the form .. math:: y = P(x) _pF_q(..; ..;\frac{\alpha x^k + \beta}{\gamma x^k + \delta})\text{,} where pFq is any of 2F1, 1F1 or 0F1 and `P` is an "arbitrary function". Currently only the 2F1 case is implemented in SymPy but the other cases are described in the paper and could be implemented in future (contributions welcome!). Examples ======== >>> from sympy import Function, dsolve, pprint >>> from sympy.abc import x >>> f = Function('f') >>> eq = (x*x - x)*f(x).diff(x,2) + (5*x - 1)*f(x).diff(x) + 4*f(x) >>> pprint(dsolve(eq, f(x), '2nd_hypergeometric')) _ / / 4 \\ |_ /-1, -1 | \ |C1 + C2*|log(x) + -----||* | | | x| \ \ x + 1// 2 1 \ 1 | / f(x) = -------------------------------------------- 3 (x - 1) References ========== - "Non-Liouvillian solutions for second order linear ODEs" by L. Chan, E.S. Cheb-Terrab 2nd_hypergeometricTcd|jj}|jj}t||}d|_|rb|\}}t |||}|rN|ddk(rFt |d|d|d||_|j|jj||d|jduS)Ntype2F1I0ru sing_point)AB)rrdrPr; match_objectr8r9r)r\rOrPrprrrQs rLrzSecondHypergeometric._matchess    - -$$ $R .  DAq*1a6AV9%(DQtWaPSfVWXdVegk(lD%((4))00aQ@  ,,rKrc|jj}|jj}|jddk(r6t |||j}|t dt |zdzdzgS)NrrrDrEz the hypergeometric method)rrOrPr r:rr)r\rrOrPrKs rLrz*SecondHypergeometric._get_general_solution&s    $$   V $ -,Rt7H7HIC{)*:SW*DG]*]2+344u rKNrrLrJrKrLrrs$5l DL- >B rKrc,eZdZdZdZdZdZddd dZy) !NthLinearConstantCoeffHomogeneousa Solves an `n`\th order linear homogeneous differential equation with constant coefficients. This is an equation of the form .. math:: a_n f^{(n)}(x) + a_{n-1} f^{(n-1)}(x) + \cdots + a_1 f'(x) + a_0 f(x) = 0\text{.} These equations can be solved in a general manner, by taking the roots of the characteristic equation `a_n m^n + a_{n-1} m^{n-1} + \cdots + a_1 m + a_0 = 0`. The solution will then be the sum of `C_n x^i e^{r x}` terms, for each where `C_n` is an arbitrary constant, `r` is a root of the characteristic equation and `i` is one of each from 0 to the multiplicity of the root - 1 (for example, a root 3 of multiplicity 2 would create the terms `C_1 e^{3 x} + C_2 x e^{3 x}`). The exponential is usually expanded for complex roots using Euler's equation `e^{I x} = \cos(x) + I \sin(x)`. Complex roots always come in conjugate pairs in polynomials with real coefficients, so the two roots will be represented (after simplifying the constants) as `e^{a x} \left(C_1 \cos(b x) + C_2 \sin(b x)\right)`. If SymPy cannot find exact roots to the characteristic equation, a :py:class:`~sympy.polys.rootoftools.ComplexRootOf` instance will be return instead. >>> from sympy import Function, dsolve >>> from sympy.abc import x >>> f = Function('f') >>> dsolve(f(x).diff(x, 5) + 10*f(x).diff(x) - 2*f(x), f(x), ... hint='nth_linear_constant_coeff_homogeneous') ... # doctest: +NORMALIZE_WHITESPACE Eq(f(x), C5*exp(x*CRootOf(_x**5 + 10*_x - 2, 0)) + (C1*sin(x*im(CRootOf(_x**5 + 10*_x - 2, 1))) + C2*cos(x*im(CRootOf(_x**5 + 10*_x - 2, 1))))*exp(x*re(CRootOf(_x**5 + 10*_x - 2, 1))) + (C3*sin(x*im(CRootOf(_x**5 + 10*_x - 2, 3))) + C4*cos(x*im(CRootOf(_x**5 + 10*_x - 2, 3))))*exp(x*re(CRootOf(_x**5 + 10*_x - 2, 3)))) Note that because this method does not involve integration, there is no ``nth_linear_constant_coeff_homogeneous_Integral`` hint. Examples ======== >>> from sympy import Function, dsolve, pprint >>> from sympy.abc import x >>> f = Function('f') >>> pprint(dsolve(f(x).diff(x, 4) + 2*f(x).diff(x, 3) - ... 2*f(x).diff(x, 2) - 6*f(x).diff(x) + 5*f(x), f(x), ... hint='nth_linear_constant_coeff_homogeneous')) x -2*x f(x) = (C1 + C2*x)*e + (C3*sin(x) + C4*cos(x))*e References ========== - https://en.wikipedia.org/wiki/Linear_differential_equation section: Nonhomogeneous_equation_with_constant_coefficients - M. Tenenbaum & H. Pollard, "Ordinary Differential Equations", Dover 1963, pp. 211 # indirect doctest %nth_linear_constant_coeff_homogeneousFcxjj}jj}jj}jjjj |||_|r<j r0tfdj Dsj dsyyy)Nc3fK|](}|dk\s j|j*ywrNrprrrr\rs rLrz=NthLinearConstantCoeffHomogeneous._matches..{)'SQAQRFq a(8'S 1#1rTFrrsrPrarQrrprr\rOrPrars` @rLrz*NthLinearConstantCoeffHomogeneous._matchesus    0 0$$  &&    !!99"dEJ TVVC'S$&&'S$S66":rKTrcr|jj}|jj}t|j||\}}|jj t |}tt||Dcgc] \}}||z c}}} t|| } |rt| g||} | gScc}}wNr) rrPrar=rprrr ziprrB) r\rrrar? collectterms constantsrjgsol_rhsrs rLrz7NthLinearConstantCoeffHomogeneous._get_general_solutions    " "  &&?ER|$$;;E ;K 3y%+@A!Q1AB"h &vr<@Dv BsB3 NrrLrJrKrLr r 2s$>~ 3DL >B rKr c,eZdZdZdZdZdZddddZy) +NthLinearConstantCoeffVariationOfParametersa Solves an `n`\th order linear differential equation with constant coefficients using the method of variation of parameters. This method works on any differential equations of the form .. math:: f^{(n)}(x) + a_{n-1} f^{(n-1)}(x) + \cdots + a_1 f'(x) + a_0 f(x) = P(x)\text{.} This method works by assuming that the particular solution takes the form .. math:: \sum_{x=1}^{n} c_i(x) y_i(x)\text{,} where `y_i` is the `i`\th solution to the homogeneous equation. The solution is then solved using Wronskian's and Cramer's Rule. The particular solution is given by .. math:: \sum_{x=1}^n \left( \int \frac{W_i(x)}{W(x)} \,dx \right) y_i(x) \text{,} where `W(x)` is the Wronskian of the fundamental system (the system of `n` linearly independent solutions to the homogeneous equation), and `W_i(x)` is the Wronskian of the fundamental system with the `i`\th column replaced with `[0, 0, \cdots, 0, P(x)]`. This method is general enough to solve any `n`\th order inhomogeneous linear differential equation with constant coefficients, but sometimes SymPy cannot simplify the Wronskian well enough to integrate it. If this method hangs, try using the ``nth_linear_constant_coeff_variation_of_parameters_Integral`` hint and simplifying the integrals manually. Also, prefer using ``nth_linear_constant_coeff_undetermined_coefficients`` when it applies, because it does not use integration, making it faster and more reliable. Warning, using simplify=False with 'nth_linear_constant_coeff_variation_of_parameters' in :py:meth:`~sympy.solvers.ode.dsolve` may cause it to hang, because it will not attempt to simplify the Wronskian before integrating. It is recommended that you only use simplify=False with 'nth_linear_constant_coeff_variation_of_parameters_Integral' for this method, especially if the solution to the homogeneous equation has trigonometric functions in it. Examples ======== >>> from sympy import Function, dsolve, pprint, exp, log >>> from sympy.abc import x >>> f = Function('f') >>> pprint(dsolve(f(x).diff(x, 3) - 3*f(x).diff(x, 2) + ... 3*f(x).diff(x) - f(x) - exp(x)*log(x), f(x), ... hint='nth_linear_constant_coeff_variation_of_parameters')) / / / x*log(x) 11*x\\\ x f(x) = |C1 + x*|C2 + x*|C3 + -------- - ----|||*e \ \ \ 6 36 /// References ========== - https://en.wikipedia.org/wiki/Variation_of_parameters - https://planetmath.org/VariationOfParameters - M. Tenenbaum & H. Pollard, "Ordinary Differential Equations", Dover 1963, pp. 233 # indirect doctest 1nth_linear_constant_coeff_variation_of_parametersTcxjj}jj}jj}jjjj |||_|r<j r0tfdj Dsj dryyy)Nc3fK|](}|dk\s j|j*ywrrrs rLrzGNthLinearConstantCoeffVariationOfParameters._matches..rrrTFrrs` @rLrz4NthLinearConstantCoeffVariationOfParameters._matchess    0 0$$  &&    !!99"dEJ TVVC'S$&&'S$SvvbzrKrc D|jj}|jjj}|jj}|jj}t |j |||\}}|jjt|}tt||D cgc] \} } | | z c} } } t||| } t||||| ||j |} |rt| g|||} | gScc} } wr)rrsrPrQrar=rprrr rrr?rB) r\rrOrrrar?rrrrhomogen_sol_rhs homogen_sols rLrzANthLinearConstantCoeffVariationOfParameters._get_general_solutions    0 0    ! ! & &      &&?!eT|$$;;E ;K #i2GHA!HI1/ 4R1ukSXZ^Z`Z`bop -{mQqT<PK} Is<D NrrLrJrKrLrrs%CH ?DL >B rKrc,eZdZdZdZdZdZddd dZy) .NthLinearConstantCoeffUndeterminedCoefficientsa Solves an `n`\th order linear differential equation with constant coefficients using the method of undetermined coefficients. This method works on differential equations of the form .. math:: a_n f^{(n)}(x) + a_{n-1} f^{(n-1)}(x) + \cdots + a_1 f'(x) + a_0 f(x) = P(x)\text{,} where `P(x)` is a function that has a finite number of linearly independent derivatives. Functions that fit this requirement are finite sums functions of the form `a x^i e^{b x} \sin(c x + d)` or `a x^i e^{b x} \cos(c x + d)`, where `i` is a non-negative integer and `a`, `b`, `c`, and `d` are constants. For example any polynomial in `x`, functions like `x^2 e^{2 x}`, `x \sin(x)`, and `e^x \cos(x)` can all be used. Products of `\sin`'s and `\cos`'s have a finite number of derivatives, because they can be expanded into `\sin(a x)` and `\cos(b x)` terms. However, SymPy currently cannot do that expansion, so you will need to manually rewrite the expression in terms of the above to use this method. So, for example, you will need to manually convert `\sin^2(x)` into `(1 + \cos(2 x))/2` to properly apply the method of undetermined coefficients on it. This method works by creating a trial function from the expression and all of its linear independent derivatives and substituting them into the original ODE. The coefficients for each term will be a system of linear equations, which are be solved for and substituted, giving the solution. If any of the trial functions are linearly dependent on the solution to the homogeneous equation, they are multiplied by sufficient `x` to make them linearly independent. Examples ======== >>> from sympy import Function, dsolve, pprint, exp, cos >>> from sympy.abc import x >>> f = Function('f') >>> pprint(dsolve(f(x).diff(x, 2) + 2*f(x).diff(x) + f(x) - ... 4*exp(-x)*x**2 + cos(2*x), f(x), ... hint='nth_linear_constant_coeff_undetermined_coefficients')) / / 3\\ | | x || -x 4*sin(2*x) 3*cos(2*x) f(x) = |C1 + x*|C2 + --||*e - ---------- + ---------- \ \ 3 // 25 25 References ========== - https://en.wikipedia.org/wiki/Method_of_undetermined_coefficients - M. Tenenbaum & H. Pollard, "Ordinary Differential Equations", Dover 1963, pp. 221 # indirect doctest 3nth_linear_constant_coeff_undetermined_coefficientsFcjj}jj}jj}jjjj |||_d}|rj rttfdj DsUj drFt|j d }tj d||}|dr |d_ d}|S)NFc3fK|](}|dk\s j|j*ywrrrs rLrzJNthLinearConstantCoeffUndeterminedCoefficients._matches..9 rrrrtrialsetT) rrsrPrarQrrprr rAr+)r\rOrPra does_matcheq_homogeneous undetcoeffrs` @rLrz7NthLinearConstantCoeffUndeterminedCoefficients._matches2 s    0 0$$  &&    !!99"dEJ TVVC'S$&&'S$Svvbz!$R$&&*!5=dffRj!TSab f%$.z$:DM!%JrKTrc|jj}|jjj}|jj}|jj}t |j |||\}}|jjt|}tt||D cgc] \} } | | z c} } } t||| } |j j|| |dt|||||j |j} |rt| g|||} | gScc} } w)Nr)rGrK simpliy_flag)rrOrPrQrar=rprrr rrrr>r+rB)r\rrOrrrar?rrrrr$r%rs rLrzDNthLinearConstantCoeffUndeterminedCoefficients._get_general_solutionB s        ! ! & &      &&?!eT|$$;;E ;K #i2GHA!HI1/   u[-XY/AaD%W &vqt\BDv Is<E NrrLrJrKrLr'r's%7p ADL >BrKr'c,eZdZdZdZdZdZddd dZy) NthLinearEulerEqHomogeneousa Solves an `n`\th order linear homogeneous variable-coefficient Cauchy-Euler equidimensional ordinary differential equation. This is an equation with form `0 = a_0 f(x) + a_1 x f'(x) + a_2 x^2 f''(x) \cdots`. These equations can be solved in a general manner, by substituting solutions of the form `f(x) = x^r`, and deriving a characteristic equation for `r`. When there are repeated roots, we include extra terms of the form `C_{r k} \ln^k(x) x^r`, where `C_{r k}` is an arbitrary integration constant, `r` is a root of the characteristic equation, and `k` ranges over the multiplicity of `r`. In the cases where the roots are complex, solutions of the form `C_1 x^a \sin(b \log(x)) + C_2 x^a \cos(b \log(x))` are returned, based on expansions with Euler's formula. The general solution is the sum of the terms found. If SymPy cannot find exact roots to the characteristic equation, a :py:obj:`~.ComplexRootOf` instance will be returned instead. >>> from sympy import Function, dsolve >>> from sympy.abc import x >>> f = Function('f') >>> dsolve(4*x**2*f(x).diff(x, 2) + f(x), f(x), ... hint='nth_linear_euler_eq_homogeneous') ... # doctest: +NORMALIZE_WHITESPACE Eq(f(x), sqrt(x)*(C1 + C2*log(x))) Note that because this method does not involve integration, there is no ``nth_linear_euler_eq_homogeneous_Integral`` hint. The following is for internal use: - ``returns = 'sol'`` returns the solution to the ODE. - ``returns = 'list'`` returns a list of linearly independent solutions, corresponding to the fundamental solution set, for use with non homogeneous solution methods like variation of parameters and undetermined coefficients. Note that, though the solutions should be linearly independent, this function does not explicitly check that. You can do ``assert simplify(wronskian(sollist)) != 0`` to check for linear independence. Also, ``assert len(sollist) == order`` will need to pass. - ``returns = 'both'``, return a dictionary ``{'sol': , 'list': }``. Examples ======== >>> from sympy import Function, dsolve, pprint >>> from sympy.abc import x >>> f = Function('f') >>> eq = f(x).diff(x, 2)*x**2 - 4*f(x).diff(x)*x + 6*f(x) >>> pprint(dsolve(eq, f(x), ... hint='nth_linear_euler_eq_homogeneous')) 2 f(x) = x *(C1 + C2*x) References ========== - https://en.wikipedia.org/wiki/Cauchy%E2%80%93Euler_equation - C. Bender & S. Orszag, "Advanced Mathematical Methods for Scientists and Engineers", Springer 1999, pp. 12 # indirect doctest nth_linear_euler_eq_homogeneousFc jj}jjjjj}jj jj | |}d_d}|r+|r)||} |z|z }|Dcic] }||||z c}_j r1t fdj Drj dsd}|Scc}w)NFc3hK|])}|dk\r"tj||+ywrr@rprrrr\rs rLrz7NthLinearEulerEqHomogeneous._matches.. 4,Q$%F%TVVAY!a8,/2rTrrdrPrarQrrpr r\rOrarlr,rkr*rrrs ` @@rLrz$NthLinearEulerEqHomogeneous._matches s    - -    ! ! & &  &&      88QqT5I U%LEX%F278Qaa(8DF 66c,&&,,66":!  9'C>Trc|jj}|jj}t|||jd}|gS)Nr)rrPrOr<rp)r\rrrOr%s rLrz1NthLinearEulerEqHomogeneous._get_general_solution sC    " "    7BGJ }rKNrrLrJrKrLr2r2S s%AD -DL&>BrKr2c,eZdZdZdZdZdZddddZy) 3NthLinearEulerEqNonhomogeneousVariationOfParametersae Solves an `n`\th order linear non homogeneous Cauchy-Euler equidimensional ordinary differential equation using variation of parameters. This is an equation with form `g(x) = a_0 f(x) + a_1 x f'(x) + a_2 x^2 f''(x) \cdots`. This method works by assuming that the particular solution takes the form .. math:: \sum_{x=1}^{n} c_i(x) y_i(x) {a_n} {x^n} \text{, } where `y_i` is the `i`\th solution to the homogeneous equation. The solution is then solved using Wronskian's and Cramer's Rule. The particular solution is given by multiplying eq given below with `a_n x^{n}` .. math:: \sum_{x=1}^n \left( \int \frac{W_i(x)}{W(x)} \, dx \right) y_i(x) \text{, } where `W(x)` is the Wronskian of the fundamental system (the system of `n` linearly independent solutions to the homogeneous equation), and `W_i(x)` is the Wronskian of the fundamental system with the `i`\th column replaced with `[0, 0, \cdots, 0, \frac{x^{- n}}{a_n} g{\left(x \right)}]`. This method is general enough to solve any `n`\th order inhomogeneous linear differential equation, but sometimes SymPy cannot simplify the Wronskian well enough to integrate it. If this method hangs, try using the ``nth_linear_constant_coeff_variation_of_parameters_Integral`` hint and simplifying the integrals manually. Also, prefer using ``nth_linear_constant_coeff_undetermined_coefficients`` when it applies, because it does not use integration, making it faster and more reliable. Warning, using simplify=False with 'nth_linear_constant_coeff_variation_of_parameters' in :py:meth:`~sympy.solvers.ode.dsolve` may cause it to hang, because it will not attempt to simplify the Wronskian before integrating. It is recommended that you only use simplify=False with 'nth_linear_constant_coeff_variation_of_parameters_Integral' for this method, especially if the solution to the homogeneous equation has trigonometric functions in it. Examples ======== >>> from sympy import Function, dsolve, Derivative >>> from sympy.abc import x >>> f = Function('f') >>> eq = x**2*Derivative(f(x), x, x) - 2*x*Derivative(f(x), x) + 2*f(x) - x**4 >>> dsolve(eq, f(x), ... hint='nth_linear_euler_eq_nonhomogeneous_variation_of_parameters').expand() Eq(f(x), C1*x + C2*x**2 + x**4/6) :nth_linear_euler_eq_nonhomogeneous_variation_of_parametersTc jj}jjjjj}jj jj | |}d_d}|r+|r)||} |z|z }|Dcic] }||||z c}_j r1t fdj Drj drd}|Scc}w)NFc3hK|])}|dk\r"tj||+ywrr6r7s rLrzONthLinearEulerEqNonhomogeneousVariationOfParameters._matches.. r8r9rTr:r;s ` @@rLrzr1Q4P UVVBZu -r ,R1uk5RVRXRXZgh1Q4CGGkoo,Etvve}+TTUVVrKNrrLrJrKrLr?r? s&4j HDL(>B WrKr?c,eZdZdZdZdZdZddd dZy) 6NthLinearEulerEqNonhomogeneousUndeterminedCoefficientsa/ Solves an `n`\th order linear non homogeneous Cauchy-Euler equidimensional ordinary differential equation using undetermined coefficients. This is an equation with form `g(x) = a_0 f(x) + a_1 x f'(x) + a_2 x^2 f''(x) \cdots`. These equations can be solved in a general manner, by substituting solutions of the form `x = exp(t)`, and deriving a characteristic equation of form `g(exp(t)) = b_0 f(t) + b_1 f'(t) + b_2 f''(t) \cdots` which can be then solved by nth_linear_constant_coeff_undetermined_coefficients if g(exp(t)) has finite number of linearly independent derivatives. Functions that fit this requirement are finite sums functions of the form `a x^i e^{b x} \sin(c x + d)` or `a x^i e^{b x} \cos(c x + d)`, where `i` is a non-negative integer and `a`, `b`, `c`, and `d` are constants. For example any polynomial in `x`, functions like `x^2 e^{2 x}`, `x \sin(x)`, and `e^x \cos(x)` can all be used. Products of `\sin`'s and `\cos`'s have a finite number of derivatives, because they can be expanded into `\sin(a x)` and `\cos(b x)` terms. However, SymPy currently cannot do that expansion, so you will need to manually rewrite the expression in terms of the above to use this method. So, for example, you will need to manually convert `\sin^2(x)` into `(1 + \cos(2 x))/2` to properly apply the method of undetermined coefficients on it. After replacement of x by exp(t), this method works by creating a trial function from the expression and all of its linear independent derivatives and substituting them into the original ODE. The coefficients for each term will be a system of linear equations, which are be solved for and substituted, giving the solution. If any of the trial functions are linearly dependent on the solution to the homogeneous equation, they are multiplied by sufficient `x` to make them linearly independent. Examples ======== >>> from sympy import dsolve, Function, Derivative, log >>> from sympy.abc import x >>> f = Function('f') >>> eq = x**2*Derivative(f(x), x, x) - 2*x*Derivative(f(x), x) + 2*f(x) - log(x) >>> dsolve(eq, f(x), ... hint='nth_linear_euler_eq_nonhomogeneous_undetermined_coefficients').expand() Eq(f(x), C1*x + C2*x**2 + log(x)/2 + 3/4) .J r8r9rrT) rrsrPrarQrrprr0rrrA) r\rOrarlr,rkr*rrrer.rrs ` @@rLrz?NthLinearEulerEqNonhomogeneousUndeterminedCoefficients._matches= s-    0 0    ! ! & &  &&      88QqT5I U%LEX%F278Qaa(8DF 66c,&&,,vvbztvvbzq#a&9:2=affRj!L f%!%J9s'ETrc @|jjj}|jj}tjtjt d}}}|j jD]@}|dk\s ||j |t||z||z|| zzjz }Btdtt||dzD]-}||j||zt||||zz }/|j|dr ||j|d||zz }t|j dj!|t#|\}} ||j!| z }t%t'|||||_|j(j+|d} | j!|t-|} | j!|t-|||j} | gS)Nrrrrr)rrPrQr rrrpkeysrrr|r+r(rk as_coeff_addr0rrr'rNconst_undet_instancerr ) r\rrrchareqrOsymbolrrrIrKs rLrzLNthLinearEulerEqNonhomogeneousUndeterminedCoefficients._get_general_solutionS s    ! ! & &    VVQVVU3ZF PAAv466!9T!V)Q%::1vg:EMMOO Pq&ff!56q89 ;A &,,vqy)$qtQ*:: :B ;   v &q ) &%%f-a015 5Btvvbzq#a&122 affRj$RScdfhijkhlnoSp$q!''<< <VWXYhhq#a&!hhqQy!A$'..0u rKNrrLrJrKrLrErE s%,Z JDL,>BrKrEc,eZdZdZdZdZdZddd dZy) SecondLinearBessela Gives solution of the Bessel differential equation .. math :: x^2 \frac{d^2y}{dx^2} + x \frac{dy}{dx} y(x) + (x^2-n^2) y(x) if `n` is integer then the solution is of the form ``Eq(f(x), C0 besselj(n,x) + C1 bessely(n,x))`` as both the solutions are linearly independent else if `n` is a fraction then the solution is of the form ``Eq(f(x), C0 besselj(n,x) + C1 besselj(-n,x))`` which can also transform into ``Eq(f(x), C0 besselj(n,x) + C1 bessely(n,x))``. Examples ======== >>> from sympy.abc import x >>> from sympy import Symbol >>> v = Symbol('v', positive=True) >>> from sympy import dsolve, Function >>> f = Function('f') >>> y = f(x) >>> genform = x**2*y.diff(x, 2) + x*y.diff(x) + (x**2 - v**2)*y >>> dsolve(genform) Eq(f(x), C1*besselj(v, x) + C2*bessely(v, x)) References ========== https://math24.net/bessel-differential-equation.html 2nd_linear_besselFc |jj}|jj}|jj}|jj}|j |}t d||g}t d|||g}t d|||g}t d|||g} t d|||g} t d|||g} t d|||j |d g} t d |||j |d g} t d |||j |d g}| |j |d z| |zz||zz}t||j |d ||gj||d k(rbr`tfd DsL|j\}}t|}t||j |d ||gj|r| d k7rt| j|||z | zz}|r|| d k(ry||}ny|rlt| j|||z| <t| j|||z| <t|j|||z|<t| |||d|| zzzz | <t| |||d|| zzzz | <t||||d|| zzzz |<t| j||z}|yt|j||z }|yt||j| d z|d |zzz}|y||d k(rd || <n&t||j| d z| || <|| || ||d|_|||j d<||j d<yy)Nrfrgr-a4b4c4d4a3rb3c3c3DK|]}|jywrW) is_polynomial)rrrps rLrz.SecondLinearBessel._matches.. s;#qv++-;s rFrm)rrTrWT)rrsrPrarQrrr,rlrrrr*r-rr)rn)r\rOrrarrrfr-rTrUrVrWrXrYrZdeqrrQrkpointcoeff1_coeff2coeff2rps @rLrzSecondLinearBessel._matches s    0 0    ! !  &&     VVAY qf % q!Bi ( $1R ) $1R ) $1R ) $1R ) $Bq! 5 6 $Bq! 5 6 $Bq! 5 6!&&A,"R%'"Q$. B VVAq\2q ! ##(5: A:!;;;((*1AYBVVAq\2q)++05: 2!1R5M''AaC"9 5E9> a 2Aqw!78" 2Aqw!78" 2Aqw!78"1R5%)Q"U2Y,,?"?@AAbE1R5%)Q"U2Y,,?"?@AAbE1R5%)Q"U2Y,,?"?@AAbEAbE]((Q0F~QrUm))!a%0GGAJ'--b!eQ!B$K.?@F~qzQr #GAJ/55b!eB =rKrQc,eZdZdZdZdZdZddd dZy) SecondLinearAirya Gives solution of the Airy differential equation .. math :: \frac{d^2y}{dx^2} + (a + b x) y(x) = 0 in terms of Airy special functions airyai and airybi. Examples ======== >>> from sympy import dsolve, Function >>> from sympy.abc import x >>> f = Function("f") >>> eq = f(x).diff(x, 2) - x*f(x) >>> dsolve(eq) Eq(f(x), C1*airyai(x) + C2*airybi(x)) 2nd_linear_airyFcv|jj}|jj}|jj}|jj}|j |}t d|||g}t d|||g}|jj|||}d} |dk(r|r|ddk7r|djrrt|d|dz j|||zz|_ |jr8|j|dk7r&|j||j|d|_ d } | S) NrTrgrUFrrr)r-r T) rrsrPrarQrrrr r)rlr]) r\rOrrarrrTrUrlr,s rLrzSecondLinearAiry._matches s    0 0    ! !  &&     VVAY $1R ) $1R )  88QF A:%E!HMQx q%(!2399"RT'B77twwr{a/#'772;4772;?DG!%JrKTrcF|jjj}|jj}|jjd\}}|jd}|jd}|j r"| t |dzz t ||zz }nS|jr$| t | dzz t | |zz}n#| t | dzz t | |zz}t|||t|z|t|zzgS)Nrrr-r ) rrPrQrr] is_positiver$ is_negativerr%r&) r\rrrrror-r rrs rLrz&SecondLinearAiry._get_general_solution s    ! ! & &    ##::q:AR GGCL GGCL ==#d1gqj.4719,C ]]#dA2hk/D!HQJ.C#dA2hk/D!HQJ.C1Q4F3K"VC[.89::rKNrrLrJrKrLrere s#" DL$>B ;rKrec2eZdZdZdZdZdZdZddd dZy ) LieGroupa This hint implements the Lie group method of solving first order differential equations. The aim is to convert the given differential equation from the given coordinate system into another coordinate system where it becomes invariant under the one-parameter Lie group of translations. The converted ODE can be easily solved by quadrature. It makes use of the :py:meth:`sympy.solvers.ode.infinitesimals` function which returns the infinitesimals of the transformation. The coordinates `r` and `s` can be found by solving the following Partial Differential Equations. .. math :: \xi\frac{\partial r}{\partial x} + \eta\frac{\partial r}{\partial y} = 0 .. math :: \xi\frac{\partial s}{\partial x} + \eta\frac{\partial s}{\partial y} = 1 The differential equation becomes separable in the new coordinate system .. math :: \frac{ds}{dr} = \frac{\frac{\partial s}{\partial x} + h(x, y)\frac{\partial s}{\partial y}}{ \frac{\partial r}{\partial x} + h(x, y)\frac{\partial r}{\partial y}} After finding the solution by integration, it is then converted back to the original coordinate system by substituting `r` and `s` in terms of `x` and `y` again. Examples ======== >>> from sympy import Function, dsolve, exp, pprint >>> from sympy.abc import x >>> f = Function('f') >>> pprint(dsolve(f(x).diff(x) + 2*x*f(x) - x*exp(-x**2), f(x), ... hint='lie_group')) / 2\ 2 | x | -x f(x) = |C1 + --|*e \ 2 / References ========== - Solving differential equations by Symmetry Groups, John Starrett, pp. 1 - pp. 14 lie_groupFcfd|jjvxrd|jjvS)Nxieta)rr[r`s rL_has_additional_paramszLieGroup._has_additional_paramsN s/t''...S5DBrKrlr6r r3r(rFr|rrrOrYrrrr"rr) r r@rsrfrRrrrgrrm)rFrrrN) __future__rtypingrrriccatirr sympy.corer r r r sympy.core.cacher sympy.core.exprtoolsrsympy.core.exprrsympy.core.functionrrrrrrrsympy.core.numbersrsympy.core.relationalrrsympy.core.symbolrrrsympy.core.mulrsympy.functionsrrr r!r"r#r$r%r&sympy.integralsr' sympy.polysr(sympy.polys.polytoolsr)r*r+sympy.simplifyr,r-r.r/r0sympy.simplify.radsimpr1sympy.utilitiesr2sympy.solvers.solversr3sympy.solvers.deutilsr4r5sympy.polys.matrices.linsolver6sympy.polys.solversr7hypergeometricr8r9r:r;nonhomogeneousr<r=r>r?r@rArBrmrCrrErNrrrrrr!r0r5rNrXrfrsr{rrrrrrrr rr'r2r?rErQrerl solver_mapoderFrrrrJrKrLrsa #%1,,,- ```".11WWW$88NN+,'86299& ' ``RP'P'f,'_,'^c?cL~'~BG(GTS)Slh&hVFFR=+=@C=,C=L:2)?:2z]&]@[&[|H1yH1Vy&<yxz&<zz<c:<[<c~sQ-sQlOOdS?Sl[[|c/cLZ_Zz]/]@VW/VWr\_\~u=u=p4;4;neeP*+-N&'B:;i  CDz   +nO!"6/0O/0O\ -!")#$:eBu'+.,*L!7 >HGrK