K iddlmZddlmZmZmZddlmZddlm Z m Z m Z m Z m Z ddlmZmZddlmZmZddlmZmZdZd d d d Zy ))combinations_with_replacement)symbolsAddDummy)Rational)cancelComputationFailedparallel_poly_from_exprreducedPoly)Monomial monomial_div) DomainErrorPolificationFailed)debugdebugfct|j\}} t||gdd\}}t |t||z zS#t$r||z cYSwxYw)z Put an expression over a common denominator, cancel and reduce. Examples ======== >>> from sympy import ratsimp >>> from sympy.abc import x, y >>> ratsimp(1/x + 1/y) (x + y)/(x*y) TF)fieldexpand)ras_numer_denomr r r)exprfgQrs \/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/simplify/ratsimp.pyratsimpr sh $< & & (DAqq1#T%81 7VAaC[   s sAAATF)quick polynomialcddlmtd|t|j \}} t ||gzg|i|\}j} | jr| j_ntd| z|ddD cgc]} | jjc} tfddfd t|jj d }t|jj d }|r||z jSt!|jj t!|jj g\} } } ss| rqt#d t%| g}| D]D\}}}}||d d}|j'|j)||j)|fFt+|d\} } | j,s7| j/d \}} | j/d \}} t1||}n t1d }| |j2z| |j4zz S#t $r|cYSwxYwcc} w)a Simplifies a rational expression ``expr`` modulo the prime ideal generated by ``G``. ``G`` should be a Groebner basis of the ideal. Examples ======== >>> from sympy.simplify.ratsimp import ratsimpmodprime >>> from sympy.abc import x, y >>> eq = (x + y**5 + y)/(x - y) >>> ratsimpmodprime(eq, [x*y**5 - x - y], x, y, order='lex') (-x**2 - x*y - x - y)/(-x**2 + x*y) If ``polynomial`` is ``False``, the algorithm computes a rational simplification which minimizes the sum of the total degrees of the numerator and the denominator. If ``polynomial`` is ``True``, this function just brings numerator and denominator into a canonical form. This is much faster, but has potentially worse results. References ========== .. [1] M. Monagan, R. Pearce, Rational Simplification Modulo a Polynomial Ideal, https://dl.acm.org/doi/pdf/10.1145/1145768.1145809 (specifically, the second algorithm) r)solveratsimpmodprimez.Cannot compute rational simplification over %sNc|dk(rdgSg}tttj|D]U}dgtjz|D]}|xxdz cc<t fdDsE|j W|Dcgc]$}t |jj&c}|dz zScc}w)z Compute all monomials with degree less than ``n`` that are not divisible by any element of ``leading_monomials``. rc3:K|]}t|duywN)r).0lmgms r z5ratsimpmodprime..staircase..bs!&C<3'4/&s)rrangelengensallappendr as_expr) nSmiisr*leading_monomialsopt staircases @rr9z"ratsimpmodprime..staircaseVs 63J /c#((m0DaH BCM!A !  &$&&  9::1# ##SXX.:Yq1u=MMM:s)C cB||}}d}|j|jz}r|dz } n|} ||z| kr||fvrnj||f||td||ftdt zt tdt zt z} t tfdtt Dj| z} t tfdtt Dj| z} t|| z|| zz j| zjd d} t | j j}|zd d }|r8td |jDs| j|}| j|}|jt!t#t%zdgt t zz}|jt!t#t%zdgt t zz}t |j}t |j}|dk(r t'd|j)| | |zf||z|k7r|dg}n|dz }|dz }|dz }||z| kr|dkDr&||||||z \}}}|||||z |\}}}|||fS)ak Computes a rational simplification of ``a/b`` which minimizes the sum of the total degrees of the numerator and the denominator. Explanation =========== The algorithm proceeds by looking at ``a * d - b * c`` modulo the ideal generated by ``G`` for some ``c`` and ``d`` with degree less than ``a`` and ``b`` respectively. The coefficients of ``c`` and ``d`` are indeterminates and thus the coefficients of the normalform of ``a * d - b * c`` are linear polynomials in these indeterminates. If these linear polynomials, considered as system of equations, have a nontrivial solution, then `\frac{a}{b} \equiv \frac{c}{d}` modulo the ideal generated by ``G``. So, by construction, the degree of ``c`` and ``d`` is less than the degree of ``a`` and ``b``, so a simpler representation has been found. After a simpler representation has been found, the algorithm tries to reduce the degree of the numerator and denominator and returns the result afterwards. As an extension, if quick=False, we look at all possible degrees such that the total degree is less than *or equal to* the best current solution. We retain a list of all solutions of minimal degree, and try to find the best one at the end. rr%z%s / %s: %s, %szc:%d)clszd:%dc34K|]}||zywr')r(r5CsM1s rr+z._ratsimpmodprime..:aBqEBqEM:c34K|]}||zywr'r=)r(r5DsM2s rr+z._ratsimpmodprime..r@rAT)orderpolys)r. particularrc3&K|] }|dk( yw)rNr=)r(r6s rr+z._ratsimpmodprime..srCr?rDG_ratsimpmodprimer8rr!r9testeds @@@@rrdz)ratsimpmodprime.._ratsimpmodprimehs:!1!ANN$44 QJEE!eun1v JJ1v 1B1B $q!Rn 5#b')u5B#b')u5BbB:5R>::CHHrMKE:5R>::CHHrMKEE AI-q#((R-!iit5568AQSXX&--/A27t4@C3z!ratsimpmodprime..s)QqTZZ\):S1=N)Nrf)key)convert)rr)sympy.solvers.solversr!rrrr rrghas_assoc_Field get_fieldrLMrEsetr r.r rr-r0rPminis_Field clear_denomsrqp)rrcrrr.argsnumdenomrFrgrrZr[rWnewsolr`rar3r_rbcndnrrdr7r8r!r9res `` @@@@@@rr"r"sF<, T",,.JC,c5\A-=MMM sZZF %%'  E5!R2$e6NO1 ??t,At,A R  QK accEAaccE?o  =sI%"I6% I32I3N) itertoolsr sympy.corerrrsympy.core.numbersr sympy.polysrr r r r sympy.polys.monomialsr rsympy.polys.polyerrorsrrsympy.utilities.miscrrrr"r=rfrrs23**'YY8B.!,+/5rf