o
    *ήc:                    @   s  d Z ddlZddlmZmZ ddl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 dd
lmZmZ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!m"Z" ddl#m$Z$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/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9 ddl:m;Z;m<Z<m=Z= ddl>m?Z? ddl@mAZAmBZBmCZCmDZD ddlEmFZF ddlGmHZHmIZI ddlJmKZKmLZLmMZMmNZN ddlOmPZPmQZQmRZRmSZS ddlTmUZUmVZV ddlWmXZXmYZY ddlZm[Z[m\Z\m]Z]m^Z^m_Z_m`Z`maZambZbmcZcmdZdmeZe ddlfmgZg ddlhmiZimjZj ddlkmlZl d d!lmmnZn dd"lompZpmqZqmrZrmsZsmtZt dd#lumvZvmwZw dd$lxmyZy dd%lzm{Z| e'd&Z}d'd( Z~d)d* Zdd+lmZ ed,Zd-d. ZG d/d0 d0eZd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Zd=d> Zd?d@ ZdAdB ZdCdD Zi adEdF ZdGdH ZdIdJ Zd}dLdMZdNdO Zd~dQdRZdSdT Zd~dUdVZdWdX Zd~dYdZZd[d\ Zd]d^ Zd_d` Zdadb Zdcdd Zdaeed}dedfZd}dgdhZdidj Zdkdl Zdmdn Zedodp Zdqdr Zdsdt Zdudv Zdwdx Zed~dydzZd{d| ZdS )a  
Integrate functions by rewriting them as Meijer G-functions.

There are three user-visible functions that can be used by other parts of the
sympy library to solve various integration problems:

- meijerint_indefinite
- meijerint_definite
- meijerint_inversion

They can be used to compute, respectively, indefinite integrals, definite
integrals over intervals of the real line, and inverse laplace-type integrals
(from c-I*oo to c+I*oo). See the respective docstrings for details.

The main references for this are:

[L] Luke, Y. L. (1969), The Special Functions and Their Approximations,
    Volume 1

[R] Kelly B. Roach.  Meijer G Function Representations.
    In: Proceedings of the 1997 International Symposium on Symbolic and
    Algebraic Computation, pages 205-211, New York, 1997. ACM.

[P] A. P. Prudnikov, Yu. A. Brychkov and O. I. Marichev (1990).
    Integrals and Series: More Special Functions, Vol. 3,.
    Gordon and Breach Science Publisher
    N)DictTuple)SYMPY_DEBUG)SExpr)Add)cacheit)r   )factor_terms)expand
expand_mulexpand_power_baseexpand_trigFunctionMul)ilcmRationalpi)EqNe_canonical_coeff)default_sort_keyordered)DummysymbolsWild)sympify)	factorial)reimargAbssign
unpolarifypolarify
polar_liftprincipal_branchunbranched_argumentperiodic_argument)exp	exp_polarlog)ceiling)coshsinh_rewrite_hyperbolics_as_expHyperbolicFunctionsqrt)	Piecewisepiecewise_fold)cossinsincTrigonometricFunction)besseljbesselybesselibesselk)
DiracDelta	Heaviside)
elliptic_k
elliptic_e)erferfcerfiEiexpintSiCiShiChifresnelsfresnelc)gamma)hypermeijerg)SingularityFunction   )Integral)AndOrBooleanAtomNotBooleanFunction)cancelfactor)multiset_partitions)debugzc                    s6   t | } t| ddrt fdd| jD S | j  S )Nis_PiecewiseFc                 3   s     | ]}t |g R  V  qd S N)_has.0if @/tmp/pip-target-vg8gfxp4/lib/python/sympy/integrals/meijerint.py	<genexpr>R       z_has.<locals>.<genexpr>)r4   getattrallargshas)resrc   rd   rb   re   r^   M   s   
r^   c                    s4	  dd }t t|d\}tddd gdt  		tjddf
fd	d
	d'
fdd	}dd }|ddfg
d< G dd dt}t	 	 d   gg g dg	 td   t	dk t	 	 d   g gdgg 	 td   t	dk tt d   	 d   gg g dg	 td   t	dk t d  t 	 d   g gdgg 	 td   t	dk 	   d gg dgg 	   t t
|d t	   d gd d gdgd d g	 dtt d  td  t   tdk  	   	  dgg dgg 	 d  tt  t  dd   	fdd}|dd |dd |tjd |tjd  fdd}|dd |dd |tjd |tjd ttd	 g g dgg  t	g dgtjgddg	d d ttdd  t	g tjgdgtjtjg	d d ttdd  t	g g tjgdg	d d tt t	g g dgtjg	d d tt t	g g dgtddg	d d ttd  	fdd	fd d!|t	 td	  d |t	 t	d  d fd"d#}|t	 |d |t	 |ttjtddgg dgdg	 fg d |tt	 |ttttddgtjgdgdtjg	 fg d |t	|tj t tjtg dgddgg 	td fg d t	dgg tjgddg	d d ttd  t	g dgddgtjg	d d tt d  t	tjgg dgtddtddgtd	d  d 	tt d  t 	g tjdgddgtjtjg	d d ttd$  d  t!	g gd dgg 	 t"	dgg tjgdg	d dtt  t#	g dgdtjgg 	d dtt  t$	tjgg dgtddg	d  	tt  t%	dgg tddgdtddgtd 	d  d% tj t&	dgg tddgdtddgtd 	d  d% tj t'	g g d g d g	d d  t(	g d  d gd  d gd  d g	d d  t)	g d d gd g d d d g	d d t t*	g g d  d gg 	d d tj t+	tjtjgg dgdg	 tj t,	tjdtj gg dgdg	 tddd  d&S )(z8 Add formulae for the function -> meijerg lookup table. c                 S   s   t | tgdS )Nexclude)r   r[   nrd   rd   re   wildX      z"_create_lookup_table.<locals>.wildpqabcrp   c                 S   s   | j o| dkS Nr   )
is_Integerxrd   rd   re   <lambda>[   s    z&_create_lookup_table.<locals>.<lambda>)
propertiesTc	           	   
      s6     t| tg | |t|||||fg||f d S r]   )
setdefault_mytyper[   appendrN   )	formulaanapbmbqr    faccondhinttablerd   re   add^   s   
z!_create_lookup_table.<locals>.addc                    s$     t| tg | |||f d S r]   )rz   r{   r[   r|   )r}   instr   r   r   rd   re   addib   s   
z"_create_lookup_table.<locals>.addic                 S   s0   | t dgg g dgtf| t g dgdgg tfgS NrP   r   )rN   r[   )ard   rd   re   constantf   s   z&_create_lookup_table.<locals>.constantrd   c                   @   s   e Zd Zedd ZdS )z2_create_lookup_table.<locals>.IsNonPositiveIntegerc                 S   s   t |}|jdu r|dkS d S )NTr   )r#   ru   )clsr    rd   rd   re   evaln   s   
z7_create_lookup_table.<locals>.IsNonPositiveInteger.evalN)__name__
__module____qualname__classmethodr   rd   rd   rd   re   IsNonPositiveIntegerl   s    r   rP   r   )r      c                 S   s(   t tdd | | d dd|     S )Nr   rP   )r   r   )rr"   nurd   rd   re   A1   s   (z _create_lookup_table.<locals>.A1c                    s   t d  |   d  |   d d dd|   d  gg |  d g|  d gd  d|     | |  d S )Nr   rP   r1   r   sgn)r   r   r   btrd   re   tmpadd   s
   , *z$_create_lookup_table.<locals>.tmpaddr   c                    s   t t   |t  td     t   |   d|  | d  gd|  | d  gdtjgg t   d |    | |  d S )Nr   rP   r   )r2   r[   r   Halfr   )r   r   r   r   pqrd   re   r      s   D2(      c                    s@   |   }t j| t| tg dg|d  dg|d  g fgS r   )r   NegativeOner   rN   subsNrp   r   rd   re   	make_log1   s   "z'_create_lookup_table.<locals>.make_log1c                    s6   |   }t |tdg|d  g g dg|d  fgS r   )r   rN   r   r   rd   re   	make_log2   s   "z'_create_lookup_table.<locals>.make_log2c                    s    | |  S r]   rd   r   )r   r   rd   re   	make_log3   s   z'_create_lookup_table.<locals>.make_log3z3/2   NT)-listmapr   r[   r   Oner   r>   rL   rR   rU   r!   r6   r   r   r   r)   r%   r.   r   r-   r2   r5   r7   r+   rN   rD   ImaginaryUnitr   rF   rG   rH   rI   rE   rA   rB   rC   rJ   rK   r9   r:   r;   r<   r?   r@   )r   rq   cr   r   r   r   r   rd   )r   r   r   r   r   r   rp   r   r   r   r   re   _create_lookup_tableV   s   .
.::4
<6.



48**2  .("
248,
",,4>>.FD2(8r   )timethisrN   c                    s\    | j vrdS | jrt| fS  fdd| jD }g }|D ]}|t|7 }q|  t|S )z4 Create a hashable entity describing the type of f. rd   c                       g | ]}t | qS rd   )r{   r`   r   rv   rd   re   
<listcomp>2      z_mytype.<locals>.<listcomp>)free_symbolsis_Functiontyperj   r   sorttuple)rc   rw   typesrl   r   rd   rv   re   r{   +  s   

r{   c                   @   s   e Zd ZdZdS )_CoeffExpValueErrorzD
    Exception raised by _get_coeff_exp, for internal use only.
    N)r   r   r   __doc__rd   rd   rd   re   r   :  s    r   c                 C   sv   ddl m} t|| |\}}|s|tjfS |\}|jr,|j|kr'td||j	fS ||kr5|tj
fS td|  )a  
    When expr is known to be of the form c*x**b, with c and/or b possibly 1,
    return c, b.

    Examples
    ========

    >>> from sympy.abc import x, a, b
    >>> from sympy.integrals.meijerint import _get_coeff_exp
    >>> _get_coeff_exp(a*x**b, x)
    (a, b)
    >>> _get_coeff_exp(x, x)
    (1, 1)
    >>> _get_coeff_exp(2*x, x)
    (2, 1)
    >>> _get_coeff_exp(x**3, x)
    (1, 3)
    r   )powsimpzexpr not of form a*x**bzexpr not of form a*x**b: %s)sympy.simplifyr   r   as_coeff_mulr   Zerois_Powbaser   r)   r   )exprrw   r   r   mrd   rd   re   _get_coeff_expA  s   



r   c                    s"    fdd t  } | || |S )a  
    Find the exponents of ``x`` (not including zero) in ``expr``.

    Examples
    ========

    >>> from sympy.integrals.meijerint import _exponents
    >>> from sympy.abc import x, y
    >>> from sympy import sin
    >>> _exponents(x, x)
    {1}
    >>> _exponents(x**2, x)
    {2}
    >>> _exponents(x**2 + x, x)
    {1, 2}
    >>> _exponents(x**3*sin(x + x**y) + 1/x, x)
    {-1, 1, 3, y}
    c                    sV   | |kr| dg d S | jr| j|kr| | jg d S | jD ]} ||| q d S NrP   )updater   r   r)   rj   )r   rw   rl   argument_exponents_rd   re   r   v  s   
z_exponents.<locals>._exponents_set)r   rw   rl   rd   r   re   
_exponentsc  s   	r   c                    s    fdd|  tD S )zB Find the types of functions in expr, to estimate the complexity. c                    s   h | ]
} |j v r|jqS rd   )r   func)r`   erv   rd   re   	<setcomp>      z_functions.<locals>.<setcomp>)atomsr   )r   rw   rd   rv   re   
_functions  s   r   c                    s<   fdddD \ fdd t  } | | |S )ap  
    Find numbers a such that a linear substitution x -> x + a would
    (hopefully) simplify expr.

    Examples
    ========

    >>> from sympy.integrals.meijerint import _find_splitting_points as fsp
    >>> from sympy import sin
    >>> from sympy.abc import x
    >>> fsp(x, x)
    {0}
    >>> fsp((x-1)**3, x)
    {1}
    >>> fsp(sin(x+3)*x, x)
    {-3, 0}
    c                    s   g | ]	}t | gd qS )rm   )r   r`   rp   rv   rd   re   r         z*_find_splitting_points.<locals>.<listcomp>pqc                    sp   t | tsd S |   }|r&| dkr&||  |   d S | jr+d S | jD ]} || q.d S rt   )
isinstancer   matchr   is_Atomrj   )r   rl   r   r   compute_innermostr   r   rw   rd   re   r     s   

z1_find_splitting_points.<locals>.compute_innermostr   )r   rw   	innermostrd   r   re   _find_splitting_points  s
   
r   c           	      C   s   t j}t j}t j}t| } t| }|D ]S}||kr||9 }q||jvr)||9 }q|jrc||jjvrc|j	|\}}||fkrIt
|j	|\}}||fkrc|||j 9 }|tt||j dd9 }q||9 }q|||fS )aq  
    Split expression ``f`` into fac, po, g, where fac is a constant factor,
    po = x**s for some s independent of s, and g is "the rest".

    Examples
    ========

    >>> from sympy.integrals.meijerint import _split_mul
    >>> from sympy import sin
    >>> from sympy.abc import s, x
    >>> _split_mul((3*x)**s*sin(x**2)*x, x)
    (3**s, x*x**s, sin(x**2))
    Fr   )r   r   r   r   	make_argsr   r   r)   r   r   r   r#   r$   )	rc   rw   r   pogrj   r   r   r   rd   rd   re   
_split_mul  s(   







r   c                 C   sf   t | }g }|D ]'}|jr+|jjr+|j}|j}|dk r#| }d| }||g| 7 }q	|| q	|S )a   
    Return a list ``L`` such that ``Mul(*L) == f``.

    If ``f`` is not a ``Mul`` or ``Pow``, ``L=[f]``.
    If ``f=g**n`` for an integer ``n``, ``L=[g]*n``.
    If ``f`` is a ``Mul``, ``L`` comes from applying ``_mul_args`` to all factors of ``f``.
    r   rP   )r   r   r   r)   ru   r   r|   )rc   rj   gsr   rp   r   rd   rd   re   	_mul_args  s   
r   c                 C   sB   t | }t|dk rdS t|dkrt|gS dd t|dD S )a  
    Find all the ways to split ``f`` into a product of two terms.
    Return None on failure.

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

    Although the order is canonical from multiset_partitions, this is
    not necessarily the best order to process the terms. For example,
    if the case of len(gs) == 2 is removed and multiset is allowed to
    sort the terms, some tests fail.

    Examples
    ========

    >>> from sympy.integrals.meijerint import _mul_as_two_parts
    >>> from sympy import sin, exp, ordered
    >>> from sympy.abc import x
    >>> list(ordered(_mul_as_two_parts(x*sin(x)*exp(x))))
    [(x, exp(x)*sin(x)), (x*exp(x), sin(x)), (x*sin(x), exp(x))]
    r   Nc                 S   s    g | ]\}}t | t | fqS rd   r   )r`   rw   yrd   rd   re   r          z%_mul_as_two_parts.<locals>.<listcomp>)r   lenr   rY   )rc   r   rd   rd   re   _mul_as_two_parts  s   
r   c              
   C   s   dd }t t| jt| j }|d| j |d   }|dt |d | j   }|t|| j||| j	||| j
||| j|| j| |||   fS )zO Return C, h such that h is a G function of argument z**n and
        g = C*h. c                    s    fddt | t D S )z5 (a1, .., ak) -> (a1/n, (a1+1)/n, ..., (ak + n-1)/n) c                    s   g | ]
\}}||   qS rd   rd   )r`   r   ra   ro   rd   re   r     r   z/_inflate_g.<locals>.inflate.<locals>.<listcomp>)	itertoolsproductrange)paramsrp   rd   ro   re   inflate  s   z_inflate_g.<locals>.inflaterP   r   )r   r   r   r   r   r   deltarN   r~   aotherr   botherr   )r   rp   r   vCrd   rd   re   
_inflate_g
  s   r   c                 C   s6   dd }t || j|| j|| j|| jd| j S )zQ Turn the G function into one of inverse argument
        (i.e. G(1/x) -> G'(x)) c                 S      dd | D S )Nc                 S      g | ]}d | qS rP   rd   r   rd   rd   re   r         z'_flip_g.<locals>.tr.<locals>.<listcomp>rd   lrd   rd   re   tr  rr   z_flip_g.<locals>.trrP   )rN   r   r   r~   r   r   )r   r   rd   rd   re   _flip_g  s   .r   c                    s   |dk rt t| | S t|j t|j}t| |\}} | j}|dt d  d   tdd   }|    } fddt	 D }|t
| j| j| jt| j| |fS )a\  
    Let d denote the integrand in the definition of the G function ``g``.
    Consider the function H which is defined in the same way, but with
    integrand d/Gamma(a*s) (contour conventions as usual).

    If ``a`` is rational, the function H can be written as C*G, for a constant C
    and a G-function G.

    This function returns C, G.
    r   r   rP   r   c                    s   g | ]}|d    qS r   rd   r   r   rd   re   r   9      z"_inflate_fox_h.<locals>.<listcomp>)_inflate_fox_hr   r   r   r   r   r   r   r   r   rN   r~   r   r   r   r   )r   r   r   Dr[   bsrd   r   re   r  #  s   

&$r  c                 K   s0   t | |fi |}||jv rt| fi |S |S )z
    Return a dummy. This will return the same dummy if the same token+name is
    requested more than once, and it is not already in expr.
    This is for being cache-friendly.
    )_dummy_r   r   )nametokenr   kwargsdrd   rd   re   _dummy?  s   
r	  c                 K   s0   | |ft vrt| fi |t | |f< t | |f S )z`
    Return a dummy associated to name and token. Same effect as declaring
    it globally.
    )_dummiesr   )r  r  r  rd   rd   re   r  K  s   r  c                    s    t  fdd| ttD  S )z Check if f(x), when expressed using G functions on the positive reals,
        will in fact agree with the G functions almost everywhere c                 3   s    | ]} |j v V  qd S r]   )r   r`   r   rv   rd   re   rf   Y  s    z_is_analytic.<locals>.<genexpr>)anyr   r>   r!   )rc   rw   rd   rv   re   _is_analyticV  s    r  Tc                    s   r|  dd t} d t| ts| S tdtd\}tk tkftt	t
tkt	t
dt  tktt
t dftt	dt
 t tkt	dt
 t tktt
dftt	dt
 t tk t	dt
 t tktjftt	t
td  td kt	t
td  td ktt
dftt	t
td  td kt	t
td  td k tjftt	t
d d d tk tt	t
d d d ttjftt	t
d d d tk tdd d d  dtjftt	ttkt	ttd	t tj  tkttttj t  dftt	ttd kt	ttt tj  td kttttj t d  dftktk |kftd dd dk@ d dkftd dtt	t
t	 dk@ t	dkftdtt	t
t	 dk@ t	dkft	t
td k tt	t
tt	d  dk@ d dkfg}| jtt fd
d| j } d}|rd}t|D ]\}\}}|j| jkrqt| jD ]\}}	||jd jv r|	|jd d}
n
d}
|	|jd s qfdd|jd|
 |j|
d d  D }|g|D ]i}t| jD ]`\}}|v rNqC||krZ|g7  nJt|tr~|jd |kr~t|tr~|jd |jv r~|g7  n&t|tr|jd |krt|tr|jd |jv r|g7  nqCq<tt|d krqfddt| jD |g }tr|dvrtd| | j| } d} q|s߇fdd}|  dd |} trtd|  | S )a  
    Do naive simplifications on ``cond``.

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

    Note that this routine is completely ad-hoc, simplification rules being
    added as need arises rather than following any logical pattern.

    Examples
    ========

    >>> from sympy.integrals.meijerint import _condsimp as simp
    >>> from sympy import Or, Eq
    >>> from sympy.abc import x, y
    >>> simp(Or(x < y, Eq(x, y)))
    x <= y
    c                 S      | j S r]   is_Relational_rd   rd   re   rx   p      z_condsimp.<locals>.<lambda>Fzp q r)r   r   r   rP   c                    s
   t |  S r]   )	_condsimpr  )firstrd   re   rx     s   
 Tc                    s   g | ]}|  qS rd   r   r`   rw   )r   rd   re   r     r   z_condsimp.<locals>.<listcomp>Nc                    s   g | ]
\}}| vr|qS rd   rd   )r`   karg_)	otherlistrd   re   r     s    
)
r   r   r                        zused new rule:c                    s   | j dks
| jdkr| S | j}|t  }|s%|tt  }|sCt|trA|j	d j
sA|j	d tju rA|j	d dkS | S |  dkS )Nz==r   rP   )rel_oprhslhsr   r    r'   r%   r   r(   rj   is_polarr   Infinity)relLHSr   )r   r   rd   re   rel_touchup  s   z_condsimp.<locals>.rel_touchupc                 S   r  r]   r  r  rd   rd   re   rx     r  z_condsimp: ) replacer   r   rV   r   r   rS   r   rR   r!   r    r   r   falser   truer'   r*   r   r5   r2   r   r   r   rj   	enumerater   r   r   r   r   print)r   r  r   ruleschangeirulefrotorp   arg1num	otherargsarg2r  arg3newargsr)  rd   )r  r   r  r   r   re   r  \  s   
(0088:6 " $40B.








+
r  c                 C   s   t | tr| S t|  S )z Re-evaluate the conditions. )r   boolr  doit)r   rd   rd   re   
_eval_cond  s   
r<  Fc                 C   s"   t | |}|s|t dd }|S )z Bring expr nearer to its principal branch by removing superfluous
        factors.
        This function does *not* guarantee to yield the principal branch,
        to avoid introducing opaque principal_branch() objects,
        unless full_pb=True. c                 S   s   | S r]   rd   )rw   r   rd   rd   re   rx     s    z&_my_principal_branch.<locals>.<lambda>)r&   r*  )r   periodfull_pbrl   rd   rd   re   _my_principal_branch  s   
r?  c           	         s   t ||\}t |j|\} | }t||}| t |d   d    } fdd}|t||j||j||j||j	|| fS )z
    Rewrite the integral fac*po*g dx, from zero to infinity, as
    integral fac*G, where G has argument a*x. Note po=x**s.
    Return fac, G.
    rP   c                        fdd| D S )Nc                    s    g | ]}|d     d  qS r   rd   r   r   srd   re   r     r   z1_rewrite_saxena_1.<locals>.tr.<locals>.<listcomp>rd   r   rA  rd   re   r        z_rewrite_saxena_1.<locals>.tr)
r   r   
get_periodr?  r!   rN   r~   r   r   r   )	r   r   r   rw   r  r   r=  r   r   rd   rA  re   _rewrite_saxena_1  s   
 $rE  c              
   C   sl  | j }t| j|\}}tt| jt| jt| jt| jg\}}}}	||	krDdd }
t	t
|
| j|
| j|
| j|
| j|| |S dd | jD dd | jD  }t| }|dd | jD 7 }|dd | jD 7 }t| }t| j |	d | d	  |	| k}d
d }|d |d||||||	f  |dt| jt| jf  |dt| jt| jf  |d|||f  g }g }d|k||	k d|kg}d|kd|kt|	|d ttt|dt||d g}d|kt|	|g}tt|d	 d D ]}|ttt||d	|  t g7 }q|dktt||t k g}t|d|g}|r(g }|||fD ]}|t|| |  g7 }q-||7 }|d| |g}|rMg }tt|d|d |k||	ktt||t k g|R  g}||7 }|d| ||g}|r|g }t||	k d|k|dkttt||t g|R  g}|t||	d	 kt|dttt|dg|R  g7 }||7 }|d| g }|t||	t|dtt|dt|dg7 }|s||g7 }g }t| j| jD ]\}}||| g7 }q|tt| dk g7 }t| }||g7 }|d|g t|dktt||t k g}|s#||g7 }t| }||g7 }|d|g t| S )aV  
    Return a condition under which the mellin transform of g exists.
    Any power of x has already been absorbed into the G function,
    so this is just $\int_0^\infty g\, dx$.

    See [L, section 5.6.1]. (Note that s=1.)

    If ``helper`` is True, only check if the MT exists at infinity, i.e. if
    $\int_1^\infty g\, dx$ exists.
    c                 S   r   )Nc                 S   r   r   rd   r  rd   rd   re   r     r   z4_check_antecedents_1.<locals>.tr.<locals>.<listcomp>rd   r   rd   rd   re   r     rr   z _check_antecedents_1.<locals>.trc                 S      g | ]	}t | d k qS r   r   r`   r   rd   rd   re   r     r   z(_check_antecedents_1.<locals>.<listcomp>c                 S      g | ]
}d d t | k qS r   rG  r   rd   rd   re   r     r   c                 S   rF  r   rG  rH  rd   rd   re   r     r   c                 S   rI  r   rG  r   rd   rd   re   r     r   rP   r   c                  W   s   t |   d S r]   _debug)msgrd   rd   re   rZ   #     z#_check_antecedents_1.<locals>.debugz$Checking antecedents for 1 function:z*  delta=%s, eta=%s, m=%s, n=%s, p=%s, q=%sz  ap = %s, %sz  bq = %s, %sz"  cond_3=%s, cond_3*=%s, cond_4=%sr   z	  case 1:z	  case 2:z	  case 3:z  extra case:z  second extra case:)r   r   r   r   r   r   r~   r   r   _check_antecedents_1rN   r   r   rR   r   r   r   r   rU   r   r,   r   r!   r'   r   zipr   rS   )r   rw   helperr   etar  r   rp   r   r   r   tmpcond_3cond_3_starcond_4rZ   condscase1tmp1tmp2tmp3r  extrar   case2case3
case_extrarB  r   r   case_extra_2rd   rd   re   rN    s   0 $8&


*
6
,

 

rN  c                 C   s   ddl m} t| j|\}}d| }| jD ]
}|t|d 9 }q| jD ]}|td| d 9 }q#| jD ]}|td| d  }q3| jD ]
}|t|d  }qC|t	|S )a  
    Evaluate $\int_0^\infty g\, dx$ using G functions,
    assuming the necessary conditions are fulfilled.

    Examples
    ========

    >>> from sympy.abc import a, b, c, d, x, y
    >>> from sympy import meijerg
    >>> from sympy.integrals.meijerint import _int0oo_1
    >>> _int0oo_1(meijerg([a], [b], [c], [d], x*y), x)
    gamma(-a)*gamma(c + 1)/(y*gamma(-d)*gamma(b + 1))
    r   )	gammasimprP   )
r   r`  r   r   r   rL   r~   r   r   r#   )r   rw   r`  rQ  r  rl   r   r   rd   rd   re   	_int0oo_1p  s   



ra  c                    s  fdd}t |\}}t |j\}}	t |j\}}
|	dk dkr+|	 }	t|}|
dk dkr8|
 }
t|}|	jr>|
js@dS |	j|	j}}|
j|
j}}t|| || }|||  }|||  }t||\}}t||\}}||}||}| || 9 } t |j\}}t |j\}}|d | d  | t||    }  fdd}t	||j
||j||j||j| }t	|j
|j|j|j| }dd	lm} || dd
||fS )a  
    Rewrite the integral ``fac*po*g1*g2`` from 0 to oo in terms of G
    functions with argument ``c*x``.

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

    Return C, f1, f2 such that integral C f1 f2 from 0 to infinity equals
    integral fac ``po``, ``g1``, ``g2`` from 0 to infinity.

    Examples
    ========

    >>> from sympy.integrals.meijerint import _rewrite_saxena
    >>> from sympy.abc import s, t, m
    >>> from sympy import meijerg
    >>> g1 = meijerg([], [], [0], [], s*t)
    >>> g2 = meijerg([], [], [m/2], [-m/2], t**2/4)
    >>> r = _rewrite_saxena(1, t**0, g1, g2, t)
    >>> r[0]
    s/(4*sqrt(pi))
    >>> r[1]
    meijerg(((), ()), ((-1/2, 0), ()), s**2*t/4)
    >>> r[2]
    meijerg(((), ()), ((m/2,), (-m/2,)), t/4)
    c              	      s@   t | j\}}|  }t| j| j| j| jt|| |  S r]   )	r   r   rD  rN   r~   r   r   r   r?  )r   r   r   per)r>  rw   rd   re   pb  s
   z_rewrite_saxena.<locals>.pbr   TNrP   c                        fdd| D S )Nc                    s   g | ]}|  qS rd   rd   r   r)   rd   re   r     r   z/_rewrite_saxena.<locals>.tr.<locals>.<listcomp>rd   r   re  rd   re   r        z_rewrite_saxena.<locals>.tr	powdenestpolar)r   r   r   is_Rationalr   r   r   r   r!   rN   r~   r   r   r   r   rh  )r   r   g1g2rw   r>  rc  r  rB  b1b2m1n1m2n2taur1r2C1C2a1r   a2r   rh  rd   )r)   r>  rw   re   _rewrite_saxena  s>   ,r{  c           +         s(  t j|\	}t j|\}ttjtjtjtjg\}}ttjtjtjtjg\}}||  d  }||  d  }	j d  d j d  d    }
d    }t	| |  t
t   t	| |  t
t	   
td td	|||f  td|||	f  td|
|
f  fdd}| }tfd	d
jD  }tfdd
jD  }tfdd
jD  }tfdd
jD  }tfdd
jD  }tfdd
jD  }t
|
dtd       d      dk}t
|
dtd       d      dk}t
t	|t	 k }tt
t	|t	 }t
t|	t	 k }tt
t|	t	 }t||	  t	 tj }t| 	 }t|	  }|d| krtt|
d||	 dktt|dt   dk t   dk }n]dd }tt|
d|d |	 dkttt|d||tt   dk t|d}tt|
d|	d | dkttt|d||tt   dk t|d}t||}	 zш t
d    t  t
	d    t
  } t| dkdkrm| dk}!n	
fdd}"t|"dd|"dd ttt	dttdf|"ttd|"ttd ttt	dttdf|"dtt	|"dtt	 ttt	dttdf|"tttt	df}#| dktt| dt|#dt|dktt| dt|#dt|dkg}$t|$ }!W n ty   d}!Y nw |df|df|df|df|df|df|df|df|df|df|d f|d!f|d"f|d#f|!d$ffD ]\}%}&td%|& |% qGg   fd&d'}' t|| | | dk|jdu |	jdu |||||g7  |'d  ttt|d|	jdu 	jdu tdk ||||	g7  |'d  ttt|	d|jdu jdu tdk ||||	g7  |'d  tttt|dt|	d	jdu jdu tdk tdk t	|||g7  |'d  tttt|dt|	d	jdu jdu t dk t	|||g7  |'d  tk|jdu |jdu |	dk||||||
g7  |'d  tk |jdu |jdu |	dk||||||
g7  |'d  tk|jdu |	jdu |dk||||||
g7  |'d  tk |jdu |	jdu |dk||||||
g7  |'d  tktt|d|	dk	jdu tdk |||||g7  |'d  tk tt|d|	dk	jdu tdk |||||g7  |'d   ttk|dkt|	djdu tdk |||||g7  |'d!  ttk |dkt|	djdu tdk |||||g7  |'d"  tk k|dk|	dk|||||||g7  |'d#  tkk |dk|	dk|||||||g7  |'d$  tkk|dk|	dk|||||||||g7  |'d(  tk k |dk|	dk|||||||||g7  |'d)  tt|d|jdu |jdu |
jdu |||g7  |'d*  tt|d|jdu |jdu |
jdu |||g7  |'d+  tt|d|jdu |	jdu |
jdu |||g7  |'d,  tt|d|jdu |	jdu |
jdu |||g7  |'d-  tt|| d|jdu |	jdu |||||g7  |'d.  tt|| d|jdu |	jdu |||||g7  |'d/ t|dd0}(t|dd0}) t|)t|d|k |jdu ||||g7  |'d1  t|)t|d|k |jdu ||||g7  |'d2  t|(t|d|k |	jdu ||||g7  |'d3  t|(t|d|k |	jdu ||||g7  |'d4 t  }*t|*dkr|*S  t|| kt|dt|
d|jdu |jdu |	jdu t
t||  d t	 k |||||!g7  |'d5  t|| kt|dt|
d|jdu |jdu |	jdu t
t||  d t	 k |||||!g7  |'d6  ttd t|dt|
d|jdu |jdu |	dk|	t	 t
tk |||||!g7  |'d7  ttd t|dt|
d|jdu |jdu |	dk|	t	 t
tk |||||!g7  |'d8  td k t|dt|
d|jdu |jdu |	dk|	t	 t
tk t
t||  d t	 k |||||!g7  |'d9  td kt|dt|
d|jdu |jdu |	dk|	t	 t
tk t
t||  d t	 k |||||!g7  |'d:  tt|dt|
d|| dk|jdu |	jdu |jdu t
t	||  d t	 k |||||!g7  |'d;  tt|dt|
d|| k|jdu |	jdu |jdu t
t	||  d t	 k |||||!g7  |'d<  tt|dt|
dtd |jdu |	jdu |dk|t	 t
t	k t
t	|d t	 k |||||!g7  |'d=  tt|dt|
dtd |jdu |	jdu |dk|t	 t
t	k t
t	|d t	 k |||||!g7  |'d>  tt|dt|
dd k |jdu |	jdu |dk|t	 t
t	k t
t	||  d t	 k |||||!g7  |'d?  tt|dt|
dd k|jdu |	jdu |dk|t	 t
t	k t
t	||  d t	 k |||||!g7  |'d@ t  S )Az> Return a condition under which the integral theorem applies. r   rP   zChecking antecedents:z1  sigma=%s, s=%s, t=%s, u=%s, v=%s, b*=%s, rho=%sz1  omega=%s, m=%s, n=%s, p=%s, q=%s, c*=%s, mu=%s,z"  phi=%s, eta=%s, psi=%s, theta=%sc                     sH    fD ]} t | j| jD ]\}}|| }|jr |jr   dS qqdS )NFT)r   r   r~   r   
is_integeris_positive)r   ra   jdiff)rl  rm  rd   re   _c1  s   z_check_antecedents.<locals>._c1c                    s,   g | ]} j D ]}td | | dkqqS rP   r   )r   r   r`   ra   r~  rm  rd   re   r         , z&_check_antecedents.<locals>.<listcomp>c                    s,   g | ]} j D ]}td | | dk qqS )rP   r   )r~   r   r  r  rd   re   r     r  c                    6   g | ]} t d | d   t   tddkqS rP   r   r   r   r_   mur   r   rd   re   r        6 c                    2   g | ]} t d |  t   tddkqS r  r  r_   r  rd   re   r        2 c                    r  r  r  r_   rhour   rd   re   r     r  c                    r  r  r  r_   r  rd   re   r     r  r   c                 S   s   | dkot td|  tk S )a  Returns True if abs(arg(1-z)) < pi, avoiding arg(0).

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

            If ``z`` is 1 then arg is NaN. This raises a
            TypeError on `NaN < pi`. Previously this gave `False` so
            this behavior has been hardcoded here but someone should
            check if this NaN is more serious! This NaN is triggered by
            test_meijerint() in test_meijerint.py:
            `meijerint_definite(exp(x), x, 0, I)`
            rP   )r!   r    r   r[   rd   rd   re   _cond"  s   z!_check_antecedents.<locals>._condFc                    sP   |   t  d    t |  t d    t  S r   )r!   r6   )c1c2)omegar   psir   sigmathetar  r   rd   re   	lambda_s0Q  s   &&z%_check_antecedents.<locals>.lambda_s0r   Tr   r   r  r  r     	   
   r  r  r   r!     z  c%s:c                    s   t d|   d  d S )Nz
  case %s:r   rJ  )count)rV  rd   re   prj     z_check_antecedents.<locals>.prr                        )rP  E1E2E3E4                            !   "   #   )r   r   r   r   r   r~   r   r   r   r   r!   r'   rK  rR   r   r   r)   r   r#   rS   r   r5   r<  r3   r"   	TypeErrorr}  is_negativerN  )+rl  rm  rw   r  rB  r   r   rp   bstarcstarphirQ  r  r  r  c3c4c5c6c7c8c9c10c11c12c13z0zoszsoc14r  c14_altlambda_cc15r  lambda_srR  r   ra   r  
mt1_exists
mt2_existsr   rd   )rV  rl  rm  r  r  r   r  r   r  r  r  r  r   re   _check_antecedents  s   00$$*
*
 ""
""
""$...$$$    ((((2222 
 
,,,,6
6
0
0
.
0
6
6
0
0
0
0
r  c                 C   s   t | j|\}}t |j|\}}dd }|| jt|j }t|j|| j }|| jt|j }	t|j|| j }
t|||	|
|| | S )a  
    Express integral from zero to infinity g1*g2 using a G function,
    assuming the necessary conditions are fulfilled.

    Examples
    ========

    >>> from sympy.integrals.meijerint import _int0oo
    >>> from sympy.abc import s, t, m
    >>> from sympy import meijerg, S
    >>> g1 = meijerg([], [], [-S(1)/2, 0], [], s**2*t/4)
    >>> g2 = meijerg([], [], [m/2], [-m/2], t/4)
    >>> _int0oo(g1, g2, t)
    4*meijerg(((1/2, 0), ()), ((m/2,), (-m/2,)), s**(-2))/s**2
    c                 S   r   )Nc                 S   s   g | ]}| qS rd   rd   r  rd   rd   re   r     s    z(_int0oo.<locals>.neg.<locals>.<listcomp>rd   r   rd   rd   re   neg  rr   z_int0oo.<locals>.neg)r   r   r   r   r~   r   r   rN   )rl  rm  rw   rQ  r  r  r  ry  rz  rn  ro  rd   rd   re   _int0oo  s   r  c                    sz   t ||\}t |j|\}  fdd}ddlm} || |    ddt||j||j||j||j|jfS )z Absorb ``po`` == x**s into g. c                    r@  )Nc                    s   g | ]}|   qS rd   rd   )r`   r   rA  rd   re   r   *  r   z2_rewrite_inversion.<locals>.tr.<locals>.<listcomp>rd   r   rA  rd   re   r   )  rC  z_rewrite_inversion.<locals>.trr   rg  Tri  )	r   r   r   rh  rN   r~   r   r   r   )r   r   r   rw   r  r   r   rh  rd   rA  re   _rewrite_inversion$  s   (r  c                    sB  t d  jt\}}|dk rt d tt S fddfddtt jt jt j	t j
g\}}}}|| | }|| | }	||	 d }
|| d	kr_tj}n
d	krfd	}ntj}d	 d t j
  t j	    j}t d
||||||	|
f  t d||f   j|d ks|d	kr||kst d dS t j jD ]\}}|| jr||krt d  dS q||krt d tfdd jD  S  fdd}fdd}fdd}fdd}g }|td	|kd	|k|
t | td k|dk|ttjt |	d	   g7 }|t|d	 |k|d	 |k|dk|td k |dk|| d	 t | td k|ttjt ||   |ttj t ||   g7 }|t||k|dk|dk| t | td k|g7 }|ttt||d kd	|k|d kt|d	 || k|| || d k|dk|td k |d	 t | td k|ttjt |	  |ttj t |	  g7 }|td	|k|
dk|dk||
t  td k || t | td k|ttjt |	  |ttj t |	  g7 }||dkg7 }t| S )z7 Check antecedents for the laplace inversion integral. z#Checking antecedents for inversion:r   z  Flipping G.c                    s   t | \}}| |9 } ||| 9 }||9 }g }|ttjt| t d  }|ttj t| t d  }	|r;|}
n|	}
|ttt|dt|dkt| dkg7 }|tt	|dtt
|dt|dkt|
dk g7 }|tt	|dtt
|dt|dkt|
dkt| dkg7 }t| S )Nr   r   r   )r   r)   r   r   r   r   rR   rS   r   r   r   )r   r   r   r[   pluscoeffexponentrV  wpwmwrv   rd   re   statement_half:  s     ,4,
z4_check_antecedents_inversion.<locals>.statement_halfc                    s"   t  | |||d | |||dS )zW Provide a convergence statement for z**a * exp(b*z**c),
             c/f sphinx docs. TF)rR   )r   r   r   r[   )r  rd   re   	statementL  s   z/_check_antecedents_inversion.<locals>.statementr   rP   z9  m=%s, n=%s, p=%s, q=%s, tau=%s, nu=%s, rho=%s, sigma=%sz   epsilon=%s, theta=%s, delta=%sz-  Computation not valid for these parameters.Fz  Not a valid G function.z$  Using asymptotic Slater expansion.c                       g | ]} |d  ddqS r  rd   r   r  r[   rd   re   r   z      z0_check_antecedents_inversion.<locals>.<listcomp>c                    s   t  fddjD  S )Nc                    r  r  rd   r   r  rd   re   r   }  r  z;_check_antecedents_inversion.<locals>.E.<locals>.<listcomp>)rR   r~   r  )r   r  r  re   E|  s   z'_check_antecedents_inversion.<locals>.Ec                    s     d  | S r   rd   r  )r  r  r  rd   re   H  rC  z'_check_antecedents_inversion.<locals>.Hc                         d  | dS )NrP   Trd   r  r  r  r  rd   re   Hp  r  z(_check_antecedents_inversion.<locals>.Hpc                    r  )NrP   Frd   r  r  rd   re   Hm  r  z(_check_antecedents_inversion.<locals>.Hm)rK  r   r   _check_antecedents_inversionr   r   r   r   r~   r   r   r   NaNr   r   r   r   r|  rR   r   r)   r   rS   )r   rw   r  r   r   rp   r   r   rt  r   r  epsilonr   r   r   r  r  r  r  rV  rd   )r   r  r  r  r  rw   r[   re   r  0  s   0$	(.$$&(r  c              	   C   sH   t | j|\}}tt| j| j| j| j|||  | \}} || |  S )zO
    Compute the laplace inversion integral, assuming the formula applies.
    )r   r   r  rN   r~   r   r   r   )r   rw   r   r   r   r   rd   rd   re   _int_inversion  s   ,r  c                     s&  ddl m}mm}m  tsi att t| trYt	| j
||\}}t|dkr,dS |d }|jr?|j|ks<|jjs>dS n||krEdS ddt| j| j| j| j|| fgdfS | }| |t} t| t}|tv r)t| }	|	D ]\}
}}}| j|
dd}|r(i }| D ]\}}tt|dddd||< q|}t|ts||}|d	krqqt|ttfst||}t|d	krqqt|ts||}g }|D ]S\}}t t||t|dd|}z||t|}W n	 t!y   Y qw t"||f  #t$j%t$j&t$j'rqt|j|j|j|jt|j
dd}|(||f  q|r(||f  S qq|s.dS t)d
  fdd}|} t*dd| }dd }z|| |||d	dd\}}}|||||}W n |yg   d}Y nw |du rt+dd}|| j,vrt-| |rz || ||| |||dd	d\}}}||||||d}W n |y   d}Y nw |du s|#t$j%t$j.t$j&rt)d dS t/0|}g }|D ]?} | |\}}t|dkrt1d|d }t |j
|\}}||dt|j|j|j|jtt|dddd||  fg7 }qt)d| |dfS )aH  
    Try to rewrite f as a sum of single G functions of the form
    C*x**s*G(a*x**b), where b is a rational number and C is independent of x.
    We guarantee that result.argument.as_coeff_mul(x) returns (a, (x**b,))
    or (a, ()).
    Returns a list of tuples (C, s, G) and a condition cond.
    Returns None on failure.
    rP   )mellin_transforminverse_mellin_transformIntegralTransformErrorMellinTransformStripErrorNr   T)old)lift)exponents_onlyFz)Trying recursive Mellin transform method.c                    sV   z| |||dddW S   y*   ddl m} |tt| |||ddd Y S w )z Calling simplify() all the time is slow and not helpful, since
            most of the time it only factors things in a way that has to be
            un-done anyway. But sometimes it can remove apparent poles. T)
as_meijergneedevalr   )simplify)r   r  rW   r
   )FrB  rw   stripr  r  r  rd   re   my_imt  s   

z_rewrite_single.<locals>.my_imtrB  zrewrite-singlec                 S   sp   t | |dd}|d ur.ddlm} |\}}t||dd}t||ft| |tjtjfdfS t| |tjtjfS )NT)only_doubler   hyperexpandnonrepsmall)rewrite)	_meijerint_definite_4r   r  _my_unpolarifyr3   rQ   r   r   r&  )rc   rw   r   r  rl   r   rd   rd   re   my_integrator  s   z&_rewrite_single.<locals>.my_integrator)
integratorr  r  r   )r  r  r  z"Recursive Mellin transform failed.zUnexpected form...z"Recursive Mellin transform worked:)2
transformsr  r  r  r  _lookup_tabler   r   rN   rX   r   r   r   r   r   r)   rk  r~   r   r   r   r   r[   r{   r   itemsr#   r$   r:  rT   r<  r   r   
ValueErrorr   rk   r   r&  ComplexInfinityNegativeInfinityr|   rK  r	  r  r   r  r  r   r   NotImplementedError) rc   rw   	recursiver  r  r  r   f_r   r   r}   termsr   r   r   subs_r2  r3  rl   r   r   ru  r  rB  r   r  r  r  r   rj   r   r   rd   r  re   _rewrite_single  s   
(




 	


 

r  c                 C   s8   t | |\}}}t|||}|r|||d |d fS dS )z
    Try to rewrite ``f`` using a (sum of) single G functions with argument a*x**b.
    Return fac, po, g such that f = fac*po*g, fac is independent of ``x``.
    and po = x**s.
    Here g is a result from _rewrite_single.
    Return None on failure.
    r   rP   N)r   r  )rc   rw   r	  r   r   r   rd   rd   re   	_rewrite1H  s
   r  c                    s   t |  \}}}t fddt|D rdS t|}|sdS tt| fdd fdd fddg}td|D ]0\}\}}t| |}	t| |}
|	rk|
rkt	|	d	 |
d	 }|d
krk|||	d |
d |f  S q;dS )a  
    Try to rewrite ``f`` as a product of two G functions of arguments a*x**b.
    Return fac, po, g1, g2 such that f = fac*po*g1*g2, where fac is
    independent of x and po is x**s.
    Here g1 and g2 are results of _rewrite_single.
    Returns None on failure.
    c                 3   s     | ]}t | d du V  qdS )FN)r  r  rv   rd   re   rf   _  rg   z_rewrite2.<locals>.<genexpr>Nc                    &   t tt| d  tt| d  S Nr   rP   )maxr   r   r   rv   rd   re   rx   e     & z_rewrite2.<locals>.<lambda>c                    r  r  )r  r   r   r   rv   rd   re   rx   f  r  c                    r  r  )r  r   r   r   rv   rd   re   rx   g  s    FTrP   Fr   )
r   r  r   r   r   r   r   r   r  rR   )rc   rw   r   r   r   r   r	  fac1fac2rl  rm  r   rd   rv   re   	_rewrite2V  s(   


r  c                 C   s   t | } g }tt| |tjhB tdD ]'}t| ||| |}|s#q|||| }t|t	t
r7|| q|  S | trgtd tt| |}|rgt|tsbddlm} |t||tS || |rott|S dS )a#  
    Compute an indefinite integral of ``f`` by rewriting it as a G function.

    Examples
    ========

    >>> from sympy.integrals.meijerint import meijerint_indefinite
    >>> from sympy import sin
    >>> from sympy.abc import x
    >>> meijerint_indefinite(sin(x), x)
    -cos(x)
    )key*Try rewriting hyperbolics in terms of exp.r   collectN)r   sortedr   r   r   r   _meijerint_indefinite_1r   r^   rM   rN   r|   rk   r0   rK  meijerint_indefiniter/   r   r   sympy.simplify.radsimpr  r	   r   r)   extendnextr   )rc   rw   resultsr   rl   rvr  rd   rd   re   r  s  s.   


r  c                    s.  t d| d ddlm}m} t| }|du rdS |\}}}}t d| tj}	|D ]\}
}}t|j\}}t|\}}||7 }||
 ||d| |    }|d | d  t	dd	tj
} fd
d}tdd ||jD rt||j||jdg ||jdg ||j| }nt||jdg ||j||j||jdg |}|jr| dtjtjsd}nd}|||||  |d}|	||| dd7 }	q*fdd}t|	dd}	|	jrg }|	jD ]\}}t||}|||fg7 }qt|ddi}	nt||	}	t|	t|ft| dfS )z0 Helper that does not attempt any substitution. z,Trying to compute the indefinite integral ofwrtr   )r  rh  Nz could rewrite:rP   r   zmeijerint-indefinitec                    rd  )Nc                    s   g | ]}|  d  qS r   rd   r   r  rd   re   r     r   z7_meijerint_indefinite_1.<locals>.tr.<locals>.<listcomp>rd   r   r$  rd   re   r     rf  z#_meijerint_indefinite_1.<locals>.trc                 s   s"    | ]}|j o|d kdkV  qdS )r   TN)r|  rH  rd   rd   re   rf     s     z*_meijerint_indefinite_1.<locals>.<genexpr>)placeTri  c                    s$   t t| dd} t|  d S )a  This multiplies out superfluous powers of x we created, and chops off
        constants:

            >> _clean(x*(exp(x)/x - 1/x) + 3)
            exp(x)

        cancel is used before mul_expand since it is possible for an
        expression to have an additive constant that does not become isolated
        with simple expansion. Such a situation was identified in issue 6369:

        Examples
        ========

        >>> from sympy import sqrt, cancel
        >>> from sympy.abc import x
        >>> a = sqrt(2*x + 1)
        >>> bad = (3*x*a**5 + 2*x - a**5 + 1)/a**2
        >>> bad.expand().as_independent(x)[0]
        0
        >>> cancel(bad).expand().as_independent(x)[0]
        1
        F)deeprP   )r   rW   r   
_from_argsas_coeff_add)rl   rv   rd   re   _clean  s   z'_meijerint_indefinite_1.<locals>._clean)evaluater*  F)rK  r   r  rh  r  r   r   r   r   r	  r   r  r   rN   r~   r   r   is_extended_nonnegativer   rk   r  r  r4   r\   rj   r  r3   rQ   )rc   rw   r  rh  r   r   r   glr   rl   r   rB  r   r   r   r  r   fac_r   r   r   r%  r)  r9  r   rd   )r  rw   re   r    sL   


..r  c                 C   s  t d| d|||f  t| } | trt d dS | tr%t d dS | |||f\}}}}td}| ||} |}||krDtjdfS g }	|tj	u r^|tj
ur^t| || || | S |tj	u rt d t| |}
t d	|
 t|
tdd
tjg D ]X}t d| |jst d q|t| ||| |}|du rt d q|t| ||| |}|du rt d q||\}}|\}}tt||}|dkrt d q||| }||f  S n|tj
u rt| ||tj
}|d  |d fS ||ftjtj
fkrt| |}|rt|d tr|	| n|S n|tj
u rWt| |D ];}|| dkdkrUt d|  t| ||| t|| |  |}|rUt|d trQ|	| q|  S q| ||| } || }d}|tj
urttjt| }t|}| ||| } | t|| | 9 } tj
}t d|| t d|  t| |}|rt|d tr|	| n|S |trt d tt||||}|rt|tsddlm } |t!|d |d "tf|dd  }|S |	#| |	rt$t%|	S dS )a  
    Integrate ``f`` over the interval [``a``, ``b``], by rewriting it as a product
    of two G functions, or as a single G function.

    Return res, cond, where cond are convergence conditions.

    Examples
    ========

    >>> from sympy.integrals.meijerint import meijerint_definite
    >>> from sympy import exp, oo
    >>> from sympy.abc import x
    >>> meijerint_definite(exp(-x**2), x, -oo, oo)
    (sqrt(pi), True)

    This function is implemented as a succession of functions
    meijerint_definite, _meijerint_definite_2, _meijerint_definite_3,
    _meijerint_definite_4. Each function in the list calls the next one
    (presumably) several times. This means that calling meijerint_definite
    can be very costly.
    Integratingzwrt %s from %s to %s.z+Integrand has DiracDelta terms - giving up.Nz5Integrand has Singularity Function terms - giving up.rw   Tz  Integrating -oo to +oo.z  Sensible splitting points:)r  reversez  Trying to split atz  Non-real splitting point.z'  But could not compute first integral.z(  But could not compute second integral.Fz)  But combined condition is always false.r   rP   zTrying x -> x + %szChanged limits tozChanged function tor  r  )&rK  r   rk   r=   rO   r   r   r   r   r  r&  meijerint_definiter   r  r   is_extended_real_meijerint_definite_2r  rR   r^   rN   r|   r>   r)   r   r    r!   r0   r/   r   r   r  r  r	   r   r  r   r   )rc   rw   r   r   r
  x_a_b_r  r!  r   r   res1res2cond1cond2r   rl   splitr  r"  r  rd   rd   re   r0    s   










*
r0  c                 C   s   | dfg}|d d }|h}t |}||vr"||dfg7 }|| t|}||vr6||dfg7 }|| |ttrRt t|}||vrR||dfg7 }|| |ttrrddl	m
} ||}||vrr||dfg7 }|| |S )	z6 Try to guess sensible rewritings for integrand f(x). zoriginal integrandr   r   r   r
   zexpand_trig, expand_mul)sincos_to_sumztrig power reduction)r   r   r
   rk   r8   r0   r   r5   r6   sympy.simplify.fur;  )rc   rw   rl   origsawexpandedr;  reducedrd   rd   re   _guess_expansion}  s.   




rA  c                 C   sj   t dd| dd}| ||} |}| dkrtjdfS t| |D ]\}}td| t||}|r2|  S qdS )a  
    Try to integrate f dx from zero to infinity.

    The body of this function computes various 'simplifications'
    f1, f2, ... of f (e.g. by calling expand_mul(), trigexpand()
    - see _guess_expansion) and calls _meijerint_definite_3 with each of
    these in succession.
    If _meijerint_definite_3 succeeds with any of the simplified functions,
    returns this result.
    rw   zmeijerint-definite2T)positiver   TryingN)r	  r   r   r   rA  rK  _meijerint_definite_3)rc   rw   dummyr   explanationrl   rd   rd   re   r2    s   


r2  c                    s   t |  }|r|d dkr|S | jrJtd  fdd| jD }tdd |D rLg }tj}|D ]\}}||7 }||g7 }q0t| }|dkrN||fS dS dS dS )	z
    Try to integrate f dx from zero to infinity.

    This function calls _meijerint_definite_4 to try to compute the
    integral. If this fails, it tries using linearity.
    rP   Fz#Expanding and evaluating all terms.c                    r   rd   )r  )r`   r   rv   rd   re   r     r   z)_meijerint_definite_3.<locals>.<listcomp>c                 s   s    | ]}|d uV  qd S r]   rd   )r`   r   rd   rd   re   rf     s    z(_meijerint_definite_3.<locals>.<genexpr>N)r  is_AddrK  rj   ri   r   r   rR   )rc   rw   rl   ressrV  r   r   rd   rv   re   rD    s$   
rD  c                 C   s   t t| S r]   )r<  r#   rb   rd   rd   re   r    rM  r  c              
   C   s,  ddl m} td|  |sut| |dd}|duru|\}}}}td||| tj}	|D ]0\}
}} |
dkr4q*t||
 |||  | |\}
} |	|
t| | 7 }	t|t	| |}|dkrZ nq*t
|}|dkrhtd ntd	|	 t
||	|fS t| |}|durd
D ]}|\}}}}}td|||| tj}	|D ]S\}}}|D ]J\}}}t|| | ||||   ||||}|du rtd    dS |\}
}}td|
|| t|t|||}|dkr n|	|
t||| 7 }	qq t
|}|dkrtd|  qtd	|	 |r|	|f  S t
||	|f  S dS dS )a  
    Try to integrate f dx from zero to infinity.

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

    This function tries to apply the integration theorems found in literature,
    i.e. it tries to rewrite f as either one or a product of two G-functions.

    The parameter ``only_double`` is used internally in the recursive algorithm
    to disable trying to rewrite f as a single G-function.
    r   r  r.  F)r	  N#Could rewrite as single G function:But cond is always False.z&Result before branch substitutions is:r  z!Could rewrite as two G functions:zNon-rational exponents.zSaxena subst for yielded:z&But cond is always False (full_pb=%s).)r   r  rK  r  r   r   rE  ra  rR   rN  r  r  r{  r  r  )rc   rw   r  r  r   r   r   r   r   rl   r   rB  r>  rl  rm  rw  s1f1rx  s2f2r   f1_f2_rd   rd   re   r    sj   







r  c                 C   sX  | }|}t ddd}| ||} td|  t| |s td dS tj}| jr,t| j}nt	| t
r5| g}nd}|rg }g }|r| }	t	|	t
r}t|	}
|
jrU||
j7 }q=zt|	jd |\}}W n tyl   d}Y nw |dkrw|| nK||	 nE|	jrt|	}
|
jr||
j7 }q=||	jjvrz
t|	j
|\}}W n ty   d}Y nw |dkr||t|	j  ||	 n||	 |s?t| }t| } || jvrtd	| | tt|d}|d
krtd dS | t||  }td|| t||||fS t| |}|dur|\}}}}td||| tj}|D ].\}}} t|| |||  | |\}} ||t| || 7 }t|t| |}|d
krJ nqt|}|d
kr[td dS td| ddl m!} t||}|"t#sx|t#|9 }|||| }t	|t$s|||| }ddl%m&} t||||f||||||ddfS dS )a  
    Compute the inverse laplace transform
    $\int_{c+i\infty}^{c-i\infty} f(x) e^{tx}\, dx$,
    for real c larger than the real part of all singularities of ``f``.

    Note that ``t`` is always assumed real and positive.

    Return None if the integral does not exist or could not be evaluated.

    Examples
    ========

    >>> from sympy.abc import x, t
    >>> from sympy.integrals.meijerint import meijerint_inversion
    >>> meijerint_inversion(1/x, x, t)
    Heaviside(t)
    r   Tri  zLaplace-invertingzBut expression is not analytic.Nr   rP   z.Expression consists of constant and exp shift:Fz3but shift is nonreal, cannot be a Laplace transformz1Result is a delta function, possibly conditional:rI  rJ  z"Result before branch substitution:r  )InverseLaplaceTransform)'r   r   rK  r  r   r   is_Mulr   rj   r   r)   popr
   r   r   r|   r   r   r   r+   r   r   r   r   r=   r3   r  r  r  rR   r  r  r   r  rk   r>   r:  r  rQ  )rc   rw   r   r
  t_shiftrj   r9  exponentialsr    r7  r   r   r   rl   r   r   r   r   r   rB  r  rQ  rd   rd   re   meijerint_inversion  s   











rW  r   )F)r   r   typingr   tDictr   tTuplesympyr   
sympy.corer   r   sympy.core.addr   sympy.core.cacher   sympy.core.containerssympy.core.exprtoolsr	   sympy.core.functionr
   r   r   r   r   sympy.core.mulr   sympy.core.numbersr   r   r   sympy.core.relationalr   r   r   sympy.core.sortingr   r   sympy.core.symbolr   r   r   sympy.core.sympifyr   (sympy.functions.combinatorial.factorialsr   $sympy.functions.elementary.complexesr   r   r    r!   r"   r#   r$   r%   r&   r'   r(   &sympy.functions.elementary.exponentialr)   r*   r+   #sympy.functions.elementary.integersr,   %sympy.functions.elementary.hyperbolicr-   r.   r/   r0   (sympy.functions.elementary.miscellaneousr2   $sympy.functions.elementary.piecewiser3   r4   (sympy.functions.elementary.trigonometricr5   r6   r7   r8   sympy.functions.special.besselr9   r:   r;   r<   'sympy.functions.special.delta_functionsr=   r>   *sympy.functions.special.elliptic_integralsr?   r@   'sympy.functions.special.error_functionsrA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   'sympy.functions.special.gamma_functionsrL   sympy.functions.special.hyperrM   rN   -sympy.functions.special.singularity_functionsrO   	integralsrQ   sympy.logic.boolalgrR   rS   rT   rU   rV   sympy.polysrW   rX   sympy.utilities.iterablesrY   sympy.utilities.miscrZ   rK  r[   r^   r   sympy.utilities.timeutilsr   timeitr{   r  r   r   r   r   r   r   r   r   r   r   r  r
  r	  r  r  r  r<  r?  rE  rN  ra  r{  r  r  r  r  r  r  r  r  r  r  r  r0  rA  r2  rD  r  r  rW  rd   rd   rd   re   <module>   s    44	 R"!$'	
{

m
I  2z 
%Z
  G