
    g                         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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)debugdebugfc                     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        R/var/www/html/ai-engine/env/lib/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#          |                     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, https://dl.acm.org/doi/pdf/10.1145/1145768.1145809
        (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lenr5   allappend)nSmiir1   leading_monomialsr(   	staircases       @r   r@   z"ratsimpmodprime.<locals>.staircaseV   s    
 663J/c#(mm0D0DaHH 	 	BCMM!A  !	 & & & &$& & & & & :::::::YYq1u=M=MMMr*   c                 |   | |}}d}|                                  |                                 z   }r|dz
  }	n|}	||z   |	k    r>||fv rn6                    ||f            |           |          t          d||f           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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%s / %s: %s, %szc:%d)clszd:%dc              3   :   K   | ]}|         |         z  V  d S r/   r$   )r'   r>   CsM1s     r   r2   z<ratsimpmodprime.<locals>._ratsimpmodprime.<locals>.<genexpr>   /      ::aBqEBqEM::::::r*   c              3   :   K   | ]}|         |         z  V  d S r/   r$   )r'   r>   DsM2s     r   r2   z<ratsimpmodprime.<locals>._ratsimpmodprime.<locals>.<genexpr>   rF   r*   T)r&   polys)r5   
particularr   c              3   "   K   | ]
}|d k    V  dS )r   Nr$   )r'   r6   s     r   r2   z<ratsimpmodprime.<locals>._ratsimpmodprime.<locals>.<genexpr>   s&      <<!qAv<<<<<<r*   zIdeal not prime?)total_degreeaddr   r   r8   r   r   sumr7   r5   r   r&   coeffsr9   valuessubsdictlistzip
ValueErrorr:   )aballsolNDcdstepsmaxdegboundngc_hatd_hatr   r<   solrD   rH   rE   rI   G_ratsimpmodprimer(   r   r!   r@   testeds                   @@@@r   rh   z)ratsimpmodprime.<locals>._ratsimpmodprimeh   s   : !1!!ANN$4$44 	QJEEE!eunn1vJJ1v1B1B$q!Rn555#b'')u555B#b'')u555BbB:::::5R>>:::::CHrMK KE:::::5R>>:::::CHrMK K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%%66$%7888ueQR8999q5F??$Rj\FQJEFAFA_ !eunnb 199++Aq&!QYGGLAq&++Aq&!e)QGGLAq&!V|r*   )r&   r-   )domainz*Looking for best minimal solution. Got: %sTFrK   c                     t          | d                                                   t          | d                                                   z   S )Nr   r-   )r8   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   rj   has_assoc_Field	get_fieldr   setr   r5   r&   r   r   r8   r:   rT   minis_Fieldclear_denomsr   qp)r   rg   r   r   r5   argsnumdenomrJ   rj   r^   r_   r[   newsolrd   re   r<   rc   rf   cndnr   rh   r?   r(   r!   r@   ri   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[[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*   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*   