
    Ed                         d dl mZ d dlmZmZmZ d dlmZ d dlm	Z	m
Z
mZmZmZ d dlmZmZ d dlmZmZ d dlmZ d Zd	d
ddZdS )    )combinations_with_replacement)symbolsAddDummy)Rational)cancelComputationFailedparallel_poly_from_exprreducedPoly)Monomialmonomial_div)DomainErrorPolificationFailed)debugc                     t          |                                           \  }}	 t          ||gdd          \  }}n# t          $ r ||z  cY S w xY wt	          | t          ||z            z   S )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)fieldexpand)r   as_numer_denomr   r	   r   )exprfgQrs        6lib/python3.11/site-packages/sympy/simplify/ratsimp.pyratsimpr   	   s     $<<&&((DAqq1#T%88811   s


 7VAaC[[  s   > AATF)quick
polynomialc                   ddl m t          d|            t          |                                           \  }}	 t          ||gz   g|R i |\  }n# t          $ r | cY S w xY wj        }	|	j        r|		                                _        nt          d|	z            fd|dd         D             t                      fddfd		t          |j        j        
          d         }t          |j        j        
          d         }|r||z                                  S  t          |j        j                  t          |j        j                  g           \  }
}}s|rt          dt!          |          z             g }|D ]S\  }}}} ||dd          }|                    |                    |          |                    |          f           Tt'          |d           \  }
}|	j        sC|
                    d          \  }}
|                    d          \  }}t-          ||          }nt-          d          }|
|j        z  ||j        z  z  S )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, http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.163.6984
        (specifically, the second algorithm)
    r   )solveratsimpmodprimez.Cannot compute rational simplification over %sc                 D    g | ]}|                     j                  S  )LMorder).0r   opts     r   
<listcomp>z#ratsimpmodprime.<locals>.<listcomp>S   s%    <<<Qci<<<       Nc                 t   | dk    rdgS g }t          t          t          j                            |           D ]_}dgt          j                  z  |D ]}|xx         dz  cc<   t	          fdD                       r|                               `fd|D              | dz
            z   S )z
        Compute all monomials with degree less than ``n`` that are
        not divisible by any element of ``leading_monomials``.
        r      c              3   <   K   | ]}t          |          d u V  d S )N)r   )r&   lmgms     r   	<genexpr>z5ratsimpmodprime.<locals>.staircase.<locals>.<genexpr>b   sB       & &C<3''4/ & & & & & &r)   c                 H    g | ]} t          |          j        j         S r#   )r   as_exprgens)r&   sr'   s     r   r(   z6ratsimpmodprime.<locals>.staircase.<locals>.<listcomp>f   s,    :::1##SX.:::r)   )r   rangelenr3   allappend)nSmiir/   leading_monomialsr'   	staircases       @r   r>   z"ratsimpmodprime.<locals>.staircaseV   s    
 6 	3J/c#(mm0D0DaHH 	 	BCMM!A  !	 & & & &$& & & & & :::::::YYq1u=M=MMMr)   c                    | |}}d}|                                  |                                 z   }r|dz
  }	n|}	||z   |	k    rD||fv rn<                    ||f            |           |          t          |d|dd           t          dt	                    z  t
                    t          dt	                    z  t
                    z   }
t          t          fd	t          t	                              D                       j	        |
z             }t          t          fd
t          t	                              D                       j	        |
z             }t          | |z  ||z  z
  j	        |
z   j        d          d         }t          |j	                                                  } |z   dd          }|rt          d |                                D                       sZ|                    |          }|                    |          }|                    t!          t#          t%          z   dgt	                    t	                    z   z                                          }|                    t!          t#          t%          z   dgt	                    t	                    z   z                                          }t          |j	                  }t          |j	                  }|dk    rt'          d          |                    |||z   f           ||z   |k    r	|d         g}n|dz  }|dz  }|dz  }||z   |	k    D|dk    r, ||||||z
            \  }}} |||||z
  |          \  }}}|||fS )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.
        r   r,   z / z: z, zc:%d)clszd:%dc                 2    g | ]}|         |         z  S r#   r#   )r&   r<   CsM1s     r   r(   z=ratsimpmodprime.<locals>._ratsimpmodprime.<locals>.<listcomp>   %    ;;;qRURU];;;r)   c                 2    g | ]}|         |         z  S r#   r#   )r&   r<   DsM2s     r   r(   z=ratsimpmodprime.<locals>._ratsimpmodprime.<locals>.<listcomp>   rD   r)   T)r%   polys)r3   
particularr   c              3   "   K   | ]
}|d k    V  dS )r   Nr#   )r&   r4   s     r   r0   z<ratsimpmodprime.<locals>._ratsimpmodprime.<locals>.<genexpr>   s&      <<!qAv<<<<<<r)   zIdeal not prime?)total_degreeaddr   r   r6   r   r   sumr5   r3   r   r%   coeffsr7   valuessubsdictlistzip
ValueErrorr8   )aballsolNDcdstepsmaxdegboundngc_hatd_hatr   r:   solrB   rF   rC   rG   G_ratsimpmodprimer'   r   r    r>   testeds                   @@@@r   rf   z)ratsimpmodprime.<locals>._ratsimpmodprimeh   s   : !1!!ANN$4$44 	QJEEE!eun /	1v JJ1v1B1Bqqq!!!RRR4555#b'')u555B#b'')u555BbB;;;;;E#b''NN;;;<<chmM ME;;;;;E#b''NN;;;<<chmM ME E	AI-q#(R-!it5 5 5568A QSX&&&--//A%27t4@@@C 3<<szz||<<<<< JJsOOJJsOO
 FF4Sb1#R3r779J2K%L%L M MNNOOFF4Sb1#R3r779J2K%L%L M MNNOOCH%%CH%%6 9$%7888ueQR8999q5F? *$Rj\FQJEFAFA_ !eun /	b 19 	H++Aq&!QYGGLAq&++Aq&!e)QGGLAq&!V|r)   )r%   r,   )domainz*Looking for best minimal solution. Got: %sTFrI   c                     t          | d                                                   t          | d                                                   z   S )Nr   r,   )r6   terms)xs    r   <lambda>z!ratsimpmodprime.<locals>.<lambda>   s3    QqTZZ\\):):S1=N=N)N r)   )key)convert)r   r   )sympy.solvers.solversr    r   r   r   r
   r   rh   has_assoc_Field	get_fieldr   setr   r3   r%   r   r6   r8   rR   minis_Fieldclear_denomsr   qp)r   re   r   r   r3   argsnumdenomrH   rh   r\   r]   rY   newsolrb   rc   r:   ra   rd   cndnr   rf   r=   r'   r    r>   rg   s    ``                   @@@@@@r   r!   r!      s@   < ,+++++	
T""" ,,..JC,c5\A-=MMMMMM
ss    ZF G%%''

<vEG G 	G =<<<%)<<<UUFN N N N N N N$Z Z Z Z Z Z Z Z Z Z Z Z| #q#(#)
4
4
4Q
7CE1chci888;E $E	!!#####S#(3:...UCHSZ0X0X0XZ\^ ^LAq& PV P:S[[HIII#) 	> 	>E5!R%2$e<<<CMM5::c??EJJsOO<====6NNOOO1? t,,At,,ARQKKacEAacE?s   A A,+A,N)	itertoolsr   
sympy.corer   r   r   sympy.core.numbersr   sympy.polysr   r	   r
   r   r   sympy.polys.monomialsr   r   sympy.polys.polyerrorsr   r   sympy.utilities.miscr   r   r!   r#   r)   r   <module>r      s    3 3 3 3 3 3 * * * * * * * * * * ' ' ' ' ' ' Y Y Y Y Y Y Y Y Y Y Y Y Y Y 8 8 8 8 8 8 8 8 B B B B B B B B & & & & & &! ! !, +/5 ~ ~ ~ ~ ~ ~ ~r)   