a
    a                    @   s  d dl Z d dlZd dl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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& ddlm'Z' ddlm(Z( ddl)m*Z* g d	Z+dddZ,dd Z-dd Z.dd Z/dd Z0dd Z1dd Z2dd Z3dddZ4dddZ5dd d!Z6d"d# Z7dd$d%Z8dd&d'Z9dd(d)Z:dd*d+Z;dd,d-Z<dd.d/Z=d0d1 Z>d2d3 Z?d4d5 Z@d6d7 ZAd8d9 ZBd:d; ZCdd=d>ZDeZEd?d@ ZFdAdB ZGdCdD ZHdEdF ZIddHdIZJdJdK ZKdLdM ZLdNdO ZMdPdQ ZNdRdS ZOdTdU ZPdVdW ZQdXdY ZRdZd[ ZSd\d] ZTd^d_ ZUd`da ZVdbdc ZWddde ZXdfdg ZYdhdi ZZdjdk Z[dldm Z\dndo Z]dpdq Z^drds Z_dtdu Z`ddvdwZaddxdyZbdzd{ Zcdd|d}Zddd~dZedddZfdddZgdS )    N)piasarrayfloorisscalar	iscomplexrealimagsqrtwheremgridsinplace
issubdtypeextractinexactnanzerossinc   )_ufuncs)	mathieu_a	mathieu_bivjvgammapsihankel1hankel2yvkvndtripochbinomhyp0f1)specfun)
orthogonal)	_comb_int)Fai_zerosassoc_laguerre	bei_zeros
beip_zeros	ber_zeros	bernoulli
berp_zerosbi_zerosclpmncombdigammadiric	erf_zeroseuler	factorial
factorial2
factorialkfresnel_zerosfresnelc_zerosfresnels_zerosr   h1vph2vpr   r   r#   r   ivpjn_zerosjnjnp_zeros	jnp_zeros
jnyn_zerosr   jvp	kei_zeros
keip_zeroskelvin_zeros	ker_zeros
kerp_zerosr   kvplmbdalpmnlpnlqmnlqnr   r   mathieu_even_coefmathieu_odd_coefr    
obl_cv_seqpbdn_seqpbdv_seqpbvv_seqperm	polygamma
pro_cv_seqr   
riccati_jn
riccati_ynr   y0_zerosy1_zeros	y1p_zerosyn_zeros	ynp_zerosr   yvpzetaTc              
   C   s~   z>|rt | } n| t| kr(t| } nt | dk r<t W n: ttfyx } z|d||W Y d }~n
d }~0 0 | S )Nr   z!{} must be a non-negative integer)operatorindexr   int
ValueError	TypeError	__class__format)nvar_namestricterr rk   d/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/scipy/special/_basic.py_nonneg_int_or_fail^   s    

(rm   c                 C   sT  t | t | } }t || |   }t | ||  } t| jtrF| j}nt}t| j|}t|j	dk rld}nt|j	dk rd}nd}|dk|t
|kB }t||t | d } t| }d| t||k @ }t|| }t||}	|t }
t||td	t|
|	d   d| d| @ }t|| }t||}	t||}t||t|	| |	|   |S )
a  Periodic sinc function, also called the Dirichlet function.

    The Dirichlet function is defined as::

        diric(x, n) = sin(x * n/2) / (n * sin(x / 2)),

    where `n` is a positive integer.

    Parameters
    ----------
    x : array_like
        Input data
    n : int
        Integer defining the periodicity.

    Returns
    -------
    diric : ndarray

    Examples
    --------
    >>> from scipy import special
    >>> import matplotlib.pyplot as plt

    >>> x = np.linspace(-8*np.pi, 8*np.pi, num=201)
    >>> plt.figure(figsize=(8, 8));
    >>> for idx, n in enumerate([2, 3, 4, 9]):
    ...     plt.subplot(2, 2, idx+1)
    ...     plt.plot(x, special.diric(x, n))
    ...     plt.title('diric, n={}'.format(n))
    >>> plt.show()

    The following example demonstrates that `diric` gives the magnitudes
    (modulo the sign and scaling) of the Fourier coefficients of a
    rectangular pulse.

    Suppress output of values that are effectively 0:

    >>> np.set_printoptions(suppress=True)

    Create a signal `x` of length `m` with `k` ones:

    >>> m = 8
    >>> k = 3
    >>> x = np.zeros(m)
    >>> x[:k] = 1

    Use the FFT to compute the Fourier transform of `x`, and
    inspect the magnitudes of the coefficients:

    >>> np.abs(np.fft.fft(x))
    array([ 3.        ,  2.41421356,  1.        ,  0.41421356,  1.        ,
            0.41421356,  1.        ,  2.41421356])

    Now find the same values (up to sign) using `diric`. We multiply
    by `k` to account for the different scaling conventions of
    `numpy.fft.fft` and `diric`:

    >>> theta = np.linspace(0, 2*np.pi, m, endpoint=False)
    >>> k * special.diric(theta, k)
    array([ 3.        ,  2.41421356,  1.        , -0.41421356, -1.        ,
           -0.41421356,  1.        ,  2.41421356])
    gC]r2<gdy=gV瞯<gHz>gMbP?r      r   )r   r   dtyper   floatr   shapenpZfinfoZepsr   r   r   r   absr   r   powround)xrg   ZytypeyminvalZmask1ZdenomZmask2ZxsubnsubZzsubmaskZdsubrk   rk   rl   r2   n   s6    @

 


r2   c                 C   sn   t | rt| | ks| dkr$tdt| } t| \}}}}|d| d  |d|  |d|  |d|  fS )a  Compute zeros of integer-order Bessel functions Jn and Jn'.

    Results are arranged in order of the magnitudes of the zeros.

    Parameters
    ----------
    nt : int
        Number (<=1200) of zeros to compute

    Returns
    -------
    zo[l-1] : ndarray
        Value of the lth zero of Jn(x) and Jn'(x). Of length `nt`.
    n[l-1] : ndarray
        Order of the Jn(x) or Jn'(x) associated with lth zero. Of length `nt`.
    m[l-1] : ndarray
        Serial number of the zeros of Jn(x) or Jn'(x) associated
        with lth zero. Of length `nt`.
    t[l-1] : ndarray
        0 if lth zero in zo is zero of Jn(x), 1 if it is a zero of Jn'(x). Of
        length `nt`.

    See Also
    --------
    jn_zeros, jnp_zeros : to get separated arrays of zeros.

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996, chapter 5.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    i  zNumber must be integer <= 1200.r   N)r   r   rc   rb   r$   Zjdzo)ntrg   mtZzork   rk   rl   r?      s
    "r?   c                 C   sX   t |rt | stdt| | ks0t||kr8td|dkrHtdtt| |S )a  Compute nt zeros of Bessel functions Jn(x), Jn'(x), Yn(x), and Yn'(x).

    Returns 4 arrays of length `nt`, corresponding to the first `nt`
    zeros of Jn(x), Jn'(x), Yn(x), and Yn'(x), respectively. The zeros
    are returned in ascending order.

    Parameters
    ----------
    n : int
        Order of the Bessel functions
    nt : int
        Number (<=1200) of zeros to compute

    Returns
    -------
    Jn : ndarray
        First `nt` zeros of Jn
    Jnp : ndarray
        First `nt` zeros of Jn'
    Yn : ndarray
        First `nt` zeros of Yn
    Ynp : ndarray
        First `nt` zeros of Yn'

    See Also
    --------
    jn_zeros, jnp_zeros, yn_zeros, ynp_zeros

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996, chapter 5.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    Arguments must be scalars.zArguments must be integers.r   znt > 0)r   rc   r   r$   Zjyzort   rg   r|   rk   rk   rl   rA      s    $rA   c                 C   s   t | |d S )a  Compute zeros of integer-order Bessel functions Jn.

    Compute `nt` zeros of the Bessel functions :math:`J_n(x)` on the
    interval :math:`(0, \infty)`. The zeros are returned in ascending
    order. Note that this interval excludes the zero at :math:`x = 0`
    that exists for :math:`n > 0`.

    Parameters
    ----------
    n : int
        Order of Bessel function
    nt : int
        Number of zeros to return

    Returns
    -------
    ndarray
        First `n` zeros of the Bessel function.

    See Also
    --------
    jv

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996, chapter 5.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    Examples
    --------
    >>> import scipy.special as sc

    We can check that we are getting approximations of the zeros by
    evaluating them with `jv`.

    >>> n = 1
    >>> x = sc.jn_zeros(n, 3)
    >>> x
    array([ 3.83170597,  7.01558667, 10.17346814])
    >>> sc.jv(n, x)
    array([-0.00000000e+00,  1.72975330e-16,  2.89157291e-16])

    Note that the zero at ``x = 0`` for ``n > 0`` is not included.

    >>> sc.jv(1, 0)
    0.0

    r   rA   r   rk   rk   rl   r>   )  s    2r>   c                 C   s   t | |d S )a  Compute zeros of integer-order Bessel function derivatives Jn'.

    Compute `nt` zeros of the functions :math:`J_n'(x)` on the
    interval :math:`(0, \infty)`. The zeros are returned in ascending
    order. Note that this interval excludes the zero at :math:`x = 0`
    that exists for :math:`n > 1`.

    Parameters
    ----------
    n : int
        Order of Bessel function
    nt : int
        Number of zeros to return

    Returns
    -------
    ndarray
        First `n` zeros of the Bessel function.

    See Also
    --------
    jvp, jv

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996, chapter 5.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    Examples
    --------
    >>> import scipy.special as sc

    We can check that we are getting approximations of the zeros by
    evaluating them with `jvp`.

    >>> n = 2
    >>> x = sc.jnp_zeros(n, 3)
    >>> x
    array([3.05423693, 6.70613319, 9.96946782])
    >>> sc.jvp(n, x)
    array([ 2.77555756e-17,  2.08166817e-16, -3.01841885e-16])

    Note that the zero at ``x = 0`` for ``n > 1`` is not included.

    >>> sc.jvp(n, 0)
    0.0

    r   r   r   rk   rk   rl   r@   ^  s    2r@   c                 C   s   t | |d S )a  Compute zeros of integer-order Bessel function Yn(x).

    Compute `nt` zeros of the functions :math:`Y_n(x)` on the interval
    :math:`(0, \infty)`. The zeros are returned in ascending order.

    Parameters
    ----------
    n : int
        Order of Bessel function
    nt : int
        Number of zeros to return

    Returns
    -------
    ndarray
        First `n` zeros of the Bessel function.

    See Also
    --------
    yn, yv

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996, chapter 5.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    Examples
    --------
    >>> import scipy.special as sc

    We can check that we are getting approximations of the zeros by
    evaluating them with `yn`.

    >>> n = 2
    >>> x = sc.yn_zeros(n, 3)
    >>> x
    array([ 3.38424177,  6.79380751, 10.02347798])
    >>> sc.yn(n, x)
    array([-1.94289029e-16,  8.32667268e-17, -1.52655666e-16])

    rn   r   r   rk   rk   rl   r\     s    +r\   c                 C   s   t | |d S )a  Compute zeros of integer-order Bessel function derivatives Yn'(x).

    Compute `nt` zeros of the functions :math:`Y_n'(x)` on the
    interval :math:`(0, \infty)`. The zeros are returned in ascending
    order.

    Parameters
    ----------
    n : int
        Order of Bessel function
    nt : int
        Number of zeros to return

    See Also
    --------
    yvp

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996, chapter 5.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    Examples
    --------
    >>> import scipy.special as sc

    We can check that we are getting approximations of the zeros by
    evaluating them with `yvp`.

    >>> n = 2
    >>> x = sc.ynp_zeros(n, 3)
    >>> x
    array([ 5.00258293,  8.3507247 , 11.57419547])
    >>> sc.yvp(n, x)
    array([ 2.22044605e-16, -3.33066907e-16,  2.94902991e-16])

       r   r   rk   rk   rl   r]     s    'r]   Fc                 C   s<   t | rt| | ks| dkr$tdd}| }t| ||S )a  Compute nt zeros of Bessel function Y0(z), and derivative at each zero.

    The derivatives are given by Y0'(z0) = -Y1(z0) at each zero z0.

    Parameters
    ----------
    nt : int
        Number of zeros to return
    complex : bool, default False
        Set to False to return only the real zeros; set to True to return only
        the complex zeros with negative real part and positive imaginary part.
        Note that the complex conjugates of the latter are also zeros of the
        function, but are not returned by this routine.

    Returns
    -------
    z0n : ndarray
        Location of nth zero of Y0(z)
    y0pz0n : ndarray
        Value of derivative Y0'(z0) for nth zero

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996, chapter 5.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    r   *Arguments must be scalar positive integer.r   r   rc   r$   Zcyzor|   complexkfZkcrk   rk   rl   rY     s
    rY   c                 C   s<   t | rt| | ks| dkr$tdd}| }t| ||S )a  Compute nt zeros of Bessel function Y1(z), and derivative at each zero.

    The derivatives are given by Y1'(z1) = Y0(z1) at each zero z1.

    Parameters
    ----------
    nt : int
        Number of zeros to return
    complex : bool, default False
        Set to False to return only the real zeros; set to True to return only
        the complex zeros with negative real part and positive imaginary part.
        Note that the complex conjugates of the latter are also zeros of the
        function, but are not returned by this routine.

    Returns
    -------
    z1n : ndarray
        Location of nth zero of Y1(z)
    y1pz1n : ndarray
        Value of derivative Y1'(z1) for nth zero

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996, chapter 5.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    r   r   r   r   r   rk   rk   rl   rZ     s
    rZ   c                 C   s<   t | rt| | ks| dkr$tdd}| }t| ||S )a  Compute nt zeros of Bessel derivative Y1'(z), and value at each zero.

    The values are given by Y1(z1) at each z1 where Y1'(z1)=0.

    Parameters
    ----------
    nt : int
        Number of zeros to return
    complex : bool, default False
        Set to False to return only the real zeros; set to True to return only
        the complex zeros with negative real part and positive imaginary part.
        Note that the complex conjugates of the latter are also zeros of the
        function, but are not returned by this routine.

    Returns
    -------
    z1pn : ndarray
        Location of nth zero of Y1'(z)
    y1z1pn : ndarray
        Value of derivative Y1(z1) for nth zero

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996, chapter 5.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    r   r   rn   r   r   rk   rk   rl   r[   3  s
    r[   c                 C   sp   t | } d}|| | |}td|d D ]:}|||| d   | }|||| | |d  | 7 }q(|d|  S )N      ?r   rn          @)r   range)vzrg   Lphasepsirk   rk   rl   _bessel_diff_formulaW  s     r   c                 C   s0   t |d}|dkrt| |S t| ||tdS dS )a  Compute derivatives of Bessel functions of the first kind.

    Compute the nth derivative of the Bessel function `Jv` with
    respect to `z`.

    Parameters
    ----------
    v : float
        Order of Bessel function
    z : complex
        Argument at which to evaluate the derivative; can be real or
        complex.
    n : int, default 1
        Order of derivative

    Returns
    -------
    scalar or ndarray
        Values of the derivative of the Bessel function.

    Notes
    -----
    The derivative is computed using the relation DLFM 10.6.7 [2]_.

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996, chapter 5.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html
    .. [2] NIST Digital Library of Mathematical Functions.
           https://dlmf.nist.gov/10.6.E7

    rg   r   ro   N)rm   r   r   r   r   rg   rk   rk   rl   rB   e  s    "

rB   c                 C   s0   t |d}|dkrt| |S t| ||tdS dS )aw  Compute derivatives of Bessel functions of the second kind.

    Compute the nth derivative of the Bessel function `Yv` with
    respect to `z`.

    Parameters
    ----------
    v : float
        Order of Bessel function
    z : complex
        Argument at which to evaluate the derivative
    n : int, default 1
        Order of derivative

    Returns
    -------
    scalar or ndarray
        nth derivative of the Bessel function.

    Notes
    -----
    The derivative is computed using the relation DLFM 10.6.7 [2]_.

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996, chapter 5.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html
    .. [2] NIST Digital Library of Mathematical Functions.
           https://dlmf.nist.gov/10.6.E7

    rg   r   ro   N)rm   r   r   r   rk   rk   rl   r^     s    !

r^   c                 C   s8   t |d}|dkrt| |S d| t| ||td S dS )a)  Compute nth derivative of real-order modified Bessel function Kv(z)

    Kv(z) is the modified Bessel function of the second kind.
    Derivative is calculated with respect to `z`.

    Parameters
    ----------
    v : array_like of float
        Order of Bessel function
    z : array_like of complex
        Argument at which to evaluate the derivative
    n : int
        Order of derivative.  Default is first derivative.

    Returns
    -------
    out : ndarray
        The results

    Examples
    --------
    Calculate multiple values at order 5:

    >>> from scipy.special import kvp
    >>> kvp(5, (1, 2, 3+5j))
    array([-1.84903536e+03+0.j        , -2.57735387e+01+0.j        ,
           -3.06627741e-02+0.08750845j])


    Calculate for a single value at multiple orders:

    >>> kvp((4, 4.5, 5), 1)
    array([ -184.0309,  -568.9585, -1849.0354])

    Notes
    -----
    The derivative is computed using the relation DLFM 10.29.5 [2]_.

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996, chapter 6.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html
    .. [2] NIST Digital Library of Mathematical Functions.
           https://dlmf.nist.gov/10.29.E5

    rg   r   ro   r   N)rm   r   r   r   rk   rk   rl   rH     s    0

rH   c                 C   s0   t |d}|dkrt| |S t| ||tdS dS )a  Compute derivatives of modified Bessel functions of the first kind.

    Compute the nth derivative of the modified Bessel function `Iv`
    with respect to `z`.

    Parameters
    ----------
    v : array_like
        Order of Bessel function
    z : array_like
        Argument at which to evaluate the derivative; can be real or
        complex.
    n : int, default 1
        Order of derivative

    Returns
    -------
    scalar or ndarray
        nth derivative of the modified Bessel function.

    See Also
    --------
    iv

    Notes
    -----
    The derivative is computed using the relation DLFM 10.29.5 [2]_.

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996, chapter 6.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html
    .. [2] NIST Digital Library of Mathematical Functions.
           https://dlmf.nist.gov/10.29.E5

    rg   r   r   N)rm   r   r   r   rk   rk   rl   r=     s    &

r=   c                 C   s0   t |d}|dkrt| |S t| ||tdS dS )a_  Compute nth derivative of Hankel function H1v(z) with respect to `z`.

    Parameters
    ----------
    v : array_like
        Order of Hankel function
    z : array_like
        Argument at which to evaluate the derivative. Can be real or
        complex.
    n : int, default 1
        Order of derivative

    Returns
    -------
    scalar or ndarray
        Values of the derivative of the Hankel function.

    Notes
    -----
    The derivative is computed using the relation DLFM 10.6.7 [2]_.

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996, chapter 5.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html
    .. [2] NIST Digital Library of Mathematical Functions.
           https://dlmf.nist.gov/10.6.E7

    rg   r   ro   N)rm   r   r   r   rk   rk   rl   r;     s    

r;   c                 C   s0   t |d}|dkrt| |S t| ||tdS dS )a_  Compute nth derivative of Hankel function H2v(z) with respect to `z`.

    Parameters
    ----------
    v : array_like
        Order of Hankel function
    z : array_like
        Argument at which to evaluate the derivative. Can be real or
        complex.
    n : int, default 1
        Order of derivative

    Returns
    -------
    scalar or ndarray
        Values of the derivative of the Hankel function.

    Notes
    -----
    The derivative is computed using the relation DLFM 10.6.7 [2]_.

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996, chapter 5.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html
    .. [2] NIST Digital Library of Mathematical Functions.
           https://dlmf.nist.gov/10.6.E7

    rg   r   ro   N)rm   r   r   r   rk   rk   rl   r<   @  s    

r<   c                 C   sj   t | rt |stdt| ddd} | dkr4d}n| }t||\}}}|d| d  |d| d  fS )a  Compute Ricatti-Bessel function of the first kind and its derivative.

    The Ricatti-Bessel function of the first kind is defined as :math:`x
    j_n(x)`, where :math:`j_n` is the spherical Bessel function of the first
    kind of order :math:`n`.

    This function computes the value and first derivative of the
    Ricatti-Bessel function for all orders up to and including `n`.

    Parameters
    ----------
    n : int
        Maximum order of function to compute
    x : float
        Argument at which to evaluate

    Returns
    -------
    jn : ndarray
        Value of j0(x), ..., jn(x)
    jnp : ndarray
        First derivative j0'(x), ..., jn'(x)

    Notes
    -----
    The computation is carried out via backward recurrence, using the
    relation DLMF 10.51.1 [2]_.

    Wrapper for a Fortran routine created by Shanjie Zhang and Jianming
    Jin [1]_.

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html
    .. [2] NIST Digital Library of Mathematical Functions.
           https://dlmf.nist.gov/10.51.E1

    arguments must be scalars.rg   Fri   r   r   N)r   rc   rm   r$   Zrctjrg   rw   n1nmZjnZjnprk   rk   rl   rW   f  s    )rW   c                 C   sj   t | rt |stdt| ddd} | dkr4d}n| }t||\}}}|d| d  |d| d  fS )a  Compute Ricatti-Bessel function of the second kind and its derivative.

    The Ricatti-Bessel function of the second kind is defined as :math:`x
    y_n(x)`, where :math:`y_n` is the spherical Bessel function of the second
    kind of order :math:`n`.

    This function computes the value and first derivative of the function for
    all orders up to and including `n`.

    Parameters
    ----------
    n : int
        Maximum order of function to compute
    x : float
        Argument at which to evaluate

    Returns
    -------
    yn : ndarray
        Value of y0(x), ..., yn(x)
    ynp : ndarray
        First derivative y0'(x), ..., yn'(x)

    Notes
    -----
    The computation is carried out via ascending recurrence, using the
    relation DLMF 10.51.1 [2]_.

    Wrapper for a Fortran routine created by Shanjie Zhang and Jianming
    Jin [1]_.

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html
    .. [2] NIST Digital Library of Mathematical Functions.
           https://dlmf.nist.gov/10.51.E1

    r   rg   Fr   r   r   N)r   rc   rm   r$   Zrctyr   rk   rk   rl   rX     s    )rX   c                 C   s.   t | | ks| dkst| s$tdt| S )a  Compute the first nt zero in the first quadrant, ordered by absolute value.

    Zeros in the other quadrants can be obtained by using the symmetries erf(-z) = erf(z) and
    erf(conj(z)) = conj(erf(z)).


    Parameters
    ----------
    nt : int
        The number of zeros to compute

    Returns
    -------
    The locations of the zeros of erf : ndarray (complex)
        Complex values at which zeros of erf(z)

    Examples
    --------
    >>> from scipy import special
    >>> special.erf_zeros(1)
    array([1.45061616+1.880943j])

    Check that erf is (close to) zero for the value returned by erf_zeros

    >>> special.erf(special.erf_zeros(1))
    array([4.95159469e-14-1.16407394e-16j])

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    r   )Argument must be positive scalar integer.)r   r   rc   r$   Zcerzor|   rk   rk   rl   r3     s    #r3   c                 C   s0   t | | ks| dkst| s$tdtd| S )a4  Compute nt complex zeros of cosine Fresnel integral C(z).

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    r   r   r   r   r   rc   r$   Zfcszor   rk   rk   rl   r9     s    
r9   c                 C   s0   t | | ks| dkst| s$tdtd| S )a2  Compute nt complex zeros of sine Fresnel integral S(z).

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    r   r   rn   r   r   rk   rk   rl   r:     s    
r:   c                 C   s<   t | | ks| dkst| s$tdtd| td| fS )aG  Compute nt complex zeros of sine and cosine Fresnel integrals S(z) and C(z).

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    r   r   rn   r   r   r   rk   rk   rl   r8     s    
r8           c                 C   s   t ||| S )a|  Compute the generalized (associated) Laguerre polynomial of degree n and order k.

    The polynomial :math:`L^{(k)}_n(x)` is orthogonal over ``[0, inf)``,
    with weighting function ``exp(-x) * x**k`` with ``k > -1``.

    Notes
    -----
    `assoc_laguerre` is a simple wrapper around `eval_genlaguerre`, with
    reversed argument order ``(x, n, k=0.0) --> (n, k, x)``.

    )r%   Zeval_genlaguerre)rw   rg   krk   rk   rl   r(   #  s    r(   c                 C   sL   t | t | } }d| d  t| d  t| d | }t| dkt||S )a.  Polygamma functions.

    Defined as :math:`\psi^{(n)}(x)` where :math:`\psi` is the
    `digamma` function. See [dlmf]_ for details.

    Parameters
    ----------
    n : array_like
        The order of the derivative of the digamma function; must be
        integral
    x : array_like
        Real valued input

    Returns
    -------
    ndarray
        Function results

    See Also
    --------
    digamma

    References
    ----------
    .. [dlmf] NIST, Digital Library of Mathematical Functions,
        https://dlmf.nist.gov/5.15

    Examples
    --------
    >>> from scipy import special
    >>> x = [2, 3, 25.5]
    >>> special.polygamma(1, x)
    array([ 0.64493407,  0.39493407,  0.03999467])
    >>> special.polygamma(0, x) == special.psi(x)
    array([ True,  True,  True], dtype=bool)

    g      r   r   r   )r   r   r_   r
   r   )rg   rw   Zfac2rk   rk   rl   rU   5  s    &&rU   c                 C   s   t | rt |std|dk r(td| t| ks<| dk rDtd|dkrvddt|  d|  d	t| |  }n(d
dt|  d|  dt| |  }t|d|   }|dkrtd d}tt| } | d rd}t| |}t|| ||}|d| S )a  Fourier coefficients for even Mathieu and modified Mathieu functions.

    The Fourier series of the even solutions of the Mathieu differential
    equation are of the form

    .. math:: \mathrm{ce}_{2n}(z, q) = \sum_{k=0}^{\infty} A_{(2n)}^{(2k)} \cos 2kz

    .. math:: \mathrm{ce}_{2n+1}(z, q) = \sum_{k=0}^{\infty} A_{(2n+1)}^{(2k+1)} \cos (2k+1)z

    This function returns the coefficients :math:`A_{(2n)}^{(2k)}` for even
    input m=2n, and the coefficients :math:`A_{(2n+1)}^{(2k+1)}` for odd input
    m=2n+1.

    Parameters
    ----------
    m : int
        Order of Mathieu functions.  Must be non-negative.
    q : float (>=0)
        Parameter of Mathieu functions.  Must be non-negative.

    Returns
    -------
    Ak : ndarray
        Even or odd Fourier coefficients, corresponding to even or odd m.

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html
    .. [2] NIST Digital Library of Mathematical Functions
           https://dlmf.nist.gov/28.4#i

    m and q must be scalars.r   q >=0zm must be an integer >=0.r         @L@fffff`@̬V@      1@@T㥛 ?笭_vOn?      ?   )Warning, too many predicted coefficients.rn   N)	r   rc   r   r	   rb   printr   r$   fcoef)r}   qqmkmkdafcrk   rk   rl   rN   `  s&    #*(
rN   c                 C   s   t | rt |std|dk r(td| t| ks<| dkrDtd|dkrvddt|  d|  d	t| |  }n(d
dt|  d|  dt| |  }t|d|   }|dkrtd d}tt| } | d rd}t| |}t|| ||}|d| S )a?  Fourier coefficients for even Mathieu and modified Mathieu functions.

    The Fourier series of the odd solutions of the Mathieu differential
    equation are of the form

    .. math:: \mathrm{se}_{2n+1}(z, q) = \sum_{k=0}^{\infty} B_{(2n+1)}^{(2k+1)} \sin (2k+1)z

    .. math:: \mathrm{se}_{2n+2}(z, q) = \sum_{k=0}^{\infty} B_{(2n+2)}^{(2k+2)} \sin (2k+2)z

    This function returns the coefficients :math:`B_{(2n+2)}^{(2k+2)}` for even
    input m=2n+2, and the coefficients :math:`B_{(2n+1)}^{(2k+1)}` for odd
    input m=2n+1.

    Parameters
    ----------
    m : int
        Order of Mathieu functions.  Must be non-negative.
    q : float (>=0)
        Parameter of Mathieu functions.  Must be non-negative.

    Returns
    -------
    Bk : ndarray
        Even or odd Fourier coefficients, corresponding to even or odd m.

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    r   r   r   zm must be an integer > 0r   r   r   r   r   r   r   r   r   r   r   r      rn   r   N)	r   rc   r   r	   rb   r   r   r$   r   )r}   r   r   r   r   br   rk   rk   rl   rO     s&    !*(
rO   c           	      C   sZ  t | rt| |krtdt |r,|dk r4tdt |sDtdt|rTtd| dk r"|  }td|d d|d f \}}tjdd~ t|dk rt||kd	d
| t|| d  t|| d  }n,t||kd	t|| d t|| d  }W d   n1 s0    Y  n| }t	
|||\}}| dk rR|| }|| }||fS )aY  Sequence of associated Legendre functions of the first kind.

    Computes the associated Legendre function of the first kind of order m and
    degree n, ``Pmn(z)`` = :math:`P_n^m(z)`, and its derivative, ``Pmn'(z)``.
    Returns two arrays of size ``(m+1, n+1)`` containing ``Pmn(z)`` and
    ``Pmn'(z)`` for all orders from ``0..m`` and degrees from ``0..n``.

    This function takes a real argument ``z``. For complex arguments ``z``
    use clpmn instead.

    Parameters
    ----------
    m : int
       ``|m| <= n``; the order of the Legendre function.
    n : int
       where ``n >= 0``; the degree of the Legendre function.  Often
       called ``l`` (lower case L) in descriptions of the associated
       Legendre function
    z : float
        Input value.

    Returns
    -------
    Pmn_z : (m+1, n+1) array
       Values for all orders 0..m and degrees 0..n
    Pmn_d_z : (m+1, n+1) array
       Derivatives for all orders 0..m and degrees 0..n

    See Also
    --------
    clpmn: associated Legendre functions of the first kind for complex z

    Notes
    -----
    In the interval (-1, 1), Ferrer's function of the first kind is
    returned. The phase convention used for the intervals (1, inf)
    and (-inf, -1) is such that the result is always real.

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html
    .. [2] NIST Digital Library of Mathematical Functions
           https://dlmf.nist.gov/14.3

    m must be <= n.r   !n must be a non-negative integer.z must be scalar.z)Argument must be real. Use clpmn instead.r   ignoreallr   ro   N)r   rt   rc   r   r   ufuncserrstater
   r   r$   rJ   )	r}   rg   r   mpmfnffixarrr   pdrk   rk   rl   rJ     s.    0
 
&N
rJ   r   c           
      C   sj  t | rt| |krtdt |r,|dk r4tdt |sDtd|dks\|dks\td| dk r&|  }td|d d|d f \}}tjd	d
z |dkrt||kdd| t|| d  t|| d  }n,t||kdt|| d t|| d  }W d   n1 s0    Y  n| }t	||t
|t||\}}	| dk rb|| }|	| }	||	fS )a  Associated Legendre function of the first kind for complex arguments.

    Computes the associated Legendre function of the first kind of order m and
    degree n, ``Pmn(z)`` = :math:`P_n^m(z)`, and its derivative, ``Pmn'(z)``.
    Returns two arrays of size ``(m+1, n+1)`` containing ``Pmn(z)`` and
    ``Pmn'(z)`` for all orders from ``0..m`` and degrees from ``0..n``.

    Parameters
    ----------
    m : int
       ``|m| <= n``; the order of the Legendre function.
    n : int
       where ``n >= 0``; the degree of the Legendre function.  Often
       called ``l`` (lower case L) in descriptions of the associated
       Legendre function
    z : float or complex
        Input value.
    type : int, optional
       takes values 2 or 3
       2: cut on the real axis ``|x| > 1``
       3: cut on the real axis ``-1 < x < 1`` (default)

    Returns
    -------
    Pmn_z : (m+1, n+1) array
       Values for all orders ``0..m`` and degrees ``0..n``
    Pmn_d_z : (m+1, n+1) array
       Derivatives for all orders ``0..m`` and degrees ``0..n``

    See Also
    --------
    lpmn: associated Legendre functions of the first kind for real z

    Notes
    -----
    By default, i.e. for ``type=3``, phase conventions are chosen according
    to [1]_ such that the function is analytic. The cut lies on the interval
    (-1, 1). Approaching the cut from above or below in general yields a phase
    factor with respect to Ferrer's function of the first kind
    (cf. `lpmn`).

    For ``type=2`` a cut at ``|x| > 1`` is chosen. Approaching the real values
    on the interval (-1, 1) in the complex plane yields Ferrer's function
    of the first kind.

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html
    .. [2] NIST Digital Library of Mathematical Functions
           https://dlmf.nist.gov/14.21

    r   r   r   r   rn   r   ztype must be either 2 or 3.r   r   r   r   ro   N)r   rt   rc   r   r   r   r
   r   r$   r/   r   r   )
r}   rg   r   typer   r   r   r   r   r   rk   rk   rl   r/      s.    7
 
&N
r/   c                 C   s   t | r| dk rtdt |r(|dk r0tdt |s@tdt| } t|}td| }td|}t|rt|||\}}nt|||\}}|d| d d|d f |d| d d|d f fS )a_  Sequence of associated Legendre functions of the second kind.

    Computes the associated Legendre function of the second kind of order m and
    degree n, ``Qmn(z)`` = :math:`Q_n^m(z)`, and its derivative, ``Qmn'(z)``.
    Returns two arrays of size ``(m+1, n+1)`` containing ``Qmn(z)`` and
    ``Qmn'(z)`` for all orders from ``0..m`` and degrees from ``0..n``.

    Parameters
    ----------
    m : int
       ``|m| <= n``; the order of the Legendre function.
    n : int
       where ``n >= 0``; the degree of the Legendre function.  Often
       called ``l`` (lower case L) in descriptions of the associated
       Legendre function
    z : complex
        Input value.

    Returns
    -------
    Qmn_z : (m+1, n+1) array
       Values for all orders 0..m and degrees 0..n
    Qmn_d_z : (m+1, n+1) array
       Derivatives for all orders 0..m and degrees 0..n

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    r   z!m must be a non-negative integer.r   r   r   N)r   rc   rb   maxr   r$   ZclqmnrL   )r}   rg   r   mmnnr   qdrk   rk   rl   rL   q  s    !

rL   c                 C   sL   t | r| dk rtdt| } | dk r.d}n| }tt|d| d  S )a'  Bernoulli numbers B0..Bn (inclusive).

    Parameters
    ----------
    n : int
        Indicated the number of terms in the Bernoulli series to generate.

    Returns
    -------
    ndarray
        The Bernoulli numbers ``[B(0), B(1), ..., B(n)]``.

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html
    .. [2] "Bernoulli number", Wikipedia, https://en.wikipedia.org/wiki/Bernoulli_number

    Examples
    --------
    >>> from scipy.special import bernoulli, zeta
    >>> bernoulli(4)
    array([ 1.        , -0.5       ,  0.16666667,  0.        , -0.03333333])

    The Wikipedia article ([2]_) points out the relationship between the
    Bernoulli numbers and the zeta function, ``B_n^+ = -n * zeta(1 - n)``
    for ``n > 0``:

    >>> n = np.arange(1, 5)
    >>> -n * zeta(1 - n)
    array([ 0.5       ,  0.16666667, -0.        , -0.03333333])

    Note that, in the notation used in the wikipedia article,
    `bernoulli` computes ``B_n^-`` (i.e. it used the convention that
    ``B_1`` is -1/2).  The relation given above is for ``B_n^+``, so the
    sign of 0.5 does not match the output of ``bernoulli(4)``.

    r   r   rn   Nr   )r   rc   rb   r$   Zbernobrg   r   rk   rk   rl   r,     s    (r,   c                 C   sH   t | r| dk rtdt| } | dk r.d}n| }t|d| d  S )a  Euler numbers E(0), E(1), ..., E(n).

    The Euler numbers [1]_ are also known as the secant numbers.

    Because ``euler(n)`` returns floating point values, it does not give
    exact values for large `n`.  The first inexact value is E(22).

    Parameters
    ----------
    n : int
        The highest index of the Euler number to be returned.

    Returns
    -------
    ndarray
        The Euler numbers [E(0), E(1), ..., E(n)].
        The odd Euler numbers, which are all zero, are included.

    References
    ----------
    .. [1] Sequence A122045, The On-Line Encyclopedia of Integer Sequences,
           https://oeis.org/A122045
    .. [2] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    Examples
    --------
    >>> from scipy.special import euler
    >>> euler(6)
    array([  1.,   0.,  -1.,   0.,   5.,   0., -61.])

    >>> euler(13).astype(np.int64)
    array([      1,       0,      -1,       0,       5,       0,     -61,
                 0,    1385,       0,  -50521,       0, 2702765,       0])

    >>> euler(22)[-1]  # Exact value of E(22) is -69348874393137901.
    -69348874393137976.0

    r   r   rn   Nr   )r   rc   rb   r$   Zeulerbr   rk   rk   rl   r4     s    )r4   c                 C   s   t | rt |stdt| ddd} | dk r4d}n| }t|rRt||\}}nt||\}}|d| d  |d| d  fS )a  Legendre function of the first kind.

    Compute sequence of Legendre functions of the first kind (polynomials),
    Pn(z) and derivatives for all degrees from 0 to n (inclusive).

    See also special.legendre for polynomial class.

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    r   rg   Fr   r   N)r   rc   rm   r   r$   ZclpnrK   )rg   r   r   Zpnr   rk   rk   rl   rK     s    rK   c                 C   s   t | rt |stdt| ddd} | dk r4d}n| }t|rRt||\}}nt||\}}|d| d  |d| d  fS )a  Legendre function of the second kind.

    Compute sequence of Legendre functions of the second kind, Qn(z) and
    derivatives for all degrees from 0 to n (inclusive).

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    r   rg   Fr   r   N)r   rc   rm   r   r$   ZclqnZlqnb)rg   r   r   qnr   rk   rk   rl   rM   (  s    rM   c                 C   s4   d}t | r t| | ks | dkr(tdt| |S )a  
    Compute `nt` zeros and values of the Airy function Ai and its derivative.

    Computes the first `nt` zeros, `a`, of the Airy function Ai(x);
    first `nt` zeros, `ap`, of the derivative of the Airy function Ai'(x);
    the corresponding values Ai(a');
    and the corresponding values Ai'(a).

    Parameters
    ----------
    nt : int
        Number of zeros to compute

    Returns
    -------
    a : ndarray
        First `nt` zeros of Ai(x)
    ap : ndarray
        First `nt` zeros of Ai'(x)
    ai : ndarray
        Values of Ai(x) evaluated at first `nt` zeros of Ai'(x)
    aip : ndarray
        Values of Ai'(x) evaluated at first `nt` zeros of Ai(x)

    Examples
    --------
    >>> from scipy import special
    >>> a, ap, ai, aip = special.ai_zeros(3)
    >>> a
    array([-2.33810741, -4.08794944, -5.52055983])
    >>> ap
    array([-1.01879297, -3.24819758, -4.82009921])
    >>> ai
    array([ 0.53565666, -0.41901548,  0.38040647])
    >>> aip
    array([ 0.70121082, -0.80311137,  0.86520403])

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    r   r   %nt must be a positive integer scalar.r   r   rc   r$   Zairyzor|   r   rk   rk   rl   r'   C  s    -r'   c                 C   s4   d}t | r t| | ks | dkr(tdt| |S )a  
    Compute `nt` zeros and values of the Airy function Bi and its derivative.

    Computes the first `nt` zeros, b, of the Airy function Bi(x);
    first `nt` zeros, b', of the derivative of the Airy function Bi'(x);
    the corresponding values Bi(b');
    and the corresponding values Bi'(b).

    Parameters
    ----------
    nt : int
        Number of zeros to compute

    Returns
    -------
    b : ndarray
        First `nt` zeros of Bi(x)
    bp : ndarray
        First `nt` zeros of Bi'(x)
    bi : ndarray
        Values of Bi(x) evaluated at first `nt` zeros of Bi'(x)
    bip : ndarray
        Values of Bi'(x) evaluated at first `nt` zeros of Bi(x)

    Examples
    --------
    >>> from scipy import special
    >>> b, bp, bi, bip = special.bi_zeros(3)
    >>> b
    array([-1.17371322, -3.2710933 , -4.83073784])
    >>> bp
    array([-2.29443968, -4.07315509, -5.51239573])
    >>> bi
    array([-0.45494438,  0.39652284, -0.36796916])
    >>> bip
    array([ 0.60195789, -0.76031014,  0.83699101])

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    rn   r   r   r   r   rk   rk   rl   r.   v  s    -r.   c           	      C   s   t | rt |std| dk r(tdt| }| | }|dk rFd}n|}|| }| t| krrt||\}}}nt||\}}}|d|d  |d|d  fS )a  Jahnke-Emden Lambda function, Lambdav(x).

    This function is defined as [2]_,

    .. math:: \Lambda_v(x) = \Gamma(v+1) \frac{J_v(x)}{(x/2)^v},

    where :math:`\Gamma` is the gamma function and :math:`J_v` is the
    Bessel function of the first kind.

    Parameters
    ----------
    v : float
        Order of the Lambda function
    x : float
        Value at which to evaluate the function and derivatives

    Returns
    -------
    vl : ndarray
        Values of Lambda_vi(x), for vi=v-int(v), vi=1+v-int(v), ..., vi=v.
    dl : ndarray
        Derivatives Lambda_vi'(x), for vi=v-int(v), vi=1+v-int(v), ..., vi=v.

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html
    .. [2] Jahnke, E. and Emde, F. "Tables of Functions with Formulae and
           Curves" (4th ed.), Dover, 1945
    r   r   zargument must be > 0.r   N)r   rc   rb   r   r$   ZlamvZlamn)	r   rw   rg   v0r   v1ZvmZvldlrk   rk   rl   rI     s     rI   c           
      C   sv   t | rt |stdt| }| | }|dk r6d}n|}|| }t||\}}}}	|d|d  |d|d  fS )a  Parabolic cylinder functions Dv(x) and derivatives.

    Parameters
    ----------
    v : float
        Order of the parabolic cylinder function
    x : float
        Value at which to evaluate the function and derivatives

    Returns
    -------
    dv : ndarray
        Values of D_vi(x), for vi=v-int(v), vi=1+v-int(v), ..., vi=v.
    dp : ndarray
        Derivatives D_vi'(x), for vi=v-int(v), vi=1+v-int(v), ..., vi=v.

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996, chapter 13.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    r   r   N)r   rc   rb   r$   Zpbdv
r   rw   rg   r   r   r   ZdvZdpZpdfZpddrk   rk   rl   rR     s    rR   c           
      C   sv   t | rt |stdt| }| | }|dkr6d}n|}|| }t||\}}}}	|d|d  |d|d  fS )a  Parabolic cylinder functions Vv(x) and derivatives.

    Parameters
    ----------
    v : float
        Order of the parabolic cylinder function
    x : float
        Value at which to evaluate the function and derivatives

    Returns
    -------
    dv : ndarray
        Values of V_vi(x), for vi=v-int(v), vi=1+v-int(v), ..., vi=v.
    dp : ndarray
        Derivatives V_vi'(x), for vi=v-int(v), vi=1+v-int(v), ..., vi=v.

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996, chapter 13.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    r   r   N)r   rc   rb   r$   Zpbvvr   rk   rk   rl   rS      s    rS   c                 C   sr   t | rt |stdt| | kr,tdt| dkr>d}n| }t||\}}|d|d  |d|d  fS )a~  Parabolic cylinder functions Dn(z) and derivatives.

    Parameters
    ----------
    n : int
        Order of the parabolic cylinder function
    z : complex
        Value at which to evaluate the function and derivatives

    Returns
    -------
    dv : ndarray
        Values of D_i(z), for i=0, ..., i=n.
    dp : ndarray
        Derivatives D_i'(z), for i=0, ..., i=n.

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996, chapter 13.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    r   zn must be an integer.r   N)r   rc   r   rt   r$   Zcpbdn)rg   r   r   ZcpbZcpdrk   rk   rl   rQ   %  s    rQ   c                 C   s0   t | rt| | ks| dkr$tdt| dS )a  Compute nt zeros of the Kelvin function ber.

    Parameters
    ----------
    nt : int
        Number of zeros to compute. Must be positive.

    Returns
    -------
    ndarray
        First `nt` zeros of the Kelvin function.

    See Also
    --------
    ber

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    r   #nt must be positive integer scalar.r   r   r   rc   r$   Zklvnzor   rk   rk   rl   r+   I  s    r+   c                 C   s0   t | rt| | ks| dkr$tdt| dS )a  Compute nt zeros of the Kelvin function bei.

    Parameters
    ----------
    nt : int
        Number of zeros to compute. Must be positive.

    Returns
    -------
    ndarray
        First `nt` zeros of the Kelvin function.

    See Also
    --------
    bei

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    r   r   rn   r   r   rk   rk   rl   r)   f  s    r)   c                 C   s0   t | rt| | ks| dkr$tdt| dS )a  Compute nt zeros of the Kelvin function ker.

    Parameters
    ----------
    nt : int
        Number of zeros to compute. Must be positive.

    Returns
    -------
    ndarray
        First `nt` zeros of the Kelvin function.

    See Also
    --------
    ker

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    r   r   r   r   r   rk   rk   rl   rF     s    rF   c                 C   s0   t | rt| | ks| dkr$tdt| dS )a  Compute nt zeros of the Kelvin function kei.

    Parameters
    ----------
    nt : int
        Number of zeros to compute. Must be positive.

    Returns
    -------
    ndarray
        First `nt` zeros of the Kelvin function.

    See Also
    --------
    kei

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    r   r   r   r   r   rk   rk   rl   rC     s    rC   c                 C   s0   t | rt| | ks| dkr$tdt| dS )a,  Compute nt zeros of the derivative of the Kelvin function ber.

    Parameters
    ----------
    nt : int
        Number of zeros to compute. Must be positive.

    Returns
    -------
    ndarray
        First `nt` zeros of the derivative of the Kelvin function.

    See Also
    --------
    ber, berp

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    r   r      r   r   rk   rk   rl   r-     s    r-   c                 C   s0   t | rt| | ks| dkr$tdt| dS )a,  Compute nt zeros of the derivative of the Kelvin function bei.

    Parameters
    ----------
    nt : int
        Number of zeros to compute. Must be positive.

    Returns
    -------
    ndarray
        First `nt` zeros of the derivative of the Kelvin function.

    See Also
    --------
    bei, beip

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    r   r      r   r   rk   rk   rl   r*     s    r*   c                 C   s0   t | rt| | ks| dkr$tdt| dS )a,  Compute nt zeros of the derivative of the Kelvin function ker.

    Parameters
    ----------
    nt : int
        Number of zeros to compute. Must be positive.

    Returns
    -------
    ndarray
        First `nt` zeros of the derivative of the Kelvin function.

    See Also
    --------
    ker, kerp

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    r   r      r   r   rk   rk   rl   rG     s    rG   c                 C   s0   t | rt| | ks| dkr$tdt| dS )a,  Compute nt zeros of the derivative of the Kelvin function kei.

    Parameters
    ----------
    nt : int
        Number of zeros to compute. Must be positive.

    Returns
    -------
    ndarray
        First `nt` zeros of the derivative of the Kelvin function.

    See Also
    --------
    kei, keip

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    r   r      r   r   rk   rk   rl   rD     s    rD   c                 C   sx   t | rt| | ks| dkr$tdt| dt| dt| dt| dt| dt| dt| d	t| d
fS )a  Compute nt zeros of all Kelvin functions.

    Returned in a length-8 tuple of arrays of length nt.  The tuple contains
    the arrays of zeros of (ber, bei, ker, kei, ber', bei', ker', kei').

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    r   r   r   rn   r   r   r   r   r   r   r   r   rk   rk   rl   rE   1  s    







rE   c                 C   s|   t | rt |rt |s td|t|ks8| t| kr@td||  dkrTtd||  d }t| ||dd d| S )a  Characteristic values for prolate spheroidal wave functions.

    Compute a sequence of characteristic values for the prolate
    spheroidal wave functions for mode m and n'=m..n and spheroidal
    parameter c.

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    r   Modes must be integers.   (Difference between n and m is too large.r   Nr   rc   r   r$   Zsegvr}   rg   cZmaxLrk   rk   rl   rV   J  s    rV   c                 C   s|   t | rt |rt |s td|t|ks8| t| kr@td||  dkrTtd||  d }t| ||dd d| S )a  Characteristic values for oblate spheroidal wave functions.

    Compute a sequence of characteristic values for the oblate
    spheroidal wave functions for mode m and n'=m..n and spheroidal
    parameter c.

    References
    ----------
    .. [1] Zhang, Shanjie and Jin, Jianming. "Computation of Special
           Functions", John Wiley and Sons, 1996.
           https://people.sc.fsu.edu/~jburkardt/f_src/special_functions/special_functions.html

    r   r   r   r   r   ro   Nr   r   rk   rk   rl   rP   b  s    rP   c                 C   s   |rt | | d ||S |r&t| |S t|t|  }} || k| dk@ |dk@ }t| |}t|tjrrd|| < n|std}|S dS )a{  The number of combinations of N things taken k at a time.

    This is often expressed as "N choose k".

    Parameters
    ----------
    N : int, ndarray
        Number of things.
    k : int, ndarray
        Number of elements taken.
    exact : bool, optional
        If `exact` is False, then floating point precision is used, otherwise
        exact long integer is computed.
    repetition : bool, optional
        If `repetition` is True, then the number of combinations with
        repetition is computed.

    Returns
    -------
    val : int, float, ndarray
        The total number of combinations.

    See Also
    --------
    binom : Binomial coefficient ufunc

    Notes
    -----
    - Array arguments accepted only for exact=False case.
    - If N < 0, or k < 0, then 0 is returned.
    - If k > N and repetition=False, then 0 is returned.

    Examples
    --------
    >>> from scipy.special import comb
    >>> k = np.array([3, 4])
    >>> n = np.array([10, 10])
    >>> comb(n, k, exact=False)
    array([ 120.,  210.])
    >>> comb(10, 3, exact=True)
    120
    >>> comb(10, 3, exact=True, repetition=True)
    220

    r   r   N)r0   r&   r   r"   
isinstancers   ndarrayfloat64)Nr   exactZ
repetitioncondvalsrk   rk   rl   r0   z  s    .


r0   c                 C   s   |rL|| ks| dk s|dk r dS d}t | | d | d D ]}||9 }q:|S t|t|  }} || k| dk@ |dk@ }t| | d |}t|tjrd|| < n|std}|S dS )aH  Permutations of N things taken k at a time, i.e., k-permutations of N.

    It's also known as "partial permutations".

    Parameters
    ----------
    N : int, ndarray
        Number of things.
    k : int, ndarray
        Number of elements taken.
    exact : bool, optional
        If `exact` is False, then floating point precision is used, otherwise
        exact long integer is computed.

    Returns
    -------
    val : int, ndarray
        The number of k-permutations of N.

    Notes
    -----
    - Array arguments accepted only for exact=False case.
    - If k > N, N < 0, or k < 0, then a 0 is returned.

    Examples
    --------
    >>> from scipy.special import perm
    >>> k = np.array([3, 4])
    >>> n = np.array([10, 10])
    >>> perm(n, k)
    array([  720.,  5040.])
    >>> perm(10, 3, exact=True)
    720

    r   r   N)r   r   r!   r   rs   r   r   )r   r   r   valr   r   r   rk   rk   rl   rT     s    $

rT   c                 C   sD   | d |k r0||  d }t | |t |d | S | |kr<| S | | S )z
    Product of a range of numbers.

    Returns the product of
    lo * (lo+1) * (lo+2) * ... * (hi-2) * (hi-1) * hi
    = hi! / (lo-1)!

    Breaks into smaller products first for speed:
    _range_prod(2, 9) = ((2*3)*(4*5))*((6*7)*(8*9))
    r   rn   )_range_prod)lohiZmidrk   rk   rl   r     s    r   c           	      C   s  |rt | dkr8t | r"| S | dk r.dS t| S t| } t | t}t | 	 rdt
}n,|d dkrvt}n|d dkrt j}nt j}t j| |d}t jdd4 ||dk }d|| d	k < d|| dk < W d
   n1 s0    Y  |jr^t|d }||| |d k< tt|d D ]8}|| d }||d  }|t||9 }||| |k< q$t | 	 r|t j}| t |  |t | < |S nt| }|S d
S )aA  
    The factorial of a number or array of numbers.

    The factorial of non-negative integer `n` is the product of all
    positive integers less than or equal to `n`::

        n! = n * (n - 1) * (n - 2) * ... * 1

    Parameters
    ----------
    n : int or array_like of ints
        Input values.  If ``n < 0``, the return value is 0.
    exact : bool, optional
        If True, calculate the answer exactly using long integer arithmetic.
        If False, result is approximated in floating point rapidly using the
        `gamma` function.
        Default is False.

    Returns
    -------
    nf : float or int or ndarray
        Factorial of `n`, as integer or float depending on `exact`.

    Notes
    -----
    For arrays with ``exact=True``, the factorial is computed only once, for
    the largest input, with each other result computed in the process.
    The output dtype is increased to ``int64`` or ``object`` if necessary.

    With ``exact=False`` the factorial is approximated using the gamma
    function:

    .. math:: n! = \Gamma(n+1)

    Examples
    --------
    >>> from scipy.special import factorial
    >>> arr = np.array([3, 4, 5])
    >>> factorial(arr, exact=False)
    array([   6.,   24.,  120.])
    >>> factorial(arr, exact=True)
    array([  6,  24, 120])
    >>> factorial(5, exact=True)
    120

    r   ro         )rp   r   r   r   rn   N)rs   ndimisnanmathr5   r   uniqueZastypeobjectanyrq   Zint64int_Z
empty_liker   sizer   lenr   r   r   Z
_factorial)	rg   r   undtoutr   r   prevcurrentrk   rk   rl   r5   	  sB    /
*
r5   c                 C   s   |r>| dk rdS | dkrdS d}t | ddD ]}||9 }q,|S t| } t| jd}| d | dk@ }d| d  | dk@ }t|| }t|| }|d }	|d }
t||t|	d tt t	d|	d   t||t|
d t	d|
  |S d	S )
a  Double factorial.

    This is the factorial with every second value skipped.  E.g., ``7!! = 7 * 5
    * 3 * 1``.  It can be approximated numerically as::

      n!! = special.gamma(n/2+1)*2**((m+1)/2)/sqrt(pi)  n odd
          = 2**(n/2) * (n/2)!                           n even

    Parameters
    ----------
    n : int or array_like
        Calculate ``n!!``.  Arrays are only supported with `exact` set
        to False.  If ``n < 0``, the return value is 0.
    exact : bool, optional
        The result can be approximated rapidly using the gamma-formula
        above (default).  If `exact` is set to True, calculate the
        answer exactly using integer arithmetic.

    Returns
    -------
    nff : float or int
        Double factorial of `n`, as an int or a float depending on
        `exact`.

    Examples
    --------
    >>> from scipy.special import factorial2
    >>> factorial2(7, exact=False)
    array(105.00000000000001)
    >>> factorial2(7, exact=True)
    105

    ro   r   r   drn   r   r   N)
r   r   r   rr   r   r   r   r	   r   ru   )rg   r   r   r   r   Zcond1Zcond2ZoddnZevennZnd2oZnd2erk   rk   rl   r6   _	  s(    "


*r6   c                 C   sL   |rD| d| k rdS | dkr dS d}t | d| D ]}|| }q2|S tdS )a  Multifactorial of n of order k, n(!!...!).

    This is the multifactorial of n skipping k values.  For example,

      factorialk(17, 4) = 17!!!! = 17 * 13 * 9 * 5 * 1

    In particular, for any integer ``n``, we have

      factorialk(n, 1) = factorial(n)

      factorialk(n, 2) = factorial2(n)

    Parameters
    ----------
    n : int
        Calculate multifactorial. If `n` < 0, the return value is 0.
    k : int
        Order of multifactorial.
    exact : bool, optional
        If exact is set to True, calculate the answer exactly using
        integer arithmetic.

    Returns
    -------
    val : int
        Multifactorial of `n`.

    Raises
    ------
    NotImplementedError
        Raises when exact is False

    Examples
    --------
    >>> from scipy.special import factorialk
    >>> factorialk(5, 1, exact=True)
    120
    >>> factorialk(5, 3, exact=True)
    10

    r   r   N)r   NotImplementedError)rg   r   r   r   jrk   rk   rl   r7   	  s    *
r7   c                 C   s&   |du rt | |S t | ||S dS )a  
    Riemann or Hurwitz zeta function.

    Parameters
    ----------
    x : array_like of float
        Input data, must be real
    q : array_like of float, optional
        Input data, must be real.  Defaults to Riemann zeta.
    out : ndarray, optional
        Output array for the computed values.

    Returns
    -------
    out : array_like
        Values of zeta(x).

    Notes
    -----
    The two-argument version is the Hurwitz zeta function

    .. math::

        \zeta(x, q) = \sum_{k=0}^{\infty} \frac{1}{(k + q)^x};

    see [dlmf]_ for details. The Riemann zeta function corresponds to
    the case when ``q = 1``.

    See Also
    --------
    zetac

    References
    ----------
    .. [dlmf] NIST, Digital Library of Mathematical Functions,
        https://dlmf.nist.gov/25.11#i

    Examples
    --------
    >>> from scipy.special import zeta, polygamma, factorial

    Some specific values:

    >>> zeta(2), np.pi**2/6
    (1.6449340668482266, 1.6449340668482264)

    >>> zeta(4), np.pi**4/90
    (1.0823232337111381, 1.082323233711138)

    Relation to the `polygamma` function:

    >>> m = 3
    >>> x = 1.25
    >>> polygamma(m, x)
    array(2.782144009188397)
    >>> (-1)**(m+1) * factorial(m) * zeta(m+1, x)
    2.7821440091883969

    N)r   Z_riemann_zetaZ_zeta)rw   r   r   rk   rk   rl   r_   	  s    <r_   )T)F)F)F)r   )r   )r   )r   )r   )r   )r   )r   )FF)F)F)F)T)NN)hr`   Znumpyrs   r   r   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r    r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   Z_combr&   __all__rm   r2   r?   rA   r>   r@   r\   r]   rY   rZ   r[   r   rB   r^   rH   r=   r;   r<   rW   rX   r3   r9   r:   r8   r(   r1   rU   rN   rO   rJ   r/   rL   r,   r4   rK   rM   r'   r.   rI   rR   rS   rQ   r+   r)   rF   rC   r-   r*   rG   rD   rE   rV   rP   r0   rT   r   r5   r6   r7   r_   rk   rk   rk   rl   <module>   s   P@J
e)-55.*
$
$
$
)
(
7
-
&
&44(
+;9L
Q523332%%$
=
7
^
9
7