o
    8VaS                     @   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
 ddlmZmZmZ ddlmZ ddlmZ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)	bernoulli	factorialharmonic)re)log	exp_polar)sqrtc                   @   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  ddl mm m}m}m}m}m}m
 | j	\	dkr#t
	S 	jr]	dkr]|d}|| 	  |dd|  }tj}	t D ]}
|	|
| 7 }	||| }qG|	|S jrtj}tj}dkr|krvd8 8   }|	fddtD  }n#dkr| d 7  }|	fddtD  }tjjg\|dt    d  ||	d   |	
fd	dtD    S trj	d t   jsd
   fv rTd
krtddg\n2 krtddg\n$  kr-td
dg\nj	d dt    }t|j|jg\| 	fddtD  S t	S )Nr   )expr   floorr   PolyDummyr   
unpolarify   tc                    s&   g | ]}|    |   qS  r   .0kanszr   H/usr/lib/python3/dist-packages/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  |    qS )r   )polylog_eval_expand_funcr   )hintsmrootr   r   zetr   r!   r"      s
       c                    sB   g | ]}d t    |     t|   qS )r$   )r   zetar   )r   r   r   pqr   r   r!   r"      s    :)sympyr   r   r   r   r   r   r   r   argsr-   
is_Integerr   ZeroreversedZ
all_coeffsdiffsubsZis_RationalOneranger.   r/   r   
isinstancer   )selfr'   r   r   r   r   r   r   startrescaddmulargr   )r   r   r   r'   r(   r   r.   r/   r)   r   r   r    r*   r!   r&   z   sb   (

" 2

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   )r1   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   targetr<   r   r   r!   _eval_rewrite_helper   s   
zlerchphi._eval_rewrite_helperc                 K      |  |||tS rD   )rG   r-   r:   r    r   r   kwargsr   r   r!   _eval_rewrite_as_zeta      zlerchphi._eval_rewrite_as_zetac                 K   rH   rD   )rG   r%   rI   r   r   r!   _eval_rewrite_as_polylog   rL   z!lerchphi._eval_rewrite_as_polylogNr#   )	__name__
__module____qualname____doc__r&   rC   rG   rK   rM   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   sD  t ||f\}}|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r:td d t	dd d  S |dkrLtd d t
t t	d  S |tdd  d krlt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 d
dlm}m}m} |t|r|s||tjkdkr | |||S d S d S )Nr$      r,      r      
   rA   Fr   )Absr   
polar_liftT)r   r   r7   r-   ZNegativeOnedirichlet_etar3   Halfr   r   r   r   is_zeroZequals$sympy.functions.elementary.complexesrV   r   rW   rE   r   )clsr   r    ZzonerV   r   rW   r   r   r!   eval  sH   




*&$$

(zpolylog.evalr   c                 C   s(   | j \}}|dkrt|d || S t)Nr$   r   )r1   r%   r   )r:   rB   r   r    r   r   r!   rC   J  s   
zpolylog.fdiffc                 K   s   |t ||d S Nr   r   )r:   r   r    rJ   r   r   r!   _eval_rewrite_as_lerchphiP  rL   z!polylog._eval_rewrite_as_lerchphic           
      K   s   ddl m}m}m} | j\}}|dkr|d|  S |jrB|dkrB|d}|d|  }t| D ]	}	||| }q0||||S t	||S )Nr   )r   
expand_mulr   r   u)
r0   r   ra   r   r1   r2   r8   r5   r6   r%   )
r:   r'   r   ra   r   r   r    rb   r;   _r   r   r!   r&   S  s   

zpolylog._eval_expand_funcc                 C   s   | j d }|jr
dS d S )Nr   Tr1   rZ   )r:   r    r   r   r!   _eval_is_zero`  s   
zpolylog._eval_is_zeror   c              	      s  ddl m}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r||| }||| |}||||| }|tju rb|S |}|g}td|D ]}||9 }||||   qlt| | S tt| ||||S )Nr   )ceilingOrder-+)dirr$   )r0   rf   rg   r1   r6   r   NaNlimitr   is_negativerZ   Zleadterm
ValueErrorNotImplementedErroris_positive_eval_nseriesZremoveOr3   r8   appendr   superr%   )r:   xr   Zlogxcdirrf   rg   Znur    Zz0rc   r   ZnewnorZtermr   r   	__class__r   r!   rq   e  s0   


zpolylog._eval_nseriesr#   )r   )rN   rO   rP   rQ   classmethodr]   rC   r`   r&   re   rq   __classcell__r   r   rx   r!   r%      s    E

1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 )r-   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   sn  |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rpd| t| d  | d  }n(|jr|jrt|t|}}d|d d  d|d   | t|  | }nd S |jr|tt|| S |t|d | S |jrtj	| S d S )Nr   r+   r$   )listmapr   	is_Numberr   rk   r7   ZInfinityrZ   rY   ComplexInfinity
is_integerr2   rm   r	   Zis_evenrp   r
   r   r   abs)r\   r    Za_r   r-   BFr   r   r!   r]     s>   




".
z	zeta.evalr   c                 K   s.   |dkr| S | j d }t|ddd|    S )Nr   r   r$   )r1   rX   r:   r   r   rJ   r   r   r!   _eval_rewrite_as_dirichlet_eta!  s   
z#zeta._eval_rewrite_as_dirichlet_etac                 K   s   t d||S r^   r_   r   r   r   r!   r`   '  s   zzeta._eval_rewrite_as_lerchphic                 C   s"   | j d d j}|d ur| S d S )Nr   r   rd   )r:   Z
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   )lenr1   r-   r   )r:   rB   r   r   r   r   r!   rC   /  s   z
zeta.fdiffrD   r#   )
rN   rO   rP   rQ   rz   r]   r   r`   r   rC   r   r   r   r!   r-     s    n
'
r-   c                   @   $   e Zd ZdZedd Zdd ZdS )rX   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 Nr   r$   )r   r-   rE   )r\   r   r    r   r   r!   r]   ^  s   
zdirichlet_eta.evalc                 K   s   ddd|   t | S r   )r-   )r:   r   rJ   r   r   r!   rK   f  s   z#dirichlet_eta._eval_rewrite_as_zetaNrN   rO   rP   rQ   rz   r]   rK   r   r   r   r!   rX   :  s
    #
rX   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   sh   ddl m} t|}|tju s|tju rtjS t|ts2||d  ||d  | dt|d    S d S Nr   )gammar   r$   )	r0   r   r-   r   r3   r7   rY   r9   r   )r\   r   r   r    r   r   r!   r]     s   
,zriemann_xi.evalc                 K   s<   ddl m} ||d  ||d  t| dt|d    S r   )r0   r   r-   r   )r:   r   rJ   r   r   r   r!   rK     s   0z riemann_xi._eval_rewrite_as_zetaNr   r   r   r   r!   r   j  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   t |}|d urt |}|tju rtjS |jr|jrtjS |jrA|tju r(tjS |dk r/tjS |js5tjS |tju rA|dv rAtjS |j	rGtjS |j
rQ|dv rQtjS |jdkrYtjS d S )Nr   r^   F)r   r   rk   r2   Zis_nonpositiver   r~   r3   Z
EulerGammaZis_extended_negativerZ   r   )r\   r   r   r   r   r!   r]     s.   


zstieltjes.evalrD   )rN   rO   rP   rQ   rz   r]   r   r   r   r!   r     s    $r   N)rQ   r0   r   Z
sympy.corer   r   r   r   r   Zsympy.core.functionr   Z%sympy.functions.combinatorial.numbersr	   r
   r   r[   r   Z&sympy.functions.elementary.exponentialr   r   Z(sympy.functions.elementary.miscellaneousr   r   r%   r-   rX   r   r   r   r   r   r!   <module>   s"     C 9 10&