K iEVdZddlmZddlmZmZddlmZmZm Z m Z ddl m Z ddl mZddlmZmZddlmZmZmZmZmZmZmZmZmZmZdd lmZdd lm Z m!Z!m"Z"m#Z#dd l$m%Z%m&Z&m'Z'm(Z(m)Z)dd l*m+Z+dd l,m-Z-ddl.m/Z/ddl0m1Z1ddl2m3Z3dZ4dZ5ddZ6dZ7dZ8dejrfdZ:dZ;y)a This File contains helper functions for nth_linear_constant_coeff_undetermined_coefficients, nth_linear_euler_eq_nonhomogeneous_undetermined_coefficients, nth_linear_constant_coeff_variation_of_parameters, and nth_linear_euler_eq_nonhomogeneous_variation_of_parameters. All the functions in this file are used by more than one solvers so, instead of creating instances in other classes for using them it is better to keep it here as separate helpers. )Counter)AddS)diffexpand_mexpand expand_mul)Eq)default_sort_key)DummyWild) expcoscoshimlogresinsinhatan2 conjugate)Integral)PolyRootOfrootofroots)collectsimplify separatevarspowsimptrigsimp)numbered_symbols)solve) wronskian) sub_func_doit)get_numbered_constantscP|jd}|j}|dkr td|dk(ry|dk(r||jvryy|jr)|j ||ry||z|jvS|j r|j||fk(S|dk(r||k(Sy)a  Linear Euler ODEs have the form K*x**order*diff(y(x), x, order) = F(x), where K is independent of x and y(x), order>= 0. So we need to check that for each term, coeff == K*x**order from some K. We have a few cases, since coeff may have several different types. rzorder should be greater than 0TFr%)argsfunc ValueError free_symbolsis_Mulhasis_Pow as_base_exp)coeffr*orderxfs f/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/solvers/ode/nonhomogeneous.py _test_termr6s ! A A qy9:: z z "" " || 99QqT?%x5::%%   "q%j00 !Ez c |jd}|j}tjt d}}|D]6}|dk\s |||t ||z||z|| zzj z }8t||}t|jDcgc]}t||} }g} tt||jdz} | jt| } tj} t}| j!D]\}}t|D] }t#|t$r3| ||z| j'zz } |dk7r t)dd|dfg| z} G|j*r/| |||z||zz| j'zz } ||dfg| z} t-|}t/|}| |||z||zz| j't1t3|||zz| j't5|||zzzzz } |||fg| z}  !t7||| } g}| D]\}}}|dk(r!|j9|||z||zz-|||z||zzt1t3|||zz}||vr8|||z||zzt5|||zz}|j9||j9|| |fScc}w)a0 Returns the solution of homogeneous part of the linear euler ODE and the list of roots of characteristic equation. The parameter ``match_obj`` is 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. rr3)numr%zValue should be 1)r)r*rZeror rrrrangedegreerlistr'reverserritems isinstancerpopr+is_realrrrabsrr append)eqr* match_objr3r4chareqsymbolik chareqroots collectterms constants charrootsgsollnroot multiplicityrerootimrootgensolssin_formcos_forms r5!_get_euler_characteristic_eq_solsrY:s ! A AVVU3ZFF O 6 y|DFAq$99!fW*DLLN NFO&& !F.3FMMO.DE6&!$EKEL+BFMMOA4EFGI  $I 66D B'oo/Dl|$ DA$'DIMMO331$$%899!"D! ~ < 1q!T'*Y]]_<<!"D! ~ < DD1qAvI.MMOc#f+be*;&<<mmoF2a5L(99:;;"#FF 34|C  DD$ adD>DG) )66 Q; NN2a5!8AvI- .!uax6 )#c&k"Q%.?*@@H7"a5!8AvI-c&A,.??x(x( ) =WFsK?c |j}|jd}|} d} t||} |rt| } t | dd} | s,t dt |zdzdzt |zdzt||k7r,t dt |zdzdzt |zdztj|z} |D]I} | | tt|Dcgc] }|| k7s | c}|| d z| z |z| z| |z z } | d z} K|rt| } t | d } t|||j| zScc}w) a Helper function for the method of variation of parameters and nonhomogeneous euler eq. See the :py:meth:`~sympy.solvers.ode.single.NthLinearConstantCoeffVariationOfParameters` docstring for more information on this method. The parameter are ``match_obj`` should be a dictionary that has the following keys: ``list`` A list of solutions to the homogeneous equation. ``sol`` The general solution. rT)deep recursive Cannot find z: solutions to the homogeneous equation necessary to apply zvariation of parameters to z (Wronskian == 0) (number of terms != order))r[) r*r)r$rr!NotImplementedErrorstrlenr NegativeOnerr rhs)rFr*r homogen_solr2rG simplify_flagr4r3rpsolwr negonetermrJsols r5_solve_variation_of_parametersrl|s$ A ! AA D 5! B b\btt 4 ".3u:"=D#E%#&(+B#02E#FG G 5zU!.3u:"=D#E%#& B#0#01 1'J  8Ie.Pssaxs.PRS$TUVWYUZ$Z[]$]_`aabccdefkdlllb ~4( adKOOd* ++ /Qs  E "E c &|jd}tjtd}}|j D]&}t |t s|dkr|||||zzz }(t||}t|d}t||k7r0t|jDcgc]}t||}}td|jD } t|} g} g} g} |D]}|| vr | j!|}t|D]o}| r-| j#||zt%||zz||dfg| z} 3t'|}t)|}|j+t,rB|j+t,r-| j#||zt%||zz||dfg| z} || vr |||fg| z} |dk(r-| j#||zt%||zz||dfg| z} | j#t/|| j#||zt%||zzt1t3||zz| j#||zt%||zzt5||zz|||fg| z} r| | fScc}w)ae Returns the roots of characteristic equation of constant coefficient linear ODE and list of collectterms which is later on used by simplification to use collect on solution. The parameter `r` is 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. rr3T)multiplec34K|]}|jywN)rC).0rJs r5 z4_get_const_characteristic_eq_sols..sGa Gs)r)rr;r keysrArarrrbr<r=rall all_coeffsrrBrErrrr.rrrrDr)rgr*r2r3rHrIrJrLrKchareq_is_complexrOrMrVconjugate_rootsrRrSrTrUs r5!_get_const_characteristic_eq_solsrxs ! AVVU3ZFF VVX% a Q  ad619n $F % && !F.K ;5 27 2HIQvfa(I IG63D3D3FGGG $ILGOD y  }}T* |$ DA q!tCQK/0!"D! ~ < XFXFzz% VZZ%6q!tCQK/0!"D! ~ < ?*%&$7#8<#GLQ;NN1a4F1H #56%&N#3l#BL&&y7q!tCqM1CF a4HHIq!tCqM1CFaK4HHI"#FF 34|C 3 D D> L  WJs&Jc p|j}|jd}|jt|j t |dk(r|dj ||k(sJ|dj}t|}|D]i\}}}t|||zt||zztt||zz}t|||zt||zzt||zz}k|D]$\}}}t|||zt||zz}&t|}t|||S)ah Helper function which collects the solution on collectterms. Ideally this should be handled by odesimp.It is used only when the simplify is set to True in dsolve. The parameter ``collectterms`` is a list of tuple (i, reroot, imroot) where `i` is the multiplicity of the root, reroot is real part and imroot being the imaginary part. r)keyr%)r*r)sortr r?rblhsrdr rrrrDrr r )rkr*rMr4r3rJrTrUs r5_get_simplified_solr}s/ A ! A*+ s8q=SVZZ1Q4// / a&**C S/C)=66c1a4F1H -c#f+a-.@@Ac1a4F1H -c&(m;<=*/66c1a4F1H -./ #,C adC=r7Nc td|g td|g t|d}i}dtf fd tf fd fd   |||d <|d rt}t j |D]i} ||}t jur;t fd |Dr'|Dchc]}||z }}t fd |Dr'|j|}k||d <|Scc}w) a Returns a trial function match if undetermined coefficients can be applied to ``expr``, and ``None`` otherwise. A trial expression can be found for an expression for use with the method of undetermined coefficients if the expression is an additive/multiplicative combination of constants, polynomials in `x` (the independent variable of expr), `\sin(a x + b)`, `\cos(a x + b)`, and `e^{a x}` terms (in other words, it has a finite number of linearly independent derivatives). Note that you may still need to multiply each term returned here by sufficient `x` to make it linearly independent with the solutions to the homogeneous equation. This is intended for internal use by ``undetermined_coefficients`` hints. SymPy currently has no way to convert `\sin^n(x) \cos^m(y)` into a sum of only `\sin(a x)` and `\cos(b x)` terms, so these are not implemented. 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. Examples ======== >>> from sympy import log, exp >>> from sympy.solvers.ode.nonhomogeneous import _undetermined_coefficients_match >>> from sympy.abc import x >>> _undetermined_coefficients_match(9*x*exp(x) + exp(-x), x) {'test': True, 'trialset': {x*exp(x), exp(-x), exp(x)}} >>> _undetermined_coefficients_match(log(x), x) {'test': False} a)excludebr)combinereturnc|jsy|jrtfd|jDS|jrm|jt t r4d}|jD]#}|jt t s|ryd}%tfd|jDS|jrV|jt t tttfvxr-t|jdjzzS|jr<|jj r&|jj"r|jdk\ry|jr@|jj$r*t|jjzzS|j xst|j$S)zV Test if ``expr`` fits the proper form for undetermined coefficients. Tc30K|] }|ywrprqrJr6r3s r5rrzG_undetermined_coefficients_match.._test_term..B;Az!Q';Fc30K|] }|ywrprrs r5rrzG_undetermined_coefficients_match.._test_term..Nrrr)r.is_Addrtr)r-rr is_Functionr*rrrboolmatchr/base is_Symbol is_Integer is_number)exprr3 foundtrigrJr6rrs ` r5r6z4_undetermined_coefficients_match.._test_term;sRxx{ ;;;;; ; ;;xxS!! -AuuS#$#((,I - ;;; ;   99c3d ;;5 ! **1Q3734 5 ;;499..4883F3FA  ;;499..qsQw/0 0~~5dnn!55r7cd}t|}|jrP|jD]?}||||vr|j||||j |||}A|S|||}|j |h}t }||k7rg|j }|j|}|||}|jr|j |||}n|j|||k7rg|}|S)aq Returns a set of trial terms for undetermined coefficients. The idea behind undetermined coefficients is that the terms expression repeat themselves after a finite number of derivatives, except for the coefficients (they are linearly dependent). So if we collect these, we should have the terms of our trial function. ctj}|jr*|jD]}|j |s||z}|S|j |r|}|S)z Returns the expression without a coefficient. Similar to expr.as_independent(x)[1], except it only works multiplicatively. )rOner-r)r.)rr3termrJs r5_remove_coefficientzU_undetermined_coefficients_match.._get_trial_set.._remove_coefficientbsY55D{{"AuuQx " K!Kr7)r rr)addunionsetcopyr)rr3exprsrrtmpsetoldset_get_trial_sets r5rz8_undetermined_coefficients_match.._get_trial_setYs  $ ;;  H&tQ/58II1$:;!KKtQ(FGE  H* 'tQ/D[[$(FUFF" yy|*43;;#\\.q&*IJFJJt$F"E r7cDtt|jS)zf This function checks whether the given trialset contains any root of homogeneous equation)rr&is_zero)req_homogeneousr*s r5is_homogeneous_solutionzA_undetermined_coefficients_match..is_homogeneous_solutionsmND$?@HHHr7testc3.K|] }|ywrpr)rqtsrs r5rrz3_undetermined_coefficients_match..sD"1"5Dstrialset) r r rrr make_argsrr;anyr)rr3r*rretdicttemp_setrJactrrr6rrrs `` @@@@@r5 _undetermined_coefficients_matchrsH S1#A S1#A 4 'DG6t6<'*e0dI !q)GFOv5t$ +A A&CQVV+DDD*-.B1R4.C.DDD~~c*H  +'  N /s< C9c Z|}tdt}g}|d}|d} |j} |jd} t ||k7r,t dt |zdzdzt |zd zd} |D]&} t|}|j|| || zz } (t|| | | }ttt|dgt |d zz}t|}tj|D]D} t!| d | g }|j#|| r||| xx|d z cc<:|d ||| <Ft%t|j'|}|st d|z| j)|}t+| | | j,|zS)a# Helper function for the method of undetermined coefficients. See the :py:meth:`~sympy.solvers.ode.single.NthLinearConstantCoeffUndeterminedCoefficients` docstring for more information on this method. The parameter ``trialset`` is the set of trial functions as returned by ``_undetermined_coefficients_match()['trialset']``. The parameter ``match`` should be a dictionary that has the following keys: ``list`` A list of solutions to the homogeneous equation. ``sol`` The general solution. r)clsr>rkrr]z9 solutions to the homogeneous equation necessary to applyz undetermined coefficients to r^r%T)dictsymbolsr1zfCould not solve `%s` using the method of undetermined coefficients (unable to solve for coefficients).)r"r r*r)rbr`ranextrEr&rr>ziprrrrgetr#valuessubsr rd)rFr*r2rrrgcoeffs coefflistrVrPr4r3 trialfuncrJceqs coeffsdicts coeffvalsrhs r5 _solve_undetermined_coefficientsrs* A cu -FIiG U8D A ! A 7|u!.3u:"=C#D(#)+.r7#3 &#&' ' I  LQqS  AaD) ,Cd3x!c(ma.?)@ABCJ 3-C ]]3 * s 3 >>!A$  qt ' *  zJqt  *d:,,./;I ! 246 78 8 >>) $D adDHHtO $$r7)T)<__doc__ collectionsr sympy.corerrsympy.core.functionrrrr sympy.core.relationalr sympy.core.sortingr sympy.core.symbolr r sympy.functionsrrrrrrrrrrsympy.integralsr sympy.polysrrrrsympy.simplifyrrrr r!sympy.utilitiesr"sympy.solvers.solversr#sympy.matricesr$ subscheckr&sympy.solvers.ode.oder'r6rYrlrxr}r;rrrr7r5rs  BB$/)$55MM,'$$8:?D0,fD!R448Od@%r7