o
    *ήcbS                     @   s  d Z ddlmZ ddlmZmZmZmZmZ ddl	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mZ ddlmZmZmZ dd	lmZmZ dd
lmZ ddl m!Z! G dd deZ"G dd deZ#G dd deZ$G dd deZ%G dd deZ&G dd deZ'dS )z$ Riemann zeta and related function.     )Add)FunctionSsympifypiI)ArgumentIndexError
expand_mul)Dummy)	bernoulli	factorialharmonic)re
unpolarifyAbs
polar_lift)log	exp_polarexp)ceilingfloor)sqrt)Polyc                   @   s:   e Zd ZdZdd ZdddZdd Zd	d
 Zdd ZdS )lerchphia]  
    Lerch transcendent (Lerch phi function).

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

    For $\operatorname{Re}(a) > 0$, $|z| < 1$ and $s \in \mathbb{C}$, the
    Lerch transcendent is defined as

    .. math :: \Phi(z, s, a) = \sum_{n=0}^\infty \frac{z^n}{(n + a)^s},

    where the standard branch of the argument is used for $n + a$,
    and by analytic continuation for other values of the parameters.

    A commonly used related function is the Lerch zeta function, defined by

    .. math:: L(q, s, a) = \Phi(e^{2\pi i q}, s, a).

    **Analytic Continuation and Branching Behavior**

    It can be shown that

    .. math:: \Phi(z, s, a) = z\Phi(z, s, a+1) + a^{-s}.

    This provides the analytic continuation to $\operatorname{Re}(a) \le 0$.

    Assume now $\operatorname{Re}(a) > 0$. The integral representation

    .. math:: \Phi_0(z, s, a) = \int_0^\infty \frac{t^{s-1} e^{-at}}{1 - ze^{-t}}
                                \frac{\mathrm{d}t}{\Gamma(s)}

    provides an analytic continuation to $\mathbb{C} - [1, \infty)$.
    Finally, for $x \in (1, \infty)$ we find

    .. math:: \lim_{\epsilon \to 0^+} \Phi_0(x + i\epsilon, s, a)
             -\lim_{\epsilon \to 0^+} \Phi_0(x - i\epsilon, s, a)
             = \frac{2\pi i \log^{s-1}{x}}{x^a \Gamma(s)},

    using the standard branch for both $\log{x}$ and
    $\log{\log{x}}$ (a branch of $\log{\log{x}}$ is needed to
    evaluate $\log{x}^{s-1}$).
    This concludes the analytic continuation. The Lerch transcendent is thus
    branched at $z \in \{0, 1, \infty\}$ and
    $a \in \mathbb{Z}_{\le 0}$. For fixed $z, a$ outside these
    branch points, it is an entire function of $s$.

    Examples
    ========

    The Lerch transcendent is a fairly general function, for this reason it does
    not automatically evaluate to simpler functions. Use ``expand_func()`` to
    achieve this.

    If $z=1$, the Lerch transcendent reduces to the Hurwitz zeta function:

    >>> from sympy import lerchphi, expand_func
    >>> from sympy.abc import z, s, a
    >>> expand_func(lerchphi(1, s, a))
    zeta(s, a)

    More generally, if $z$ is a root of unity, the Lerch transcendent
    reduces to a sum of Hurwitz zeta functions:

    >>> expand_func(lerchphi(-1, s, a))
    zeta(s, a/2)/2**s - zeta(s, a/2 + 1/2)/2**s

    If $a=1$, the Lerch transcendent reduces to the polylogarithm:

    >>> expand_func(lerchphi(z, s, 1))
    polylog(s, z)/z

    More generally, if $a$ is rational, the Lerch transcendent reduces
    to a sum of polylogarithms:

    >>> from sympy import S
    >>> expand_func(lerchphi(z, s, S(1)/2))
    2**(s - 1)*(polylog(s, sqrt(z))/sqrt(z) -
                polylog(s, sqrt(z)*exp_polar(I*pi))/sqrt(z))
    >>> expand_func(lerchphi(z, s, S(3)/2))
    -2**s/z + 2**(s - 1)*(polylog(s, sqrt(z))/sqrt(z) -
                          polylog(s, sqrt(z)*exp_polar(I*pi))/sqrt(z))/z

    The derivatives with respect to $z$ and $a$ can be computed in
    closed form:

    >>> lerchphi(z, s, a).diff(z)
    (-a*lerchphi(z, s, a) + lerchphi(z, s - 1, a))/z
    >>> lerchphi(z, s, a).diff(a)
    -s*lerchphi(z, s + 1, a)

    See Also
    ========

    polylog, zeta

    References
    ==========

    .. [1] Bateman, H.; Erdelyi, A. (1953), Higher Transcendental Functions,
           Vol. I, New York: McGraw-Hill. Section 1.11.
    .. [2] http://dlmf.nist.gov/25.14
    .. [3] https://en.wikipedia.org/wiki/Lerch_transcendent

    c           	         s  | j \ dkrt S jrIdkrItd}t|    |dd|  }tj}t D ]}||| 7 }||	| }q3|
|S  jrtj}tj} dkr|t  krbd8  8    }t fddtD  }n# dkrt  d  7   }t fddtD  }t j jg\tdt t  	d  ||d   t	fddtD    S ttrj d tt  jsd	tt fv r;d	krtddg\n2tkrtdd
g\n$t krtd	d
g\nj d dt t  }t|j|jg\t fddtD  S t S )N   r   tc                    s&   g | ]}|    |   qS  r   .0kanszr   M/tmp/pip-target-vg8gfxp4/lib/python/sympy/functions/special/zeta_functions.py
<listcomp>   s   & z.lerchphi._eval_expand_func.<locals>.<listcomp>c                    s,   g | ]}d  |   | d    qS r   r   r   r    r   r%   r&      s   ,    c                    s>   g | ]}t |  jd i  t|    qS )r   )polylog_eval_expand_funcr   r   )hintsmrootr#   zetr   r%   r&      s
       c                    sB   g | ]}t d t t |     t|    qS )r(   )r   r   r   zetar   )r!   pqr#   r   r%   r&      s    :)argsr1   
is_Integerr
   r   r   Zeroreversed
all_coeffsdiffsubsis_RationalOner   r   ranger2   r3   r   r   r   
isinstancer   r   )	selfr+   r   startrescaddmulargr   )
r!   r+   r,   r"   r2   r3   r-   r#   r$   r.   r%   r*   }   s`   

" 0
zlerchphi._eval_expand_funcr   c                 C   sZ   | j \}}}|dkr| t||d | S |dkr+t||d ||t|||  | S t)N   r   )r4   r   r   )r?   argindexr$   r#   r!   r   r   r%   fdiff   s   $zlerchphi.fdiffc                 C   s   |   }||r|S | S N)r*   has)r?   r$   r#   r!   targetrA   r   r   r%   _eval_rewrite_helper   s   
zlerchphi._eval_rewrite_helperc                 K      |  |||tS rI   )rL   r1   r?   r$   r#   r!   kwargsr   r   r%   _eval_rewrite_as_zeta      zlerchphi._eval_rewrite_as_zetac                 K   rM   rI   )rL   r)   rN   r   r   r%   _eval_rewrite_as_polylog   rQ   z!lerchphi._eval_rewrite_as_polylogNr'   )	__name__
__module____qualname____doc__r*   rH   rL   rP   rR   r   r   r   r%   r      s    i
<	r   c                       sP   e Zd ZdZedd ZdddZdd Zd	d
 Zdd Z	d fdd	Z
  ZS )r)   a  
    Polylogarithm function.

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

    For $|z| < 1$ and $s \in \mathbb{C}$, the polylogarithm is
    defined by

    .. math:: \operatorname{Li}_s(z) = \sum_{n=1}^\infty \frac{z^n}{n^s},

    where the standard branch of the argument is used for $n$. It admits
    an analytic continuation which is branched at $z=1$ (notably not on the
    sheet of initial definition), $z=0$ and $z=\infty$.

    The name polylogarithm comes from the fact that for $s=1$, the
    polylogarithm is related to the ordinary logarithm (see examples), and that

    .. math:: \operatorname{Li}_{s+1}(z) =
                    \int_0^z \frac{\operatorname{Li}_s(t)}{t} \mathrm{d}t.

    The polylogarithm is a special case of the Lerch transcendent:

    .. math:: \operatorname{Li}_{s}(z) = z \Phi(z, s, 1).

    Examples
    ========

    For $z \in \{0, 1, -1\}$, the polylogarithm is automatically expressed
    using other functions:

    >>> from sympy import polylog
    >>> from sympy.abc import s
    >>> polylog(s, 0)
    0
    >>> polylog(s, 1)
    zeta(s)
    >>> polylog(s, -1)
    -dirichlet_eta(s)

    If $s$ is a negative integer, $0$ or $1$, the polylogarithm can be
    expressed using elementary functions. This can be done using
    ``expand_func()``:

    >>> from sympy import expand_func
    >>> from sympy.abc import z
    >>> expand_func(polylog(1, z))
    -log(1 - z)
    >>> expand_func(polylog(0, z))
    z/(1 - z)

    The derivative with respect to $z$ can be computed in closed form:

    >>> polylog(s, z).diff(z)
    polylog(s - 1, z)/z

    The polylogarithm can be expressed in terms of the lerch transcendent:

    >>> from sympy import lerchphi
    >>> polylog(s, z).rewrite(lerchphi)
    z*lerchphi(z, s, 1)

    See Also
    ========

    zeta, lerchphi

    c                 C   s   |t ju r	t|S |t ju rt| S |t ju rt jS |dkr|t jkr2td d tdd d  S |dkrDtd d t	t td  S |t
dd  d krdtd  d tt
dd d d d  S |t
dd  d krtd  d tt
dd d d  S |dt
d d krtd d tt
dd d d  S |t
dd d krtd d tt
dd d d  S |jrt jS |t j}|rt|S |d	u r|t ju r|d|  S |t ju r|d| d  S |jr|d|  S |ttr|st|t jkd
kr| |t|S d S d S )Nr(      r0      r      
   rF   FT)r   r<   r1   NegativeOnedirichlet_etar6   Halfr   r   r   r   is_zeroequalsrJ   r   r   r   r   )clsr#   r$   zoner   r   r%   eval  sD   




*&$$

(zpolylog.evalr   c                 C   s(   | j \}}|dkrt|d || S t)Nr(   r   )r4   r)   r   )r?   rG   r#   r$   r   r   r%   rH   I  s   
zpolylog.fdiffc                 K   s   |t ||d S Nr   r   )r?   r#   r$   rO   r   r   r%   _eval_rewrite_as_lerchphiO  rQ   z!polylog._eval_rewrite_as_lerchphic                 K   sz   | j \}}|dkrtd|  S |jr8|dkr8td}|d|  }t| D ]	}||| }q&t|||S t||S )Nr   r   u)	r4   r   r5   r
   r=   r9   r	   r:   r)   )r?   r+   r#   r$   rf   r@   _r   r   r%   r*   R  s   

zpolylog._eval_expand_funcc                 C   s   | j d }|jr
dS d S )Nr   Tr4   r^   )r?   r$   r   r   r%   _eval_is_zero^  s   
zpolylog._eval_is_zeror   c              	      s  ddl m} | j\}}||d}|tju r%|j|dt|jr!dndd}|j	rz	|
|\}	}
W n ttfy>   |  Y S w |
jrt||
 }||| |}||||| }|tju r`|S |}|g}td|D ]}||9 }||||   qjt| | S tt| ||||S )Nr   )Order-+)dirr(   )sympy.series.orderrj   r4   r:   r   NaNlimitr   is_negativer^   leadterm
ValueErrorNotImplementedErroris_positiver   _eval_nseriesremoveOr6   r=   appendr   superr)   )r?   xr"   logxcdirrj   nur$   z0rg   r   newnortermr#   r   	__class__r   r%   rv   c  s0   


zpolylog._eval_nseriesr'   )r   )rS   rT   rU   rV   classmethodrb   rH   re   r*   ri   rv   __classcell__r   r   r   r%   r)      s    E

.r)   c                   @   sD   e Zd ZdZedddZdddZddd	Zd
d ZdddZ	dS )r1   a  
    Hurwitz zeta function (or Riemann zeta function).

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

    For $\operatorname{Re}(a) > 0$ and $\operatorname{Re}(s) > 1$, this
    function is defined as

    .. math:: \zeta(s, a) = \sum_{n=0}^\infty \frac{1}{(n + a)^s},

    where the standard choice of argument for $n + a$ is used. For fixed
    $a$ with $\operatorname{Re}(a) > 0$ the Hurwitz zeta function admits a
    meromorphic continuation to all of $\mathbb{C}$, it is an unbranched
    function with a simple pole at $s = 1$.

    Analytic continuation to other $a$ is possible under some circumstances,
    but this is not typically done.

    The Hurwitz zeta function is a special case of the Lerch transcendent:

    .. math:: \zeta(s, a) = \Phi(1, s, a).

    This formula defines an analytic continuation for all possible values of
    $s$ and $a$ (also $\operatorname{Re}(a) < 0$), see the documentation of
    :class:`lerchphi` for a description of the branching behavior.

    If no value is passed for $a$, by this function assumes a default value
    of $a = 1$, yielding the Riemann zeta function.

    Examples
    ========

    For $a = 1$ the Hurwitz zeta function reduces to the famous Riemann
    zeta function:

    .. math:: \zeta(s, 1) = \zeta(s) = \sum_{n=1}^\infty \frac{1}{n^s}.

    >>> from sympy import zeta
    >>> from sympy.abc import s
    >>> zeta(s, 1)
    zeta(s)
    >>> zeta(s)
    zeta(s)

    The Riemann zeta function can also be expressed using the Dirichlet eta
    function:

    >>> from sympy import dirichlet_eta
    >>> zeta(s).rewrite(dirichlet_eta)
    dirichlet_eta(s)/(1 - 2**(1 - s))

    The Riemann zeta function at positive even integer and negative odd integer
    values is related to the Bernoulli numbers:

    >>> zeta(2)
    pi**2/6
    >>> zeta(4)
    pi**4/90
    >>> zeta(-1)
    -1/12

    The specific formulae are:

    .. math:: \zeta(2n) = (-1)^{n+1} \frac{B_{2n} (2\pi)^{2n}}{2(2n)!}
    .. math:: \zeta(-n) = -\frac{B_{n+1}}{n+1}

    At negative even integers the Riemann zeta function is zero:

    >>> zeta(-4)
    0

    No closed-form expressions are known at positive odd integers, but
    numerical evaluation is possible:

    >>> zeta(3).n()
    1.20205690315959

    The derivative of $\zeta(s, a)$ with respect to $a$ can be computed:

    >>> from sympy.abc import a
    >>> zeta(s, a).diff(a)
    -s*zeta(s + 1, a)

    However the derivative with respect to $s$ has no useful closed form
    expression:

    >>> zeta(s, a).diff(s)
    Derivative(zeta(s, a), s)

    The Hurwitz zeta function can be expressed in terms of the Lerch
    transcendent, :class:`~.lerchphi`:

    >>> from sympy import lerchphi
    >>> zeta(s, a).rewrite(lerchphi)
    lerchphi(1, s, a)

    See Also
    ========

    dirichlet_eta, lerchphi, polylog

    References
    ==========

    .. [1] http://dlmf.nist.gov/25.11
    .. [2] https://en.wikipedia.org/wiki/Hurwitz_zeta_function

    Nc                 C   sr  |d u rt tt|df\}}nt tt||f\}}|jr3|tju r&tjS |tju r3|d ur3| |S |jrV|tju r>tjS |tju rFtjS |jrNtj	| S |tju rVtj
S |jr|jr|jrqtj| t| d  | d  }n)|jr|jrt|t|}}tj|d d  d|d   | t|  | }nd S |jr|tt|| S |t|d | S |jrtj	| S d S Nr   r(   )listmapr   	is_Numberr   ro   r<   Infinityr^   r]   ComplexInfinity
is_integerr5   rq   r[   r   is_evenru   r   r   r   abs)r`   r$   a_r!   r1   BFr   r   r%   rb     s>   




$0
z	zeta.evalr   c                 K   s.   |dkr| S | j d }t|ddd|    S )Nr   r   r(   )r4   r\   r?   r#   r!   rO   r   r   r%   _eval_rewrite_as_dirichlet_eta  s   
z#zeta._eval_rewrite_as_dirichlet_etac                 K   s   t d||S rc   rd   r   r   r   r%   re   %  s   zzeta._eval_rewrite_as_lerchphic                 C   s"   | j d d j}|d ur| S d S )Nr   r   rh   )r?   
arg_is_oner   r   r%   _eval_is_finite(  s   zzeta._eval_is_finitec                 C   sH   t | jdkr| j\}}n| jd \}}|dkr"| t|d | S t)Nr(   r'   r   )lenr4   r1   r   )r?   rG   r#   r!   r   r   r%   rH   -  s   z
zeta.fdiffrI   r'   )
rS   rT   rU   rV   r   rb   r   re   r   rH   r   r   r   r%   r1     s    n
'
r1   c                   @   $   e Zd ZdZedd Zdd ZdS )r\   a  
    Dirichlet eta function.

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

    For $\operatorname{Re}(s) > 0$, this function is defined as

    .. math:: \eta(s) = \sum_{n=1}^\infty \frac{(-1)^{n-1}}{n^s}.

    It admits a unique analytic continuation to all of $\mathbb{C}$.
    It is an entire, unbranched function.

    Examples
    ========

    The Dirichlet eta function is closely related to the Riemann zeta function:

    >>> from sympy import dirichlet_eta, zeta
    >>> from sympy.abc import s
    >>> dirichlet_eta(s).rewrite(zeta)
    (1 - 2**(1 - s))*zeta(s)

    See Also
    ========

    zeta

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Dirichlet_eta_function

    c                 C   s:   |dkrt dS t|}|tsddd|   | S d S r   )r   r1   rJ   )r`   r#   r$   r   r   r%   rb   \  s   
zdirichlet_eta.evalc                 K   s   ddd|   t | S r   )r1   )r?   r#   rO   r   r   r%   rP   d  s   z#dirichlet_eta._eval_rewrite_as_zetaNrS   rT   rU   rV   r   rb   rP   r   r   r   r%   r\   8  s
    #
r\   c                   @   r   )
riemann_xia  
    Riemann Xi function.

    Examples
    ========

    The Riemann Xi function is closely related to the Riemann zeta function.
    The zeros of Riemann Xi function are precisely the non-trivial zeros
    of the zeta function.

    >>> from sympy import riemann_xi, zeta
    >>> from sympy.abc import s
    >>> riemann_xi(s).rewrite(zeta)
    s*(s - 1)*gamma(s/2)*zeta(s)/(2*pi**(s/2))

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Riemann_Xi_function

    c                 C   sd   ddl m} t|}|tjtjfv rtjS t|ts0||d  ||d  | dt|d    S d S Nr   )gammar   r(   )	'sympy.functions.special.gamma_functionsr   r1   r   r6   r<   r]   r>   r   )r`   r#   r   r$   r   r   r%   rb     s   
,zriemann_xi.evalc                 K   s<   ddl m} ||d  ||d  t| dt|d    S r   )r   r   r1   r   )r?   r#   rO   r   r   r   r%   rP     s   0z riemann_xi._eval_rewrite_as_zetaNr   r   r   r   r%   r   h  s
    
	r   c                   @   s   e Zd ZdZedddZdS )	stieltjesa  
    Represents Stieltjes constants, $\gamma_{k}$ that occur in
    Laurent Series expansion of the Riemann zeta function.

    Examples
    ========

    >>> from sympy import stieltjes
    >>> from sympy.abc import n, m
    >>> stieltjes(n)
    stieltjes(n)

    The zero'th stieltjes constant:

    >>> stieltjes(0)
    EulerGamma
    >>> stieltjes(0, 1)
    EulerGamma

    For generalized stieltjes constants:

    >>> stieltjes(n, m)
    stieltjes(n, m)

    Constants are only defined for integers >= 0:

    >>> stieltjes(-1)
    zoo

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Stieltjes_constants

    Nc                 C   s   |d urt |}|tju rtjS |jr|jrtjS |jr=|tju r$tjS |dk r+tjS |js1tjS |tju r=|dv r=tjS |j	rCtjS |j
rM|dv rMtjS |jdkrUtjS d S )Nr   rc   F)r   r   ro   r5   is_nonpositiver   r   r6   
EulerGammais_extended_negativer^   r   )r`   r"   r!   r   r   r%   rb     s,   


zstieltjes.evalrI   )rS   rT   rU   rV   r   rb   r   r   r   r%   r     s    $r   N)(rV   sympy.core.addr   
sympy.corer   r   r   r   r   sympy.core.functionr   r	   sympy.core.symbolr
   %sympy.functions.combinatorial.numbersr   r   r   $sympy.functions.elementary.complexesr   r   r   r   &sympy.functions.elementary.exponentialr   r   r   #sympy.functions.elementary.integersr   r   (sympy.functions.elementary.miscellaneousr   sympy.polys.polytoolsr   r   r)   r1   r\   r   r   r   r   r   r%   <module>   s(     B 5 10&