
    Edf                        d dl 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 dl
mZ d d	lmZmZmZ d d
lmZ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#m$Z$m%Z%m&Z&m'Z' d dl m(Z(m)Z)m*Z*m+Z+m,Z,m-Z- d dl m.Z.m/Z/m0Z0m1Z1m2Z2 d dl m3Z3m4Z4m5Z5m6Z6 d dl m7Z7m8Z8m9Z9m:Z: d dl;m<Z<m=Z=m>Z>m?Z?m@Z@ d dlAmBZB d dlCmDZDmEZE d dlFmGZG d dlHmIZImJZJ d dlKmLZL d dlMmNZNmOZO d dlPmQZQ d dlRmSZSmTZTmUZUmVZVmWZWmXZX d dlYmZZZ d dl[m\Z\ d dl]m^Z^ d dl_m`Z` d d lambZb d d!lcmdZd d" Zei Zfd# Zg	 	 	 d-d'Zh G d( d)          Zid%aj G d* d+          Zk	 	 	 d-d,Zld%S ).    )DictList)permutations)reduce)Add)Basic)Mul)WildDummySymbol)sympify)RationalpiI)EqNe)S)ordered)iterfreeargs)expsincostancotasinatan)logsinhcoshtanhcothasinh)sqrterferfiliEi)besseljbesselybesselibesselk)hankel1hankel2jnyn)Absreimsignarg)LambertW)floorceiling)	Piecewise)	Heaviside
DiracDelta)collect)AndOr)uniq)quogcdlcmfactor_listcancelPolynomialError)itermonomials)root_factors)PolyRing)solve_lin_sys)construct_domain)	integratec                 f   t                      }|                     |          r| j        r| j        r|                    |            n| j        s| j        r3| j        D ]}|t          ||          z  }|                    |            n| j	        r|t          | j
        |          z  }| j        j        s^| j        j        r6|                    | j
        t          d| j        j                  z             n:|t          | j        |          | hz  z  }n| j        D ]}|t          ||          z  }|S )a  
    Returns a set of all functional components of the given expression
    which includes symbols, function applications and compositions and
    non-integer powers. Fractional powers are collected with
    minimal, positive exponents.

    Examples
    ========

    >>> from sympy import cos, sin
    >>> from sympy.abc import x
    >>> from sympy.integrals.heurisch import components

    >>> components(sin(x)*cos(x)**2, x)
    {x, sin(x), cos(x)}

    See Also
    ========

    heurisch
       )sethas_free	is_symbolis_commutativeaddis_Functionis_Derivativeargs
componentsis_Powbaser   
is_Integeris_Rationalr   q)fxresultgs       8lib/python3.11/site-packages/sympy/integrals/heurisch.pyrU   rU   ,   s=   , UUFzz!}} +; 	+1+ 	+JJqMMMM] 	+ao 	+V + +*Q***JJqMMMMX 
	+j+++F5# 95$ 9JJqvx157';';;<<<<j22aS88FV + +*Q***M    c           	          	 t           |          }n# t          $ r g }|t           | <   Y nw xY wt          |          |k     rG|                    t	          d| t          |          fz                       t          |          |k     G|d|         S )z*get vector of symbols local to this modulez%s%iN)_symbols_cacheKeyErrorlenappendr   )namenlsymss      r_   _symbolsri   _   s    %t$ % % %$t% e**q. ;eFdCJJ%7788::: e**q. ; !9s    ))FN   c	                    ddl m}	m}
 t          |           } |                     |          s| |z  S t          | ||||||||	  	        }t          |t                    s|S g }t           |
|                    D ]&}	 | |	|gd|f          z  }# t          $ r Y #w xY w|s|S t          t          |                    }g  |
|           D ]&}	  |	|gd|f          z  # t          $ r Y #w xY wfd|D             }|s|S t          |          dk    rJg }|D ]3}|                    d |                                D                        4 |	|d|f          |z   }g }|D ]}t          |                     |          ||||||||	  	        }t!          d |                                D              }t#          d	 |                                D              }|#t%          |                     |          |          }|                    ||f           t          |          dk    r*t          | ||||||||	  	        |f|d         d         dfg}n,|                    t          | ||||||||	  	        df           t)          | S )
a  
    A wrapper around the heurisch integration algorithm.

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

    This method takes the result from heurisch and checks for poles in the
    denominator. For each of these poles, the integral is reevaluated, and
    the final integration result is given in terms of a Piecewise.

    Examples
    ========

    >>> from sympy import cos, symbols
    >>> from sympy.integrals.heurisch import heurisch, heurisch_wrapper
    >>> n, x = symbols('n x')
    >>> heurisch(cos(n*x), x)
    sin(n*x)/n
    >>> heurisch_wrapper(cos(n*x), x)
    Piecewise((sin(n*x)/n, Ne(n, 0)), (x, True))

    See Also
    ========

    heurisch
    r   )solvedenomsT)dictexcludec                     g | ]}|v|	S  rq   ).0sslns0s     r_   
<listcomp>z$heurisch_wrapper.<locals>.<listcomp>   s"    ...!q~.A...r`   rL   c                 4    g | ]\  }}t          ||          S rq   r   rr   keyvalues      r_   ru   z$heurisch_wrapper.<locals>.<listcomp>   s$    JJJ:33JJJr`   c                 4    g | ]\  }}t          ||          S rq   rw   rx   s      r_   ru   z$heurisch_wrapper.<locals>.<listcomp>   s$    GGG
URU^^GGGr`   c                 4    g | ]\  }}t          ||          S rq   )r   rx   s      r_   ru   z$heurisch_wrapper.<locals>.<listcomp>   s$    III*#ur#u~~IIIr`   )sympy.solvers.solversrl   rm   r   rN   heurisch
isinstancer   r   NotImplementedErrorlistr>   rd   extenditemssubsr<   r=   rJ   re   r8   )r[   r\   rewritehintsmappingsretriesdegree_offsetunnecessary_permutations_try_heurischrl   rm   resslnsdeqssub_dictpairsexprcondgenericrt   s                       @r_   heurisch_wrapperr   m   sv   : 43333333

A::a== s

1a%7M+]< <Cc5!! 
 DVVC[[!!  	EE1#D1$7777DD" 	 	 	D	 
T

DEVAYY  	UUA3TA48888EE" 	 	 	D	....t...D 

4yy1} : 	L 	LHJJJJ9I9IJJJKKKKuStaT222T9E # #x((!WeXw%'?%' ' GGhnn6F6FGGGHII8H8HIIIJ 	1QVVH--a00DdD\"""" 5zzQ 
%1a%7+-E+- - &' (1+t$	& 	hq!WeXw+-E+- - #$ 	% 	% 	% es$   >B
BBC&&
C32C3c                   *    e Zd ZdZd Zd Zd Zd ZdS )BesselTablez~
    Derivatives of Bessel functions of orders n and n-1
    in terms of each other.

    See the docstring of DiffCache.
    c                     i | _         t          d          | _        t          d          | _        |                                  d S )Nrg   z)tabler   rg   r   _create_table)selfs    r_   __init__zBesselTable.__init__   s;    
ssr`   c                     | j         | j        | j        }}}t          t          t
          t          fD ]L} ||dz
  |          | |||          z  |z  z
  |dz
   ||dz
  |          z  |z   |||          z
  f||<   Mt          } ||dz
  |          | |||          z  |z  z
  |dz
   ||dz
  |          z  |z   |||          z   f||<   t          } ||dz
  |           | |||          z  |z  z
  |dz
   ||dz
  |          z  |z   |||          z
  f||<   t          t          fD ]O} ||dz
  |          |dz    |||          z  |z  z
  |dz
   ||dz
  |          z  |z   |||          z
  f||<   Pd S )NrL   )r   rg   r   r(   r)   r,   r-   r*   r+   r.   r/   )tr   rg   r   r[   s        r_   r   zBesselTable._create_table   s   gqsAC!q7GW5 	5 	5A!A#q		Aaa1ggIaK/1aa!Qii)AAaGG35E!HH Aac1II!!Aq''	!+qS!!AaC))OA%!Q/1aQqsAYYJ11Q771,qS!!AaC))OA%!Q/1a b 	5 	5A!A#q		QqS!!Aq''M!O31aa!Qii)AAaGG35E!HH	5 	5r`   c                     || j         v rL| j         |         \  }}| j        |f| j        |fg}|                    |          |                    |          fS d S N)r   rg   r   r   )r   r[   rg   r   diff0diff1repls          r_   diffszBesselTable.diffs   sb    < 	871:LE5S!HqsAh'DJJt$$ejj&6&677	8 	8r`   c                     || j         v S r   )r   )r   r[   s     r_   haszBesselTable.has   s    AG|r`   N)__name__
__module____qualname____doc__r   r   r   r   rq   r`   r_   r   r      sZ           5 5 5"8 8 8    r`   r   c                       e Zd ZdZd Zd ZdS )	DiffCacheau  
    Store for derivatives of expressions.

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

    The standard form of the derivative of a Bessel function of order n
    contains two Bessel functions of orders n-1 and n+1, respectively.
    Such forms cannot be used in parallel Risch algorithm, because
    there is a linear recurrence relation between the three functions
    while the algorithm expects that functions and derivatives are
    represented in terms of algebraically independent transcendentals.

    The solution is to take two of the functions, e.g., those of orders
    n and n-1, and to express the derivatives in terms of the pair.
    To guarantee that the proper form is used the two derivatives are
    cached as soon as one is encountered.

    Derivatives of other functions are also cached at no extra cost.
    All derivatives are with respect to the same variable `x`.
    c                 P    i | _         || _        t          st                      ad S d S r   )cacher\   _bessel_tabler   )r   r\   s     r_   r   zDiffCache.__init__  s1    
  	*'MMMMM	* 	*r`   c                    | j         }||v rnt          |d          rt                              |j                  s+t          |                    | j                            ||<   nj|j        \  }}t          	                    |j        ||          \  }}| 
                    |          }||z  ||<   ||z  ||                    |dz
  |          <   ||         S )NfuncrL   )r   hasattrr   r   r   rC   diffr\   rT   r   get_diff)r   r[   r   rg   r   d0d1dzs           r_   r   zDiffCache.get_diff  s    
: 
	*!V$$ 	*!!!&))	*affTVnn--E!HH6DAq"((A66FBq!!B"uE!H$&rEE!&&1a..!Qxr`   N)r   r   r   r   r   r   rq   r`   r_   r   r      s<         ,* * *    r`   r   c	                   ./0123456789:;< t          |           } |durL|                     t          t          t          t
          t          t          t          t          t          	  	        rdS |                               s| z  S | j        s|                               \  }	} nt          j        }	t           t"          t$          ft&          t(          t*          t,          ft.          i}
|r1|
                                D ]\  }}|                     ||          } n%|
                                D ]} | j        | r nd}t7          |           }t9                    5|4|st;          dg          }t;          dg          }t;          dg          }t=          |          D ]}|j        rOtA          |tB                    r|j"        d         #                    ||z  z            }||$                    tC          ||         ||         z  z            ||         ||         z  z  d||         z  z  tK          ||         d	z   tM          ||         ||         z  z            z  ||         z            z  z
  z             tA          |tN                    r]|j"        d         #                    |d
z  z            }|||         j(        r9|$                    tS          tU          ||                   z                       n9|$                    tW          tU          ||                    z                       |j"        d         #                    |d
z  z  |z  z   |z             }|q||         j(        r|$                    tU          tX          dz  ||          z            tO          ||         ||         d
z  d||         z  z  z
            z  tS          tU          ||                   z  ||         d
tU          ||                   z  z  z             z             n||         j-        r|$                    tU          tX          dz  ||          z            tO          ||         ||         d
z  d||         z  z  z
            z  tW          tU          ||                    z  ||         d
tU          ||                    z  z  z
            z             |j"        d         #                    |tM                    d
z  z            }|||         j(        ra|$                    tS          tU          ||                   tM                    z  d	d
tU          ||                   z  z  z                        ||         j-        rc|$                    tW          tU          ||                    tM                    z  d	d
tU          ||                    z  z  z
                       Z|j.        ru|j'        j/        rh|j'        j0        d
k    rW|j1        #                    |d
z  z  |z             }|||         j(        r||         j(        rB|$                    te          tU          ||         ||         z            z                       nO||         j-        rB|$                    tg          tU          ||          ||         z            z                       |j1        #                    |d
z  z  |z
            }|a||         j(        rS||         j(        rd	tU          ||         d
z  z  ||         z
            z  }tM          d
tU          ||                   z  tU          ||         d
z  z  ||         z
            z  d
||         z  z  z             tU          ||                   z  .|5j4        .<   |$                    .           H||         j-        r|$                    ||          d
z  tU          ||                    z  tk          tU          ||                    z  tU          ||         d
z  z  ||         z
            z            z             n|t=          |          z  }t=          |          D ](}|t7          56                    |                    z  })to          dtq          |                    /ts          tu          ts          tw          ty          fdtw          |/          D                                  d	                             7d 7D             }|:7d         d         k    sJ 7=                    d          g}t}          7          }n|pg }7fd4|D ]t7ts          7          77|z   745fd|D             }d |D             }t          /fd|D                       r(  4|           j@        / rt          /fd|          6 n u|st          | d||          }||	|z  S dS 6fd|D             8/8fd1/01fd0/13fd3i }|D ]}|j        rtA          |t&                    rd|d	 4|          d
z  z   <   3tA          |t.                    r#d|d	 4|          z   <   d|d	 4|          z
  <   ktA          |t                    rd| 4|          <    4|           ..D                                \  }} 36          } 3|          }t=          ts          |          |d         gz   ts          |                                          z             }|d         t          d |                                D              z  }/fd|||fD             } d| v rdS d | D             \  }}}||d         z   0|d	                   z  F                                :2fd2 2|           |t          ||          z   }"}!|!d	k    r:|"d	k    r4t          ty          t          /|!|"z   d	z
  |z                                 }#n0t          ty          t          /|!|"z   |z                                 }#to          dtq          |#                    9t          9fdt          |#          D              ;t=                      <ty          |          D ]D}$t          |$g/R  \  }%}&<$                    |%           |&D ]\  }'}(<$                    |'           Ed%./169:;<fd 	})t          d! /D                       r.jM        t=          /          z
  }*n[.N                                }+|+O                    t          tw          /d" /D                                           jM        |+jM        z  }*|*s |)d#          },|,
 |)            },n
 |)            },|,]|,Q                    |          }-t          |-          S                                }-|-j        r|-                              d	         }-|	|-z  S |dk    r t          | ||||d	z
  |$          }||	|z  S dS )&a,
  
    Compute indefinite integral using heuristic Risch algorithm.

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

    This is a heuristic approach to indefinite integration in finite
    terms using the extended heuristic (parallel) Risch algorithm, based
    on Manuel Bronstein's "Poor Man's Integrator".

    The algorithm supports various classes of functions including
    transcendental elementary or special functions like Airy,
    Bessel, Whittaker and Lambert.

    Note that this algorithm is not a decision procedure. If it isn't
    able to compute the antiderivative for a given function, then this is
    not a proof that such a functions does not exist.  One should use
    recursive Risch algorithm in such case.  It's an open question if
    this algorithm can be made a full decision procedure.

    This is an internal integrator procedure. You should use top level
    'integrate' function in most cases, as this procedure needs some
    preprocessing steps and otherwise may fail.

    Specification
    =============

     heurisch(f, x, rewrite=False, hints=None)

       where
         f : expression
         x : symbol

         rewrite -> force rewrite 'f' in terms of 'tan' and 'tanh'
         hints   -> a list of functions that may appear in anti-derivate

          - hints = None          --> no suggestions at all
          - hints = [ ]           --> try to figure out
          - hints = [f1, ..., fn] --> we know better

    Examples
    ========

    >>> from sympy import tan
    >>> from sympy.integrals.heurisch import heurisch
    >>> from sympy.abc import x, y

    >>> heurisch(y*tan(x), x)
    y*log(tan(x)**2 + 1)/2

    See Manuel Bronstein's "Poor Man's Integrator":

    References
    ==========

    .. [1] http://www-sop.inria.fr/cafe/Manuel.Bronstein/pmint/index.html

    For more information on the implemented algorithm refer to:

    .. [2] K. Geddes, L. Stefanus, On the Risch-Norman Integration
       Method and its Implementation in Maple, Proceedings of
       ISSAC'89, ACM Press, 212-217.

    .. [3] J. H. Davenport, On the Parallel Risch Algorithm (I),
       Proceedings of EUROCAM'82, LNCS 144, Springer, 144-157.

    .. [4] J. H. Davenport, On the Parallel Risch Algorithm (III):
       Use of Tangents, SIGSAM Bulletin 16 (1982), 3-6.

    .. [5] J. H. Davenport, B. M. Trager, On the Parallel Risch
       Algorithm (II), ACM Transactions on Mathematical
       Software 11 (1985), 356-362.

    See Also
    ========

    sympy.integrals.integrals.Integral.doit
    sympy.integrals.integrals.Integral
    sympy.integrals.heurisch.components
    TNa)ro   bcr   rL         r\   c                 V    g | ]%}|d                                         d         |f&S )r   rL   )as_independent)rr   r   r\   s     r_   ru   zheurisch.<locals>.<listcomp>  s5    ???A!A$

a
 
 
#Q	'???r`   c                     i | ]\  }}||	S rq   rq   rr   kvs      r_   
<dictcomp>zheurisch.<locals>.<dictcomp>  s    ,,,DAq1a,,,r`   c                 .    |                                S r   )r   )r   mappings    r_   _substitutezheurisch.<locals>._substitute  s    yy!!!r`   c                 L    g | ] }                      |                    !S rq   )r   )rr   r^   r   dcaches     r_   ru   zheurisch.<locals>.<listcomp>  s/    BBBa++fooa0011BBBr`   c                 B    g | ]}|                                 d          S )rL   )as_numer_denom)rr   r^   s     r_   ru   zheurisch.<locals>.<listcomp>  s)    999Q1##%%a(999r`   c              3   ,   K   | ]} |j          V  d S r   )is_polynomial)rr   hVs     r_   	<genexpr>zheurisch.<locals>.<genexpr>  s,      33qq"333333r`   c                      t          | |gR  S r   )rA   )prZ   r   s     r_   <lambda>zheurisch.<locals>.<lambda>  s    Aq1 r`   )r   r   r   c                 4    g | ]}t          |z            S rq   )rC   )rr   r^   denoms     r_   ru   zheurisch.<locals>.<listcomp>  s#    ///1veAg///r`   c                 L     t           fdt                    D              S )Nc                 F    g | ]\  }}|                     |          z  S rq   )r   )rr   r   r   r   s      r_   ru   z1heurisch.<locals>._derivation.<locals>.<listcomp>  s+    @@@1a!&&))m@@@r`   )r   zip)r   r   numerss   `r_   _derivationzheurisch.<locals>._derivation  s,    @@@@FA@@@AAr`   c                 F   D ]}|                      |          s |           t          j        urm|                     |                                          \  }} |          t          ||                    |                                                    z  c S | S r   )r   r   Zeroas_poly	primitiver@   r   as_expr)r   yr   rZ   r   
_deflationr   s       r_   r   zheurisch.<locals>._deflation  s     	A 	AA5588 {1~~QV+ Ayy||--//1!z!}}SAFF1II%6%6%>%>%@%@@@@@A r`   c           
         D ]I}|                      |          s 	|          t          j        ur|                     |                                          \  }}|                                }t          | 	|          |          }t          |t          ||                    |          |          |          } 
|          }|                    |          	                                dk    r|d         ||d         z  fc S  
t          ||z                      }|d         |d         z  |z  |d         |d         z  fc S Kt          j        | fS )Nr   rL   )r   r   r   r   r   r   r@   r?   r   degreerC   One)r   r   r   rZ   r   rs   c_splitq_splitr   r   	_splitters           r_   r   zheurisch.<locals>._splitter!  sW    	H 	HA5588 {1~~QV+ Hyy||--//1IIKK;;q>>1--3q!&&))Q//33#)A,,99Q<<&&((A- 8#AJGAJ7777#)F1q5MM22
71:-a/GAJ1FGGGGH" qzr`   Fc                     g | ]	\  }}||
S rq   rq   r   s      r_   ru   zheurisch.<locals>.<listcomp>N  s!    @@@$!QQ@A@@@r`   c                 $    g | ]} |j          S rq   )r   )rr   r   r   s     r_   ru   zheurisch.<locals>.<listcomp>O  s!    3331A333r`   c                 6    g | ]}|                                 S rq   )total_degree)rr   r   s     r_   ru   zheurisch.<locals>.<listcomp>U  s"    444Q  444r`   c                 R   | j         rp| j        j        rb| j        j        dk    rR| j        j        dk    r| j        j        | j        j        z   dz
  S t          | j        j        | j        j        z             S dS | j        s'| j        r t          fd| j        D                       S dS )NrL   r   c                 &    g | ]} |          S rq   rq   )rr   r   	_exponents     r_   ru   z/heurisch.<locals>._exponent.<locals>.<listcomp>c  s!    777!1777r`   )	rV   r   rY   rZ   r   absis_AtomrT   max)r^   r   s    r_   r   zheurisch.<locals>._exponentY  s    8 	u  QUW\ 57Q; 257QUW,q00quw0111q 	qv 	7777qv7778881r`   Ac                 ,    g | ]\  }}|         |z  S rq   rq   )rr   imonomialpoly_coeffss      r_   ru   zheurisch.<locals>.<listcomp>p  s6     / / /Ax #1~h. / / /r`   c                 	   t                      }t                      }| dk    rt          %          }nt                    }t                      }t          %          D ]W}|t          t          |                    z  }t          |          D ]&}t          t          |||                     }||z  } Xg g }
}	t          |          D ]}t	          |t
          d          }|                    t
          t          j                  }|r|                    t          j	        t          j                  }|
                    t
                    s|
                    t
                    r|                    ||f           |                    |           |r|                                \  }}|| f|v rs|                    || f           |                                r| }|                    ||z  ||z  z              |                    t          ||z                       n |                    |t
          |z  z              |t!          dt#          |                    }t!          dt#          |                    }t%          t'          t)          t          |          |                              D ]I\  }} |j
         r:"                    |           |	                    |t-          |          z             Jt%          t'          t)          t          |          |                              D ]<\  }} |j
         r-"                    |           |
                    ||z             =$#z  t/          |	 z   t/          |
 z   }  |          !z  z
  }|                                d         }t          "          t                    z  t                      fd	  |           t3          d	
          \  }}n# t4          $ r Y d S w xY wt7          "|          }t7          |          }	 |                    |          }n# t:          $ r t4          w xY wt=          |                                |d          }|d S |                     |                               tC          t)          "t          j        gt#          "          z                                S )NQ)filterF)evaluateBCr   c                     | j         s| j        rd S | v rd S  | j         s                    |            d S | j        s| j        s| j        r$t          t          | j	                             d S t          r   )rX   rY   rN   rQ   is_Addis_MulrV   r   maprT   rD   )r   find_non_symsnon_symssymss    r_   r  z3heurisch.<locals>._integrate.<locals>.find_non_syms  s     &$"2 & 	&"T]D) &T""""" & &t{ &S	2233333 &%r`   T)field)_raw)"rM   r   r   rF   r;   r   getr   r   r   r   rQ   removepopcould_extract_minus_signr   ri   rd   reversedr   r   re   r   r   r   rI   rD   rG   	from_expr
ValueErrorrH   coeffsxreplacern   )&r  atansr   irreduciblessetVpolyzVr   rs   log_part	atan_partmr   r\   r   r   r   r   	candidater   	raw_numerground_
coeff_ringringnumersolutionr  r  r  Fr   r   r   r   
poly_denom	poly_part
reducibless&                              @@@r_   
_integratezheurisch.<locals>._integrate{  s    C< 
	z??LLq66D55L
++  CT 2 2333   ALq???@@A A%L ")L)) 	* 	*Da%000Aa  A *EE!%((5588 quuQxx 		1a&!!!##D))) 
	*99;;DAqA2w% *a!W%%%--// A  1qs+++		$qs))$$$$  QqS)))  
	* S#l++,,S#e**%%  S)>)>%B%B C CDD 	/ 	/GD!tx| /""1%%%CII...S%;%; < <== 	+ 	+GD!tx| +""1%%%  T*** j(3>9COK	I&&..$$&&q)	 ;#a&&(55	& 	& 	& 	& 	& 	& 	&	?M)$$$ )>>>IFAA  	 	 	44	
 k622
:&&	"NN9--EE 	" 	" 	"!!	" %HHH 	C4%%h//88Sqvhs;/?/?&?@@AAC C Cs   7P 
P%$P%	Q Q1c              3   @   K   | ]}t          |t                    V  d S r   )r   r   rr   r  s     r_   r   zheurisch.<locals>.<genexpr>  s,      
,
,Q:a  
,
,
,
,
,
,r`   c              3   2   K   | ]}t                      V  d S r   )r   r'  s     r_   r   zheurisch.<locals>.<genexpr>  s&      ,@,@UWW,@,@,@,@,@,@r`   r   )r   r   r   r   r   r   )Tr   r   r0   r1   r2   r3   r9   r:   r6   r7   r4   rN   r   r   r   r   r   r   r   r   r   r   r!   r    r   r   keysrU   r   r
   rM   rR   r   r&   rT   matchrQ   r'   r   r   is_positiver%   r#   r$   r   is_negativerV   rY   rZ   rW   r"   r   r   r   r   ri   rd   r   r  r   r   r
  r   allis_rational_functionr   r~   r5   r   r	   r   r   tuplerE   r   	enumeraterB   free_symbolsas_dummyr  rn   r   rC   expand)=r[   r\   r   r   r   r   r   r   r   indeprewritables
candidatesruletermsr   r   r   r^   MdFrev_mappingr   rm   r]   specialtermPr   u_splitv_splitpolysrs   polifiedr   r   monomsr  coefffactorsfactmulr%  	more_freeFdr   	antiderivr!  r   r   r   r   r   r   r   r   r   r   r   r"  r#  r$  s=    `                                            @@@@@@@@@@@@@@@r_   r~   r~   '  s   f 	

A
 D  55b"dIz5'3OO 	F::a== s
8 ##A&&qq 
c3	tTDK
   + 1 1 3 3 	, 	,J		*d++AA	, &**,, 	 	Jquj!  Gq!Eq\\F C  B	 S1#&&&AS1#&&&AS1#&&&AZZ :W :W= 9W!!R(( "WF1IOOAadF33 ~!IIq"QqT!QqT'\*:*:ad1ad7lbQRSTQUg=VWY[\]^[_`a[acfghijgklmopqroslsgsctctZtuvwxuyZyWzWz=z*z'{}}}
 $As++ WF1IOOAadF33 > t/ > %		$tAaDzz!|*<*< = = = = %		#dAaD5kk!m*<*< = = =F1IOOAadFQqSL1,<== U t/ U %		$r!tadU|*<*<S1!aQRSTUVSWQWHXAX=Y=Y*Y*.tAaDzz!|adAd1Q4jjL>Q/Q*R*R+S !T !T !T !T!"1!1 U %		$r!tadU|*<*<S1!aQRSTUVSWQWHXAX=Y=Y*Y*-dAaD5kk!madAdAaD5kkM>R.R*S*S+T !U !U !U F1IOOAc!ffaiK88 W t/ V %		$tAaDzz#a&&/@1aQqT

lCS/S*T*T U U U t/ W %		#dAaD5kk#a&&.@1aaPQdUmCT.T*U*U V V VX Wu( WQUW\ WFLL1a4!44 DQqT-= D t/ D %		%QqT!A$Y0A*B*B C C C C!"1!1 D %		$tQqTE!A$J/?/?/A*B*B C C CFLL1a4!44 WQqT-= W t/ W%&tAaDAI!,<'='=%=$'$qt**T!A$q!t)ad:J5K5K(KaPQRSPTfUVh(V$W$WX\]^_`]aXbXb$b24Q %		!!"1!1 W %		1Q4%'$!u++*=+/adUAd1Q419qQRtCS>T>T0T+U+U+V !W !W !Ws:Wz SZZEZZ 3 3FOOA..222 	c%jj!!A 8Dg????UA???'A 'A "B C CCDF G G H HG,,G,,,K Br{1~""""$+KKOO#4 ((#;#Ar " " " " "   w--44BBBBB5BBB99%9993333F33333 	8[A8[]^8_ 	5555v>>EE 	$aD)AC C CF  $V|#t///////FB B B B B B	 	 	 	 	 	 	      0 G 2 2 	2$$$ 249KK--q0011D$'' 216KK---.16KK---..D(++ 2-1D))*AADAqiGillGW'!*.gllnn1E1EEFFE
S@@'--//@@@AAA3333Aq	333Hx t 54(444GAq!gaj.::gaj#9#99BBDDJ     9Q<<SAYYqA1u IQ Iw}QA	M0IJJKKLLw}QA0EFFGGHH3F,,K / / / /$V,,/ / / 0I J ! !$T.A...wu  	! 	!ID#NN4    	!hC hC hC hC hC hC hC hC hC hC hC hC hCT 
,
,!
,
,
,,, -NSVV+		ZZ\\KKS,@,@a,@,@,@%A%A B B  R_-	  :c?? 	$!z||H:<< MM+..	9%%,,..	 	7!0033A6IYa< 	$aXwe]dgh]h  D\  ]  ]  ]F $V|#tr`   )FNNrj   r   NN)mtypingr   tDictr   	itertoolsr   	functoolsr   sympy.core.addr   sympy.core.basicr   sympy.core.mulr	   sympy.core.symbolr
   r   r   r   sympy.core.numbersr   r   r   sympy.core.relationalr   r   sympy.core.singletonr   sympy.core.sortingr   sympy.core.traversalr   sympy.functionsr   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   $sympy.functions.elementary.complexesr0   r1   r2   r3   r4   &sympy.functions.elementary.exponentialr5   #sympy.functions.elementary.integersr6   r7   $sympy.functions.elementary.piecewiser8   'sympy.functions.special.delta_functionsr9   r:   sympy.simplify.radsimpr;   sympy.logic.boolalgr<   r=   sympy.utilities.iterablesr>   sympy.polysr?   r@   rA   rB   rC   rD   sympy.polys.monomialsrE   sympy.polys.polyrootsrF   sympy.polys.ringsrG   sympy.polys.solversrH   sympy.polys.constructorrI   sympy.integrals.integralsrJ   rU   rb   ri   r   r   r   r   r~   rq   r`   r_   <module>rh     s   & & & & & & & & " " " " " "             " " " " " "       1 1 1 1 1 1 1 1 1 1 $ $ $ $ $ $ . . . . . . . . . . ( ( ( ( ( ( ( ( " " " " " " & & & & & & - - - - - - ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? > > > > > > > > > > > > > > > > 3 3 3 3 3 3 3 3 3 3 3 3 3 3 > > > > > > > > > > > > 4 4 4 4 4 4 4 4 4 4 4 4 G G G G G G G G G G G G G G ; ; ; ; ; ; > > > > > > > > : : : : : : I I I I I I I I * * * * * * ' ' ' ' ' ' ' ' * * * * * * K K K K K K K K K K K K K K K K / / / / / / . . . . . . & & & & & & - - - - - - 4 4 4 4 4 4 / / / / / /, , ,^    NO?C#'^ ^ ^ ^@& & & & & & & &P . . . . . . . .` FG7;\ \ \ \ \ \r`   