K iEddlmZddlmZddlmZmZddlmZddl m Z ddl m Z ddl mZddlmZmZmZdd lmZdd lmZdd lmZmZdd lmZdd lmZddlmZddlm Z m!Z!ddl"m#Z#dZ$ddZ%dZ&dZ'dZ(dddZ)y))Add) factor_terms) expand_log_mexpand)Pow)S)ordered)Dummy)LambertWexplog)root)roots)Polyfactor) separatevars)collect)powsimp)solve_invert)uniqc |jDchc]}||jvs|}}t|D]G}d|z }||vs ||vs|jdtj ur|}|j |I|Scc}w)aprocess the generators of ``poly``, returning the set of generators that have ``symbol``. If there are two generators that are inverses of each other, prefer the one that has no denominator. Examples ======== >>> from sympy.solvers.bivariate import _filtered_gens >>> from sympy import Poly, exp >>> from sympy.abc import x >>> _filtered_gens(Poly(x + 1/x + exp(x)), x) {x, exp(x)} )gens free_symbolslistas_numer_denomrOneremove)polysymbolgrags ]/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/solvers/bivariate.py_filtered_gensr%s$yy =!Fann$s BBNc,|jDcgc]=}|r7|jr||jvs|js|j|r|?}}t |dk(r|dS|r"t t t|fdSycc}w)a+Returns the term in lhs which contains the most of the func-type things e.g. log(log(x)) wins over log(x) if both terms appear. ``func`` can be a function (exp, log, etc...) or any other SymPy object, like Pow. If ``X`` is not ``None``, then the function returns the term composed with the most ``func`` having the specified variable. Examples ======== >>> from sympy.solvers.bivariate import _mostfunc >>> from sympy import exp >>> from sympy.abc import x, y >>> _mostfunc(exp(x) + exp(exp(x) + 2), exp) exp(exp(x) + 2) >>> _mostfunc(exp(x) + exp(exp(y) + 2), exp) exp(exp(y) + 2) >>> _mostfunc(exp(x) + exp(exp(y) + 2), exp, x) exp(x) >>> _mostfunc(x, exp, x) is None True >>> _mostfunc(exp(x) + exp(x*y), exp, x) exp(x) rrc&|jSN)count)xfuncs r$z_mostfunc..Ps )keyN)atoms is_Symbolrhaslenmaxrr )lhsr+Xtmpftermss ` r$ _mostfuncr8/s6!YYt_)cQ S--- KKCGGAJ)F) 6{aay 4(.EFF )sABc`t|j}|j|\}}|jr'|jrt ||\}}}||z||z|fS|jsd}||}}n!|}t |j|d\}}|jr| }| }|||fS)aReturn ``a, b, X`` assuming ``arg`` can be written as ``a*X + b`` where ``X`` is a symbol-dependent factor and ``a`` and ``b`` are independent of ``symbol``. Examples ======== >>> from sympy.solvers.bivariate import _linab >>> from sympy.abc import x, y >>> from sympy import exp, S >>> _linab(S(2), x) (2, 0, 1) >>> _linab(2*x, x) (2, 0, x) >>> _linab(y + y*x + 2*x, x) (y + 2, y, x) >>> _linab(3 + 2*exp(x), x) (2, 3, exp(x)) rFas_Add)rexpandas_independentis_Mulis_Add_linabrcould_extract_minus_sign)argr!inddepabr*s r$r@r@Ts( szz| $C!!&)HC zzcjjf%1a1uc!eQ :: C1 C //u/E1!!# B B a7Nr-c<tt|}t|t|}|sgS|j |d}t | trY||z j ||j d}|j d}t |tsgS| j d }||z }||jvrgSt||\}}}t||z |}|j|}|||jvrgS|j d} t| |\} } } | |k7rgStdt| z |} ddg}g}| |z| |zz |z | z j\}}|j\}}t||z }td}t!||z|z |j#Dcgc] }||| zz |z}}|D]N}|D]G}t%||}|r |j&s| | z ||z |zz|j)fd| DIP|Scc}w)z Given an expression assumed to be in the form ``F(X, a..f) = a*log(b*X + c) + d*X + f = 0`` where X = g(x) and x = g^-1(X), return the Lambert solution, ``x = g^-1(-c/b + (a/d)*W(d/(a*b)*exp(c*d/a/b)*exp(-f/a)))``. rrhstc3BK|]}|jywr()subs).0xurHus r$ z_lambert..s92rwwq#9s)rrr8r rL isinstanceargsrr@ras_coefficientr rr as_coeff_Mulr rkeysr is_realextend)eqr*mainlogotherdfX2logtermrElogargrFcX1xusolnslambert_real_branchessolnumdenperJrRrBkwrHrOs @@r$_lambertrkys@ *R. !BC#G  GGGQ E5&#5j  w Q 8,,q/'3'I&q!! e """ eQHAq"b5j'*Gw'AyA' \\!_Ffa HAq" Rx  e ABFAG G C1QqS! A --/HC    FAs CG A c A$QTAXq1668 9!AqsGAI 9D 9:& :Aa A"Q$!A#q.C JJ99 9  :: J :s0Hc fd}|jd\}}| }Dcgc]@}|jttfvs$|jr|jj vr|B}}|s t |js |jr:td ij|jfdfd}|jr|jr|jd} || z } || z } | js| r| jtjtj st#t| t| z } || S|jrO|rMt#t|d}t|}|jr|jr||z } || S|j%i}t't)|d}t} t+|| z \}}|j%| |i}g}|st-|t}|r|jr'|dk7r"t/t|t|z }n|jr|j|d}|r|js|j1t2Dcgc]}|j vr|c}rP|st|t||z z }nt||z t||z z }t/t#|}nt/||z }|st-|t}|rt5||}|jr0|dk7r+t/t#t|t|z }n~|jrr|j|d}||z }||z }|j7r|j7r |d z}|d z}t|t|z }t/t#|}|st-|t2}|r|jj vrt5||}|jr0|dk7r+t/t#t|t|z }nT|jrH|j|d}||z }||z }t|t|z }t/t#|}|st d |zt9t;|Scc}wcc}w) aReturn solution to ``f`` if it is a Lambert-type expression else raise NotImplementedError. For ``f(X, a..f) = a*log(b*X + c) + d*X - f = 0`` the solution for ``X`` is ``X = -c/b + (a/d)*W(d/(a*b)*exp(c*d/a/b)*exp(f/a))``. There are a variety of forms for `f(X, a..f)` as enumerated below: 1a1) if B**B = R for R not in [0, 1] (since those cases would already be solved before getting here) then log of both sides gives log(B) + log(log(B)) = log(log(R)) and X = log(B), a = 1, b = 1, c = 0, d = 1, f = log(log(R)) 1a2) if B*(b*log(B) + c)**a = R then log of both sides gives log(B) + a*log(b*log(B) + c) = log(R) and X = log(B), d=1, f=log(R) 1b) if a*log(b*B + c) + d*B = R and X = B, f = R 2a) if (b*B + c)*exp(d*B + g) = R then log of both sides gives log(b*B + c) + d*B + g = log(R) and X = B, a = 1, f = log(R) - g 2b) if g*exp(d*B + h) - b*B = c then the log form is log(g) + d*B + h - log(b*B + c) = 0 and X = B, a = -1, f = -h - log(g) 3) if d*p**(a*B + g) - b*B = c then the log form is log(d) + (a*B + g)*log(p) - log(b*B + c) = 0 and X = B, a = -1, d = a*log(p), f = -log(d) - g*log(p) cdDcgc]}|j|||zic}\}}t||}||k7r|jt||tt |Scc}w)aReturn the unique solutions of equations derived from ``expr`` by replacing ``t`` with ``+/- symbol``. Parameters ========== expr : Expr The expression which includes a dummy variable t to be replaced with +symbol and -symbol. symbol : Symbol The symbol for which a solution is being sought. Returns ======= List of unique solution of the two equations generated by replacing ``t`` with positive and negative ``symbol``. Notes ===== If ``expr = 2*log(t) + x/2` then solutions for ``2*log(x) + x/2 = 0`` and ``2*log(-x) + x/2 = 0`` are returned by this function. Though this may seem counter-intuitive, one must note that the ``expr`` being solved here has been derived from a different expression. For an expression like ``eq = x**2*g(x) = 1``, if we take the log of both sides we obtain ``log(x**2) + log(g(x)) = 0``. If x is positive then this simplifies to ``2*log(x) + log(g(x)) = 0``; the Lambert-solving routines will return solutions for this, but we must also consider the solutions for ``2*log(-x) + log(g(x))`` since those must also be a solution of ``eq`` which has the same value when the ``x`` in ``x**2`` is negated. If `g(x)` does not have even powers of symbol then we do not want to replace the ``x`` there with ``-x``. So the role of the ``t`` in the expression received by this function is to mark where ``+/-x`` should be inserted before obtaining the Lambert solutions. )rIr)xreplace_solve_lambertrWrr)exprrJr!sgnnlhsplhssolsrs r$_solve_even_degree_exprz/_solve_lambert.._solve_even_degree_exprspV7>?/2DMM1c&j/ *? ddFD1 4< KKtVT: ; DJ?sA+Tr:cn|jxr'|jk(xr|jjSr()is_Powbaser is_even)ir!s r$r,z _solve_lambert..(s*?QVVv-?!%%--r-c"|jzSr()r )rzrJs r$r,z _solve_lambert..*s155r-r)force)deeprIz:%s does not appear to have a solution in terms of LambertW)rJ)r=r+r r rwrNotImplementedErrorr?r>r assumptions0replacer1rLrComplexInfinityNaNrrnrrrr8rkr/rrrArr )r\r!rrunrhsr4rHr6lamcheckt_indept_term_rhsrXrrzsolnrYrZdiffmainexpmaintermmainpowrJs `` @r$rorosgD4 l   5ID# %C#BHHc *#''*>*> >BHB !## zzSZZ  -,, -kk @  ::#''!*hhq!nG7]F=D==TJJq00!%%8F c$i 78.r1f== ZZCSXT2Cc(Cwwqzcjj3Y.r1f==llAv;' &4( )C A S1Wf %FAs **aX C D Cf- zzcQhC3s8 3V<!,',{{3'737 #!S%5%5537"5zC ,<<"3;/#cEk2BB#Jt$4f=D$C#Iv6D Cf- #w'CzzcQh 3s8c#h+> ?H!,;Ek557002NH2IC8}s3x/ 4 0&9 Cf- v!9!99#w'CzzcQh 3s8c#h+> ?H!,;Ek8}s3x/ 4 0&9 !# "##$% %  EB@37s AS S%Tfirstc tdd}|rt|}|j}t}t}tt|j ||i||||d}|r2||i} |dj | |dj | |dfSy |}|j}t j|j} g} | D]G} t| j |z } | j} | vs| vrn!| j| Izt | |fSfd }g} |j}|j|k(r_t|j|z|} t|j|z|}||||zz | z } | | z|zz| |fSg} |j}|j|k(rtdD]t}t|j|z|zz|} t|j|z|}||||zz | z z } | | zz|zz| |fcScvy y ) aGiven an expression, f, 3 tests will be done to see what type of composite bivariate it might be, options for u(x, y) are:: x*y x+y x*y+x x*y+y If it matches one of these types, ``u(x, y)``, ``P(u)`` and dummy variable ``u`` will be returned. Solving ``P(u)`` for ``u`` and equating the solutions to ``u(x, y)`` and then solving for ``x`` or ``y`` is equivalent to solving the original expression for ``x`` or ``y``. If ``x`` and ``y`` represent two functions in the same variable, e.g. ``x = g(t)`` and ``y = h(t)``, then if ``u(x, y) - p`` can be solved for ``t`` then these represent the solutions to ``P(u) = 0`` when ``p`` are the solutions of ``P(u) = 0``. Only positive values of ``u`` are considered. Examples ======== >>> from sympy import solve >>> from sympy.solvers.bivariate import bivariate_type >>> from sympy.abc import x, y >>> eq = (x**2 - 3).subs(x, x + y) >>> bivariate_type(eq, x, y) (x + y, _u**2 - 3, _u) >>> uxy, pu, u = _ >>> usol = solve(pu, u); usol [sqrt(3)] >>> [solve(uxy - s) for s in solve(pu, u)] [[{x: -y + sqrt(3)}]] >>> all(eq.subs(s).equals(0) for sol in _ for s in sol) True rOT)positiveFrrrNcjt|j||}|j}|vs|vrdS|Sr()rrLr)r\vr`newfreer*ys r$okzbivariate_type..oks9qvva|$T Q$Yt8S8r-)r ras_exprbivariate_typerLrnr make_argsrrappenddegreercoeff_monomialrange)r\r*rrrOrg_x_yrvrepsrRrrErrr[rFitrys `` r$rrsN cD!A AqM IIK W W DB2!7R@"bPU V 2q>Da5>>$'A)=r!uD D A A == %D C ! QVVAqs^ $~~ 9T  1 !sCIq  9 C  Axx{a !!!Q$' + !!!Q$' +AAaC{# ?Q319c1$ $ C  Axx{a!H DQ%%ad1a4i0!4AQ%%ad+Q/AQA!GQ;q=)Cs1uqs{C**aDAq  r-r()*sympy.core.addrsympy.core.exprtoolsrsympy.core.functionrrsympy.core.powerrsympy.core.singletonrsympy.core.sortingr sympy.core.symbolr &sympy.functions.elementary.exponentialr r r (sympy.functions.elementary.miscellaneousrsympy.polys.polyrootsrsympy.polys.polytoolsrrsympy.simplify.simplifyrsympy.simplify.radsimprrsympy.solvers.solversrrsympy.utilities.iterablesrr%r8r@rkrorr-r$rsb-4 "&#GG9'.0*+0*8"J"JEP]@&*\r-