o
    oh                    @   sp  d 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 ddlmZ dd	lmZ dd
lmZ ddl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mZm Z  ddl!m"Z" ddl#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+ ddl,m-Z-m.Z. ddl/m0Z0 ddl1m2Z2 ddl3m4Z4m5Z5m6Z6m7Z7m8Z8 ddl9m:Z: ddl;m<Z< dd Z=G dd dZ>dd Z?G dd dZ@G d d! d!eAZBd"d# ZCd$d%d&d'ZDd(d) ZEdXd*d+ZFd,d- ZGdYd/d0ZHdZd1d2ZId3d4 ZJd5d6 ZKd7d8 ZLd9d: ZMd[d;d<ZNd[d=d>ZOd\d@dAZPdBdC ZQdDdE ZRdFdG ZSd[dHdIZTdJdK ZUd]dMdNZVdOdP ZWd[dQdRZXG dSdT dTe.ZY	U	.	Ld^dVdWZZd.S )_a  
The Risch Algorithm for transcendental function integration.

The core algorithms for the Risch algorithm are here.  The subproblem
algorithms are in the rde.py and prde.py files for the Risch
Differential Equation solver and the parametric problems solvers,
respectively.  All important information concerning the differential extension
for an integrand is stored in a DifferentialExtension object, which in the code
is usually called DE.  Throughout the code and Inside the DifferentialExtension
object, the conventions/attribute names are that the base domain is QQ and each
differential extension is x, t0, t1, ..., tn-1 = DE.t. DE.x is the variable of
integration (Dx == 1), DE.D is a list of the derivatives of
x, t1, t2, ..., tn-1 = t, DE.T is the list [x, t1, t2, ..., tn-1], DE.t is the
outer-most variable of the differential extension at the given level (the level
can be adjusted using DE.increment_level() and DE.decrement_level()),
k is the field C(x, t0, ..., tn-2), where C is the constant field.  The
numerator of a fraction is denoted by a and the denominator by
d.  If the fraction is named f, fa == numer(f) and fd == denom(f).
Fractions are returned as tuples (fa, fd).  DE.d and DE.t are used to
represent the topmost derivation and extension variable, respectively.
The docstring of a function signifies whether an argument is in k[t], in
which case it will just return a Poly in t, or in k(t), in which case it
will return the fraction (fa, fd). Other variable names probably come
from the names used in Bronstein's book.
    )GeneratorType)reduce)Lambda)Mul)ilcm)I)Pow)Ne)S)ordereddefault_sort_key)DummySymbollogexp)coshcothsinhtanh)	Piecewise)atansincostanacotcotasinacos   )	integrateIntegral)_symbols)PolynomialError)
real_rootscancelPolygcdreduced)RootSum)numbered_symbolsc           
         s   i }| D ]%}|  D ]\}}t|| }|jr |||f  n	q
|tjfg||< qi }|  D ]\}}ttdd |D  |  } fdd|D }	|	||< q0tt	|  dd dS )a  
    Rewrites a list of expressions as integer multiples of each other.

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

    For example, if you have [x, x/2, x**2 + 1, 2*x/3], then you can rewrite
    this as [(x/6) * 6, (x/6) * 3, (x**2 + 1) * 1, (x/6) * 4]. This is useful
    in the Risch integration algorithm, where we must write exp(x) + exp(x/2)
    as (exp(x/2))**2 + exp(x/2), but not as exp(x) + sqrt(exp(x)) (this is
    because only the transcendental case is implemented and we therefore cannot
    integrate algebraic extensions). The integer multiples returned by this
    function for each term are the smallest possible (their content equals 1).

    Returns a list of tuples where the first element is the base term and the
    second element is a list of `(item, factor)` terms, where `factor` is the
    integer multiplicative factor that must multiply the base term to obtain
    the original item.

    The easiest way to understand this is to look at an example:

    >>> from sympy.abc import x
    >>> from sympy.integrals.risch import integer_powers
    >>> integer_powers([x, x/2, x**2 + 1, 2*x/3])
    [(x/6, [(x, 6), (x/2, 3), (2*x/3, 4)]), (x**2 + 1, [(x**2 + 1, 1)])]

    We can see how this relates to the example at the beginning of the
    docstring.  It chose x/6 as the first base term.  Then, x can be written as
    (x/2) * 2, so we get (0, 2), and so on. Now only element (x**2 + 1)
    remains, and there are no other terms that can be written as a rational
    multiple of that, so we get that it can be written as (x**2 + 1) * 1.

    c                 S   s   g | ]
\}}|  d  qS r   )as_numer_denom).0_i r0   i/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/integrals/risch.py
<listcomp>o       z"integer_powers.<locals>.<listcomp>c                    s   g | ]
\}}||  fqS r0   r0   r-   r/   jcommon_denomr0   r1   r2   r   r3   c                 S   s   | d   S Nr   )sort_key)itemr0   r0   r1   <lambda>u   s    z integer_powers.<locals>.<lambda>key)
itemsr%   is_Rationalappendr
   Oner   r   sortediter)
exprstermstermtrmtrm_listanewterms	term_listnewtermnewmultsr0   r6   r1   integer_powers5   s&   )

rN   c                   @   s   e Zd ZdZdZd$ddZdd	 Zd
d Zdd Zdd Z	dd Z
dd Zedd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# ZdS )%DifferentialExtensiona  
    A container for all the information relating to a differential extension.

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

    The attributes of this object are (see also the docstring of __init__):

    - f: The original (Expr) integrand.
    - x: The variable of integration.
    - T: List of variables in the extension.
    - D: List of derivations in the extension; corresponds to the elements of T.
    - fa: Poly of the numerator of the integrand.
    - fd: Poly of the denominator of the integrand.
    - Tfuncs: Lambda() representations of each element of T (except for x).
      For back-substitution after integration.
    - backsubs: A (possibly empty) list of further substitutions to be made on
      the final integral to make it look more like the integrand.
    - exts:
    - extargs:
    - cases: List of string representations of the cases of T.
    - t: The top level extension variable, as defined by the current level
      (see level below).
    - d: The top level extension derivation, as defined by the current
      derivation (see level below).
    - case: The string representation of the case of self.d.
    (Note that self.T and self.D will always contain the complete extension,
    regardless of the level.  Therefore, you should ALWAYS use DE.t and DE.d
    instead of DE.T[-1] and DE.D[-1].  If you want to have a list of the
    derivations or variables only up to the current level, use
    DE.D[:len(DE.D) + DE.level + 1] and DE.T[:len(DE.T) + DE.level + 1].  Note
    that, in particular, the derivation() function does this.)

    The following are also attributes, but will probably not be useful other
    than in internal use:
    - newf: Expr form of fa/fd.
    - level: The number (between -1 and -len(self.T)) such that
      self.T[self.level] == self.t and self.D[self.level] == self.d.
      Use the methods self.increment_level() and self.decrement_level() to change
      the current level.
    )fxTDfafdTfuncsbacksubsextsextargscasescasetdnewfleveltsdummyNr   Fc              
      s  |rd|vr
t d|D ]
}t| |||  q|   dS |du s% du r)t d|dvr5t dt| || _ | _|| _|   d\}}	|du rQt| j	 v }|r}t
tttttttftttttfti}
|
 D ]\}}| j||| _qit| j| _nt fdd	| j	t
tttttD rtd
t }t }t }t }t }t }	 | jj| j  rnJ|s|	stdt| d | !|||||	\}}}}}	| "||\}}|dks|	s| #|}|du r| j| _|   d}q|dks|s| $|}	qt%| j| j&\| _'| _(|   dS )a  
        Tries to build a transcendental extension tower from ``f`` with respect to ``x``.

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

        If it is successful, creates a DifferentialExtension object with, among
        others, the attributes fa, fd, D, T, Tfuncs, and backsubs such that
        fa and fd are Polys in T[-1] with rational coefficients in T[:-1],
        fa/fd == f, and D[i] is a Poly in T[i] with rational coefficients in
        T[:i] representing the derivative of T[i] for each i from 1 to len(T).
        Tfuncs is a list of Lambda objects for back replacing the functions
        after integrating.  Lambda() is only used (instead of lambda) to make
        them easier to test and debug. Note that Tfuncs corresponds to the
        elements of T, except for T[0] == x, but they should be back-substituted
        in reverse order.  backsubs is a (possibly empty) back-substitution list
        that should be applied on the completed integral to make it look more
        like the original integrand.

        If it is unsuccessful, it raises NotImplementedError.

        You can also create an object by manually setting the attributes as a
        dictionary to the extension keyword argument.  You must include at least
        D.  Warning, any attribute that is not given will be set to None. The
        attributes T, t, d, cases, case, x, and level are set automatically and
        do not need to be given.  The functions in the Risch Algorithm will NOT
        check to see if an attribute is None before using it.  This also does not
        check to see if the extension is valid (non-algebraic) or even if it is
        self-consistent.  Therefore, this should only be used for
        testing/debugging purposes.
        rS   zUAt least the key D must be included with the extension flag to DifferentialExtension.Nz8Either both f and x or a manual extension must be given.r   z,handle_first must be 'log' or 'exp', not %s.)TTc                 3   s    | ]}|  V  qd S N)hasr-   r/   rQ   r0   r1   	<genexpr>   s    z1DifferentialExtension.__init__.<locals>.<genexpr>z1Trigonometric extensions are not supported (yet!)TzJCouldn't find an elementary transcendental extension for %s.  Try using a z)manual extension with the extension flag.r   r   ))
ValueErrorsetattr_auto_attrsstrrP   rQ   ra   resetr   atomsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r>   r^   rewriter%   anyNotImplementedErrorsetis_rational_functionrR   _rewrite_exps_pows_rewrite_logs	_exp_part	_log_partfrac_inr\   rT   rU   )selfrP   rQ   handle_firstra   	extensionrewrite_complexattrexp_new_extensionlog_new_extensionrewritables
candidatesruleexpspowsnumpowssympowslogssymlogsr0   re   r1   __init__   sx   "(

zDifferentialExtension.__init__c                 C   s&   || j vrtdt| t|f d S )Nz%s has no attribute %s)	__slots__AttributeErrorrepr)rw   r{   r0   r0   r1   __getattr__!  s   
z!DifferentialExtension.__getattr__c                    s  ddl m} dd  jtD }dd |D }  jdd |D 7  _ jt| _t| jt	 fdd}t| jt fd	d}t|t
| fd
d}t|t
|t
|  fdd}t|D ]}	|	}
t	|	j	t|	j }|	|v r|	j	jrtdt|	 t|	j j\}}||| }|du rň j|
|i _  j||
fg7  _ t|	jg}t| jt	 fdd}qi|\}}}t	|	j	t||  } j|	|i _n|	|vrqi|} j||
f  j|
|i _|| qi|||||fS )z:
        Rewrite exps/pows for better processing.
        r   
is_deriv_kc                 S   s$   g | ]}t |jtr|jjr|qS r0   )
isinstancebaser   r?   rd   r0   r0   r1   r2   :  s
    

z<DifferentialExtension._rewrite_exps_pows.<locals>.<listcomp>c                 S   s&   g | ]}||j j |j|j j  fqS r0   )r   r   rd   r0   r0   r1   r2   =  s    c                 S   s   g | ]\}}||fqS r0   r0   r4   r0   r0   r1   r2   ?      c                       | j j j o| j j j S rb   r   rq   rR   rc   r/   rw   r0   r1   r;   P      z:DifferentialExtension._rewrite_exps_pows.<locals>.<lambda>c                    r   rb   r   r   r   r0   r1   r;   S  r   c                    s   | j j j  S rb   )r   rc   rR   r   r   r0   r1   r;   V  s    c                    s   | j j j o| jj S rb   )r   rq   rR   r   
is_Integerr   r   r0   r1   r;   X  s    
z,Algebraic extensions are not supported (%s).Nc                    r   rb   r   r   r   r0   r1   r;   v  s   )prder   r^   rl   r   rW   xreplacedictupdate_setsr   rp   r   r   r   r?   ro   rj   rv   r\   ru   r@   )rw   r   r   r   r   r}   r   ratpowsratpows_replr/   oldnewbaseabasedAansuconstrL   r0   r   r1   rr   '  sZ   






z(DifferentialExtension._rewrite_exps_powsc                    s    j t}t|| fdd}t|| fdd}t|D ](}t|jd j}|| |jd j| } j 	||i _  j
||f qtt|td}||fS )z5
        Rewrite logs for better processing.
        c                    s$   | j d j j o| j d j j S r8   )argsrq   rR   rc   r   r   r0   r1   r;     s    z5DifferentialExtension._rewrite_logs.<locals>.<lambda>c                    s<   | j  j o| jd jo| jd jj j o| jd jj S r8   )rc   rR   r   is_Powr   rq   r   r   r   r   r0   r1   r;     s    r   r<   )r^   rl   r   r   r   r   r   r@   r   r   rW   rB   rp   r   )rw   r   r   rl   r/   lbaser   r0   r   r1   rs     s   

	
z#DifferentialExtension._rewrite_logsc                 C   sx   | j sdd | jD | _ | js| j d | _dd t| j| j D | _d| _| j | j | _| j| j | _| j| j | _dS )zB
        Set attributes that are generated automatically.
        c                 S      g | ]}|j qS r0   )genrd   r0   r0   r1   r2         z5DifferentialExtension._auto_attrs.<locals>.<listcomp>r   c                 S   s   g | ]	\}}t ||qS r0   )get_case)r-   r]   r\   r0   r0   r1   r2         N)	rR   rS   rQ   ziprZ   r_   r\   r]   r[   r   r0   r0   r1   ri     s   z!DifferentialExtension._auto_attrsc                    s  ddl m} d}d}dd |D }t|}|D ]8\}}|jdd d t|j\}	}
||	|
}|d	ur|\ }d
krQd|d
C }d
9 dd  D  dkr{jt|tt	dd  D   i_j fdd|D _qst
 dkrt	fdd D  jfdd|D _jttttjtfddjD _d} ntdt|j\}	}
|
tt|	j |	tt|
j  }|
d }|j|dd\}}| |  }tj_jj j| jd j|jjddtjjdd  jr*td}ntd} jt |t|!j"|g7  _jfdd|D _d}q|rVd	S |S )a  
        Try to build an exponential extension.

        Returns
        =======

        Returns True if there was a new extension, False if there was no new
        extension but it was able to rewrite the given exponentials in terms
        of the existing extension, and None if the entire extension building
        process should be restarted.  If the process fails because there is no
        way around an algebraic extension (e.g., exp(log(x)/2)), it will raise
        NotImplementedError.
        r   )is_log_deriv_k_t_radicalFc                 S   r   r0   r   rd   r0   r0   r1   r2     r   z3DifferentialExtension._exp_part.<locals>.<listcomp>c                 S   s   | d S )Nr   r0   r   r0   r0   r1   r;     s    z1DifferentialExtension._exp_part.<locals>.<lambda>r<   Nr   c                 S   s   g | ]	\}}|| fqS r0   r0   r4   r0   r0   r1   r2     r   c                 S      g | ]\}}|| qS r0   r0   r-   r   powerr0   r0   r1   r2     s    c                    s8   i | ]\}}t || t | td d  D   qS )c                 S   r   r0   r0   r   r0   r0   r1   r2     r   z>DifferentialExtension._exp_part.<locals>.<dictcomp>.<listcomp>)r   r   r-   expargp)r   r   r0   r1   
<dictcomp>  s
    
z3DifferentialExtension._exp_part.<locals>.<dictcomp>c                    s   g | ]
\}}||   qS r0   r0   )r-   rF   r   )nr0   r1   r2   
  r3   c                    s*   i | ]\}}t || t  |  qS r0   r   r   )r   radr0   r1   r     s
    
c                       g | ]}| j qS r0   re   r-   rP   r   r0   r1   r2         Tz+Cannot integrate over algebraic extensions.   includer   expandr/   c                    s    i | ]\}}t | j| qS r0   )r   r\   r   r   r0   r1   r   )  s     )#r   r   rN   sortrv   r\   r^   r   r   r   lenr   listr   reversedrR   rV   ro   
derivationr&   r%   as_exprnextr`   r@   rY   rX   rS   as_polyra   r   r   r   subsrQ   )rw   r   r   new_extensionrestartexpargsipargothersargaargdr   r   dargadargddargr/   r0   )r   r   r   r   rw   r1   rt     sx   



$zDifferentialExtension._exp_partc              
   C   s  ddl m} d}dd |D }t|D ]}t|| j\}}|||| }|dur>|\}	}
}t||
 }| jt||i| _qt|| j\}}|tt	|| j|  |tt	|| j|   }|d }|
 |
  }t| j| _| j| j | j| | jd | jt|
 | j| jdd	 | jrtd
}ntd
}|  jt|t|| j|g7  _| jt|| ji| _d}q|S )a;  
        Try to build a logarithmic extension.

        Returns
        =======

        Returns True if there was a new extension and False if there was no new
        extension but it was able to rewrite the given logarithms in terms
        of the existing extension.  Unlike with exponential extensions, there
        is no way that a logarithm is not transcendental over and cannot be
        rewritten in terms of an already existing extension in a non-algebraic
        way, so this function does not ever return None or raise
        NotImplementedError.
        r   r   Fc                 S   s   g | ]}|j d  qS )r   )r   rd   r0   r0   r1   r2   A  r   z3DifferentialExtension._log_part.<locals>.<listcomp>Nr   r   r   r/   T)r   r   r   rv   r\   r   r^   r   r   r&   r   r   r`   rR   r@   rY   rX   rS   r%   r   ra   r   r   rV   r   r   rQ   )rw   r   r   r   logargsr   r   r   r   r   r   r   rL   r   r   r   r/   r0   r0   r1   ru   0  s>   

$zDifferentialExtension._log_partc                 C   s$   | j | j| j| j| j| j| j| jfS )z
        Returns some of the more important attributes of self.

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

        Used for testing and debugging purposes.

        The attributes are (fa, fd, D, T, Tfuncs, backsubs,
        exts, extargs).
        )rT   rU   rS   rR   rV   rW   rX   rY   r   r0   r0   r1   _important_attrse  s   z&DifferentialExtension._important_attrsc                    s$    fdd j D } jjd|  S )Nc                    s*   g | ]}t t |ts|t |fqS r0   )r   getattrr   )r-   r{   r   r0   r1   r2   {  s    z2DifferentialExtension.__repr__.<locals>.<listcomp>z
(dict(%r)))r   	__class____name__)rw   rr0   r   r1   __repr__y  s   zDifferentialExtension.__repr__c                 C   s   | j jd| j| j| jf  S )Nz({fa=%s, fd=%s, D=%s}))r   r   rT   rU   rS   r   r0   r0   r1   __str__  s   zDifferentialExtension.__str__c                 C   s@   | j jD ]}t| |t||}}t|ts||ks dS qdS )NFT)r   r   r   r   r   )rw   otherr{   d1d2r0   r0   r1   __eq__  s   zDifferentialExtension.__eq__c                 C   sp   | j | _| j g| _td| j g| _d| _dg| _dg| _| jr't	dt
d| _nt	d| _g | _g | _| j| _dS )zD
        Reset self to an initial state.  Used by __init__.
        r   r   Nr\   )cls)rQ   r\   rR   r&   rS   r_   rX   rY   ra   r*   r   r`   rW   rV   rP   r^   r   r0   r0   r1   rk     s   

zDifferentialExtension.resetc                    s    fddt | jD S )aV  
        Parameters
        ==========

        extension : str
            Represents a valid extension type.

        Returns
        =======

        list: A list of indices of 'exts' where extension of
            type 'extension' is present.

        Examples
        ========

        >>> from sympy.integrals.risch import DifferentialExtension
        >>> from sympy import log, exp
        >>> from sympy.abc import x
        >>> DE = DifferentialExtension(log(x) + exp(x), x, handle_first='exp')
        >>> DE.indices('log')
        [2]
        >>> DE.indices('exp')
        [1]

        c                    s   g | ]
\}}| kr|qS r0   r0   )r-   r/   extry   r0   r1   r2     r3   z1DifferentialExtension.indices.<locals>.<listcomp>)	enumeraterX   )rw   ry   r0   r   r1   indices  s   zDifferentialExtension.indicesc                 C   sN   | j dkr	td|  j d7  _ | j| j  | _| j| j  | _| j| j  | _dS )a,  
        Increment the level of self.

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

        This makes the working differential extension larger.  self.level is
        given relative to the end of the list (-1, -2, etc.), so we do not need
        do worry about it when building the extension.
        r   zJThe level of the differential extension cannot be incremented any further.r   N)r_   rg   rR   r\   rS   r]   rZ   r[   r   r0   r0   r1   increment_level  s   
z%DifferentialExtension.increment_levelc                 C   sV   | j t| j krtd|  j d8  _ | j| j  | _| j| j  | _| j| j  | _dS )a,  
        Decrease the level of self.

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

        This makes the working differential extension smaller.  self.level is
        given relative to the end of the list (-1, -2, etc.), so we do not need
        do worry about it when building the extension.
        zJThe level of the differential extension cannot be decremented any further.r   N)	r_   r   rR   rg   r\   rS   r]   rZ   r[   r   r0   r0   r1   decrement_level  s   z%DifferentialExtension.decrement_level)NNr   FNN)r   
__module____qualname____doc__r   r   r   rr   rs   ri   rt   ru   propertyr   r   r   r   rk   r   r   r   r0   r0   r0   r1   rO   x   s&    -
wk"m5
rO   c                 C   s6   t | }||}|| }|tt|| t|S rb   )rp   intersectionupdater   filter)seqrl   funcsr   r0   r0   r1   r     s
   
r   c                   @   s,   e Zd ZdZdZdd Zdd Zdd Zd	S )
DecrementLevelzR
    A context manager for decrementing the level of a DifferentialExtension.
    DEc                 C   s
   || _ d S rb   r   )rw   r   r0   r0   r1   r     s   zDecrementLevel.__init__c                 C      | j   d S rb   )r   r   r   r0   r0   r1   	__enter__     zDecrementLevel.__enter__c                 C   r   rb   )r   r   )rw   exc_type	exc_value	tracebackr0   r0   r1   __exit__  r   zDecrementLevel.__exit__N)r   r   r   r   r   r   r   r   r0   r0   r0   r1   r     s    r   c                   @      e Zd ZdZdS )NonElementaryIntegralExceptionz
    Exception used by subroutines within the Risch algorithm to indicate to one
    another that the function being integrated does not have an elementary
    integral in the given differential field.
    Nr   r   r   r   r0   r0   r0   r1   r     s    	r   c                 C   sX   |  |\}}|||9 }|r| | kr||\}}|||   |}||fS )a  
    Extended Euclidean Algorithm, Diophantine version.

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

    Given ``a``, ``b`` in K[x] and ``c`` in (a, b), the ideal generated by ``a`` and
    ``b``, return (s, t) such that s*a + t*b == c and either s == 0 or s.degree()
    < b.degree().
    )
half_gcdexexquodegreediv)rI   bcr   gr.   r\   r0   r0   r1   gcdex_diophantine  s   r  Fr%   c                K   s   t | tr| \}}| |  } |   \}}|j|fi ||j|fi |}}|r7|j|dd\}}|du s?|du rGtd| |f ||fS )ar  
    Returns the tuple (fa, fd), where fa and fd are Polys in t.

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

    This is a common idiom in the Risch Algorithm functions, so we abstract
    it out here. ``f`` should be a basic expression, a Poly, or a tuple (fa, fd),
    where fa and fd are either basic expressions or Polys, and f == fa/fd.
    **kwargs are applied to Poly.
    Tr   Nz(Could not turn %s into a fraction in %s.)r   tupler   r,   r   r%   rg   )rP   r\   r%   kwargsrT   rU   r0   r0   r1   rv   )  s   
&rv   c                 C   s   t | |dd\}}|jstd| ||f ||\}}|j||dd}|rV|j}|| }	| |  }
|||	|	|
  }|||	 
| }||j||dd7 }|S )a  
    (Hackish) way to convert an element ``p`` of K[t, 1/t] to K[t, z].

    In other words, ``z == 1/t`` will be a dummy variable that Poly can handle
    better.

    See issue 5131.

    Examples
    ========

    >>> from sympy import random_poly
    >>> from sympy.integrals.risch import as_poly_1t
    >>> from sympy.abc import x, z

    >>> p1 = random_poly(x, 10, -10, 10)
    >>> p2 = random_poly(x, 10, -10, 10)
    >>> p = p1 + p2.subs(x, 1/x)
    >>> as_poly_1t(p, x, z).as_expr().subs(z, 1/x) == p
    True
    Tr  z$%s is not an element of K[%s, 1/%s].Fr   )rv   is_monomialr#   r  r   oner  	transformreplaceto_field
quo_groundLC)r   r\   zpapdt_part	remainderr   r  tpr   z_partr0   r0   r1   
as_poly_1tA  s   r  c                 C   s  |rd}nt d|j}|j}|r|jt|j kr|S |  |jdt|j|j d  }|jdt|j|j d  }t||D ]1\}}	| |	}
|
du sQ|rU| 	 }
|rc||	 |

|	 7 }qB||	 |

|		  |7 }qB|rzt|}|r|  |S )aC  
    Computes Dp.

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

    Given the derivation D with D = d/dx and p is a polynomial in t over
    K(x), return Dp.

    If coefficientD is True, it computes the derivation kD
    (kappaD), which is defined as kD(sum(ai*Xi**i, (i, 0, n))) ==
    sum(Dai*Xi**i, (i, 1, n)) (Definition 3.2.2, page 80).  X in this case is
    T[-1], so coefficientD computes the derivative just with respect to T[:-1],
    with T[-1] treated as a constant.

    If ``basic=True``, the returns a Basic expression.  Elements of D can still be
    instances of Poly.
    r   Nr   )r&   r\   r_   r   rR   r   rS   r   r   r   diffr%   r   )r   r   coefficientDbasicr   r\   rS   rR   r]   vpvr0   r0   r1   r   o  s,   
"r   c                 C   sd   | j |s| jrdS dS | t||jrdS | td|d  |jr'dS | |dkr0dS dS )	z
    Returns the type of the derivation d.

    Returns one of {'exp', 'tan', 'base', 'primitive', 'other_linear',
    'other_nonlinear'}.
    r   	primitiver   r   r   r   other_nonlinearother_linear)exprrc   is_oneremr&   is_zeror  )r]   r\   r0   r0   r1   r     s   r   Nc                 C   s  dd |j d|j D }|r|| td|j|  d}t| ||d}| jr,| |fS | j	|jsK| j
| |j
| 
|j}| |}||fS |js| | }	| | |j }
|	|
}||jdkrq| |fS t| |||d}|d |d | fS | |fS )a   
    Splitting factorization.

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

    Given a derivation D on k[t] and ``p`` in k[t], return (p_n, p_s) in
    k[t] x k[t] such that p = p_n*p_s, p_s is special, and each square
    factor of p_n is normal.

    Page. 100
    c                 S      g | ]}d | qS r+   r0   r-   rQ   r0   r0   r1   r2         zsplitfactor.<locals>.<listcomp>Nr   )domain)r  r   )rR   r_   r@   r&   r\   
get_domainr   r%  r"  rc   r   r'   r  r  r  r  splitfactor)r   r   r  r  kinvrA   Dpr   r   hr  q_splitr0   r0   r1   r+    s(   


r+  c              	   C   s   dd |j d|j D |j d|j  }|r|g}g }g }|  }| jr,| dffdfS |D ]>\}	}
|	j| t|	|||dj| |j}t|	|j}	t||j}|		|}|j
sb|||
f |j
sl|||
f q.t|t|fS )aO  
    Splitting Square-free Factorization.

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

    Given a derivation D on k[t] and ``p`` in k[t], returns (N1, ..., Nm)
    and (S1, ..., Sm) in k[t]^m such that p =
    (N1*N2**2*...*Nm**m)*(S1*S2**2*...*Sm**m) is a splitting
    factorization of ``p`` and the Ni and Si are square-free and coprime.
    c                 S   r&  r+   r0   r'  r0   r0   r1   r2     r(  z#splitfactor_sqf.<locals>.<listcomp>Nr   r0   )r  r  )rR   r_   sqf_list_includer%  r   r'   r   r\   r&   r  r#  r@   r	  )r   r   r  r  r  kkinvr
   Np_sqfpir/   SiNir0   r0   r1   splitfactor_sqf  s2   *
r7  c           
      C   s   t d|  |j}| |||} }| |\}}t||\}}t||j||j||j\}}	||j|	|j}}	|||f|	|ffS )ak  
    Canonical Representation.

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

    Given a derivation D on k[t] and f = a/d in k(t), return (f_p, f_s,
    f_n) in k[t] x k(t) x k(t) such that f = f_p + f_s + f_n is the
    canonical representation of f (f_p is a polynomial, f_s is reduced
    (has a special denominator), and f_n is simple (has a normal
    denominator).
    r   )r&   r  r\   mulr  r+  r  r   )
rI   r]   r   lqr   dndsr  r  r0   r0   r1   canonical_representation  s   (r=  c                 C   sL  t d|  |j}| |||} }t| ||\}}}|\} }t d|  |j}| |||} }t d|j}t d|j}t||}	t| |	 |j}
|	|
\}}|

|jdkrt|
|}t|
 | }|
	|\}}||}|	|
\}}t||j ||j| |j\}}||j||j}}t|||j}|	|\}}||j|||j } ||
 ||  }||
 }|j|dd\}}|}
|

|jdksd| 	|\}}|j|dd\}}|j|dd\}}||d  ||d   |d  }|d }|j|dd\}}||f||f||ffS )z
    Hermite Reduction - Mack's Linear Version.

    Given a derivation D on k(t) and f = a/d in k(t), returns g, h, r in
    k(t) such that f = Dg + h + r, h is simple, and r is reduced.

    r   r   Tr   )r&   r  r\   r8  r=  r   r'   r  r   r  r  r  r%   )rI   r]   r   r9  fpfsfngagddddmr<  r.   ddmdm2dmsds_ddm	ds_ddm_dmr  r  dbds_dmsr:  r   rrarrdr0   r0   r1   hermite_reduce"  sH   	


 rN  c                 C   s   t d|j}| |j|j|jkrW| |j|j|j d }t |j| |jt | |j ||j   |j}||7 }| t|| } | |j|j|jks|| fS )z
    Polynomial Reduction.

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

    Given a derivation D on k(t) and p in k[t] where t is a nonlinear
    monomial over k, return q, r in k[t] such that p = Dq  + r, and
    deg(r) < deg_t(Dt).
    r   r   )r&   r\   r  r]   r8  r   r  r   )r   r   r:  mq0r0   r0   r1   polynomial_reduceZ  s   "rQ  c           &      C   s"  |  dkrdS td|}td}|d| td|j}td|j}||| }	| |	t|| |j }
}t||}t|	|td|j\}}t||td|j\}}|}g g g }}}t	d|D ]"}t||}|
 |d  }|| |t||d  ||  qctd|id}t	d|D ]}t|||  |j|	|d   |
 }|}t||fd t||fd }}||}t	d|d D ]}||| || }q|t|
|dd|j |
t||dd|j  |t|
|dd|j  |
t||dd|j  }t|d |j|d  }||}
}|t||\}}t||j\} }!|  |j|! |j dkrt||j|d|   |||   }"|"}#||# |"|! | }#t| }$t|$D ]*}%|t|j|% ||  |j t|#|%|j }|t|j|% ||  |j }q`q|||fS )	a  
    Contribution of ``F`` to the full partial fraction decomposition of A/D.

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

    Given a field K of characteristic 0 and ``A``,``D``,``F`` in K[x] with D monic,
    nonzero, coprime with A, and ``F`` the factor of multiplicity n in the square-
    free factorization of D, return the principal parts of the Laurent series of
    A/D at all the zeros of ``F``.
    r   r  r   rS   r   r   T)r  )r  r"   r   insertr&   r\   quor   r  ranger   r@   rO   r%   r   r   r  r'   rv   r$  r$   r   eval)&rI   r]   Fr   r   Zr  delta_adelta_dEhahddFBr.   CF_storeV	DE_D_listH_listr5   r  DE_newzEhazEhdPaPdQr/   DhaDhdFfF_staraF_stardQBCHalphasalphar0   r0   r1   laurent_seriesp  sb   



$"

$
0"
rs  c                 C   s   d}| j |dd\} }| |\}}t||d|d\}}d}	|D ]%\}
}t|||
|	|\}}}t||d  }||urAd} |S |	d }	q |S )a  
    Compute the squarefree factorization of the denominator of f
    and for each Di the polynomial H in K[x] (see Theorem 2.7.1), using the
    LaurentSeries algorithm. Write Di = GiEi where Gj = gcd(Hn, Di) and
    gcd(Ei,Hn) = 1. Since the residues of f at the roots of Gj are all 0, and
    the residue of f at a root alpha of Ei is Hi(a) != 0, f is the derivative of a
    rational function if and only if Ei = 1 for each i, which is equivalent to
    Di | H[-1] for each i.
    Tr   r  r  r   r   F)r%   r  r7  rs  r'   r   )rI   r]   r   r  flagr.   r   NpSpr5   r   rX  rY  rp  r  r0   r0   r1   recognize_derivative  s   

rx  c                 C   s   |pt d}| j|dd\} }| |\}} t||j}t||}| ||  }|j|dd\}}t||}t||d|d\}	}
|
D ]\}}t|	|} t
dd | D sX dS qAdS )	a  
    There exists a v in K(x)* such that f = dv/v
    where f a rational function if and only if f can be written as f = A/D
    where D is squarefree,deg(A) < deg(D), gcd(A, D) = 1,
    and all the roots of the Rothstein-Trager resultant are integers. In that case,
    any of the Rothstein-Trager, Lazard-Rioboo-Trager or Czichowski algorithm
    produces u in K(x) such that du/dx = uf.
    r  Tr   
includePRSrt  c                 s   s    | ]}|j V  qd S rb   )r   )r-   r5   r0   r0   r1   rf     s    z+recognize_log_derivative.<locals>.<genexpr>F)r   r%   r  r&   r\   r   	resultantr7  r$   r   all)rI   r]   r   r  r.   pzDdr:  r   rv  rw  r   r0   r0   r1   recognize_log_derivative  s   


r  Tc              	      s  pt d| j|dd\} }|  d|  | d|  } }dd  jd j D  jd j  }| jrBg dfS | |\}} t	 j
}t| }| ||  }	| j
| j
krp|j|	dd\}
}n	|	j|dd\}
}i g }}|D ]}||| < qt	|
}
t|
 dd	\}}|D ]\}}|| j
krt	| }|||f q||}|du rqt	| j
  j
dd
}|jdd}|D ]\} }t	| j
dd
t	t| || g|R   j
}qt	| }|rJt	| j
  j
ddd}|jdd
|tjg}}| dd D ]}t|||j |gd }||  q"t	ttt|  | j
}|||f qt! fdd|D  }||fS )aE  
    Lazard-Rioboo-Rothstein-Trager resultant reduction.

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

    Given a derivation ``D`` on k(t) and f in k(t) simple, return g
    elementary over k(t) and a Boolean b in {True, False} such that f -
    Dg in k[t] if b == True or f + h and f + h - Dg do not have an
    elementary integral over k(t) for any h in k<t> (reduced) if b ==
    False.

    Returns (G, b), where G is a tuple of tuples of the form (s_i, S_i),
    such that g = Add(*[RootSum(s_i, lambda z: z*log(S_i(z, t))) for
    S_i, s_i in G]). f - Dg is the remaining integral, which is elementary
    only if b == True, and hence the integral of f is elementary only if
    b == True.

    f - Dg is not calculated in this function because that would require
    explicitly calculating the RootSum.  Use residue_reduce_derivation().
    r  Tr   r   c                 S   r&  r+   r0   r'  r0   r0   r1   r2     r(  z"residue_reduce.<locals>.<listcomp>Nry  rt  field)r|  F)r  r   c                 3   s*    | ]\}}t |  jV  qd S rb   )r%   r   rc   r\   )r-   r/   r.   r   r  r0   r1   rf   =  s   ( z!residue_reduce.<locals>.<genexpr>)"r   r%   r  
mul_groundr  rR   r_   r%  r  r&   r\   r   r  r{  r7  monicr@   getr   r0  r  r'   invertr
   rA   coeffsr(   gensr   r   r   r   monomsrn   )rI   r]   r   r  r  r1  r.   r}  r~  r:  r   RR_maprp  r/   rv  rw  r   r.  h_lch_lc_sqfr5   invr  coeffLr  r0   r  r1   residue_reduce  sT   .*



&r  c                    sJ   t dttt jt fdd jD tfdd| D S )zR
    Converts the tuple returned by residue_reduce() into a Basic expression.
    r/   c                    r   r0   re   r   r   r0   r1   r2   H  r   z+residue_reduce_to_basic.<locals>.<listcomp>c              
   3   sJ    | ] }t |d  t  t|d   i V  qdS r   r   N)r)   r   r   r   r   r   r-   rI   )r/   r   r  r0   r1   rf   J  s    $z*residue_reduce_to_basic.<locals>.<genexpr>)r   r   r   r   rR   rV   sumrp  r   r  r0   )r   r/   r   r  r1   residue_reduce_to_basicB  s
   (r  c                    s&   t dtt fdd| D S )z
    Computes the derivation of an expression returned by residue_reduce().

    In general, this is a rational function in t, so this returns an
    as_expr() result.
    r/   c              
   3   sX    | ]'}t |d  tt|d    |d   V  qdS r  )r)   r   r   r   r   r   r  r   r/   r  r0   r1   rf   W  s    z,residue_reduce_derivation.<locals>.<genexpr>)r   r
   r  r  r0   r  r1   residue_reduce_derivationN  s   
r  c              	   C   s  t d|j}t d|j}| j|js|| dfS ddlm} 	 | j|js+|| dfS t|j|j|j	d  \}}t
|B |  }t||j\}}	z|||	||fg|}
|
du r[t|
\\}}}W n tyw   || df Y W  d   S w W d   n1 sw   Y  | |j}|d |jt |j|d  |d  |j | |  |jt |j| |j  }| t|| } || }q)aH  
    Integration of primitive polynomials.

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

    Given a primitive monomial t over k, and ``p`` in k[t], return q in k[t],
    r in k, and a bool b in {True, False} such that r = p - Dq is in k if b is
    True, or r = p - Dq does not have an elementary integral over k(t) if b is
    False.
    r   Tr   )limited_integrateNF)r&   r\   r"  rc   r   r  rv   r]   rR   r_   r   r  r   r  r   r   r   )r   r   Zeror:  r  DtaDtbrI   aaadrvbabdr  rO  rP  r0   r0   r1   integrate_primitive_polynomial[  s<   



,(r  c                    s  |pt d}ttt jt fdd jD }t| | \}}}t|d |d  |d\}}	|	st| 	 |	  |d t
|d   |d t
|d    	 |d d 	   t| | }
tt|
| j}
|d 	 |d 	  |t| | |
|	fS t|d 	 |d 	  t| | |d 	 |d 	   }| j}t| \}}
}	|d 	 |d 	  |	  |t| | }|	stt|
	 | j}
nt|
	 }
||
|	fS )a[  
    Integration of primitive functions.

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

    Given a primitive monomial t over k and f in k(t), return g elementary over
    k(t), i in k(t), and b in {True, False} such that i = f - Dg is in k if b
    is True or i = f - Dg does not have an elementary integral over k(t) if b
    is False.

    This function returns a Basic expression for the first argument.  If b is
    True, the second argument is Basic expression in k to recursively integrate.
    If b is False, the second argument is an unevaluated Integral, which has
    been proven to be nonelementary.
    r  c                    r   r0   re   r   r   r0   r1   r2     r   z'integrate_primitive.<locals>.<listcomp>r   r   r  r   )r   r   r   r   rR   rV   rN  r  r%   r   r   r  NonElementaryIntegralr   rQ   r  r   r\   r  )rI   r]   r   r  r   g1r.  r   g2r  r/   r   r:  retr0   r   r1   integrate_primitive  s@   ($

$

r  c              
   C   s  |j }|jt|j |j }td|j }td|j }d}| jr$|||fS ddlm} t| t| 	| | 	|d D ]}	|	sBq=|	dk rR| j
|dd|	 }
n
| j
|dd|	}
t|
|j dd\}}|j|dd\}}t|	|| }t||j dd\}}z|||t||j t||j |\}}t||f|dd	\}}W n ty   d}Y q=w || |t||	  |  }||9 }q=W d
   n1 sw   Y  |||fS )aG  
    Integration of hyperexponential polynomials.

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

    Given a hyperexponential monomial t over k and ``p`` in k[t, 1/t], return q in
    k[t, 1/t] and a bool b in {True, False} such that p - Dq in k if b is True,
    or p - Dq does not have an elementary integral over k(t) if b is False.
    r   r   T)rischDEFr   r  r   r  N)r\   r]   r  r&   r%  sympy.integrals.rder  r   rT  r  r   nthrv   r%   r   )r   r   r  t1dttqaqdr  r  r/   rI   r  r  iDtiDtaiDtdvavdr0   r0   r1   %integrate_hyperexponential_polynomial  s<   

 $

r  	piecewisec                    sv  |pt d}ttt jt fdd jD }t| | \}}}t|d |d  |d\}	}
|
st| 	 |	  |d t
|d   |d t
|d    	 |d d 	   t|	 | }tt|| j}|d 	 |d 	  |t|	 | ||
fS t|d 	 |d 	  t|	 | |d 	 |d 	   }t| j|}t| |\}}}
|dd}|d 	 |d 	  |t|	 | }|	 |}|	 |}|dkr
 j|jvr
|t|| t|dft||  jd| jd	f7 }n||| 7 }|
s6||t
|  |t
|   	 |d 	   }tt|| j}|||
fS )
ap  
    Integration of hyperexponential functions.

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

    Given a hyperexponential monomial t over k and f in k(t), return g
    elementary over k(t), i in k(t), and a bool b in {True, False} such that
    i = f - Dg is in k if b is True or i = f - Dg does not have an elementary
    integral over k(t) if b is False.

    This function returns a Basic expression for the first argument.  If b is
    True, the second argument is Basic expression in k to recursively integrate.
    If b is False, the second argument is an unevaluated Integral, which has
    been proven to be nonelementary.
    r  c                    r   r0   re   r   r   r0   r1   r2     r   z.integrate_hyperexponential.<locals>.<listcomp>r   r   r  r   r  T)r   r   r   r   rR   rV   rN  r  r%   r   r   r  r  r   rQ   r  r  r\   r  r  free_symbolsr   r	   r    )rI   r]   r   r  condsr   r  r.  r   r  r  r/   r   ppr  r  r  qasqdsr0   r   r1   integrate_hyperexponential  sV   ($


" 

r  c                 C   sR   t | |\}}|jt|jd d |j}t|dd|   |j}||fS )ah  
    Integration of hypertangent polynomials.

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

    Given a differential field k such that sqrt(-1) is not in k, a
    hypertangent monomial t over k, and p in k[t], return q in k[t] and
    c in k such that p - Dq - c*D(t**2 + 1)/(t**1 + 1) is in k and p -
    Dq does not have an elementary integral over k(t) if Dc != 0.
    r   r   )rQ  r]   r  r&   r\   r  r   )r   r   r:  r   rI   r  r0   r0   r1   !integrate_hypertangent_polynomial/  s   r  c                    sD  |pt d}ttt jt fdd jD }t| | \}}}t|d |d  |d\}}	|	sJ|d  |d   	|t
| | |	fS t|d  |d   t| |  |d  |d     j}
t|
 \}}|j jrd}	nd}	t|d  |d   |  	|t
| | }||	fS )	a  
    Integration of nonlinear monomials with no specials.

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

    Given a nonlinear monomial t over k such that Sirr ({p in k[t] | p is
    special, monic, and irreducible}) is empty, and f in k(t), returns g
    elementary over k(t) and a Boolean b in {True, False} such that f - Dg is
    in k if b == True, or f - Dg does not have an elementary integral over k(t)
    if b == False.

    This function is applicable to all nonlinear extensions, but in the case
    where it returns b == False, it will only have proven that the integral of
    f - Dg is nonelementary if Sirr is empty.

    This function returns a Basic expression.
    r  c                    r   r0   re   r   r   r0   r1   r2   Y  r   z3integrate_nonlinear_no_specials.<locals>.<listcomp>r   r   r  FT)r   r   r   r   rR   rV   rN  r  r   r   r  r%   r  r   r\   rQ  r"  rc   )rI   r]   r   r  r   r  r.  r   r  r  r   q1q2r  r0   r   r1   integrate_nonlinear_no_specialsB  s6   (
(
r  c                   @   r   )r  a!  
    Represents a nonelementary Integral.

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

    If the result of integrate() is an instance of this class, it is
    guaranteed to be nonelementary.  Note that integrate() by default will try
    to find any closed-form solution, even in terms of special functions which
    may themselves not be elementary.  To make integrate() only give
    elementary solutions, or, in the cases where it can prove the integral to
    be nonelementary, instances of this class, use integrate(risch=True).
    In this case, integrate() may raise NotImplementedError if it cannot make
    such a determination.

    integrate() uses the deterministic Risch algorithm to integrate elementary
    functions or prove that they have no elementary integral.  In some cases,
    this algorithm can split an integral into an elementary and nonelementary
    part, so that the result of integrate will be the sum of an elementary
    expression and a NonElementaryIntegral.

    Examples
    ========

    >>> from sympy import integrate, exp, log, Integral
    >>> from sympy.abc import x

    >>> a = integrate(exp(-x**2), x, risch=True)
    >>> print(a)
    Integral(exp(-x**2), x)
    >>> type(a)
    <class 'sympy.integrals.risch.NonElementaryIntegral'>

    >>> expr = (2*log(x)**2 - log(x) - x**2)/(log(x)**3 - x**2*log(x))
    >>> b = integrate(expr, x, risch=True)
    >>> print(b)
    -log(-x + log(x))/2 + log(x + log(x))/2 + Integral(1/log(x), x)
    >>> type(b.atoms(Integral).pop())
    <class 'sympy.integrals.risch.NonElementaryIntegral'>

    Nr   r0   r0   r0   r1   r  q  s    .r  r   c                 C   s  t | } |pt| ||d|d}|j|j}}	t j}
t|jD ]}|j|j	sA|	j|j	sA|dksA|
  t||	f|j	\}}	q|j|	dd\}}	|dkrZt||	||d\}}}n*|dkrht||	|\}}}n|dkrt| |	  |jdd	}d}t j}ntd
|
|7 }
|r|
  t||j	\}}	q|
|j}
|jst|j|j|j}|s|
|7 }
|
  S t|tr|
|f  S |
df  S dS )a  
    The Risch Integration Algorithm.

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

    Only transcendental functions are supported.  Currently, only exponentials
    and logarithms are supported, but support for trigonometric functions is
    forthcoming.

    If this function returns an unevaluated Integral in the result, it means
    that it has proven that integral to be nonelementary.  Any errors will
    result in raising NotImplementedError.  The unevaluated Integral will be
    an instance of NonElementaryIntegral, a subclass of Integral.

    handle_first may be either 'exp' or 'log'.  This changes the order in
    which the extension is built, and may result in a different (but
    equivalent) solution (for an example of this, see issue 5109).  It is also
    possible that the integral may be computed with one but not the other,
    because not all cases have been implemented yet.  It defaults to 'log' so
    that the outer extension is exponential when possible, because more of the
    exponential case has been implemented.

    If ``separate_integral`` is ``True``, the result is returned as a tuple (ans, i),
    where the integral is ans + i, ans is elementary, and i is either a
    NonElementaryIntegral or 0.  This useful if you want to try further
    integrating the NonElementaryIntegral part using other algorithms to
    possibly get a solution in terms of special functions.  It is False by
    default.

    Examples
    ========

    >>> from sympy.integrals.risch import risch_integrate
    >>> from sympy import exp, log, pprint
    >>> from sympy.abc import x

    First, we try integrating exp(-x**2). Except for a constant factor of
    2/sqrt(pi), this is the famous error function.

    >>> pprint(risch_integrate(exp(-x**2), x))
      /
     |
     |    2
     |  -x
     | e    dx
     |
    /

    The unevaluated Integral in the result means that risch_integrate() has
    proven that exp(-x**2) does not have an elementary anti-derivative.

    In many cases, risch_integrate() can split out the elementary
    anti-derivative part from the nonelementary anti-derivative part.
    For example,

    >>> pprint(risch_integrate((2*log(x)**2 - log(x) - x**2)/(log(x)**3 -
    ... x**2*log(x)), x))
                                             /
                                            |
      log(-x + log(x))   log(x + log(x))    |   1
    - ---------------- + --------------- +  | ------ dx
             2                  2           | log(x)
                                            |
                                           /

    This means that it has proven that the integral of 1/log(x) is
    nonelementary.  This function is also known as the logarithmic integral,
    and is often denoted as Li(x).

    risch_integrate() currently only accepts purely transcendental functions
    with exponentials and logarithms, though note that this can include
    nested exponentials and logarithms, as well as exponentials with bases
    other than E.

    >>> pprint(risch_integrate(exp(x)*exp(exp(x)), x))
     / x\
     \e /
    e
    >>> pprint(risch_integrate(exp(exp(x)), x))
      /
     |
     |  / x\
     |  \e /
     | e     dx
     |
    /

    >>> pprint(risch_integrate(x*x**x*log(x) + x**x + x*x**x, x))
       x
    x*x
    >>> pprint(risch_integrate(x**x, x))
      /
     |
     |  x
     | x  dx
     |
    /

    >>> pprint(risch_integrate(-1/(x*log(x)*log(log(x))**2), x))
         1
    -----------
    log(log(x))

    T)rx   ra   rz   r   r   r   )r  r  F)rischzDOnly exponential and logarithmic extensions are currently supported.r   N)r
   rO   rT   rU   r  r   rZ   r"  rc   r\   r   rv   r%   r  r  r    r   rQ   ro   r   rW   r%  r  functionlimitsr   )rP   rQ   ry   rx   separate_integralrz   r  r   rT   rU   resultr[   r   r/   r  r0   r0   r1   risch_integrate  sF   l$
r  )FF)FN)FNFrb   )NT)Nr  )Nr   FNr  )[r   typesr   	functoolsr   sympy.core.functionr   sympy.core.mulr   sympy.core.intfuncr   sympy.core.numbersr   sympy.core.powerr   sympy.core.relationalr	   sympy.core.singletonr
   sympy.core.sortingr   r   sympy.core.symbolr   r   &sympy.functions.elementary.exponentialr   r   %sympy.functions.elementary.hyperbolicr   r   r   r   $sympy.functions.elementary.piecewiser   (sympy.functions.elementary.trigonometricr   r   r   r   r   r   r   r   	integralsr    r!   heurischr"   sympy.polys.polyerrorsr#   sympy.polys.polytoolsr$   r%   r&   r'   r(   sympy.polys.rootoftoolsr)   sympy.utilities.iterablesr*   rN   rO   r   r   	Exceptionr   r  rv   r  r   r   r+  r7  r=  rN  rQ  rs  rx  r  r  r  r  r  r  r  r  r  r  r  r  r0   r0   r0   r1   <module>   st    (C    x
.4

+&8
B

X
.1
4A
/2