
    wg,                         d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	m
Z
 ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZmZ d Zd ZddZd Zd Zy)zAThis module implements tools for integrating rational functions.     )Lambda)I)S)DummySymbolsymbols)log)atan)roots)cancel)RootSum)Poly	resultantZZc                 x   t        | t              r| \  }}n| j                         \  }}t        ||dd      t        ||dd      }}|j	                  |      \  }}}|j                  |      \  }}|j                  |      j                         }|j                  r||z  S t        |||      \  }}	|	j                         \  }
}t        |
|      }
t        ||      }|
j                  |      \  }}|||j                  |      j                         z   z  }|j                  s|j                  dd      }t        |t              st        |      }n|j                         }t        ||||      }|j                  d      }|dt        | t              r'| \  }}|j                         |j                         z  }n| j                         }||hz
  D ]  }|j                   rd} n d}t"        j$                  }|sS|D ]M  \  }	}|	j'                         \  }}	|t)        |t+        ||t-        |	j                               z        d      z  }O nh|D ]c  \  }	}|	j'                         \  }}	t/        |	|||      }|||z  }/|t)        |t+        ||t-        |	j                               z        d      z  }e ||z  }||z  S )aa  
    Performs indefinite integration of rational functions.

    Explanation
    ===========

    Given a field :math:`K` and a rational function :math:`f = p/q`,
    where :math:`p` and :math:`q` are polynomials in :math:`K[x]`,
    returns a function :math:`g` such that :math:`f = g'`.

    Examples
    ========

    >>> from sympy.integrals.rationaltools import ratint
    >>> from sympy.abc import x

    >>> ratint(36/(x**5 - 2*x**4 - 2*x**3 + 4*x**2 + x - 2), x)
    (12*x + 6)/(x**2 - 1) + 4*log(x - 2) - 4*log(x + 1)

    References
    ==========

    .. [1] M. Bronstein, Symbolic Integration I: Transcendental
       Functions, Second Edition, Springer-Verlag, 2005, pp. 35-70

    See Also
    ========

    sympy.integrals.integrals.Integral.doit
    sympy.integrals.rationaltools.ratint_logpart
    sympy.integrals.rationaltools.ratint_ratpart

    FT)	compositefieldsymboltreal)	quadratic)
isinstancetupleas_numer_denomr   r   div	integrateas_expris_zeroratint_ratpartgetr   r   as_dummyratint_logpartatomsis_extended_realr   Zero	primitiver   r   r	   log_to_real)fxflagspqcoeffpolyresultghPQrr   r   Lr   r#   elteps_Rs                         b/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/integrals/rationaltools.pyratintr;      s   D !U1!11T2DAVZ4[qA((1+KE1aeeAhGD!^^A&&(FyyV|!Q"DAqDAqQ
AQ
A558DAq
a!++a.((***F998S)&&)fA!A1aA&yy <!U#1	AGGI-	s{ ++ D
 ff F1{{}1wva3qyy{#3!34F FF
  J1{{}11a+=1HC76!Qs199;'7%78DJ JCJ 	#<    c           
         ddl m} t        | |      } t        ||      }|j                  |j	                               \  }}}|j                         }|j                         }t        d|      D 	cg c]  }	t        dt        ||	z
        z          }
}	t        d|      D 	cg c]  }	t        dt        ||	z
        z          }}	|
|z   }t        |
|t        |         }t        ||t        |         }| |j	                         |z  z
  ||j	                         |z  j                  |      z  z   ||z  z
  } ||j                         |      }|j                         j                  |      }|j                         j                  |      }t        ||j                         z  |      }t        ||j                         z  |      }||fS c c}	w c c}	w )a  
    Horowitz-Ostrogradsky algorithm.

    Explanation
    ===========

    Given a field K and polynomials f and g in K[x], such that f and g
    are coprime and deg(f) < deg(g), returns fractions A and B in K(x),
    such that f/g = A' + B and B has square-free denominator.

    Examples
    ========

        >>> from sympy.integrals.rationaltools import ratint_ratpart
        >>> from sympy.abc import x, y
        >>> from sympy import Poly
        >>> ratint_ratpart(Poly(1, x, domain='ZZ'),
        ... Poly(x + 1, x, domain='ZZ'), x)
        (0, 1/(x + 1))
        >>> ratint_ratpart(Poly(1, x, domain='EX'),
        ... Poly(x**2 + y**2, x, domain='EX'), x)
        (0, 1/(x**2 + y**2))
        >>> ratint_ratpart(Poly(36, x, domain='ZZ'),
        ... Poly(x**5 - 2*x**4 - 2*x**3 + 4*x**2 + x - 2, x, domain='ZZ'), x)
        ((12*x + 6)/(x**2 - 1), 12/(x**2 - x - 2))

    See Also
    ========

    ratint, ratint_logpart
    r   )solveab)domain)sympy.solvers.solversr>   r   	cofactorsdiffdegreeranger   strr   quocoeffsr   subsr   )r(   r0   r)   r>   uvr8   nmiA_coeffsB_coeffsC_coeffsABHr/   rat_partlog_parts                      r:   r   r   |   s   @ ,Q
AQ
Akk!&&(#GAq!	
A	
A271+?QsSQZ'(?H?271+?QsSQZ'(?H?("HXqH.AXqH.A	AFFHQJAFFHQJ++A...14A188:x(F			 A			 Aa		mQ'Ha		mQ'HX% @?s   0!G!!GNc                    t        | |      t        ||      }} |xs t        d      }|| |j                         t        ||      z  z
  }}t        ||d      \  }}t        ||d      }|sJ d|d|d       i g }	}|D ]  }
|
||
j	                         <    d	 }|j                         \  }} |||       |D ]  \  }}|j                         \  }}|j	                         |k(  r|	j                  ||f       A||   }t        |j                         |d
      }|j                  d      \  }} |||       |D ]2  \  }}|j                  t        |j                  |      |z  |            }4 |j                  |      t        j                  g}}|j                         dd D ]P  }|j                  |j                         }||z  j#                  |      }|j                  |j%                                R t        t'        t)        t+        |j-                         |                  |      }|	j                  ||f        |	S )an  
    Lazard-Rioboo-Trager algorithm.

    Explanation
    ===========

    Given a field K and polynomials f and g in K[x], such that f and g
    are coprime, deg(f) < deg(g) and g is square-free, returns a list
    of tuples (s_i, q_i) of polynomials, for i = 1..n, such that s_i
    in K[t, x] and q_i in K[t], and::

                           ___    ___
                 d  f   d  \  `   \  `
                 -- - = --  )      )   a log(s_i(a, x))
                 dx g   dx /__,   /__,
                          i=1..n a | q_i(a) = 0

    Examples
    ========

    >>> from sympy.integrals.rationaltools import ratint_logpart
    >>> from sympy.abc import x
    >>> from sympy import Poly
    >>> ratint_logpart(Poly(1, x, domain='ZZ'),
    ... Poly(x**2 + x + 1, x, domain='ZZ'), x)
    [(Poly(x + 3*_t/2 + 1/2, x, domain='QQ[_t]'),
    ...Poly(3*_t**2 + 1, _t, domain='ZZ'))]
    >>> ratint_logpart(Poly(12, x, domain='ZZ'),
    ... Poly(x**2 - x - 2, x, domain='ZZ'), x)
    [(Poly(x - 3*_t/8 - 1/2, x, domain='QQ[_t]'),
    ...Poly(-_t**2 + 16, _t, domain='ZZ'))]

    See Also
    ========

    ratint, ratint_ratpart
    r   T)
includePRSF)r   zBUG: resultant(z, z) cannot be zeroc                     | j                   r7| dk  dk(  r.|d   \  }}| j                  |j                        }||z  |f|d<   y y y )Nr   T)r$   as_polygens)csqfr1   kc_polys        r:   _include_signz%ratint_logpart.<locals>._include_sign   sM    1q5T/q6DAqYYqvv&FvXq[CF #2r<   )r   )all   N)r   r   rD   r   rE   sqf_listr&   appendLCrH   gcdinvertr   OnerI   r[   r\   remr   dictlistzipmonoms)r(   r0   r)   r   r?   r@   resr9   R_maprU   r4   ra   Cres_sqfr,   rO   r8   r1   h_lcr]   h_lc_sqfjinvrI   r-   Ts                             r:   r"   r"      s   L 1:tAqzqA	U3ZAa!&&(41:%%qAq!-FC
sA
'C@1a@@321E ahhj! JAw!W 1{{}188:?HHaVaA.D--D-1KAx!X&  01EE$quuQx{A./0 ++a.155'CAB +chh/YOOA&aiik*+
 T$s188:v678!<AHHaV14 Hr<   c                    | j                         |j                         k  r| | }} | j                         } |j                         }| j                  |      \  }}|j                  rdt	        |j                               z  S |j                  |        \  }}}| |z  ||z  z   j                  |      }dt	        |j                               z  }|t        ||      z   S )a0  
    Convert complex logarithms to real arctangents.

    Explanation
    ===========

    Given a real field K and polynomials f and g in K[x], with g != 0,
    returns a sum h of arctangents of polynomials in K[x], such that:

                   dh   d         f + I g
                   -- = -- I log( ------- )
                   dx   dx        f - I g

    Examples
    ========

        >>> from sympy.integrals.rationaltools import log_to_atan
        >>> from sympy.abc import x
        >>> from sympy import Poly, sqrt, S
        >>> log_to_atan(Poly(x, x, domain='ZZ'), Poly(1, x, domain='ZZ'))
        2*atan(x)
        >>> log_to_atan(Poly(x + S(1)/2, x, domain='QQ'),
        ... Poly(sqrt(3)/2, x, domain='EX'))
        2*atan(2*sqrt(3)*x/3 + sqrt(3)/3)

    See Also
    ========

    log_to_real
       )	rE   to_fieldr   r   r
   r   gcdexrH   log_to_atan)	r(   r0   r+   r,   sr   r1   rK   rS   s	            r:   r|   r|     s    > 	xxzAHHJr11	

A	

A558DAqyyaiik"""''1"+1aqS1Q3YOOAd199;;q!$$$r<   c           
      R   ddl m} t        dt              \  }}| j	                         j                  ||t        |z  z   i      j                         }|j	                         j                  ||t        |z  z   i      j                         } ||t        d      }	 ||t        d      }
|	j                  t        j                  t        j                        |	j                  t        t        j                        }}|
j                  t        j                  t        j                        |
j                  t        t        j                        }}t        t        |||      |      }t        |d      }t        |      |j!                         k7  ry	t        j                  }|j#                         D ]  }t        |j                  ||i      |      }|s-t        |j                  ||i      |      }t        j                  }t        |d      }t        |      |j!                         k7  r y	g }|D ]Z  }||vs| |vs|j$                  s|j'                         r|j)                  |        =|j*                  rJ|j)                  |       \ |D ]  }|j                  ||||i      }|j-                  d
      dk7  r.t        |j                  ||||i      |      }t        |j                  ||||i      |      }|dz  |dz  z   j	                         }||t/        |      z  |t1        ||      z  z   z  }  t        |d      }t        |      |j!                         k7  ry	|j#                         D ]1  }||t/        | j	                         j3                  ||            z  z  }3 |S )aw  
    Convert complex logarithms to real functions.

    Explanation
    ===========

    Given real field K and polynomials h in K[t,x] and q in K[t],
    returns real function f such that:
                          ___
                  df   d  \  `
                  -- = --  )  a log(h(a, x))
                  dx   dx /__,
                         a | q(a) = 0

    Examples
    ========

        >>> from sympy.integrals.rationaltools import log_to_real
        >>> from sympy.abc import x, y
        >>> from sympy import Poly, S
        >>> log_to_real(Poly(x + 3*y/2 + S(1)/2, x, domain='QQ[y]'),
        ... Poly(3*y**2 + 1, y, domain='ZZ'), x, y)
        2*sqrt(3)*atan(2*sqrt(3)*x/3 + sqrt(3)/3)/3
        >>> log_to_real(Poly(x**2 - 1, x, domain='ZZ'),
        ... Poly(-2*y + 1, y, domain='ZZ'), x, y)
        log(x**2 - 1)/2

    See Also
    ========

    log_to_atan
    r   )collectzu,v)clsF)evaluater9   )filterNT)chopry   )sympy.simplify.radsimpr   r   r   r   xreplacer   expandr    r   ri   r%   r   r   r   lencount_rootskeysis_negativecould_extract_minus_signre   r   evalfr	   r|   rJ   )r1   r,   r)   r   r   rK   rL   rU   r3   H_mapQ_mapr?   r@   r]   dr9   R_ur/   r_urq   R_v
R_v_pairedr_vDrS   rT   ABR_qr4   s                                r:   r'   r'   G  s   B /5e$DAq			aQqS\*113A			aQqS\*113AAq5)EAq5)E99QUUAFF#UYYq!&&%9qA99QUUAFF#UYYq!&&%9qAYq!Q#A
#
C
3x1==?"VVFxxz &:QH%q) QZZC)1-A
 AAc"s8q}}&
 	+C*$#Z)???c&B&B&D%%sd+%%c*	+  	:C

AsAs+,AwwDw!Q&QZZCC 0115AQZZCC 0115AQ$A+&&(Bc#b'kCAq(9$999F	:5&:P #
C
3x1==?"XXZ 0!C		((A.///0 Mr<   )N)__doc__sympy.core.functionr   sympy.core.numbersr   sympy.core.singletonr   sympy.core.symbolr   r   r   &sympy.functions.elementary.exponentialr	   (sympy.functions.elementary.trigonometricr
   sympy.polys.polyrootsr   sympy.polys.polytoolsr   sympy.polys.rootoftoolsr   sympy.polysr   r   r   r;   r   r"   r|   r'    r<   r:   <module>r      sL    G &   " 6 6 6 9 ' ( + + +jZ<~Xv.%bfr<   