a
    SG5dI                    @   s  d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ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(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZB ddlCmDZDmEZE ddlFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZS ddlTmUZUmVZVmWZW ddlXmYZYmZZZm[Z[ ddl\m]Z] ddl^m_Z_ ddl`maZa dd Zbdd Zcdd Zddd Zedd ZfG dd deZgG dd deZhed ZiG d!d" d"ZjG d#d$ d$ZkG d%d& d&ZlG d'd( d(ZmG d)d* d*ZnG d+d, d,enZoG d-d. d.enZpG d/d0 d0enZqG d1d2 d2enZrG d3d4 d4enZsG d5d6 d6enZtG d7d8 d8enZuG d9d: d:enZvG d;d< d<enZwG d=d> d>enZxG d?d@ d@enZyG dAdB dBenZzG dCdD dDenZ{G dEdF dFenZ|dGdH Z}dIdJ Z~dKdL ZdMdN ZdOdP ZdQdR ZdSdT ZdUdV ZdWdX ZdYdZ Zd[d\ Zd]ag ed^d_dd`fdadbZdcdd Zd]adjdfdgZdkdhdiZd]S )la@  
Expand Hypergeometric (and Meijer G) functions into named
special functions.

The algorithm for doing this uses a collection of lookup tables of
hypergeometric functions, and various of their properties, to expand
many hypergeometric functions in terms of special functions.

It is based on the following paper:
      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.

It is described in great(er) detail in the Sphinx documentation.
    )defaultdict)product)reduce)prod)SYMPY_DEBUG)SDummysymbolssympifyTupleexpandIpiMul
EulerGammaoozooexpand_funcAddnanExprRational)Moddefault_sort_key)!expsqrtrootlog
lowergammacosbesseligamma
uppergammaexpinterfsinbesseljEiCiSiShisinhcoshChifresnelsfresnelc
polar_lift	exp_polarfloorceilingrf	factoriallerchphi	Piecewisere
elliptic_k
elliptic_e)polarify
unpolarify)hyperHyperRep_atanhHyperRep_power1HyperRep_power2HyperRep_log1HyperRep_asin1HyperRep_asin2HyperRep_sqrts1HyperRep_sqrts2HyperRep_log2HyperRep_cosasinHyperRep_sinasinmeijerg)Matrixeyezeros)apartpolyPoly)residue)	powdenestsiftc                 C   s*   | j rt| dS |  \}} t|d|  S N   )	is_Numberr   as_coeff_Add)xc r[   V/var/www/html/django/DPS/env/lib/python3.9/site-packages/sympy/simplify/hyperexpand.py_mod1U   s    
r]   c                    s  t dtd\  fdd} fdd}|ddt | fdt   |  tj fd  ftt t tj d gtd	d
ggt tj  d	  tj   d	  g d	   d  d	  gg |ddttd	gtd d
ggtd
d	  gd
d
gg |tjd	ftdftt	d	gtd	d
ggtt
ddd	d	  d gd
d
gg |tjtjftdftttt
ddgtd	d
ggtt
ddtjgd
d	  d gg | tj  ftjftt  t  tj  gtd	d
ggtd
  gd  d	  d d	  tjd  d	  d	   gg |   gtjgtt t gtd	d
ggtd
  g  d	  d	d	  d gg |d	d	gdtj gttd	gtd	d
ggttj d	  d	d	  d gd
d
gg |tjtjgtjgtttgtdt d
ggtt
dddd d  gt
ddtjgg |t
ddtjgtjgtttgtd
dt ggtt
dddd d  gt
ddtjgg |t
ddd	d	gtjdgtt	 td	gtt
ddtj d  t
ddggttjd
d	  d gd
d
d	  gg dg |t
ddd	d	gddgtttjtd	gtt
dddd   dd  dd  ggtd d	  d
d
gd	dd	   d
tjgg dg |d	ggtd	  t td	  d	gtd	 d
ggtd	  d	gd
d
gg | gd  gttj   td  t tj d  t tj  dtj    tj   td  t tj d  t tj  dtj    gtd	d
ggtd d gd d d   gg td }| g d	 gt|     t |  t gtd	d
ggt  d	gd
gg |t
ddgtjgttt t  ttt    |d	gt
ddt
ddgtttttdt  tdtd ttt d  tt  t dt t!dtd ttt d  tt    tt t d  dtd  tttd tdt t!dtd ttt d  tt  tt dt  tdtd ttt d  tt    tt t d  d d	gtg dgtt
ddd	t
d	dgt
d	dd
gg dg |tj gt
dd d	 gt d  d	  t  tt  ttt   d  d	  td     t td   d  d	  t gtg dgtt
ddd
d	gd
  d	gd
d
gg |d	d	gddgtt"t# td	t$gtd	 d
d
d ggtg dd
d
d
gg dg dg |dtjft dt  |g gttd	 d   td	 dt  td	d    tdt  gtd	d
ggtd
d	gd	 gg dt
d	d  fdd}fdd}|g tj  tj gt|d  d	 |d  t
d	d  |d  d	 t |d  t
dd  gdd    td   d	d   d   tg dgtg dd
tj  d	d
gd
d
tjd	gd
d
d	  gg dd t
d	d  t%tt d  |g   tj d  gdttd  d	d    td  d  tt&d  d	 td  d	  td  t&d  d	  td  d	 t&d     d td   t&d   d td  t&d  d	  td  d	 t&d     g tg dgtd
t
d	dd
d
gd
d	d   d t
ddd
gd
d
d	d   t
d	dgd  d
d
d	  gg | g tj d  gttj   t tj td  d	   t tj t t t
dd t t
dd   t t
dd td  gtt tj d  dtj    dt tj  t tj  dd	    d
ggtd	d   d	d
gd tj  tjgd
d
gg |tjgd gtd	  t't  ttd	 ttd	 t tt ttd	 t td	 ttt   t ttt g tg dgtd	 tjd
gd
gd
tj gg |tjgt
ddt
ddgtt(dt d t tdt d t t dt gtg dgtt
ddtjd
gd
t
ddtjgd
d d
gg |t
ddgt
ddt
d!dgttttt d td d tt tttt d td d tt d   tdt t t dt gtg d"gtt
d#dt
d	dd
gd
t
ddd	gd
d
gg |t
d	dgtjt
ddgttttt t d  t!dtd ttt d  tt  dtd  t dt tdt t gtg dgtt
ddt
d	dd
gg d$d
tjgg |  tj gd  d   d	 gttd   d	  td d	d     ttd	 ttd   t ttt td   t ttd	 t td   d	 t tttd   d	 t g tg dgtd
tjtjd
gd d	 d
d gd d
d   d gd
tjtjd  gg |d	d	gddt
ddgtt)dt t#dt  t dt ttdt  d	t$gtd	 d
d
d
d ggtd
tjd
t
ddd
gg d%d
tjd
d
gg d&g d&g |d	d	 gdd d	 gt t# t*d	  t$   d d   d	         t t+     d	 d   t  d d   d	    d d   d	   gtd	  d	d d	ggtdd
d d	gd
  d	d
gd
d
d
gg d'g d(S ))z Create our knowledge base. za b c, z)clsc              	      s(   t | |}t|| f d S NHyper_FunctionappendFormula)apbqresfuncabrZ   formulaezr[   r\   addh   s    
zadd_formulae.<locals>.addc              
      s.   t | |}t|d  f||| d S r_   r`   )rd   re   BCMrg   rh   r[   r\   addbl   s    
zadd_formulae.<locals>.addbr[      rV   r   )rV   rV   )rr   z3/2   )r   r   r      	         )rV   r   r   )rV   rs   r   )r   rV   rs   r   )r   r   r   r   c                    s   t |  t|   S r_   r!   r'   ri   rl   rY   r[   r\   fp   s    zadd_formulae.<locals>.fpc                    s   t |  t|   S r_   rz   r{   r|   r[   r\   fm   s    zadd_formulae.<locals>.fm)rV   r   r   r   )r   rV   r   r   i   )   r   r   )r   r   rV   )r   r   rV   r   r   )r   r   r   r   r   )r   r   r   rs   N),r	   r   r   r@   r   HalfrK   rA   rB   r?   r   rC   rE   rF   rH   rI   rD   Oner:   r;   r   rG   r   r!   r"   r1   r   r   r%   r,   r/   r   r-   r0   r(   r   r   r2   r'   r&   r+   r.   r$   r#   )rk   rm   rq   Zmzr}   r~   r[   )ri   rj   rZ   rk   rY   rl   r\   add_formulaed   sl   
,$&"
8	,,&062*.,*&>
D8H<2 &".(",2

	&.$ "$&,"*2	$&
62

,	$
&2(*2& (6.
r   c                    s  t ttd\ td fdd} fdd}|  gg   gg ttd    t t  td      gtddggt d	gd  gg|  fd
d}tdt }t	dt }t
dt td  }tdt }| gg    tj gg ttt tj   || ||   tt   || ||   tt   gtg dgt tj d	dg tjgdd gg| d S )NZabczrhoc                    s,    t| ||| g||||
 d S r_   )rb   MeijerFormula)anrd   bmre   rn   ro   rp   matcherri   rj   rZ   rk   r   rl   r[   r\   rm     s    z!add_meijerg_formulae.<locals>.addc                    s   | j d }| j\}}d}t||  s6d}|| }}t||  sR|| dkrVd S ||g}|rj||g}| || it|gg |g fS )Nr   FT)r   r   r]   simplify
G_Function)rg   rY   yrl   swappedl)ri   r   r[   r\   detect_uppergamma  s    


z/add_meijerg_formulae.<locals>.detect_uppergammarV   r   rs   c                    sD  | j d  | j\}}}t||  dkrdt||  dkrBdS tjtjtjf}|||  }}}nVt |  dkrtjtjtjf}|||  }}}n tjtjtjf}|||  }}}t |  dkst |  dkst |  tjks | dks | dkrdS  it gg  fdd|D g fS )z-http://functions.wolfram.com/07.34.03.0984.01r   Nc                    s   g | ]} t j | qS r[   )r   r   ).0tr|   r[   r\   
<listcomp>      z=add_meijerg_formulae.<locals>.detect_3113.<locals>.<listcomp>)r   r   r]   r   r   r   Zeror   )rg   uvwsigx1x2r   ri   r|   r\   detect_3113  s.    


z)add_meijerg_formulae.<locals>.detect_3113rr   )rt   r   r   )listmapr   rK   r"   r   r#   r&   r   r    r*   r   r)   r   r   )rk   rm   r   r   sc_ZS_ro   r[   r   r\   add_meijerg_formulae  s4    &&&r   c                    s    fdd}|S )z@ Create a function that simplifies rational functions in ``z``. c                    sD   |   \}}| }t| t| \}}}||  |  S )z6 Efficiently simplify the rational function ``expr``. )as_numer_denomr   rO   cancelas_expr)exprnumerdenomrZ   rl   r[   r\   simp  s    zmake_simp.<locals>.simpr[   )rl   r   r[   r   r\   	make_simp  s    r   c                  G   s$   t r | D ]}t|dd qt  d S )N )end)r   print)argsri   r[   r[   r\   debug  s    r   c                       sp   e Zd ZdZ fddZedd Zedd Zedd	 Z fd
dZ	dd Z
dd Zdd Zdd Z  ZS )ra   z( A generalized hypergeometric function. c                    s8   t  | }tttt| |_tttt| |_|S r_   )super__new__r   r   r   r   rd   re   )r^   rd   re   obj	__class__r[   r\   r     s    zHyper_Function.__new__c                 C   s   | j | jfS r_   )rd   re   selfr[   r[   r\   r     s    zHyper_Function.argsc                 C   s   t | jt | jfS r_   )lenrd   re   r   r[   r[   r\   sizes  s    zHyper_Function.sizesc                 C   s   t dd | jD S )zt
        Number of upper parameters that are negative integers

        This is a transformation invariant.
        c                 s   s   | ]}t |jo|jV  qd S r_   )bool
is_integeris_negativer   rY   r[   r[   r\   	<genexpr>  r   z'Hyper_Function.gamma.<locals>.<genexpr>)sumrd   r   r[   r[   r\   r"     s    zHyper_Function.gammac                    s   t   | j| jf S r_   )r   _hashable_contentrd   re   r   r   r[   r\   r     s    z Hyper_Function._hashable_contentc                 C   s   t | j| j|S r_   )r>   rd   re   )r   argr[   r[   r\   __call__  s    zHyper_Function.__call__c                 C   s6   t | jtt | jt }}dd }| j||||fS )a6  
        Compute the invariant vector.

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

        The invariant vector is:
            (gamma, ((s1, n1), ..., (sk, nk)), ((t1, m1), ..., (tr, mr)))
        where gamma is the number of integer a < 0,
              s1 < ... < sk
              nl is the number of parameters a_i congruent to sl mod 1
              t1 < ... < tr
              ml is the number of parameters b_i congruent to tl mod 1

        If the index pair contains parameters, then this is not truly an
        invariant, since the parameters cannot be sorted uniquely mod1.

        Examples
        ========

        >>> from sympy.simplify.hyperexpand import Hyper_Function
        >>> from sympy import S
        >>> ap = (S.Half, S.One/3, S(-1)/2, -2)
        >>> bq = (1, 2)

        Here gamma = 1,
             k = 3, s1 = 0, s2 = 1/3, s3 = 1/2
                    n1 = 1, n2 = 1,   n2 = 2
             r = 1, t1 = 0
                    m1 = 2:

        >>> Hyper_Function(ap, bq).build_invariants()
        (1, ((0, 1), (1/3, 1), (1/2, 2)), ((0, 2),))
        c                 S   sD   t |  } tdd | D s.| jdd d tdd | D } | S )Nc                 s   s   | ]}t |d  tV  qdS )r   N)
isinstancer   r   r[   r[   r\   r   $  r   z>Hyper_Function.build_invariants.<locals>.tr.<locals>.<genexpr>c                 S   s   t | d S Nr   r   r|   r[   r[   r\   <lambda>%  r   z=Hyper_Function.build_invariants.<locals>.tr.<locals>.<lambda>keyc                 S   s    g | ]\}}|r|t |fqS r[   r   )r   modvaluesr[   r[   r\   r   &  s   z?Hyper_Function.build_invariants.<locals>.tr.<locals>.<listcomp>)r   itemsanysorttuple)bucketr[   r[   r\   tr"  s
    z+Hyper_Function.build_invariants.<locals>.tr)rT   rd   r]   re   r"   )r   abucketsbbucketsr   r[   r[   r\   build_invariants  s    #zHyper_Function.build_invariantsc                 C   s   | j |j krdS dd | j| j|j|jfD \}}}}d}||f||ffD ]\}}tt| t|  D ]}	|	|vs|	|vst||	 t||	 kr  dS t||	 }
t||	 }|
  |  t|
|D ]\}}|t	|| 7 }qqnqJ|S )zd Estimate how many steps it takes to reach ``func`` from self.
            Return -1 if impossible. rs   c                 S   s   g | ]}t |tqS r[   rT   r]   r   paramsr[   r[   r\   r   1  s   z-Hyper_Function.difficulty.<locals>.<listcomp>r   )
r"   rd   re   setr   keysr   r   zipabs)r   rg   Z	oabucketsZ	obbucketsr   r   diffr   obucketr   l1l2ijr[   r[   r\   
difficulty,  s&     zHyper_Function.difficultyc                 C   st   | j D ]0}| jD ]$}|| jr|| jdu r  dS qq| j D ]}|dkr> dS q>| jD ]}|jrX|jrX dS qXdS )a  
        Decide if ``self`` is a suitable origin.

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

        A function is a suitable origin iff:
        * none of the ai equals bj + n, with n a non-negative integer
        * none of the ai is zero
        * none of the bj is a non-positive integer

        Note that this gives meaningful results only when none of the indices
        are symbolic.

        Fr   T)rd   re   r   r   is_nonpositive)r   ri   rj   r[   r[   r\   _is_suitable_originC  s    



z"Hyper_Function._is_suitable_origin)__name__
__module____qualname____doc__r   propertyr   r   r"   r   r   r   r   r   __classcell__r[   r[   r   r\   ra     s   


/ra   c                       sT   e Zd ZdZ fddZedd Z fddZdd	 Zd
d Z	edd Z
  ZS )r   z A Meijer G-function. c                    s`   t  | }tttt| |_tttt| |_tttt| |_tttt| |_	|S r_   )
r   r   r   r   r   r   r   rd   r   re   )r^   r   rd   r   re   r   r   r[   r\   r   c  s    zG_Function.__new__c                 C   s   | j | j| j| jfS r_   )r   rd   r   re   r   r[   r[   r\   r   k  s    zG_Function.argsc                    s   t   | j S r_   )r   r   r   r   r   r[   r\   r   o  s    zG_Function._hashable_contentc                 C   s   t | j| j| j| j|S r_   )rJ   r   rd   r   re   )r   rl   r[   r[   r\   r   r  s    zG_Function.__call__c                    s   dd t dD  }\}}}}t|| j| j| j| jfD ]$\}}|D ]}|t| | qDq8t|dD ]@\}}	| D ].\}
}|d  |j	 fdd|	d |||
< qxqht
d	d |D S )
a  
        Compute buckets for the fours sets of parameters.

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

        We guarantee that any two equal Mod objects returned are actually the
        same, and that the buckets are sorted by real part (an and bq
        descendending, bm and ap ascending).

        Examples
        ========

        >>> from sympy.simplify.hyperexpand import G_Function
        >>> from sympy.abc import y
        >>> from sympy import S

        >>> a, b = [1, 3, 2, S(3)/2], [1 + y, y, 2, y + 3]
        >>> G_Function(a, b, [2], [y]).compute_buckets()
        ({0: [3, 2, 1], 1/2: [3/2]},
        {0: [2], y: [y, y + 1, y + 3]}, {0: [2]}, {y: [y]})

        c                 S   s   g | ]}t tqS r[   )r   r   )r   r   r[   r[   r\   r     r   z.G_Function.compute_buckets.<locals>.<listcomp>rv   )TFFTr   c                    s   |   S r_   r[   r|   x0r[   r\   r     r   z,G_Function.compute_buckets.<locals>.<lambda>)r   reversec                 S   s   g | ]}t |qS r[   )dictr   r   r[   r[   r\   r     r   )ranger   r   rd   r   re   r]   rb   r   r   r   )r   dictspanpappbmZpbqdicZlisrY   flipmr   r[   r   r\   compute_bucketsu  s    "zG_Function.compute_bucketsc                 C   s$   t | jt | jt | jt | jfS r_   )r   r   rd   r   re   r   r[   r[   r\   	signature  s    zG_Function.signature)r   r   r   r   r   r   r   r   r   r   r   r   r[   r[   r   r\   r   `  s   
%r   rY   c                   @   s6   e Zd ZdZdd ZdddZedd Zd	d
 ZdS )rc   a-  
    This class represents hypergeometric formulae.

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

    Its data members are:
    - z, the argument
    - closed_form, the closed form expression
    - symbols, the free symbols (parameters) in the formula
    - func, the function
    - B, C, M (see _compute_basis)

    Examples
    ========

    >>> from sympy.abc import a, b, z
    >>> from sympy.simplify.hyperexpand import Formula, Hyper_Function
    >>> func = Hyper_Function((a/2, a/3 + b, (1+a)/2), (a, b, (a+b)/7))
    >>> f = Formula(func, z, None, [a, b])

    c                 C   s   dd | j jD }dd | j jD }tt|  | jt|   }t|t}| d }|g}t|D ] }|	| j|d 
| j  qbt|| _tdgdg|  g| _t|}	|	dt|d}	| dd }
|
  |	|t|
g | d  | _dS )z
        Compute a set of functions B=(f1, ..., fn), a nxn matrix M
        and a 1xn matrix C such that:
           closed_form = C B
           z d/dz B = M B.
        c                 S   s   g | ]}t | qS r[   _xr   ri   r[   r[   r\   r     r   z*Formula._compute_basis.<locals>.<listcomp>c                 S   s   g | ]}t | d  qS rV   r   r   rj   r[   r[   r\   r     r   rV   rs   r   N)rg   rd   re   r   r   rl   rP   degreer   rb   r   rK   rn   ro   rL   
col_insertrM   
all_coeffsr   
row_insertrp   )r   closed_formafactorsbfactorsr   rO   nrj   _r   r   r[   r[   r\   _compute_basis  s    

zFormula._compute_basisNc                    s`   t |}t |} fddt |D }|| _|| _|| _|| _|| _ | _|d ur\| | d S )Nc                    s   g | ]}  |r|qS r[   hasr   rg   r[   r\   r     r   z$Formula.__init__.<locals>.<listcomp>)r
   rl   r	   rn   ro   rp   rg   r  )r   rg   rl   rf   r	   rn   ro   rp   r[   r
  r\   __init__  s    zFormula.__init__c                 C   s   t dd t| j| jtjS )Nc                 S   s   | |d |d   S Nr   rV   r[   r   r   r[   r[   r\   r     r   z%Formula.closed_form.<locals>.<lambda>r   r   ro   rn   r   r   r   r[   r[   r\   r    s    zFormula.closed_formc              
      s  ddl m} |j}|j}t|tjjks@t|tjjkrHtdg }jD ]F  jjjv rp|	| qR jjjv r|	| qRt
d f qRfddt| D }dd ||fD \}}dd ||fD \}	}
d	d jD }g }t }|D ]fd
djjjjfD \}}||f||ffD ]\}}tt| t|  D ]}||vs||vst|| t|| kr q2tj|D ]\ }  jrq fdd|| D } }|   |7  < |D ]F}|| D ]6}|||| |\}|jrt
d|	| qqqqXq2g }tj|D ]L\ }  tt|}tt|}|	fddt||d D  qJ|fddt| D  q|S )z
        Find substitutions of the free symbols that match ``func``.

        Return the substitution dictionaries as a list. Note that the returned
        instantiations need not actually match, or be valid!

        r   )solvez-Cannot instantiate other number of parameterszAAt least one of the parameters of the formula must be equal to %sc                    s    g | ]}t tt j|qS r[   r   r   r   r	   )r   r   r   r[   r\   r      s   z/Formula.find_instantiations.<locals>.<listcomp>c                 S   s   g | ]}t |tqS r[   r   r   r[   r[   r\   r     r   c                 S   s   g | ]}d d |  D qS )c                 S   s   i | ]\}}|t |qS r[   r   )r   ri   valsr[   r[   r\   
<dictcomp>  r   z:Formula.find_instantiations.<locals>.<listcomp>.<dictcomp>)r   )r   r   r[   r[   r\   r     s   c                 S   s   g | ]
}d gqS r   r[   )r   r  r[   r[   r\   r     r   c                    s   g | ]}t | fd dqS )c                    s   t |  S r_   )r]   xreplacer|   replr[   r\   r   	  r   z8Formula.find_instantiations.<locals>.<listcomp>.<lambda>rS   r   r  r[   r\   r   	  s   c                    s   g | ]}|  r|qS r[   r  )r   r   r   r[   r\   r     r   zValue should not be truec                    s   g | ]} | qS r[   r[   )r   r  )a0r[   r\   r   "  r   rV   c                 3   s"   | ]}t tt j|V  qd S r_   r  )r   r   r   r[   r\   r   #  r   z.Formula.find_instantiations.<locals>.<genexpr>)sympy.solversr  rd   re   r   rg   	TypeErrorr	   r   rb   
ValueErrorr   r   r   r   r   r   free_symbolscopyr  r3   minr4   maxr   extend)r   rg   r  rd   re   Zsymbol_valuesZ	base_replr   r   a_invZb_invcritical_valuesresult_nZsymb_aZsymb_br   r   r   r  exprsZrepl0r   targetn0r   Zmin_Zmax_r[   )ri   r  r  r   r\   find_instantiations  sl    (





 &zFormula.find_instantiations)NNN)	r   r   r   r   r  r  r   r  r'  r[   r[   r[   r\   rc     s   

rc   c                   @   s    e Zd ZdZdd Zdd ZdS )FormulaCollectionz- A collection of formulae to use as origins. c                 C   st   i | _ i | _g | _t| j | jD ]L}|jj}t|jdkrR| j |g 	| q"|j
 }|| j|i |< q"dS )z7 Doing this globally at module init time is a pain ... r   N)symbolic_formulaeconcrete_formulaerk   r   rg   r   r   r	   
setdefaultrb   r   )r   fr   invr[   r[   r\   r  ,  s    


zFormulaCollection.__init__c                 C   s   |  }|j}|| jv r4|| j| v r4| j| | S || jvrBdS g }| j| D ]T}||}|D ]@}|j|}| s|qb||}	|	dkrqb|	|	|||f qbqP|j
dd d |D ]`\}
}}}t||jdg |j||j||j|}tdd |j|j|jfD s|  S qdS )a{  
        Given the suitable target ``func``, try to find an origin in our
        knowledge base.

        Examples
        ========

        >>> from sympy.simplify.hyperexpand import (FormulaCollection,
        ...     Hyper_Function)
        >>> f = FormulaCollection()
        >>> f.lookup_origin(Hyper_Function((), ())).closed_form
        exp(_z)
        >>> f.lookup_origin(Hyper_Function([1], ())).closed_form
        HyperRep_power1(-1, _z)

        >>> from sympy import S
        >>> i = Hyper_Function([S('1/4'), S('3/4 + 4')], [S.Half])
        >>> f.lookup_origin(i).closed_form
        HyperRep_sqrts1(-1/4, _z)
        Nrs   c                 S   s   | d S r   r[   r|   r[   r[   r\   r   k  r   z1FormulaCollection.lookup_origin.<locals>.<lambda>r   c                 s   s"   | ]}| tjtt tV  qd S r_   )r	  r   NaNr   r   )r   er[   r[   r\   r   o  r   z2FormulaCollection.lookup_origin.<locals>.<genexpr>)r   r   r*  r)  r'  rg   r  r   r   rb   r   rc   rl   rn   subsro   rp   r   )r   rg   r-  r   possibler,  Zreplsr  func2r   r  f2r[   r[   r\   lookup_origin?  s6    




zFormulaCollection.lookup_originNr   r   r   r   r  r4  r[   r[   r[   r\   r(  )  s   r(  c                   @   s,   e Zd ZdZdd Zedd Zdd ZdS )	r   z
    This class represents a Meijer G-function formula.

    Its data members are:
    - z, the argument
    - symbols, the free symbols (parameters) in the formula
    - func, the function
    - B, C, M (c/f ordinary Formula)
    c                 C   sV   dd ||||fD \}}}}t ||||| _|| _|| _|
| _|| _|| _|	| _d S )Nc                 S   s   g | ]}t ttt| qS r[   )r   r   r   r   r   r[   r[   r\   r     r   z*MeijerFormula.__init__.<locals>.<listcomp>)r   rg   rl   r	   _matcherrn   ro   rp   )r   r   rd   r   re   rl   r	   rn   ro   rp   r   r[   r[   r\   r    s    zMeijerFormula.__init__c                 C   s   t dd t| j| jtjS )Nc                 S   s   | |d |d   S r  r[   r  r[   r[   r\   r     r   z+MeijerFormula.closed_form.<locals>.<lambda>r  r   r[   r[   r\   r    s    zMeijerFormula.closed_formc                 C   sl   |j | jj krdS | |}|durh|\}}t|j|j|j|j| jg | j	
|| j
|| j
|d
S dS )z
        Try to instantiate the current formula to (almost) match func.
        This uses the _matcher passed on init.
        N)r   rg   r6  r   r   rd   r   re   rl   rn   r0  ro   rp   )r   rg   rf   r0  newfuncr[   r[   r\   try_instantiate  s    
zMeijerFormula.try_instantiateN)r   r   r   r   r  r   r  r8  r[   r[   r[   r\   r   u  s
   


r   c                   @   s    e Zd ZdZdd Zdd ZdS )MeijerFormulaCollectionz=
    This class holds a collection of meijer g formulae.
    c                 C   sD   g }t | tt| _|D ]}| j|jj | qt| j| _d S r_   )r   r   r   rk   rg   r   rb   r   )r   rk   formular[   r[   r\   r    s    
z MeijerFormulaCollection.__init__c                 C   s@   |j | jvrdS | j|j  D ]}||}|dur|  S qdS )z* Try to find a formula that matches func. N)r   rk   r8  )r   rg   r:  rf   r[   r[   r\   r4    s    
z%MeijerFormulaCollection.lookup_originNr5  r[   r[   r[   r\   r9    s   r9  c                   @   s   e Zd ZdZdd ZdS )Operatora  
    Base class for operators to be applied to our functions.

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

    These operators are differential operators. They are by convention
    expressed in the variable D = z*d/dz (although this base class does
    not actually care).
    Note that when the operator is applied to an object, we typically do
    *not* blindly differentiate but instead use a different representation
    of the z*d/dz operator (see make_derivative_operator).

    To subclass from this, define a __init__ method that initializes a
    self._poly variable. This variable stores a polynomial. By convention
    the generator is z*d/dz, and acts to the right of all coefficients.

    Thus this poly
        x**2 + 2*z*x + 1
    represents the differential operator
        (z*d/dz)**2 + 2*z**2*d/dz.

    This class is used only in the implementation of the hypergeometric
    function expansion algorithm.
    c                 C   s   | j  }|  |g}|dd D ]}|||d  q$|d |d  }t|dd |dd D ]\}}||| 7 }qf|S )a  
        Apply ``self`` to the object ``obj``, where the generator is ``op``.

        Examples
        ========

        >>> from sympy.simplify.hyperexpand import Operator
        >>> from sympy.polys.polytools import Poly
        >>> from sympy.abc import x, y, z
        >>> op = Operator()
        >>> op._poly = Poly(x**2 + z*x + y, x)
        >>> op.apply(z**7, lambda f: f.diff(z))
        y*z**7 + 7*z**7 + 42*z**5
        rV   Nrs   r   )_polyr   r   rb   r   )r   r   opcoeffsdiffsrZ   rdr[   r[   r\   apply  s    
"zOperator.applyN)r   r   r   r   rB  r[   r[   r[   r\   r;    s   r;  c                   @   s   e Zd ZdZdd ZdS )MultOperatorz! Simply multiply by a "constant" c                 C   s   t |t| _d S r_   )rP   r   r<  )r   pr[   r[   r\   r    s    zMultOperator.__init__N)r   r   r   r   r  r[   r[   r[   r\   rC    s   rC  c                   @   s    e Zd ZdZdd Zdd ZdS )ShiftAz Increment an upper index. c                 C   s0   t |}|dkrtdtt| d t| _d S )Nr   z"Cannot increment zero upper index.rV   r
   r  rP   r   r<  )r   air[   r[   r\   r    s    zShiftA.__init__c                 C   s   dd| j  d   S )Nz<Increment upper %s.>rV   r   r<  r   r   r[   r[   r\   __str__  s    zShiftA.__str__Nr   r   r   r   r  rI  r[   r[   r[   r\   rE    s   rE  c                   @   s    e Zd ZdZdd Zdd ZdS )ShiftBz Decrement a lower index. c                 C   s4   t |}|dkrtdtt|d  d t| _d S )NrV   z"Cannot decrement unit lower index.rF  r   bir[   r[   r\   r    s    zShiftB.__init__c                 C   s   dd| j  d  d  S )Nz<Decrement lower %s.>rV   r   rH  r   r[   r[   r\   rI    s    zShiftB.__str__NrJ  r[   r[   r[   r\   rK    s   rK  c                   @   s    e Zd ZdZdd Zdd ZdS )UnShiftAz Decrement an upper index. c                 C   s&  t tt|||g\}}}|| _|| _|| _t |}t |}||d }|dkrZtdt|| t	}|D ]}|tt	| t	9 }qlt
d}t|| | | }	}
|D ]}|	|
|d | 9 }	q|	d }|dkrtdtt|	 dd | |t	| d t	}	t|	| | t	| _dS ) Note: i counts from zero! rV   r   z"Cannot decrement unit upper index.Az0Cannot decrement upper index: cancels with lowerNrs   r   r   r
   _ap_bq_ipopr  rP   r   r   as_polynthr   r   r0  r<  )r   rd   re   r   rl   rG  r   ri   rP  r  Drj   b0r[   r[   r\   r    s*    0zUnShiftA.__init__c                 C   s   d| j | j| jf S )Nz&<Decrement upper index #%s of %s, %s.>rT  rR  rS  r   r[   r[   r\   rI  /  s    zUnShiftA.__str__NrJ  r[   r[   r[   r\   rN    s   !rN  c                   @   s    e Zd ZdZdd Zdd ZdS )UnShiftBz Increment a lower index. c                 C   s:  t tt|||g\}}}|| _|| _|| _t |}t |}||d }|dkrZtdtt	|d  t	}|D ]}|tt	| d t	9 }qpt
d}t|d | | d |}	t||}
|D ]}|
|	|| 9 }
q|
d}|dkrtdtt|
 dd | |t	|d  d t	}
t||
 | t	| _dS )rO  rV   r   z Cannot increment -1 lower index.rn   z*Cannot increment index: cancels with upperNrs   rQ  )r   rd   re   r   rl   rM  r   rj   rn   rX  r  ri   rY  r[   r[   r\   r  7  s4    

zUnShiftB.__init__c                 C   s   d| j | j| jf S )Nz&<Increment lower index #%s of %s, %s.>rZ  r   r[   r[   r\   rI  Y  s    zUnShiftB.__str__NrJ  r[   r[   r[   r\   r[  4  s   "r[  c                   @   s    e Zd ZdZdd Zdd ZdS )MeijerShiftAz Increment an upper b index. c                 C   s   t |}t|t t| _d S r_   r
   rP   r   r<  rL  r[   r[   r\   r  a  s    zMeijerShiftA.__init__c                 C   s   d| j  d  S )Nz<Increment upper b=%s.>rV   rH  r   r[   r[   r\   rI  e  s    zMeijerShiftA.__str__NrJ  r[   r[   r[   r\   r\  ^  s   r\  c                   @   s    e Zd ZdZdd Zdd ZdS )MeijerShiftBz Decrement an upper a index. c                 C   s    t |}td| t t| _d S rU   r]  rL  r[   r[   r\   r  l  s    zMeijerShiftB.__init__c                 C   s   dd| j  d   S )Nz<Decrement upper a=%s.>rV   rH  r   r[   r[   r\   rI  p  s    zMeijerShiftB.__str__NrJ  r[   r[   r[   r\   r^  i  s   r^  c                   @   s    e Zd ZdZdd Zdd ZdS )MeijerShiftCz Increment a lower b index. c                 C   s   t |}t| t t| _d S r_   r]  rL  r[   r[   r\   r  w  s    zMeijerShiftC.__init__c                 C   s   d| j  d   S )Nz<Increment lower b=%s.>rV   rH  r   r[   r[   r\   rI  {  s    zMeijerShiftC.__str__NrJ  r[   r[   r[   r\   r_  t  s   r_  c                   @   s    e Zd ZdZdd Zdd ZdS )MeijerShiftDz Decrement a lower a index. c                 C   s    t |}t|d t t| _d S rU   r]  rL  r[   r[   r\   r    s    zMeijerShiftD.__init__c                 C   s   d| j  d d  S )Nz<Decrement lower a=%s.>rV   rH  r   r[   r[   r\   rI    s    zMeijerShiftD.__str__NrJ  r[   r[   r[   r\   r`    s   r`  c                   @   s    e Zd ZdZdd Zdd ZdS )MeijerUnShiftAz Decrement an upper b index. c                    sH  t tt|||||g\}}}}}|| _|| _|| _|| _|| _t |}t |}t |}t |}||d }t	dt
tdd |D  tdd |D  }td}	t	||	 |	 t	||	t fdd|D  t fdd|D  }
|
d}|dkrtd	t	t	|
 d
d |	 |	|t
 t
}
t	||
 | t
| _d
S )rO  rV   c                 s   s   | ]}t |t tV  qd S r_   rP   r   r   r[   r[   r\   r     r   z*MeijerUnShiftA.__init__.<locals>.<genexpr>c                 s   s   | ]}t t| tV  qd S r_   rb  r   r[   r[   r\   r     r   rP  c                 3   s   | ]} d  | V  qdS rV   Nr[   r   rX  r[   r\   r     r   c                 3   s   | ]}  | d  V  qdS rc  r[   r   rd  r[   r\   r     r   r   z(Cannot decrement upper b index (cancels)Nrs   )r   r   r
   _anrR  _bmrS  rT  rU  rP   r   r   r   rW  r  r   r   r0  r<  )r   r   rd   r   re   r   rl   rM  r   rP  r  rY  r[   rd  r\   r    s(    ".6

,zMeijerUnShiftA.__init__c                 C   s   d| j | j| j| j| jf S )Nz0<Decrement upper b index #%s of %s, %s, %s, %s.>rT  re  rR  rf  rS  r   r[   r[   r\   rI    s    zMeijerUnShiftA.__str__NrJ  r[   r[   r[   r\   ra    s   ra  c                   @   s    e Zd ZdZdd Zdd ZdS )MeijerUnShiftBz Increment an upper a index. c                 C   sn  t tt|||||g\}}}}}|| _|| _|| _|| _|| _t |}t |}t |}t |}||d }t	|t
}|D ]}	|t	d|	 t
 t
9 }q||D ]}	|t	|	d t
 t
9 }qtd}
t	|
| d |
}t	d|
}|D ]}|| | 9 }q|D ]}||| 9 }q|d}|dkr&tdt	t	| dd |
 |
d| t
 t
}t	|| | t
| _dS )rO  rV   rn   r   z(Cannot increment upper a index (cancels)Nrs   r   r   r
   re  rR  rf  rS  rT  rU  rP   r   r   rW  r  r   r   r0  r<  r   r   rd   r   re   r   rl   rG  r   ri   rn   rX  r  rj   rY  r[   r[   r\   r    s@    "



zMeijerUnShiftB.__init__c                 C   s   d| j | j| j| j| jf S )Nz0<Increment upper a index #%s of %s, %s, %s, %s.>rg  r   r[   r[   r\   rI    s    zMeijerUnShiftB.__str__NrJ  r[   r[   r[   r\   rh    s   'rh  c                   @   s    e Zd ZdZdd Zdd ZdS )MeijerUnShiftCz Decrement a lower b index. c                 C   sf  t tt|||||g\}}}}}|| _|| _|| _|| _|| _t |}t |}t |}t |}||d }t	dt
}|D ]}	|t	|	t
 t
9 }q||D ]}	|t	t
|	 t
9 }qtd}
t	||
 |
}t	||
}|D ]}||d | 9 }q|D ]}|| | d 9 }q|d}|dkr"tdt	t	| dd |
 |
t
| t
}t	|| | t
| _dS )rO  rV   ro   r   z(Cannot decrement lower b index (cancels)Nrs   ri  )r   r   rd   r   re   r   rl   rM  r   rj   ro   rX  r  ri   rY  r[   r[   r\   r    s8    "



,zMeijerUnShiftC.__init__c                 C   s   d| j | j| j| j| jf S )Nz0<Decrement lower b index #%s of %s, %s, %s, %s.>rg  r   r[   r[   r\   rI    s    zMeijerUnShiftC.__str__NrJ  r[   r[   r[   r\   rk    s   &rk  c                   @   s    e Zd ZdZdd Zdd ZdS )MeijerUnShiftDz Increment a lower a index. c                 C   sn  t tt|||||g\}}}}}|| _|| _|| _|| _|| _t |}t |}t |}t |}||d }t	|t
}|D ]}	|t	d|	 t
 t
9 }q||D ]}	|t	|	d t
 t
9 }qtd}
t	|d |
 |
}t	d|
}|D ]}|| | 9 }q|D ]}||| 9 }q|d}|dkr&tdt	t	| dd |
 |
|d t
 t
}t	|| | t
| _dS )rO  rV   rn   r   z(Cannot increment lower a index (cancels)Nrs   ri  rj  r[   r[   r\   r    s@    "



zMeijerUnShiftD.__init__c                 C   s   d| j | j| j| j| jf S )Nz0<Increment lower a index #%s of %s, %s, %s, %s.>rg  r   r[   r[   r\   rI  >  s    zMeijerUnShiftD.__str__NrJ  r[   r[   r[   r\   rl    s   'rl  c                   @   sD   e Zd ZdZdd Zedd Zedd Zedd	 Zd
d Z	dS )ReduceOrderz8 Reduce Order by cancelling an upper and a lower index. c                 C   s   t |}t |}|| }|jr&|dk r*dS |jr:|jr:dS t| }tj}t|D ]}|t	| | ||  9 }qRt
|t	|_||_||_|S )z< For convenience if reduction is not possible, return None. r   N)r
   
is_Integerr   r   r;  r   r   r   r   r   rP   r<  _a_b)r^   rG  bjr  r   rD  kr[   r[   r\   r   F  s    
zReduceOrder.__new__c                 C   s   t |}t |}|| }|js$|js(dS t| }tj}t|D ]}||t | | 9 }q@t	|t|_
|dkr|||_||_n(td|d dd|_td|d dd|_|S )zN Cancel b + sign*s and a + sign*s
            This is for meijer G functions. Nrs   rV   F)evaluate)r
   r   rn  r;  r   r   r   r   r   rP   r<  ro  rp  r   )r^   rj   ri   signr  r   rD  rr  r[   r[   r\   _meijer\  s     
zReduceOrder._meijerc                 C   s   |  ||dS )Nrs   ru  )r^   rj   ri   r[   r[   r\   meijer_minusv  s    zReduceOrder.meijer_minusc                 C   s   |  d| d| dS rU   rv  )r^   ri   rj   r[   r[   r\   meijer_plusz  s    zReduceOrder.meijer_plusc                 C   s   d| j | jf S )Nz4<Reduce order by cancelling upper %s with lower %s.>)ro  rp  r   r[   r[   r\   rI  ~  s    
zReduceOrder.__str__N)
r   r   r   r   r   classmethodru  rw  rx  rI  r[   r[   r[   r\   rm  C  s   


rm  c           	      C   s   t | } t |}| j|d |j|d g }g }| D ]\}d}tt|D ](}|||| }|durH||  qrqH|du r|| q4|| q4|||fS )z? Order reduction algorithm used in Hypergeometric and Meijer G r   N)r   r   r   r   rU  rb   )	rd   re   genr   nap	operatorsri   r=  r   r[   r[   r\   _reduce_order  s"    
r}  c                 C   s.   t | j| jtt\}}}tt| t| |fS )a  
    Given the hypergeometric function ``func``, find a sequence of operators to
    reduces order as much as possible.

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

    Return (newfunc, [operators]), where applying the operators to the
    hypergeometric function newfunc yields func.

    Examples
    ========

    >>> from sympy.simplify.hyperexpand import reduce_order, Hyper_Function
    >>> reduce_order(Hyper_Function((1, 2), (3, 4)))
    (Hyper_Function((1, 2), (3, 4)), [])
    >>> reduce_order(Hyper_Function((1,), (1,)))
    (Hyper_Function((), ()), [<Reduce order by cancelling upper 1 with lower 1.>])
    >>> reduce_order(Hyper_Function((2, 4), (3, 3)))
    (Hyper_Function((2,), (3,)), [<Reduce order by cancelling
    upper 4 with lower 3.>])
    )r}  rd   re   rm  r   ra   r   )rg   r{  nbqr|  r[   r[   r\   reduce_order  s    r  c                 C   sN   t | j| jtjdd \}}}t | j| jtjt\}}}t	|||||| fS )a  
    Given the Meijer G function parameters, ``func``, find a sequence of
    operators that reduces order as much as possible.

    Return newfunc, [operators].

    Examples
    ========

    >>> from sympy.simplify.hyperexpand import (reduce_order_meijer,
    ...                                         G_Function)
    >>> reduce_order_meijer(G_Function([3, 4], [5, 6], [3, 4], [1, 2]))[0]
    G_Function((4, 3), (5, 6), (3, 4), (2, 1))
    >>> reduce_order_meijer(G_Function([3, 4], [5, 6], [3, 4], [1, 8]))[0]
    G_Function((3,), (5, 6), (3, 4), (1,))
    >>> reduce_order_meijer(G_Function([3, 4], [5, 6], [7, 5], [1, 5]))[0]
    G_Function((3,), (), (), (1,))
    >>> reduce_order_meijer(G_Function([3, 4], [5, 6], [7, 5], [5, 3]))[0]
    G_Function((), (), (), ())
    c                 S   s
   t |  S r_   r   r|   r[   r[   r\   r     r   z%reduce_order_meijer.<locals>.<lambda>)
r}  r   re   rm  rx  r   rd   rw  r   r   )rg   r   r~  Zops1Znbmr{  Zops2r[   r[   r\   reduce_order_meijer  s    

r  c                    s    fdd}|S )z? Create a derivative operator, to be passed to Operator.apply. c                    s(   |   |    }|t}|S r_   )r   	applyfuncr   )ro   r@  rp   rl   r[   r\   doit  s    z&make_derivative_operator.<locals>.doitr[   )rp   rl   r  r[   r  r\   make_derivative_operator  s    r  c                 C   s"   | }t |D ]}|||}q|S )zk
    Apply the list of operators ``ops`` to object ``obj``, substituting
    ``op`` for the generator.
    )reversedrB  )r   opsr=  rf   or[   r[   r\   apply_operators  s    r  c                    sv  dd | j | j|j |jfD \}}}}tt| tt| ksftt| tt| krvtd| |f g }dd   fdd} fdd	}	tt| t|  td
D ]}
d}d}d}d}|
|v r||
 }||
 }|
|v r||
 }||
 }t|t|ks*t|t|kr:td| |f dd ||||fD \}}}}dd }|||
}|||
}t|dkr||	g ||||7 }nt|dkr|||g |||7 }n|d }|d }|d | dks|d | dkrtd|| dkr0|||||||7 }||	|||||7 }n(||	|||||7 }|||||||7 }|||
< |||
< q|  |S )a(  
    Devise a plan (consisting of shift and un-shift operators) to be applied
    to the hypergeometric function ``target`` to yield ``origin``.
    Returns a list of operators.

    Examples
    ========

    >>> from sympy.simplify.hyperexpand import devise_plan, Hyper_Function
    >>> from sympy.abc import z

    Nothing to do:

    >>> devise_plan(Hyper_Function((1, 2), ()), Hyper_Function((1, 2), ()), z)
    []
    >>> devise_plan(Hyper_Function((), (1, 2)), Hyper_Function((), (1, 2)), z)
    []

    Very simple plans:

    >>> devise_plan(Hyper_Function((2,), ()), Hyper_Function((1,), ()), z)
    [<Increment upper 1.>]
    >>> devise_plan(Hyper_Function((), (2,)), Hyper_Function((), (1,)), z)
    [<Increment lower index #0 of [], [1].>]

    Several buckets:

    >>> from sympy import S
    >>> devise_plan(Hyper_Function((1, S.Half), ()),
    ...             Hyper_Function((2, S('3/2')), ()), z) #doctest: +NORMALIZE_WHITESPACE
    [<Decrement upper index #0 of [3/2, 1], [].>,
    <Decrement upper index #0 of [2, 3/2], [].>]

    A slightly more complicated plan:

    >>> devise_plan(Hyper_Function((1, 3), ()), Hyper_Function((2, 2), ()), z)
    [<Increment upper 2.>, <Decrement upper index #0 of [2, 2], [].>]

    Another more complicated plan: (note that the ap have to be shifted first!)

    >>> devise_plan(Hyper_Function((1, -1), (2,)), Hyper_Function((3, -2), (4,)), z)
    [<Decrement lower 3.>, <Decrement lower 4.>,
    <Decrement upper index #1 of [-1, 2], [4].>,
    <Decrement upper index #1 of [-1, 3], [4].>, <Increment upper -2.>]
    c                 S   s   g | ]}t |tqS r[   r   r   r[   r[   r\   r     s   zdevise_plan.<locals>.<listcomp>z%s not reachable from %sc                 S   sr   g }t t| D ]\}|| | |  dkr2|}d}n|}d}|| | | kr||| |g7 }| |  |7  < q:q|S )Nr   rV   rs   )r   r   )frotoincdecr  r   shchr[   r[   r\   	do_shifts"  s    zdevise_plan.<locals>.do_shiftsc                    s    | |dd  fddS )z( Shift us from (nal, nbk) to (al, nbk). c                 S   s   t | | S r_   )rE  rD  r   r[   r[   r\   r   4  r   z2devise_plan.<locals>.do_shifts_a.<locals>.<lambda>c                    s   t |    |S r_   )rN  r  )aotherbothernbkrl   r[   r\   r   5  r   r[   )nalr  alr  r  r  rl   )r  r  r  r\   do_shifts_a2  s    z devise_plan.<locals>.do_shifts_ac                    s    || fdddd S )z( Shift us from (nal, nbk) to (nal, bk). c                    s   t   |  |S r_   )r[  r  )r  r  r  rl   r[   r\   r   :  r   z2devise_plan.<locals>.do_shifts_b.<locals>.<lambda>c                 S   s   t | | S r_   )rK  r  r[   r[   r\   r   ;  r   r[   )r  r  bkr  r  r  )r  r  r  r\   do_shifts_b7  s    z devise_plan.<locals>.do_shifts_br   r[   c                 S   s   g | ]}t t|td qS )r   )sortedr   r   r   r[   r[   r\   r   K  s   c                 S   s2   g }|   D ] \}}||kr|t| | 7 }q|S r_   )r   r   )r   r   r   rr  valuer[   r[   r\   othersN  s
    zdevise_plan.<locals>.othersr   rs   zNon-suitable parameters.)	rd   re   r   r   r   r  r  r   r   )r%  originrl   r   r   Z	nabucketsZ	nbbucketsr  r  r  r@  r  r  r  r  r  r  r  Znamaxamaxr[   r  r\   devise_plan  s^    . &
$


$
r  c                    s  t | jtt | jt }}t|tj dkr0dS |tj d }|dkrJdS tj|vrXdS t|tj }|  |d   dkrdS t| j}|	| t| j}|	   d8   fdd|D } fdd|D }g }t
|d D ]|td  q|  t |   }	|	t fdd|D  9 }	|	t fdd|D   }	|t|	g7 }d}
t
 D ]R| t }|tfd	d|D  9 }|tfd
d|D   }|
|7 }
qdt||||
 fS )z? Try to recognise a hypergeometric sum that starts from k > 0. rV   Nr   c                    s   g | ]}|  qS r[   r[   r   rr  r[   r\   r     r   z#try_shifted_sum.<locals>.<listcomp>c                    s   g | ]}|  qS r[   r[   r   r  r[   r\   r     r   c                    s   g | ]}t | qS r[   r5   r   r  r[   r\   r     r   c                    s   g | ]}t | qS r[   r  r   r  r[   r\   r     r   c                    s   g | ]}t | qS r[   r  r   r  r[   r\   r     r   c                    s   g | ]}t | qS r[   r  r   r  r[   r\   r     r   )rT   rd   r]   re   r   r   r   r   r   remover   rb   rE  r   r6   r   rC  ra   )rg   rl   r   r   r@  r   r{  r~  r  facrD  r   r[   )rr  r  r\   try_shifted_sumt  sF    




r  c           
         s  t | jtt | jt }}|tj }|tj }|  |  dd |D }dd |D   rxt fdd|D rxtS |sdS |d }d}tj	}	t
tt|  D ]X||9 }|d  }|tfd	d| jD  9 }|tfd
d| jD   }|	|7 }	q|	S )zj Recognise polynomial cases. Returns None if not such a case.
        Requires order to be fully reduced. c                 S   s   g | ]}|d kr|qS r  r[   r   r[   r[   r\   r     r   z"try_polynomial.<locals>.<listcomp>c                 S   s   g | ]}|d kr|qS r  r[   r   r[   r[   r\   r     r   c                 3   s   | ]}| d  k V  qdS )rs   Nr[   r   )bl0r[   r\   r     r   z!try_polynomial.<locals>.<genexpr>Nrs   rV   c                    s   g | ]}|  qS r[   r[   r   r  r[   r\   r     r   c                    s   g | ]}|  qS r[   r[   r   r  r[   r\   r     r   )rT   rd   r]   re   r   r   r   allr   r   r   r   r   r   )
rg   rl   r   r   r  rY  Zal0ri   r  rf   r[   )r  r  r\   try_polynomial  s*    


r  c           '      C   s  t | jtt | jt }}i }| D ]F\}}|dkrD||vrD dS || }t|t|f||< ||d q&|i krzdS tj|vrdS |tj \}}||dg f|tj< t	d}	tj
}
tj
}| D ]\}\}}t|t|kr dS t||D ]f\}}|| jr0|| }|
t||	 |9 }
|t||9 }q|| }|
t||9 }
|t||	 |9 }qqt|
| |	}t|}g }i }|D ]4}| \}
}||	st|
|	}|jstd| \\}}||| |fg7 }q|
|	rtd||	\}\}d}|jr|j}|j}||	kr,|dk nn|jr||	\}}d}||	kr^||	\}}|||	 | kr|td| || }||| 9 }ntd||g  |
| |f qi }i }t	d	}|j!d
d d ddd|  i}|r t"|d d D ] }||| #| ||d < q|D ]&\}}|tj
g  |||   q$| D ]\}}|D ]$\} }|t$|||g  |  q`|j!dd d t"d|d d d D ]8}| t$|||fdt$||d |fg|t$|||< q| t$|d|fdd|  tj
fg|t$|d|< qTi }!t%tj
gt|&  D ]\}}||!|< q6dd t't|! dd dD }"t(|"}#t(dgt|# g}$| D ]\}} t|  |$|!| < qt)t|#}%| D ].\}}|D ]\} }&| |%|!| |!|& f< q̐qt*| |dg |#|$|%S )z
    Try to find an expression for Hyper_Function ``func`` in terms of Lerch
    Transcendents.

    Return None if no such expression can be found.
    r   NrV   r   zp should be monomialz<Need partial fraction decomposition with linear denominatorszunrecognised form %sz%unrecognised form of partial fractionrl   c                 S   s   | d S rU   r[   r|   r[   r[   r\   r      r   ztry_lerchphi.<locals>.<lambda>r   rs   c                 S   s   | d S rU   r[   r|   r[   r[   r\   r   *  r   rr   c                 S   s   g | ]\}}t |qS r[   )r   )r   rj   r  r[   r[   r\   r   3  r   z try_lerchphi.<locals>.<listcomp>c                 S   s   | d S rU   r[   r|   r[   r[   r\   r   4  r   )+rT   rd   r]   re   r   r   rU  r   r   r   r   r   r   is_positiver5   rN   r   	make_argsr   r	  rP   is_monomialr  LTNotImplementedErroras_coeff_mulis_Powr   baseis_Addas_independentr+  rb   r   r   r   r7   	enumerater   r  rK   rM   rc   )'rg   r   r   pairedr   r  Zbvalueaintsbintsr   r   r   Zavalueri   rj   rr  partr   	monomialstermsr   rD  indepdepr  tmpr  derivr>  rl   monr   rZ   transbasisrn   ro   rp   b2r[   r[   r\   try_lerchphi  s    






  
r  c                 C   s  t d}| jrdd | jD }dd | jD }tt|  |t|   }t|t}| }g }t|}t|D ]d}	| jd |	 }
|t	|
gt
| jdd  | j|g7 }|	|d k rn|
 ||	|	f< |
||	|	d f< qnt|}tdgdg|d   g}t|g}t|D ]}	||||	   q| }|  dg| }t|D ]:\}	}t|||	  D ]\}}||  || 7  < q\qDt|D ]@\}	}| ||d  d|d f  | d  ||d |	f< qt| |dg |||S g }t
| jdd }tt|D ](}|t	g ||g7 }||  d7  < q |t	g ||g7 }t|}t|}tdgdg|d   g}t|}|t| j  |d|d f< td|D ]8}	| j|	d  ||	|	d f< | j|	d   ||	|	f< qt| |dg |||S dS )zU
    Create a formula object representing the hypergeometric function ``func``.

    rl   c                 S   s   g | ]}t | qS r[   r   r   r[   r[   r\   r   K  r   z0build_hypergeometric_formula.<locals>.<listcomp>c                 S   s   g | ]}t | d  qS r   r   r   r[   r[   r\   r   L  r   r   rV   N)r   rd   re   r   r   rP   r   rM   r   r>   r   rK   rL   rb   r   r   r  rc   r   )rg   rl   r  r  r   rO   r  r  rp   rr  ri   rn   ro   derivsr   rf   rZ   r@  rA  re   r   r[   r[   r\   build_hypergeometric_formula@  sX    	
(

:r  c           
      C   s  t | t | }}|}t|}|dkr,tjS ddlm} |dkrx|dkrx| | \}}}	|dkrt|	| | t|	 t|	|  t|	|  S |dkr||| |	 dkr|| }}|dkrx||| |	 dkrx|jr8|jr8dt	t
| d  t|  t|| d  t| d  t|d | d  S t|d d t|| d  t|d  t|d | d  S t| ||S )z
    Try to find a closed-form expression for hyper(ap, bq, z), where ``z``
    is supposed to be a "special" value, e.g. 1.

    This function tries various of the classical summation formulae
    (Gauss, Saalschuetz, etc).
    r   r   rr   rV   rs   )r   r=   r   r   sympy.simplify.simplifyr   r"   r   r   r    r   r>   )
rd   re   rl   rD  qZz_r   ri   rj   rZ   r[   r[   r\   hyperexpand_specialz  s2    0
 ,
r  Nz0rV   defaultc                    s"  j rtjS ddlm} tdddkr0d fdd}td	u rTt atd
|  t	| \} }	|	rztd|  ntd t
| }
|
d	urtd t|
|	fdd}t|  fdd}t||S tj}t| }
|
d	ur|
\} }}td|  |	|7 }	t||	fdd}t|  fdd}||}tdv rt| jt| jfdkrt| }|||	tt}|ts|| S t| }|d	u rt| }|d	u rtdd t| }td|jd|j |	t| |j7 }	|||	| }t|ddttS )a7  
    Try to find an expression for the hypergeometric function ``func``.

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

    The result is expressed in terms of a dummy variable ``z0``. Then it
    is multiplied by ``premult``. Then ``ops0`` is applied.
    ``premult`` must be a*z**prem for some a independent of ``z``.
    r   r  F)r0  r  nonrepsmallc              	      s   t | j| j|t| j| j}t | t| j| jt| jjd   }dkrr|t	}t
dd t|| j| jtj }|}r|}|S )Nr   rV   c                 S   s   | |d |d   S r  r[   r  r[   r[   r\   r     r   z5_hyperexpand.<locals>.carryout_plan.<locals>.<lambda>)r  ro   r0  rl   r  rp   rL   shaper  r   r   r   rn   r   r   rewrite)r,  r  ro   r@  rf   ops0prempremultr  rl   r  r[   r\   carryout_plan  s"    (
z#_hyperexpand.<locals>.carryout_planNz)Trying to expand hypergeometric function   Reduced order to   Could not reduce order.z  Recognised polynomial.c                    s    |    S r_   r   r,  r  r[   r\   r     r   z_hyperexpand.<locals>.<lambda>c                    s    |    S r_   r  r  r  r[   r\   r     r   z+  Recognised shifted sum, reduced order to c                    s    |    S r_   r  r  r  r[   r\   r     r   c                    s    |    S r_   r  r  r  r[   r\   r     r   )rV   rs   )rr   rV   z  Could not find an origin. z@Will return answer in terms of simpler hypergeometric functions.z  Found an origin:  Tpolar)is_zeror   r   r  r   r<   _collectionr(  r   r  r  r  r=   r0  r   r  r   rd   re   r  replacer>   r  r	  r4  r  r  rg   r  rR   )rg   rl   r  r  r  r  r  r   r  r  rf   rD  Znopsr,  r@  r:  r[   r  r\   _hyperexpand  s\    





(


r  c                    s`  dd }t | j t | jt | jt | jg }d}|rd}| |j fddd }|dur||g7 }d}q8||j fd	dd }|dur||g7 }d}q8||j fd
dd  }|dur||g7 }d}q8||j fddd  }|dur<||g7 }d}q8| |j fdddg }|durp||g7 }d}q8||jfdddg }|dur||g7 }d}q8||jfdddg }|dur||g7 }d}q8||jfdddg }|dur8||g7 }d}q8q8 t |jksLt |jksLt |jksLt |jkrTtd|  |S )a  
    Find operators to convert G-function ``fro`` into G-function ``to``.

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

    It is assumed that ``fro`` and ``to`` have the same signatures, and that in fact
    any corresponding pair of parameters differs by integers, and a direct path
    is possible. I.e. if there are parameters a1 b1 c1  and a2 b2 c2 it is
    assumed that a1 can be shifted to a2, etc. The only thing this routine
    determines is the order of shifts to apply, nothing clever will be tried.
    It is also assumed that ``fro`` is suitable.

    Examples
    ========

    >>> from sympy.simplify.hyperexpand import (devise_plan_meijer,
    ...                                         G_Function)
    >>> from sympy.abc import z

    Empty plan:

    >>> devise_plan_meijer(G_Function([1], [2], [3], [4]),
    ...                    G_Function([1], [2], [3], [4]), z)
    []

    Very simple plans:

    >>> devise_plan_meijer(G_Function([0], [], [], []),
    ...                    G_Function([1], [], [], []), z)
    [<Increment upper a index #0 of [0], [], [], [].>]
    >>> devise_plan_meijer(G_Function([0], [], [], []),
    ...                    G_Function([-1], [], [], []), z)
    [<Decrement upper a=0.>]
    >>> devise_plan_meijer(G_Function([], [1], [], []),
    ...                    G_Function([], [2], [], []), z)
    [<Increment lower a index #0 of [], [1], [], [].>]

    Slightly more complicated plans:

    >>> devise_plan_meijer(G_Function([0], [], [], []),
    ...                    G_Function([2], [], [], []), z)
    [<Increment upper a index #0 of [1], [], [], [].>,
    <Increment upper a index #0 of [0], [], [], [].>]
    >>> devise_plan_meijer(G_Function([0], [], [0], []),
    ...                    G_Function([-1], [], [1], []), z)
    [<Increment upper b=0.>, <Decrement upper a=0.>]

    Order matters:

    >>> devise_plan_meijer(G_Function([0], [], [0], []),
    ...                    G_Function([1], [], [1], []), z)
    [<Increment upper a index #0 of [0], [], [1], [].>, <Increment upper b=0.>]
    c                    sp   t t| |D ]\\}\ } | jr|  | dkrt fdd|D r||}| |  |7  < |  S qdS )aD   Try to apply ``shifter`` in order to bring some element in ``f``
            nearer to its counterpart in ``to``. ``diff`` is +/- 1 and
            determines the effect of ``shifter``. Counter is a list of elements
            blocking the shift.

            Return an operator if change was possible, else None.
        r   c                 3   s   | ]} |kV  qd S r_   r[   r   r   r[   r\   r   R  r   z8devise_plan_meijer.<locals>.try_shift.<locals>.<genexpr>N)r  r   r   r  )r,  r   Zshifterr   counteridxrj   r  r[   r   r\   	try_shiftG  s    z%devise_plan_meijer.<locals>.try_shiftTFc                    s   t  | S r_   )rh  r   fanfapfbmfbqrl   r[   r\   r   _  r   z$devise_plan_meijer.<locals>.<lambda>rV   Nc                    s   t  | S r_   )rl  r  r  r[   r\   r   f  r   c                    s   t  | S r_   )ra  r  r  r[   r\   r   m  r   rs   c                    s   t  | S r_   )rk  r  r  r[   r\   r   t  r   c                    s   t  |  S r_   )r^  r  )r  r[   r\   r   z  r   c                    s   t  |  S r_   )r`  r  )r  r[   r\   r     r   c                    s   t  |  S r_   )r\  r  )r  r[   r\   r     r   c                    s   t  |  S r_   )r_  r  )r  r[   r\   r     r   zCould not devise plan.)r   r   rd   r   re   r  r   )r  r  rl   r  r  changer=  r[   r  r\   devise_plan_meijer  s    :















0r  Fc                    s  t du rt a |dkrd}| }td|  td}t| \} rLtd|  ntd t | }|durtd|j t|j| |7 t|j	
|j|t|j
|j||}|t|}||j
|j| }	|	d 
||}	t|	d	d
S td dd   fdd}
td|
| j| j| j| j||\}}dd }D ](}t|j
|d tt it|_q>|
|| j|| j|| j|| jd| \}}t|
||d	d
}t|
d| d	d
}t|ts|
d| }| |}|jdks>|jdkrZt|jt|jkrZt|jdk durZt|tdkrZ|durLd	}|durZd	}|d	u rv| |ppd}n| |pd}|d	u r| |pd}n| |pd}|dur|dur|dkrd}|t!krd}t|ts|
||}t|ts|
||}dd }|||}|||}t"||ddt#fkrV||k rR|S |S t$|d |d dkrt$|d |d dkrt%||f||f||d	fS t%||f||f||d	f}	|	&t'r|std |	&t'r|r|	S ||S )a  
    Try to find an expression for the Meijer G function specified
    by the G_Function ``func``. If ``allow_hyper`` is True, then returning
    an expression in terms of hypergeometric functions is allowed.

    Currently this just does Slater's theorem.
    If expansions exist both at zero and at infinity, ``place``
    can be set to ``0`` or ``zoo`` for the preferred choice.
    Nr  z1Try to expand Meijer G function corresponding to rl   r  r  z  Found a Meijer G formula: r   Tr  z;  Could not find a direct formula. Trying Slater's theorem.c                 S   sP   | D ]F}t | | dkrd}||v r0t || }|d t | | k r dS qdS )z Test if slater applies. rV   r   FTr   )r   r   r   r   r[   r[   r\   can_do  s    z_meijergexpand.<locals>.can_doc           %   
      s   t | |||}| \}}}	}||	s2tjdfS t| t| t|t| k }
t| t| t|t| kr~tdk }
|
du rtjdfS tj}|D ]Z}t|| dkr|| d d}t|}| |D ]}|t| 9 }q| D ]}|td | 9 }q|D ]}|td |  }q|D ]}|t|  }q0fddt| t| D }fddt|t| D }t	tj
t|t|  }|| }|  }tt||||d d}||| 7 }q|| d fdd|| dd  D }t|}fd	d|	| d |d  D }t|}|| D ]}|| q@t|}|	| d | D ]}|| qj|d
 }dd t||D }td}| } |D ]6}t|ds|jrtt|}| t|| 9 } q| D ]}| td| | 9 } q|D ]}| td| |  } q|D ]}| t||  } q(t| } ttt|D ]2}!t| ||! }"t|"fdd}"||"8 }qX|  t	tj
t|t| d  }|| }|   } fddt| t| D dg } fddt|t| D }tt|||| d d}tj
| t| }#t|D ]2}$|#tj
||$  t|||$  d ||$  9 }#q>| D ]}|#td|   9 }#qv|D ]}|#t|  9 }#q|D ]}|#t|   }#q|D ]}|#td|    }#q||#| 7 }q||
fS )NFrV   r   c                    s   g | ]}d   | qS r   r[   r   bhr[   r\   r     r   z5_meijergexpand.<locals>.do_slater.<locals>.<listcomp>c                    s   g | ]}d   | qS r   r[   r   r  r[   r\   r     r   r  c                    s   g | ]}|  qS r[   r[   )r   rM  b_r[   r\   r   	  r   c                    s   g | ]}|  qS r[   r[   )r   rG  r  r[   r\   r   	  r   rs   c                 S   s   g | ]\}}|| qS r[   r[   )r   r   rr  r[   r[   r\   r   	  r   r   c                    s    |    S r_   r  r  r   r[   r\   r   !	  r   z3_meijergexpand.<locals>.do_slater.<locals>.<lambda>c                    s   g | ]}d   | qS r   r[   r   aur[   r\   r   )	  r   c                    s   g | ]}d   | qS r   r[   r   r  r[   r\   r   *	  r   )r   r   r   r   r   r   r   r  r"   r1   NegativeOner  ra   r   r   r   rW   introundr   r   rQ   r  r6   r5   )%r   r   rd   re   rl   Zzfinalrg   r  r   r   condrf   r   r  Zborq  ajr{  r~  rr  Zhargr  Zhypkir   lirj   Zaori   ludir   	integrandr@  residro   r   r  r  r   )r  r  r  rl   r\   	do_slater  s    

  


"$0z!_meijergexpand.<locals>.do_slaterr   c                 S   s   dd | D S )Nc                 S   s   g | ]}d | qS r   r[   r   r[   r[   r\   r   C	  r   z._meijergexpand.<locals>.tr.<locals>.<listcomp>r[   )r   r[   r[   r\   r   B	  s    z_meijergexpand.<locals>.trrV   rs   FZnonrepr  c                 S   sJ   |du rd}n|du rd}nd}|  ttt tr6d}|| t|  fS )NTr   FrV   rr   ru   )r	  r   r   r   countr>   	count_ops)r   r  c0r[   r[   r\   weightr	  s    z_meijergexpand.<locals>.weightz@  Could express using hypergeometric functions, but not allowed.)(_meijercollectionr9  r   r   r  r4  rg   r  r  ro   r0  rl   r  rp   r  r   rn   rR   r   r   rd   re   rP   r<  r   r   r   deltar   r9   nur1   r  r   r  r   r  r8   r	  r>   )rg   r  allow_hyperr  placeZfunc0rl   r,  ro   r@  r  Zslater1cond1r   r=  Zslater2cond2r   r  w1w2r[   r  r\   _meijergexpand  s    


g&"








0r  c                    s8   t | } fdd} fdd}| t|t|S )a  
    Expand hypergeometric functions. If allow_hyper is True, allow partial
    simplification (that is a result different from input,
    but still containing hypergeometric functions).

    If a G-function has expansions both at zero and at infinity,
    ``place`` can be set to ``0`` or ``zoo`` to indicate the
    preferred choice.

    Examples
    ========

    >>> from sympy.simplify.hyperexpand import hyperexpand
    >>> from sympy.functions import hyper
    >>> from sympy.abc import z
    >>> hyperexpand(hyper([], [], z))
    exp(z)

    Non-hyperegeometric parts of the expression and hypergeometric expressions
    that are not recognised are left unchanged:

    >>> hyperexpand(1 + hyper([1, 1, 1], [], z))
    hyper((1, 1, 1), (), z) + 1
    c                    s0   t t| || d}|d u r(t| ||S |S d S )Nr  )r  ra   r>   rd   re   rl   r@  r  r[   r\   
do_replace	  s    zhyperexpand.<locals>.do_replacec                    sF   t t| d | d |d |d | d}|tttt sB|S d S )Nr   rV   )r  r  )r  r   r	  r   r   r   r  r  r  r  r[   r\   	do_meijer	  s
     zhyperexpand.<locals>.do_meijer)r
   r  r>   rJ   )r,  r  r  r  r  r  r[   r  r\   hyperexpand	  s    r  )Fr  N)Fr  N)r   collectionsr   	itertoolsr   	functoolsr   mathr   sympyr   
sympy.corer   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   Zsympy.core.modr   sympy.core.sortingr   sympy.functionsr   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   $sympy.functions.elementary.complexesr<   r=   sympy.functions.special.hyperr>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   sympy.matricesrK   rL   rM   sympy.polysrN   rO   rP   Zsympy.seriesrQ   sympy.simplify.powsimprR   sympy.utilities.iterablesrT   r]   r   r   r   r   ra   r   r   rc   r(  r   r9  r;  rC  rE  rK  rN  r[  r\  r^  r_  r`  ra  rh  rk  rl  rm  r}  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r[   r[   r[   r\   <module>   s   :L<  "C @ L)5)*&/31@	 
- :%
k   
  