K i+ddlmZmZmZddlmZddlmZddlm Z ddl m Z m Z ddl mZmZddlmZmZddlmZmZmZmZmZmZdd lmZmZmZmZmZm Z m!Z!m"Z"m#Z#dd l$m%Z%dd l&m'Z'm(Z(m)Z)m*Z*m+Z+dd l,m-Z-m.Z.dd l/m0Z0m1Z1m2Z2ddl3m4Z4m5Z5ddl6m7Z7ddl8m9Z9ddl:m;Z;ddlm?Z?m@Z@ddlAmBZBmCZCmDZDddlEmFZFddlGmHZHmIZIdZJGddeKZLGdde?ZMdZNdZOdCdZPdDd ZQdDd!ZRd"ZSd#ZTd$ZU dEd%ZVd&ZWd'ZXd(ZYd)ZZd*Z[d+Z\dDd,Z]d-Z^d.Z_d/Z`dFd0Zad1Zbd2Zcd3Zdd4Zed5Zfd6Zgd7Zhd8fd9Zid:Zjd;Zkd<Zld=Zmd>Zn dGd?ZodHd@ZpdIdAZqdJdBZry)K)AddMulS)Tuple) factor_terms)I)EqEquality)default_sort_keyordered)DummySymbol) expand_mulexpand Derivative AppliedUndefFunctionSubs) expimcossinre Piecewisepiecewise_foldsqrtlog factorial)zerosMatrixNonSquareMatrixError MatrixBaseeye)Polytogether)collectradsimpsignsimp) powdenestpowsimp)ratsimp)simplify) FiniteSet ode_order)NonlinearErrorsolveset)connected_componentsiterablestrongly_connected_components) filldedent)Integral integratecV|Dcic]tfd|Dc}Scc}w)Nc36K|]}t|ywNr/).0eqfuncs _/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/solvers/ode/systems.py z"_get_func_order..s8biD)8)max)eqsfuncsr>s `r?_get_func_orderrEs'EJ KTD#8C88 8 KK Ks&ceZdZdZy) ODEOrderErrorz@Raised by linear_ode_to_matrix if the system has the wrong orderN__name__ __module__ __qualname____doc__r?rGrG sJrNrGceZdZdZy)ODENonlinearErrorz9Raised by linear_ode_to_matrix if the system is nonlinearNrHrMrNr?rPrP%sCrNrPc |j}|j}t|}t|j t }t |g|ddi}|Dcgc] }t|}}|s |j}tdtdg g}t|j|jD]\}}t|}t|trt fd|j D}nt#|j% }t'dt||D}|j)||zt+|t-|Scc}w)NrFC1C2c3\K|]#\}}t|j|f%ywr;)r,r')r<coefcondsymss r?r@z_simpsol..8s)a d!6!6t!.;s:Aa1f:)lhsrhsr+listatomsrr%rgensrzipcoeffsmonomsr isinstancerargsr,r'rappendr r) soleqr\solr`prYtermscoeffmonomrWs @r?_simpsolrm*s" ))C ))C #,C  # D S&4&&A%) *LO *D * vv 4L&, 'D EAHHJ 3$ uu% eY 'aV[V`V`abEEN**40E:T5)9:; UU]#$ c3;  +sEctt|j|\}}t|}|j t d}||zS)Nc*tt|Sr;)rr)as r?z_solsimp..Ds\!_)=rN)r+ras_independentr,replacer)etno_thas_ts r?_solsimprx@sC*Q-(77:KD% 4=D MM#= >E %<rNc F fd}d fd d d |rFtjd|D}|Dcic]}|t|j}}ni}|Dcgc]+}t |j ||j |||-}}|Scc}wcc}w)z&Simplify solutions from dsolve_system.cn|r|j|}t|}|||}t|}|S)z#Simplify the rhs of an ODE solution)subsrr))r]repwrt1wrt2simp_coeff_deps r?simprhszsimpsol..simprhsps9 ((3-C3S$-sm rNc fd fd d fd}tj||}i}|D]x}|jddi\}}|}|tjur/t |} | j s|| z}tj}||vr|||<l||xx|z cc<zfd|jD}  fd| D} td | DS) zGSplit rhs into terms, split terms into dep and coeff and collect on depc>|jxr|jSr;)is_Addhas)rtr}s r?rqz1simpsol..simp_coeff_dep..{s!((";uquud|rNc^|jxrtt|jSr;)is_Mulanymapre)rt add_dep_termss r?rqz1simpsol..simp_coeff_dep..|s qxxKCM1660J,KrNct|dS)NF)deep)r)rts r?rqz1simpsol..simp_coeff_dep..}s 15 9rNc(|jSr;)rs)rt expand_func expandables r?rqz1simpsol..simp_coeff_dep..~s199Z#ErNas_AddFc3:K|]\}}||fywr;rM)r<dc simpcoeffr~s r?r@z2simpsol..simp_coeff_dep..s!DAi4(!,Dc3:K|]\}}||fywr;rM)r<rrrr~s r?r@z2simpsol..simp_coeff_dep..s!L$!Q.D115Lrc3,K|] \}}||zywr;rM)r<rrs r?r@z2simpsol..simp_coeff_dep..s1tq!QU1r[)r make_argsrrrOnerritems)exprr}r~expand_mul_modrjdctermrkdepdep2 termpairsrrrrrsimpdeps `` @@@r?rzsimpsol..simp_coeff_depys; K 9 E nT23  !D,,,dA5AJE3#t$C!%%#C(txxTME%%C"}335 ! !&ED  L)LI1y122rNc fd}t|}|jtDcic]!}|t||jd#}}|j |}|Scc}w)z.simpdep..canonicalises? QA'')HCS/C#t$C9 rNr)r+r_rrer{)rr}rrtr|s ` r?rzsimpsol..simpdeps] t}8< 3H1q#l166!9-..HHyy~ Is&A$c @t|}|jrtt|}|7t |t t |j t|z z}nt t |j }t||}t|}|S)z2Bring to a common fraction and cancel with ratsimp) r& is_polynomialr,r(r^r free_symbolssetr')rkr~rWs r?rzsimpsol..simpcoeffs    GEN+E  :WU-?-?#d)-K%L MMD 2 234Dt$ rNc3FK|]}|jtywr;)r_r7)r<ss r?r@zsimpsol..s!A!''("3!As!r;)runionrdoitr r\r]) rhr}r~rr integralsrZr|rrrrs @@@r?simpsolrIsN3@&* CEKK!AS!AB 2;>> from sympy import symbols, Matrix >>> from sympy.solvers.ode.systems import linodesolve_type >>> t = symbols("t") >>> A = Matrix([[1, 1], [2, 3]]) >>> b = Matrix([t, 1]) >>> linodesolve_type(A, t) {'antiderivative': None, 'type_of_equation': 'type1'} >>> linodesolve_type(A, t, b=b) {'antiderivative': None, 'type_of_equation': 'type2'} >>> A_t = Matrix([[1, t], [-t, 1]]) >>> linodesolve_type(A_t, t) {'antiderivative': Matrix([ [ t, t**2/2], [-t**2/2, t]]), 'type_of_equation': 'type3'} >>> linodesolve_type(A_t, t, b=b) {'antiderivative': Matrix([ [ t, t**2/2], [-t**2/2, t]]), 'type_of_equation': 'type4'} >>> A_non_commutative = Matrix([[1, t], [t, -1]]) >>> linodesolve_type(A_non_commutative, t) Traceback (most recent call last): ... NotImplementedError: The system does not have a commutative antiderivative, it cannot be solved by linodesolve. Returns ======= Dict Raises ====== NotImplementedError When the coefficient matrix does not have a commutative antiderivative See Also ======== linodesolve: Function for which linodesolve_type gets the information Ntype{}z{}{})type_of_equationantiderivativez The system does not have a commutative antiderivative, it cannot be solved by linodesolve. rb) _matrix_is_constantis_zero_matrixformatintupdate_is_commutative_anti_derivativeNotImplementedErrorr6_first_order_type5_6_subs) Arurmatchis_non_constantis_non_homogeneoustypeB is_commutings r?linodesolve_typerst E-a33O4i;1+;+;< ??3v}}S-A3GYCZ[]^_bcc dD A LLda@A9!Q?<%j2'  #$ .q!q9: LrNci}t||}|duxs |j}|tdj|}t |d|}t t |||}t|tr||jtsgt|dk(rY|d}|s&||dz }|j|t|d}djd| z} |j|||| |d|S)N{}_rrr) func_coefftaut_rr])_factor_matrixrrrr8r2r rdr.rrlenr{r^r) rrurrris_homogeneousrF_tinversers r?rr;s E!!Q'L$Y2!"2"2N ELLO $ Q+2b#;* gy )'++i2HG !QA! Q'FF1d7mA./??1N(:#;>> from sympy import Function, Symbol, Matrix, Eq >>> from sympy.solvers.ode.systems import linear_ode_to_matrix >>> t = Symbol('t') >>> x = Function('x') >>> y = Function('y') We can create a system of linear ODEs like >>> eqs = [ ... Eq(x(t).diff(t), x(t) + y(t) + 1), ... Eq(y(t).diff(t), x(t) - y(t)), ... ] >>> funcs = [x(t), y(t)] >>> order = 1 # 1st order system Now ``linear_ode_to_matrix`` can represent this as a matrix differential equation. >>> (A1, A0), b = linear_ode_to_matrix(eqs, funcs, t, order) >>> A1 Matrix([ [1, 0], [0, 1]]) >>> A0 Matrix([ [1, 1], [1, -1]]) >>> b Matrix([ [1], [0]]) The original equations can be recovered from these matrices: >>> eqs_mat = Matrix([eq.lhs - eq.rhs for eq in eqs]) >>> X = Matrix(funcs) >>> A1 * X.diff(t) - A0 * X - b == eqs_mat True If the system of equations has a maximum order greater than the order of the system specified, a ODEOrderError exception is raised. >>> eqs = [Eq(x(t).diff(t, 2), x(t).diff(t) + x(t)), Eq(y(t).diff(t), y(t) + x(t))] >>> linear_ode_to_matrix(eqs, funcs, t, 1) Traceback (most recent call last): ... ODEOrderError: Cannot represent system in 1-order form If the system of equations is nonlinear, then ODENonlinearError is raised. >>> eqs = [Eq(x(t).diff(t), x(t) + y(t)), Eq(y(t).diff(t), y(t)**2 + x(t))] >>> linear_ode_to_matrix(eqs, funcs, t, 1) Traceback (most recent call last): ... ODENonlinearError: The system of ODEs is nonlinear. Parameters ========== eqs : list of SymPy expressions or equalities The equations as expressions (assumed equal to zero). funcs : list of applied functions The dependent variables of the system of ODEs. t : symbol The independent variable. order : int The order of the system of ODEs. Returns ======= The tuple ``(As, b)`` where ``As`` is a tuple of matrices and ``b`` is the the matrix representing the rhs of the matrix equation. Raises ====== ODEOrderError When the system of ODEs have an order greater than what was specified ODENonlinearError When the system of ODEs is nonlinear See Also ======== linear_eq_to_matrix: for systems of linear algebraic equations. References ========== .. [1] https://en.wikipedia.org/wiki/Matrix_differential_equation r)linear_eq_to_matrixc3JK|]}D]}t||kDywr;r/)r<r=r>rDorders r?r@z'linear_ode_to_matrix..s* G2 G9R  & G & Gs #z(Cannot represent system in {}-order formz The system of ODEs is nonlinear.) sympy.solvers.solvesetrrrGrrangediffr1rP applyfuncrrf)rCrDrurrmsgAsor>rWAirr=r]s ` ` r?linear_ode_to_matrixrVsl; G GG8CJJu-.. B 5"b !,12D !Q22 H'T2EB\\* % U ", !"#2B3#C#C%( s7N%3  H#$FG G H$sC, C, CCcLt||\}}||z|jzS)a Matrix exponential $\exp(A*t)$ for the matrix ``A`` and scalar ``t``. Explanation =========== This functions returns the $\exp(A*t)$ by doing a simple matrix multiplication: .. math:: \exp(A*t) = P * expJ * P^{-1} where $expJ$ is $\exp(J*t)$. $J$ is the Jordan normal form of $A$ and $P$ is matrix such that: .. math:: A = P * J * P^{-1} The matrix exponential $\exp(A*t)$ appears in the solution of linear differential equations. For example if $x$ is a vector and $A$ is a matrix then the initial value problem .. math:: \frac{dx(t)}{dt} = A \times x(t), x(0) = x0 has the unique solution .. math:: x(t) = \exp(A t) x0 Examples ======== >>> from sympy import Symbol, Matrix, pprint >>> from sympy.solvers.ode.systems import matrix_exp >>> t = Symbol('t') We will consider a 2x2 matrix for comupting the exponential >>> A = Matrix([[2, -5], [2, -4]]) >>> pprint(A) [2 -5] [ ] [2 -4] Now, exp(A*t) is given as follows: >>> pprint(matrix_exp(A, t)) [ -t -t -t ] [3*e *sin(t) + e *cos(t) -5*e *sin(t) ] [ ] [ -t -t -t ] [ 2*e *sin(t) - 3*e *sin(t) + e *cos(t)] Parameters ========== A : Matrix The matrix $A$ in the expression $\exp(A*t)$ t : Symbol The independent variable See Also ======== matrix_exp_jordan_form: For exponential of Jordan normal form References ========== .. [1] https://en.wikipedia.org/wiki/Jordan_normal_form .. [2] https://en.wikipedia.org/wiki/Matrix_exponential )matrix_exp_jordan_forminv)rruPexpJs r? matrix_exprs*N%Q*GAt t8aeeg rNc |j\}}||k7rtd|d|d|jr tdd}||}t|j t }|jt  }g}gt} |D]\} } | D]} t| } |r-| | jk7r| j|vr| | vr>| j| jtt| z}t| z}tt|t!|gt!| t|ggt| | fdtd| zd| zfd }|j#||zt%| D]<}j#t| |j#t| |>?j'| fd }t| | |}|j#t| z|ztj(|}t||fd }||fS) a Matrix exponential $\exp(A*t)$ for the matrix *A* and scalar *t*. Explanation =========== Returns the Jordan form of the $\exp(A*t)$ along with the matrix $P$ such that: .. math:: \exp(A*t) = P * expJ * P^{-1} Examples ======== >>> from sympy import Matrix, Symbol >>> from sympy.solvers.ode.systems import matrix_exp, matrix_exp_jordan_form >>> t = Symbol('t') We will consider a 2x2 defective matrix. This shows that our method works even for defective matrices. >>> A = Matrix([[1, 1], [0, 1]]) It can be observed that this function gives us the Jordan normal form and the required invertible matrix P. >>> P, expJ = matrix_exp_jordan_form(A, t) Here, it is shown that P and expJ returned by this function is correct as they satisfy the formula: P * expJ * P_inverse = exp(A*t). >>> P * expJ * P.inv() == matrix_exp(A, t) True Parameters ========== A : Matrix The matrix $A$ in the expression $\exp(A*t)$ t : Symbol The independent variable References ========== .. [1] https://en.wikipedia.org/wiki/Defective_matrix .. [2] https://en.wikipedia.org/wiki/Jordan_matrix .. [3] https://en.wikipedia.org/wiki/Jordan_normal_form z$Needed square matrix but got shape (z, )zMatrix A should not depend on tc|j\}}t|jdDcgc] }|dd|f }}d}i}|D]>}|d}|jd} ||vrg||<||j|||| z|| z }@|Scc}w)aChains from Jordan normal form analogous to M.eigenvects(). Returns a dict with eignevalues as keys like: {e1: [[v111,v112,...], [v121, v122,...]], e2:...} where vijk is the kth vector in the jth chain for eigenvalue i. rNrrr) jordan_cellsrshaperf) rrblocksrZbasisnchainsreigvalsizes r? jordan_chainsz-matrix_exp_jordan_form..jordan_chainsps NN$ 6!&qwwqz!23A1Q333  AtWF771:DV#!#v 6N ! !%!D&/ 2 IA   4sB keycZ||k\r||z zzt||z z StddSNr)rr )rZjimblockrus r?rqz(matrix_exp_jordan_form..s9?@Av!ac(*Yqs^;"1a[rNrc4|dz|dzf|dz|dzfSrrM)rZr expJblock2s r?rqz(matrix_exp_jordan_form..s-AqDAI9NqQRsSTUVSVw9WrNc@||k\r||z zt||z z SdSNrr)rZrrus r?rqz(matrix_exp_jordan_form..s&Q!V!ac(9QqS>"9rNc||Sr;rM)rZrvectorss r?rqz(matrix_exp_jordan_form..sArN)r ValueErrorrsortedrr rrr conjugateaddrrrr!rrrfrextenddiag)rruNMr eigenchainseigenchains_iterisrealrseen_conjugatertrchainrexprtimrt expJblockrZfunrrrrrs ` @@@r?rr6sh 77DAqAv!QOPP q:;;& "Kk//17GHq\F FGUN%6 6 6EE A!q{{},+1M&""1;;=1BqEAI!uqy 3t9c$i"8$'I:s4y"9";<#Aq+)* #1Q3!-WX  ei/0q1ANN2eAh<0NN2eAh<01u%J"1a-  c!a%j945/ 664 ;; Dq!./A d7NrNc  t|tsttd|jst td|Qt|tsttd|j |j k7rttd|qt|tsttd|jst td|j |j k7rttdtfd td d Dsd k(sttd |j }tt|Dcgc] }tc}} | tfddDr t|d }|du} } d k(rt||} | d| d}dvrPd} | d k7rI|Gt||} | s#ttdj| d}| d| d}| d}fd} dvrit|\}}t!|}dvr ||| zz}n|j# }||z||j%z|zj'| | zz}na|t)|\}}dk(r|j+| z}n7|j+| j+|zj'| | zz}| r|j#|}|j-t*}dk7r|Dcgc] }t/|}}|Dcgc]}t1|t3|d}}|r|Dcgc]}|j5}}|Scc}wcc}wcc}wcc}w) ai System of n equations linear first-order differential equations Explanation =========== This solver solves the system of ODEs of the following form: .. math:: X'(t) = A(t) X(t) + b(t) Here, $A(t)$ is the coefficient matrix, $X(t)$ is the vector of n independent variables, $b(t)$ is the non-homogeneous term and $X'(t)$ is the derivative of $X(t)$ Depending on the properties of $A(t)$ and $b(t)$, this solver evaluates the solution differently. When $A(t)$ is constant coefficient matrix and $b(t)$ is zero vector i.e. system is homogeneous, the system is "type1". The solution is: .. math:: X(t) = \exp(A t) C Here, $C$ is a vector of constants and $A$ is the constant coefficient matrix. When $A(t)$ is constant coefficient matrix and $b(t)$ is non-zero i.e. system is non-homogeneous, the system is "type2". The solution is: .. math:: X(t) = e^{A t} ( \int e^{- A t} b \,dt + C) When $A(t)$ is coefficient matrix such that its commutative with its antiderivative $B(t)$ and $b(t)$ is a zero vector i.e. system is homogeneous, the system is "type3". The solution is: .. math:: X(t) = \exp(B(t)) C When $A(t)$ is commutative with its antiderivative $B(t)$ and $b(t)$ is non-zero i.e. system is non-homogeneous, the system is "type4". The solution is: .. math:: X(t) = e^{B(t)} ( \int e^{-B(t)} b(t) \,dt + C) When $A(t)$ is a coefficient matrix such that it can be factorized into a scalar and a constant coefficient matrix: .. math:: A(t) = f(t) * A Where $f(t)$ is a scalar expression in the independent variable $t$ and $A$ is a constant matrix, then we can do the following substitutions: .. math:: tau = \int f(t) dt, X(t) = Y(tau), b(t) = b(f^{-1}(tau)) Here, the substitution for the non-homogeneous term is done only when its non-zero. Using these substitutions, our original system becomes: .. math:: Y'(tau) = A * Y(tau) + b(tau)/f(tau) The above system can be easily solved using the solution for "type1" or "type2" depending on the homogeneity of the system. After we get the solution for $Y(tau)$, we substitute the solution for $tau$ as $t$ to get back $X(t)$ .. math:: X(t) = Y(tau) Systems of "type5" and "type6" have a commutative antiderivative but we use this solution because its faster to compute. The final solution is the general solution for all the four equations since a constant coefficient matrix is always commutative with its antidervative. An additional feature of this function is, if someone wants to substitute for value of the independent variable, they can pass the substitution `tau` and the solution will have the independent variable substituted with the passed expression(`tau`). Parameters ========== A : Matrix Coefficient matrix of the system of linear first order ODEs. t : Symbol Independent variable in the system of ODEs. b : Matrix or None Non-homogeneous term in the system of ODEs. If None is passed, a homogeneous system of ODEs is assumed. B : Matrix or None Antiderivative of the coefficient matrix. If the antiderivative is not passed and the solution requires the term, then the solver would compute it internally. type : String Type of the system of ODEs passed. Depending on the type, the solution is evaluated. The type values allowed and the corresponding system it solves are: "type1" for constant coefficient homogeneous "type2" for constant coefficient non-homogeneous, "type3" for non-constant coefficient homogeneous, "type4" for non-constant coefficient non-homogeneous, "type5" and "type6" for non-constant coefficient homogeneous and non-homogeneous systems respectively where the coefficient matrix can be factorized to a constant coefficient matrix. The default value is "auto" which will let the solver decide the correct type of the system passed. doit : Boolean Evaluate the solution if True, default value is False tau: Expression Used to substitute for the value of `t` after we get the solution of the system. Examples ======== To solve the system of ODEs using this function directly, several things must be done in the right order. Wrong inputs to the function will lead to incorrect results. >>> from sympy import symbols, Function, Eq >>> from sympy.solvers.ode.systems import canonical_odes, linear_ode_to_matrix, linodesolve, linodesolve_type >>> from sympy.solvers.ode.subscheck import checkodesol >>> f, g = symbols("f, g", cls=Function) >>> x, a = symbols("x, a") >>> funcs = [f(x), g(x)] >>> eqs = [Eq(f(x).diff(x) - f(x), a*g(x) + 1), Eq(g(x).diff(x) + g(x), a*f(x))] Here, it is important to note that before we derive the coefficient matrix, it is important to get the system of ODEs into the desired form. For that we will use :obj:`sympy.solvers.ode.systems.canonical_odes()`. >>> eqs = canonical_odes(eqs, funcs, x) >>> eqs [[Eq(Derivative(f(x), x), a*g(x) + f(x) + 1), Eq(Derivative(g(x), x), a*f(x) - g(x))]] Now, we will use :obj:`sympy.solvers.ode.systems.linear_ode_to_matrix()` to get the coefficient matrix and the non-homogeneous term if it is there. >>> eqs = eqs[0] >>> (A1, A0), b = linear_ode_to_matrix(eqs, funcs, x, 1) >>> A = A0 We have the coefficient matrices and the non-homogeneous term ready. Now, we can use :obj:`sympy.solvers.ode.systems.linodesolve_type()` to get the information for the system of ODEs to finally pass it to the solver. >>> system_info = linodesolve_type(A, x, b=b) >>> sol_vector = linodesolve(A, x, b=b, B=system_info['antiderivative'], type=system_info['type_of_equation']) Now, we can prove if the solution is correct or not by using :obj:`sympy.solvers.ode.checkodesol()` >>> sol = [Eq(f, s) for f, s in zip(funcs, sol_vector)] >>> checkodesol(eqs, sol) (True, [0, 0]) We can also use the doit method to evaluate the solutions passed by the function. >>> sol_vector_evaluated = linodesolve(A, x, b=b, type="type2", doit=True) Now, we will look at a system of ODEs which is non-constant. >>> eqs = [Eq(f(x).diff(x), f(x) + x*g(x)), Eq(g(x).diff(x), -x*f(x) + g(x))] The system defined above is already in the desired form, so we do not have to convert it. >>> (A1, A0), b = linear_ode_to_matrix(eqs, funcs, x, 1) >>> A = A0 A user can also pass the commutative antiderivative required for type3 and type4 system of ODEs. Passing an incorrect one will lead to incorrect results. If the coefficient matrix is not commutative with its antiderivative, then :obj:`sympy.solvers.ode.systems.linodesolve_type()` raises a NotImplementedError. If it does have a commutative antiderivative, then the function just returns the information about the system. >>> system_info = linodesolve_type(A, x, b=b) Now, we can pass the antiderivative as an argument to get the solution. If the system information is not passed, then the solver will compute the required arguments internally. >>> sol_vector = linodesolve(A, x, b=b) Once again, we can verify the solution obtained. >>> sol = [Eq(f, s) for f, s in zip(funcs, sol_vector)] >>> checkodesol(eqs, sol) (True, [0, 0]) Returns ======= List Raises ====== ValueError This error is raised when the coefficient matrix, non-homogeneous term or the antiderivative, if passed, are not a matrix or do not have correct dimensions NonSquareMatrixError When the coefficient matrix or its antiderivative, if passed is not a square matrix NotImplementedError If the coefficient matrix does not have a commutative antiderivative See Also ======== linear_ode_to_matrix: Coefficient matrix computation function canonical_odes: System of ODEs representation change linodesolve_type: Getting information about systems of ODEs to pass in this solver zT The coefficients of the system of ODEs should be of type Matrix z< The coefficient matrix must be a square Nze The non-homogeneous terms of the system of ODEs should be of type Matrix z The system of ODEs should have the same number of non-homogeneous terms and the number of equations zn The antiderivative of coefficients of the system of ODEs should be of type Matrix zZ The antiderivative of the coefficient matrix must be a square zv The coefficient matrix and its antiderivative should have same dimensions c3FK|]}dj|k(yw)rN)r)r<rZrs r?r@zlinodesolve..s?atxq))?!rautozI The input type should be a valid one c3(K|] }|k( ywr;rM)r<typrs r?r@zlinodesolve..sLL)type2type4type6rrr)type5rTzI The system passed isn't {}. rrrrc$|r t|SdSr)r7)xrus r?rqzlinodesolve..sA(1a.1rN)type1rrr)r!rtype3r!)exact)rdr#rr6 is_squarer"rowsrrr!r r rrrrr-r{rrrrr_rr'r r)rrurrrrrr_Cvectis_transformed passed_type system_info intx_wrttrJ sol_vectorJinvr`rs ` ` r? linodesolver/sd a $%    ;;":/ $   }!Z(Z)  66QVV Z)   }!Z(Z) {{&z3(  66QVV Z)  ?5A;? ?PV%  A U1X.EG. /EySL0KLL !QK_NK v~&q!q1 -. ( ) !! & {71B "$Z1t &&''"%(%$$4I 33%a+1 QK % %a%iJ66!aR=DQ4!%%'>A#5"@"@"Ke"STJ 921a8DAq 7?5JqbXXZ!^$>$>y$IE$QRJ__Q,   C D w-78jm8 8AKLA'!WT]$7LJL (231affh3 3 y/j9L4s"M1M60M;Nc,tfd|DS)z2Checks if the matrix M is independent of t or not.c3PK|]}|jdddk(yw)T)rrrN)rr)r<rUrus r?r@z&_matrix_is_constant..s+Itt""1T"215:Is#&)all)r rus `r?rrs IqI IIrNc Pddlm}t||}||g|Dcgc]}|j|||c}ddi}g}|D]U}|Dcgc]7}t |j|||||j|||9} }|j | W|Scc}wcc}w)a Function that solves for highest order derivatives in a system Explanation =========== This function inputs a system of ODEs and based on the system, the dependent variables and their highest order, returns the system in the following form: .. math:: X'(t) = A(t) X(t) + b(t) Here, $X(t)$ is the vector of dependent variables of lower order, $A(t)$ is the coefficient matrix, $b(t)$ is the non-homogeneous term and $X'(t)$ is the vector of dependent variables in their respective highest order. We use the term canonical form to imply the system of ODEs which is of the above form. If the system passed has a non-linear term with multiple solutions, then a list of systems is returned in its canonical form. Parameters ========== eqs : List List of the ODEs funcs : List List of dependent variables t : Symbol Independent variable Examples ======== >>> from sympy import symbols, Function, Eq, Derivative >>> from sympy.solvers.ode.systems import canonical_odes >>> f, g = symbols("f g", cls=Function) >>> x, y = symbols("x y") >>> funcs = [f(x), g(x)] >>> eqs = [Eq(f(x).diff(x) - 7*f(x), 12*g(x)), Eq(g(x).diff(x) + g(x), 20*f(x))] >>> canonical_eqs = canonical_odes(eqs, funcs, x) >>> canonical_eqs [[Eq(Derivative(f(x), x), 7*f(x) + 12*g(x)), Eq(Derivative(g(x), x), 20*f(x) - g(x))]] >>> system = [Eq(Derivative(f(x), x)**2 - 2*Derivative(f(x), x) + 1, 4), Eq(-y*f(x) + Derivative(g(x), x), 0)] >>> canonical_system = canonical_odes(system, funcs, x) >>> canonical_system [[Eq(Derivative(f(x), x), -1), Eq(Derivative(g(x), x), y*f(x))], [Eq(Derivative(f(x), x), 3), Eq(Derivative(g(x), x), y*f(x))]] Returns ======= List r)solvedictT)sympy.solvers.solversr4rErr rf) rCrDrur4rr> canon_eqssystemsr=systems r?canonical_odesr:st, C 'EcU5I4TYYq%+6IUPTUIG[`aSW"TYYq%+.499Qd 3L0MNaav NJbs B >> from sympy import symbols, Matrix >>> from sympy.solvers.ode.systems import _is_commutative_anti_derivative >>> t = symbols("t") >>> A = Matrix([[1, t], [-t, 1]]) >>> B, is_commuting = _is_commutative_anti_derivative(A, t) >>> is_commuting True Returns ======= Matrix, Boolean F)r8rrrr)rrurrs r?rr9sSV !QAaC!A#I((0::<HWWL(05lL l?rNcd}|D]+}|j|d}|j|s)|}n|,||z jt}t ||}|r||fnd}|S)Nr)rrrrr,r)rrurelement temp_term A_factored can_factors r?rrlsz D**1-a0 == D    f''0 (Q7 %/j!T KrNct||}d}|d|dz }|j}|rt|j|}|j }|dddvrt |d}|\}}} } } t t|d} t t| d} t t| d| z| zz d}|d| z|zk(xr | d|z| zk(}| |dzz||zz| z}||fSd}||fS) NFrr)rrBT)forcer)rrr%rrc_get_poly_coeffsr*r)rruris_type2polyrccsrprrrrta1c1b1s r?_is_second_order_type2rK|s !Q DH ay%%'DKKM1% !9Q<6 !!$*BMAq!Q47$/B47$/B4AbDG ,D9BQrT"W 81"RB&D T>H T>rNct|dzDcgc]}d}}t|j|jD]\}}||d|dz <|Scc}w)Nrrr)rrarbrc)rFrr&rGrms r?rDrDs^57^ $! $B $DKKM4;;=112ad7  I %s Ac ~ddi}|jd}t||rt||r|S|||zzjtjr|j d|d|S|jr| |jrt ||\}}|rtt||d\}}} ||dzjzjt|dzdz || zz t||zz} td|z |} td j|} |j d | t|| d | d |S) a Works only for second order system in its canonical form. Type 0: Constant coefficient matrix, can be simply solved by introducing dummy variables. Type 1: When the substitution: $U = t*X' - X$ works for reducing the second order system to first order system. Type 2: When the system is of the form: $poly * X'' = A*X$ where $poly$ is square of a quadratic polynomial with respect to *t* and $A$ is a constant coefficient matrix. rtype0rr!)rA1rrBrrrT)rA0g(t)rr(r)rrrrrrrKrDr%rr,r$r8rrr) rPrQrurrrrErrprrrrs r?_match_second_order_typerSs:  )E  A2q!&9"a&@  RT Z(77 '<= L   Q-=-=/A6$ &tD!}a8GAq!T1W$$&&11':ad1fqslCPQSTI=UUAAdFA&C Q(B LLgQ"&t*SD "$ % LrNc t|Dcgc]}||j|z|z c}}t||||z}t||Dcgc]\}}t ||} }}t | ||d} | Scc}wcc}}w)a For a linear, second order system of ODEs, a particular substitution. A system of the below form can be reduced to a linear first order system of ODEs: .. math:: X'' = A(t) * (t*X' - X) + b(t) By substituting: .. math:: U = t*X' - X To get the system: .. math:: U' = t*(A(t)*U + b(t)) Where $U$ is the vector of dependent variables, $X$ is the vector of dependent variables in `funcs` and $X'$ is the first order derivative of $X$ with respect to $t$. It may or may not reduce the system into linear first order system of ODEs. Then a check is made to determine if the system passed can be reduced or not, if this substitution works, then the system is reduced and its solved for the new substitution. After we get the solution for $U$: .. math:: U = a(t) We substitute and return the reduced system: .. math:: a(t) = t*X' - X Parameters ========== A: Matrix Coefficient matrix($A(t)*t$) of the second order system of this form. b: Matrix Non-homogeneous term($b(t)$) of the system of ODEs. funcs: List List of dependent variables t: Symbol Independent variable of the system of ODEs. Returns ======= List r)r!rr/rar r:) rrrDrur>Urhru reduced_eqss r?_second_order_subs_type1rXsb %8$$))A,%89A aAaC C(+C 412a84K4 eQ7:K  95s A9A>c h|Dcgc]}|jj}}|Dcgc]+}ttdj ||-}}|t |z}t ||Dcgc]!\}}t|j|d|#} }}| |fScc}wcc}wcc}}w)a Returns a second order system based on the coefficient matrix passed. Explanation =========== This function returns a system of second order ODE of the following form: .. math:: X'' = A * X Here, $X$ is the vector of dependent variables, but a bit modified, $A$ is the coefficient matrix passed. Along with returning the second order system, this function also returns the new dependent variables with the new independent variable `t_` passed. Parameters ========== A: Matrix Coefficient matrix of the system funcs: List List of old dependent variables t_: Symbol New independent variable Returns ======= List, List rr) r>rIrr rr!rar r) rrDrr> func_namesname new_funcsrhssr]new_eqss r?_second_order_subs_type2r_sD277$))$$7J7EOPT4% T 234R8PIP vi D:=i:NOYT3r$))B"C(OGO I  8POsB$0B)7&B.c>tfdt|DS)Nc3nK|],\}}t||zzjt.ywr;)rrr,)r<rZrrus r?r@z#_is_euler_system..)s0]tq!"AadF#5#5g#>B]s25)r2 enumerate)rrus `r?_is_euler_systemrc(s ]yY[}] ]]rNct|t|k7rtd|z|D](}t|jdk7std|zt||t fd|D}|dkD}|dk(xrt fd|D} |s t ||n|g}t|dk(rt|d||\} } nd|d } | S d} | jrdnd } t|||| | dd } | s| | d <t fd| D}|sU| d}|| d<t|}|| d< t|| }| j|| jd}|s|| d<| Sd| d<|r+| dd \}}t||}| j|d| d<| ddk(r|st!| }|r0t#dj%}| jdd|dndd}t'| d}t d| ddDr=|;||ds0|dj)\}}| jd||d|dd| ddk7r|r| jdd || d<| S#t$rYy wxYw#t$rYy wxYw) a Returns a dictionary with details of the eqs if the system passed is linear and can be classified by this function else returns None Explanation =========== This function takes the eqs, converts it into a form Ax = b where x is a vector of terms containing dependent variables and their derivatives till their maximum order. If it is possible to convert eqs into Ax = b, then all the equations in eqs are linear otherwise they are non-linear. To check if the equations are constant coefficient, we need to check if all the terms in A obtained above are constant or not. To check if the equations are homogeneous or not, we need to check if b is a zero matrix or not. Parameters ========== eqs: List List of ODEs funcs: List List of dependent variables t: Symbol Independent variable of the equations in eqs is_canon: Boolean If True, then this function will not try to get the system in canonical form. Default value is False Returns ======= match = { 'no_of_equation': len(eqs), 'eq': eqs, 'func': funcs, 'order': order, 'is_linear': is_linear, 'is_constant': is_constant, 'is_homogeneous': is_homogeneous, } Dict or list of Dicts or None Dict with values for keys: 1. no_of_equation: Number of equations 2. eq: The set of equations 3. func: List of dependent variables 4. order: A dictionary that gives the order of the dependent variable in eqs 5. is_linear: Boolean value indicating if the set of equations are linear or not. 6. is_constant: Boolean value indicating if the set of equations have constant coefficients or not. 7. is_homogeneous: Boolean value indicating if the set of equations are homogeneous or not. 8. commutative_antiderivative: Antiderivative of the coefficient matrix if the coefficient matrix is non-constant and commutative with its antiderivative. This key may or may not exist. 9. is_general: Boolean value indicating if the system of ODEs is solvable using one of the general case solvers or not. 10. rhs: rhs of the non-homogeneous system of ODEs in Matrix form. This key may or may not exist. 11. is_higher_order: True if the system passed has an order greater than 1. This key may or may not exist. 12. is_second_order: True if the system passed is a second order ODE. This key may or may not exist. This Dict is the answer returned if the eqs are linear and constant coefficient. Otherwise, None is returned. zDNumber of functions given is not equal to the number of equations %srzOdsolve() and classify_sysode() work with functions of one variable only, not %sc3(K|] }| ywr;rMr<r>rs r?r@z*_classify_linear_system..s5tuT{5rrc3.K|] }|dk(yw)rNrMrfs r?r@z*_classify_linear_system..s/STd q0@/SsrT) is_implicitr7NF)no_of_equationr=r>r is_linearr is_generalr]c36K|]}t|ywr;)r)r<A_rus r?r@z*_classify_linear_system..s>R)"a0>rAr is_constantrrcommutative_antiderivativerOris_second_orderrr!)r(rrcT|tj|jd|dk(S)Nrr)r! jordan_blockr)r s r?rqz)_classify_linear_system..s$a6+>+>qwwqz1T7+S&SrNrc34K|]}|jywr;)r)r<rs r?r@z*_classify_linear_system..s:Aq'':sr)rr,f(t)rr(is_higher_order)rrrerErBr2r:rrPrrrrrpoprSrcrrr jordan_form)rCrDruis_canonr> system_orderrurpr7rrrrjrrnrr*rrPrQmatch_second_orderis_eulerr is_jordanrjrr,rs ` @r?_classify_linear_systemr},s)\ 5zSX_bgghh= tyy>Q 57;<= == C 'E5u55L"Q&O"a'SC/SU/S,SO 9AN3q1u y>Q (1uaNEB $&E LI--T5N c(( E e >2>>K  qEl*!Q/ *m *1a15K  [!#342@E. / $+ ! VFB!9"b!!D  LL+ ,'+E# $ # $ / 'A.HELLO, '$&()T &r"vq1:Ab::u?PYbchijckYl 8//1DAqLLgA*/(d"TU'(G3 +T2#2  c L#  s$;I+1I:+ I76I7: JJcrg}|D]/}|jt|tr|n t|d1|Sr)rfrdr r )rC processed_eqsr=s r?_preprocess_eqsrs?ML:b(#;RBKL rNci}i}t|}|D]S}|jjt\}|jjt|hz |z}|||<|||<U||fSr;)rr\r_rr])rCrDeqsorigeqsmapfuncsetr=f1f2ss r? _eqs2dictrstG F%jGffll<(vv||L)RD0G;r   7?rNct|}|jDcgc]\}}|D]}||f}}}}||f}|Scc}}}wr;)r^r)rnodesrrf2edgesGs r? _dict2graphrsN GE%&WWY = ='"c =2b"X =X =E = A H >s?c|\}} t|||d\\}}}t||r |jryy#ttf$rYywxYw)NrFT)rrPrGrr)sccrurCrDrPrQrs r? _is_type1r s[JC*3q!< R!2q!a&6&6  } -s5AActt|Dcgc]\}}t||s|}}}t}t|D]J\}||dzdD]:}t fd||Ds|||z||<|j <Lt|Dcgc] \}}||vs |}}}|Scc}}wcc}}w)Nrc3FK|]}|jywr;)r)r<eq2rDrZs r?r@z,_combine_type1_subsystems..s=37758$=r)rbrarrrr) subsystemrDrurZsysindicesremoveiprs ` ` r?_combine_type1_subsystemsrs(Y)>?UVQ9SRSCTqUGU UF7#AA A= ! ==(|il: !  1   $-Y#7KC1F?KIK VLsB=B=' C4Cc Ht||\}}g}tt|D]i}|Dcic]}||| }}tt|} | D cgc]} | Dcgc]}|| c}} } }t | | |} |j | k|Scc}wcc}wcc}} wr;)rr3rr5rrf) rCrDrurr subsystemsccfeqsmap_csccsrrs r?_component_divisionr&s  U+OFGJ";v#67%*,-QAvayL--,[-BC:>?3#.Qgaj.? ?-iqA )$ %  ..?s B B B*BBc.|d}|d}|jdd}|jdd}d|vr|dn|}|d}|jdd}|d}t|||||| }t||D cgc]\} } t| | } } } | Scc} } w) Nrur>r]rrrror)rrrr)getr/rar ) rrurDr]rrrrr-rrrhs r?_linear_ode_solverr9s c A &ME ))E4 C ))E4 Cu}d !A lA  .5A # $DQSA"&C1J!$E: 6 712a8 7C 7 J 8s6Bc|Sr;rM)r s r?rqrqOsrNc |Dcic]}|j|j}}|Dcgc]}t||||c}Scc}wcc}wr;)r\r]r )rCrDrrteq_dictrs r?_select_equationsrOsI%()quuaee|)G),1 2qBq'#a&/ " 22* 2s AA c|d}|d}|d|d}|jdd}|jdd}|jd d}|xr|d k(}|xr |d k(xrd |v}|rWt|||jd d|jdd|jdd||jdd\} } nWt|||||jdd|jdd|jd d|jdd \} } |r|jd|s)t| | D cgc]} | j c} } d} |s t | | } |  t| | } | | S|xr|d k(} |rdndt| |fd} |jd dr| rL|d}|d}| Dcgc]5}t|j|jj||z7} }| S|r4|d|d}| Dcgc]}|j|t} }| S|rS|d }|t| Dcgc]}|jc}z}t||D cgc]\} }t| |} } }| Scc} w#t $rd} Y5wxYw#t $rd} Y7wxYwcc}wcc}wcc}wcc}} w)Nr=r>rurrrOrpFr(r!rrrPrQr]r)rPrQrrrr,rt)rDrr,f_trr__r&c ttdj|jjS)Nz{}{}0)rr rr>rI)r ru underscoress r?rqz*_higher_order_ode_solver..s.*g(5Yd9e3f*ghi*jrNrrRr)r_second_order_to_first_order_higher_order_to_first_orderrr_strong_component_solverr_component_solverr r\r]r{rr!ra)rrCrDsysorderrrpr(r{is_higher_order_type2r^r\rrhis_second_order_type2g_trrrr-rurs @@r?_higher_order_ode_solverrTs +C &ME c AW~H 99' 1Dii 159OYY/7N1$'/H*OtwO3%<9#ua=BYYtT=RW\W`W`aegkWlCiiPT>U?12a8?C? JW.L # C "E5%9?sB5J J ) J=:J1 "J6J; -K JJ J.-J.cddlm}m}t|||d}d}|r||d<|j ddr t |}n|j ddr t |}|tt|d k(rf||d|d }t|dj}tt|d|dDcgc] }t} }|||| }|g}|Scc}w) Nr)dsolveconstant_renumberT)rxruruFrjr)r>) variables newconstants) sympy.solvers.ode.oderrr}rrrrrrrr0r ) rCrDrurrrrhrr& new_constantss r?rrs? #CD AE C  c 99& .*51C YY{E *$U+C ;3s8q=QeAh/Cc!f 22I.3Ic!feAh4O.PQUWQMQ#C9=YC%C J Rs1CcZ|Dcgc]}|jjdc}Scc}wr)r\re)rCr=s r?_get_funcs_from_canonrs"%( )rBFFKKN )) )s (c rg}|D]}||z } t|}t|||}|r|Sg}|D]z}|}t|}|Dcgc]6}|j|Dcic]}|j|jc}8}}}t|||} | t t d|| z }||Scc}wcc}}w)NzZ The system of ODEs passed cannot be solved by dsolve_system. )rrr{r\r]rr6) wccrurCrrDrhr=rcomp_eqsscc_sols r?_weak_component_solverrs C s  !# &E "3q 1C  C%c*DGGRBGG37aQUUAEE\78GG*8UA> ?%j2'  w!$ J8GsB3B.6 B3.B3cPt|||}g}|D]}|t||z }|Sr;)rr)rCrDru componentsrhrs r?rrs?$S%3J C. %c1--. JrNcz|du}|du} |dk(r|s|dk(r| s |dk(r9|s| r5t|||d\\} }}}| jsttd|dk(r6t |||} | d}| j dd}| j d d}t j|d} |dk(r:|tt|}t||||}t j|d } |dk(rD|td j|}|}t|||\}}t j|d} t|| || S) a Expects the system to be in second order and in canonical form Explanation =========== Reduces a second order system into a first order one depending on the type of second order system. 1. "type0": If this is passed, then the system will be reduced to first order by introducing dummy variables. 2. "type1": If this is passed, then a particular substitution will be used to reduce the the system into first order. 3. "type2": If this is passed, then the system will be transformed with new dependent variables and independent variables. This transformation is a part of solving the corresponding system of ODEs. `A1` and `A0` are the coefficient matrices from the system and it is assumed that the second order system has the form given below: .. math:: A2 * X'' = A1 * X' + A0 * X + b Here, $A2$ is the coefficient matrix for the vector $X''$ and $b$ is the non-homogeneous term. Default value for `b` is None but if `A1` and `A0` are passed and `b` is not passed, then the system will be assumed homogeneous. Nr!rrrzG The system must be in its canonical form. rrPrQrrrD)r is_Identityrr6rSrr5fromkeysr rrXrrr_r) rCrDrurrPrQrris_a1is_a0A2r sys_orders r?rrsM> $JE $JE Etw5 FN.sE1a@ Ra~~Z)  v~(R3'( YYtT " YYtT " eQ'I w 9c#hA&r1eQ7MM%+  w : Q(B -b%< UMM%+ 'Y GGrNc || t||sttd|"| |jsttdt |Dcgc]?}t t dj|jj|Ac}}|j||||z|zz } |"|js| |j|zz} t| ||d} | |fScc}w)Nzi Correctly input for args 'A' and 'f_t' for Linear, Higher Order, Type 2 zN Provide the keyword 'P' for matrix P in A = P * J * P-1. z{}__0r) rrr6rr!rr rr>rIrrr:) r,rrDru max_orderrrrr\r^s r?"_higher_order_type2_to_sub_systemsr;s yCK':1a'@%     yQ]1+;+;%   UZ[PQHw~~affoo'F!GHK[\InnQ *S1Wy-@@G}Q--1557Q;Wi3A6G I \sAC0c  !|j}|dk(rtdj|!|Dcgc]?}tt dj|j j !A}}tfd|D}tt||} t!| |<tt  fdtd|dzD]}  t!j!| !| zz} | } t| dzD cgc] } | | vr| | nd}} t !fdt|D|| zz }| j!t||Dcic].\}}|j|| |j# !|0c}}|Dcgc]}|j#| }}t||Dcic] \}}|| }}}t%||!d}t'||!|S|d k(rn|j)d d}|j)d d}|j)d d}|j)d d}tfd|D}t+|||||||Sg}|D]#}|j j }tt dj||}|j-|||i} g}td|D]}tt dj|||}|| |j||<|j-|| |j||dz }t/|j||}|j-||Dcgc]}|j#| c}|z}&||fScc}wcc} wcc}}wcc}wcc}}wcc}w)Nr!rc3(K|] }| ywr;rMr<r>rs r?r@z/_higher_order_to_first_order..\:D $:rct|tr/|jdd}|jd}t||diSt|trB|jd}t |jdj d}||iSt|trdi}|jD]Q}t|tr|j|+t |j d}d||<S|Sy)Nrr) rdrrer0rr^keysrr)r free_symbolrrkrrbarg _get_coeffs_from_subs_expressions r?rzF_higher_order_to_first_order.._get_coeffs_from_subs_expressionbs$%"iil1o yy|!$ 4a88$$ ! =diilKPPRSTUVu~%$$99*C!#s+ &Fs&KL!%%Ec%J%O%O%Q RST U()u * %rNrrc3ZK|]"\}}j||z$ywr;)r)r<rZr free_functionrs r?r@z/_higher_order_to_first_order..~s0+TQ}R055b!.rr)rrz{}_0z{}_{})rrrrr r>rIrBr5rarrrrsumrbrr{r:rrrrfr )"rCrrurDrkwargsrr\r subs_dictrr coeff_dictrrb expr_to_subsnfr=r^ new_sys_orderr,rrr prev_func func_namer>rZnew_funcprev_fnew_eqrrrs" ` @@@r?rrTs }  w ELLO $QVWACXeELL$ABCBGW W:E:: UI./ 2w !  )  0q)a-( CA R)..r15b!e;D9$?JSXYZ]^Y^S_`%5J+>j'AE`F`+!&)++-.T2L   +.ui+@B"'!R ffQlL,=,=mB>OQS,TTB C  C144"2779%447:5)7LMeaYq\)M M )R8;+G]BiXX  w JJsD !jj% JJsD ! JJsD !:E:: 1!S%IQRVWXXI; NN++ 8xfmmI678;% q)I./ #ADxgnnY&B CDQGH.6IinnQ* +   X &y~~a156F A1F NN6 " #-00brwwy!07:!;$  >iXDaB5MR1s$AN1!N633N; 4OO O cTddlm}m}m}t |st t dt|}|$t|tst t d|||}td|Drt t dt|t|k7rt t d|$t|tst t d |Ctt|djtdjtd}g} t|||} | D]0} t!| ||} | t%| ||} | j'| 2| rg} t)|j*}| D]} t-| |} || | } |r>t)| j*|z }|| |||}| Dcgc]}|j/|} }|r%t)| j*|z }t1| |g|| } | j'| | } | S#t"$rd} YwxYwcc}w) aS Solves any(supported) system of Ordinary Differential Equations Explanation =========== This function takes a system of ODEs as an input, determines if the it is solvable by this function, and returns the solution if found any. This function can handle: 1. Linear, First Order, Constant coefficient homogeneous system of ODEs 2. Linear, First Order, Constant coefficient non-homogeneous system of ODEs 3. Linear, First Order, non-constant coefficient homogeneous system of ODEs 4. Linear, First Order, non-constant coefficient non-homogeneous system of ODEs 5. Any implicit system which can be divided into system of ODEs which is of the above 4 forms 6. Any higher order linear system of ODEs that can be reduced to one of the 5 forms of systems described above. The types of systems described above are not limited by the number of equations, i.e. this function can solve the above types irrespective of the number of equations in the system passed. But, the bigger the system, the more time it will take to solve the system. This function returns a list of solutions. Each solution is a list of equations where LHS is the dependent variable and RHS is an expression in terms of the independent variable. Among the non constant coefficient types, not all the systems are solvable by this function. Only those which have either a coefficient matrix with a commutative antiderivative or those systems which may be divided further so that the divided systems may have coefficient matrix with commutative antiderivative. Parameters ========== eqs : List system of ODEs to be solved funcs : List or None List of dependent variables that make up the system of ODEs t : Symbol or None Independent variable in the system of ODEs ics : Dict or None Set of initial boundary/conditions for the system of ODEs doit : Boolean Evaluate the solutions if True. Default value is True. Can be set to false if the integral evaluation takes too much time and/or is not required. simplify: Boolean Simplify the solutions for the systems. Default value is True. Can be set to false if simplification takes too much time and/or is not required. Examples ======== >>> from sympy import symbols, Eq, Function >>> from sympy.solvers.ode.systems import dsolve_system >>> f, g = symbols("f g", cls=Function) >>> x = symbols("x") >>> eqs = [Eq(f(x).diff(x), g(x)), Eq(g(x).diff(x), f(x))] >>> dsolve_system(eqs) [[Eq(f(x), -C1*exp(-x) + C2*exp(x)), Eq(g(x), C1*exp(-x) + C2*exp(x))]] You can also pass the initial conditions for the system of ODEs: >>> dsolve_system(eqs, ics={f(0): 1, g(0): 0}) [[Eq(f(x), exp(x)/2 + exp(-x)/2), Eq(g(x), exp(x)/2 - exp(-x)/2)]] Optionally, you can pass the dependent variables and the independent variable for which the system is to be solved: >>> funcs = [f(x), g(x)] >>> dsolve_system(eqs, funcs=funcs, t=x) [[Eq(f(x), -C1*exp(-x) + C2*exp(x)), Eq(g(x), C1*exp(-x) + C2*exp(x))]] Lets look at an implicit system of ODEs: >>> eqs = [Eq(f(x).diff(x)**2, g(x)**2), Eq(g(x).diff(x), g(x))] >>> dsolve_system(eqs) [[Eq(f(x), C1 - C2*exp(x)), Eq(g(x), C2*exp(x))], [Eq(f(x), C1 + C2*exp(x)), Eq(g(x), C2*exp(x))]] Returns ======= List of List of Equations Raises ====== NotImplementedError When the system of ODEs is not solvable by this function. ValueError When the parameters passed are not in the required form. r) solve_ics_extract_funcsrzQ List of equations should be passed. The input is not valid. NzG Input to the funcs should be a list of functions. c3LK|]}t|jdk7yw)rN)rre)r<r>s r?r@z dsolve_system.. s 143tyy>Q  1s"$zn dsolve_system can solve a system of ODEs with only one independent variable. zN Number of equations and number of functions do not match zE The independent variable must be of type Symbol )r)r)rrrrr4rr6rrdr^rrrr_rr:rrrrfrrrr{r)rCrDruicsrr-rrrsolsr7canon_eqrh final_solsr constantssolved_constantsrs r? dsolve_systemrs\zSR C=%    # C E4!8%    }s# 15 11%     3x3u:%    }Z62%    y c!fll:./288@ A! D DsE1-I  *8UA>C ;#HeQ7C C   3K,,  #C#C/C#C9=C!3K44y@ #,S%C#H 9<=Aqvv./==!3K44y@ cA3 =   c " #  K=# C (>s6 H<H% H"!H")Tr;)NNrFN)F)rNNNN)NN)NrO)NNNFT)s sympy.corerrrsympy.core.containersrsympy.core.exprtoolsrsympy.core.numbersrsympy.core.relationalr r sympy.core.sortingr r sympy.core.symbolr rsympy.core.functionrrrrrrsympy.functionsrrrrrrrrr(sympy.functions.combinatorial.factorialsrsympy.matricesr r!r"r#r$ sympy.polysr%r&sympy.simplifyr'r(r)sympy.simplify.powsimpr*r+sympy.simplify.ratsimpr,sympy.simplify.simplifyr-sympy.sets.setsr.sympy.solvers.deutilsr0rr1r2sympy.utilities.iterablesr3r4r5sympy.utilities.miscr6sympy.integrals.integralsr7r8rErrGrPrmrxrrrrrrr/rr:rrrKrDrSrXr_rcr}rrrrrrrrrrrrrrrrrrMrNr?r sz""'- .8+??888>OO&555*,%+;FF+9L J    ,Dm`6RjHVsn9>|~ J EP0f  :!H7t'T^CJ    &,'23 FX@* "L AE59@HF2[|erN