a
    BCCf                  
   @   s  d dl Zd dlmZ d dlm  mZ d dlm	Z	 dddddddddd	dd	Z
ejfd
dZdZdd Zdd Zed e_ed e_d ge_de_dejfddZdd Zdd Zdd Zdd Zdd Zd*ddZdd  Zd!dded"ddfd#d$Zd+d&d'Zd(d) Z dS ),    N)special)_RichResult F   )	argslogmaxfunmaxlevelminlevelatolrtolpreserve_shapecallbackc       	   1         s  t | |||| ||
|\} }}}} }}
}tjdddd | |  d |j}t|t| }}|| d ||< || d ||< d|||@ < tj| |f|d|
d}W d   n1 s0    Y  |\} }}}}}t	||
| }t	||
| }t||\}}}}}}d	\}}rDtj nd}|tj}| d }t|j}du rrd
t| n|d
 tj|||d }tj|t|t|B t|d B < t|ddddddf }tj|tj|d } tj|tjtd }!tt|dtj|tj |d }"tj|tj|d }#tj||d }$tj|tj|d }%tj|tj|d }&tj||d }'tj||d }(t||| ||||dd|dd|||!|"|#|$|%|&|'|(||||ddd})g d}*fdd}+fdd}, fdd}-dd }.fdd}/tjdddd4 t|)|||| |||+|,|-|.|/|*|
}0W d   n1 s0    Y  |0S )u2  Evaluate a convergent integral numerically using tanh-sinh quadrature.

    In practice, tanh-sinh quadrature achieves quadratic convergence for
    many integrands: the number of accurate *digits* scales roughly linearly
    with the number of function evaluations [1]_.

    Either or both of the limits of integration may be infinite, and
    singularities at the endpoints are acceptable. Divergent integrals and
    integrands with non-finite derivatives or singularities within an interval
    are out of scope, but the latter may be evaluated be calling `_tanhsinh` on
    each sub-interval separately.

    Parameters
    ----------
    f : callable
        The function to be integrated. The signature must be::
            func(x: ndarray, *fargs) -> ndarray
         where each element of ``x`` is a finite real and ``fargs`` is a tuple,
         which may contain an arbitrary number of arrays that are broadcastable
         with `x`. ``func`` must be an elementwise-scalar function; see
         documentation of parameter `preserve_shape` for details.
         If ``func`` returns a value with complex dtype when evaluated at
         either endpoint, subsequent arguments ``x`` will have complex dtype
         (but zero imaginary part).
    a, b : array_like
        Real lower and upper limits of integration. Must be broadcastable.
        Elements may be infinite.
    args : tuple, optional
        Additional positional arguments to be passed to `func`. Must be arrays
        broadcastable with `a` and `b`. If the callable to be integrated
        requires arguments that are not broadcastable with `a` and `b`, wrap
        that callable with `f`. See Examples.
    log : bool, default: False
        Setting to True indicates that `f` returns the log of the integrand
        and that `atol` and `rtol` are expressed as the logs of the absolute
        and relative errors. In this case, the result object will contain the
        log of the integral and error. This is useful for integrands for which
        numerical underflow or overflow would lead to inaccuracies.
        When ``log=True``, the integrand (the exponential of `f`) must be real,
        but it may be negative, in which case the log of the integrand is a
        complex number with an imaginary part that is an odd multiple of π.
    maxlevel : int, default: 10
        The maximum refinement level of the algorithm.

        At the zeroth level, `f` is called once, performing 16 function
        evaluations. At each subsequent level, `f` is called once more,
        approximately doubling the number of function evaluations that have
        been performed. Accordingly, for many integrands, each successive level
        will double the number of accurate digits in the result (up to the
        limits of floating point precision).

        The algorithm will terminate after completing level `maxlevel` or after
        another termination condition is satisfied, whichever comes first.
    minlevel : int, default: 2
        The level at which to begin iteration (default: 2). This does not
        change the total number of function evaluations or the abscissae at
        which the function is evaluated; it changes only the *number of times*
        `f` is called. If ``minlevel=k``, then the integrand is evaluated at
        all abscissae from levels ``0`` through ``k`` in a single call.
        Note that if `minlevel` exceeds `maxlevel`, the provided `minlevel` is
        ignored, and `minlevel` is set equal to `maxlevel`.
    atol, rtol : float, optional
        Absolute termination tolerance (default: 0) and relative termination
        tolerance (default: ``eps**0.75``, where ``eps`` is the precision of
        the result dtype), respectively. The error estimate is as
        described in [1]_ Section 5. While not theoretically rigorous or
        conservative, it is said to work well in practice. Must be non-negative
        and finite if `log` is False, and must be expressed as the log of a
        non-negative and finite number if `log` is True.
    preserve_shape : bool, default: False
        In the following, "arguments of `f`" refers to the array ``x`` and
        any arrays within ``fargs``. Let ``shape`` be the broadcasted shape
        of `a`, `b`, and all elements of `args` (which is conceptually
        distinct from ``fargs`` passed into `f`).

        - When ``preserve_shape=False`` (default), `f` must accept arguments
          of *any* broadcastable shapes.

        - When ``preserve_shape=True``, `f` must accept arguments of shape
          ``shape`` *or* ``shape + (n,)``, where ``(n,)`` is the number of
          abscissae at which the function is being evaluated.

        In either case, for each scalar element ``xi`` within `x`, the array
        returned by `f` must include the scalar ``f(xi)`` at the same index.
        Consequently, the shape of the output is always the shape of the input
        ``x``.

        See Examples.

    callback : callable, optional
        An optional user-supplied function to be called before the first
        iteration and after each iteration.
        Called as ``callback(res)``, where ``res`` is a ``_RichResult``
        similar to that returned by `_differentiate` (but containing the
        current iterate's values of all variables). If `callback` raises a
        ``StopIteration``, the algorithm will terminate immediately and
        `_tanhsinh` will return a result object.

    Returns
    -------
    res : _RichResult
        An instance of `scipy._lib._util._RichResult` with the following
        attributes. (The descriptions are written as though the values will be
        scalars; however, if `func` returns an array, the outputs will be
        arrays of the same shape.)
        success : bool
            ``True`` when the algorithm terminated successfully (status ``0``).
        status : int
            An integer representing the exit status of the algorithm.
            ``0`` : The algorithm converged to the specified tolerances.
            ``-1`` : (unused)
            ``-2`` : The maximum number of iterations was reached.
            ``-3`` : A non-finite value was encountered.
            ``-4`` : Iteration was terminated by `callback`.
            ``1`` : The algorithm is proceeding normally (in `callback` only).
        integral : float
            An estimate of the integral
        error : float
            An estimate of the error. Only available if level two or higher
            has been completed; otherwise NaN.
        maxlevel : int
            The maximum refinement level used.
        nfev : int
            The number of points at which `func` was evaluated.

    See Also
    --------
    quad, quadrature

    Notes
    -----
    Implements the algorithm as described in [1]_ with minor adaptations for
    finite-precision arithmetic, including some described by [2]_ and [3]_. The
    tanh-sinh scheme was originally introduced in [4]_.

    Due to floating-point error in the abscissae, the function may be evaluated
    at the endpoints of the interval during iterations. The values returned by
    the function at the endpoints will be ignored.

    References
    ----------
    [1] Bailey, David H., Karthik Jeyabalan, and Xiaoye S. Li. "A comparison of
        three high-precision quadrature schemes." Experimental Mathematics 14.3
        (2005): 317-329.
    [2] Vanherck, Joren, Bart Sorée, and Wim Magnus. "Tanh-sinh quadrature for
        single and multiple integration using floating-point arithmetic."
        arXiv preprint arXiv:2007.15057 (2020).
    [3] van Engelen, Robert A.  "Improving the Double Exponential Quadrature
        Tanh-Sinh, Sinh-Sinh and Exp-Sinh Formulas."
        https://www.genivia.com/files/qthsh.pdf
    [4] Takahasi, Hidetosi, and Masatake Mori. "Double exponential formulas for
        numerical integration." Publications of the Research Institute for
        Mathematical Sciences 9.3 (1974): 721-741.

    Example
    -------
    Evaluate the Gaussian integral:

    >>> import numpy as np
    >>> from scipy.integrate._tanhsinh import _tanhsinh
    >>> def f(x):
    ...     return np.exp(-x**2)
    >>> res = _tanhsinh(f, -np.inf, np.inf)
    >>> res.integral  # true value is np.sqrt(np.pi), 1.7724538509055159
     1.7724538509055159
    >>> res.error  # actual error is 0
    4.0007963937534104e-16

    The value of the Gaussian function (bell curve) is nearly zero for
    arguments sufficiently far from zero, so the value of the integral
    over a finite interval is nearly the same.

    >>> _tanhsinh(f, -20, 20).integral
    1.772453850905518

    However, with unfavorable integration limits, the integration scheme
    may not be able to find the important region.

    >>> _tanhsinh(f, -np.inf, 1000).integral
    4.500490856620352

    In such cases, or when there are singularities within the interval,
    break the integral into parts with endpoints at the important points.

    >>> _tanhsinh(f, -np.inf, 0).integral + _tanhsinh(f, 0, 1000).integral
    1.772453850905404

    For integration involving very large or very small magnitudes, use
    log-integration. (For illustrative purposes, the following example shows a
    case in which both regular and log-integration work, but for more extreme
    limits of integration, log-integration would avoid the underflow
    experienced when evaluating the integral normally.)

    >>> res = _tanhsinh(f, 20, 30, rtol=1e-10)
    >>> res.integral, res.error
    4.7819613911309014e-176, 4.670364401645202e-187
    >>> def log_f(x):
    ...     return -x**2
    >>> np.exp(res.integral), np.exp(res.error)
    4.7819613911306924e-176, 4.670364401645093e-187

    The limits of integration and elements of `args` may be broadcastable
    arrays, and integration is performed elementwise.

    >>> from scipy import stats
    >>> dist = stats.gausshyper(13.8, 3.12, 2.51, 5.18)
    >>> a, b = dist.support()
    >>> x = np.linspace(a, b, 100)
    >>> res = _tanhsinh(dist.pdf, a, x)
    >>> ref = dist.cdf(x)
    >>> np.allclose(res.integral, ref)

    By default, `preserve_shape` is False, and therefore the callable
    `f` may be called with arrays of any broadcastable shapes.
    For example:

    >>> shapes = []
    >>> def f(x, c):
    ...    shape = np.broadcast_shapes(x.shape, c.shape)
    ...    shapes.append(shape)
    ...    return np.sin(c*x)
    >>>
    >>> c = [1, 10, 30, 100]
    >>> res = _tanhsinh(f, 0, 1, args=(c,), minlevel=1)
    >>> shapes
    [(4,), (4, 66), (3, 64), (2, 128), (1, 256)]

    To understand where these shapes are coming from - and to better
    understand how `_tanhsinh` computes accurate results - note that
    higher values of ``c`` correspond with higher frequency sinusoids.
    The higher frequency sinusoids make the integrand more complicated,
    so more function evaluations are required to achieve the target
    accuracy:

    >>> res.nfev
    array([ 67, 131, 259, 515])

    The initial ``shape``, ``(4,)``, corresponds with evaluating the
    integrand at a single abscissa and all four frequencies; this is used
    for input validation and to determine the size and dtype of the arrays
    that store results. The next shape corresponds with evaluating the
    integrand at an initial grid of abscissae and all four frequencies.
    Successive calls to the function double the total number of abscissae at
    which the function has been evaluated. However, in later function
    evaluations, the integrand is evaluated at fewer frequencies because
    the corresponding integral has already converged to the required
    tolerance. This saves function evaluations to improve performance, but
    it requires the function to accept arguments of any shape.

    "Vector-valued" integrands, such as those written for use with
    `scipy.integrate.quad_vec`, are unlikely to satisfy this requirement.
    For example, consider

    >>> def f(x):
    ...    return [x, np.sin(10*x), np.cos(30*x), x*np.sin(100*x)**2]

    This integrand is not compatible with `_tanhsinh` as written; for instance,
    the shape of the output will not be the same as the shape of ``x``. Such a
    function *could* be converted to a compatible form with the introduction of
    additional parameters, but this would be inconvenient. In such cases,
    a simpler solution would be to use `preserve_shape`.

    >>> shapes = []
    >>> def f(x):
    ...     shapes.append(x.shape)
    ...     x0, x1, x2, x3 = x
    ...     return [x0, np.sin(10*x1), np.cos(30*x2), x3*np.sin(100*x3)]
    >>>
    >>> a = np.zeros(4)
    >>> res = _tanhsinh(f, a, 1, preserve_shape=True)
    >>> shapes
    [(4,), (4, 66), (4, 64), (4, 128), (4, 256)]

    Here, the broadcasted shape of `a` and `b` is ``(4,)``. With
    ``preserve_shape=True``, the function may be called with argument
    ``x`` of shape ``(4,)`` or ``(4, n)``, and this is what we observe.

    ignore)Zoverinvaliddivider      r   T)
complex_okr   Nr   r   g      ?dtype)SnSkaerrhr   r   piepsabnnitnfevstatusxr0fr0wr0xl0fl0wl0d4ainfbinfabinfa0))r#   r#   )integralr   )errorr   )r!   r!   )r"   r"   c                    s    d| j   | _t| j  | j| j kd\}}t||| j| j\| _| _| j	 }|| j
 d|| j
 d   || j
< d|| j  d | j| j  || j< || j  d9  < |S )Nr   )r   	inclusiver   r   )r    r   
_get_pairsr   _transform_to_limitsr   r   xjwjcopyr-   r,   r.   r+   )workxjcr5   r4   )h0r
   r   U/var/www/html/django/DPS/env/lib/python3.9/site-packages/scipy/integrate/_tanhsinh.pypre_func_evalt  s    

"$z _tanhsinh.<locals>.pre_func_evalc              
      s6  |j rr||j  t d|j|j d  dt d|j|j d    7  < ||j  dt |j|j  8  < nX||j  d|j|j d  d|j|j d  d  9  < ||j  |j|j d 9  < t||\}}|jjd r&|jd d df } rtj	|t d |gddn
|d | }||_
||_d S )Nr   r   g       r   r   axis)r   r-   npr4   r,   _euler_maclaurin_sumr   shaper   	logsumexpfjwjr   )xfjr7   rB   r   Snm1)r   r   r:   post_func_eval  s"    "&"
z!_tanhsinh.<locals>.post_func_evalc                    s"  t j| jjtd}| jdkrf| j| jk }r8t j	 nd}|| j|< || j
|< tj| j|< d||< nft| \| _| _
r| jk | jt | j  k B n| jk | jt| j  k B }tj| j|< d||< rt t | jt | jB | @ }nt | j | @ }tj| j|< d||< |S )z>Terminate due to convergence or encountering non-finite valuesr   r   T)r>   zerosr   r@   boolr!   r   r   ravelinfr   eimZ_ECONVERGEDr#   _estimate_errorrerrrealabsisposinfisnanisfiniteZ
_EVALUEERR)r7   stopizero)r   r   r   r   r:   check_termination  s(    



$&z$_tanhsinh.<locals>.check_terminationc                 S   s8   |  j d7  _ tj| j| jd d tjf fdd| _d S )Nr   r   r<   )r    r>   concatenater   r   newaxis)r7   r   r   r:   post_termination_check  s    &z)_tanhsinh.<locals>.post_termination_checkc                    s    rDt rD| d jt j}t d}| d | |  | d< n| d   d9  < | d  d | d< d| d | d dk< | d= |S )Nr/                 ?r   r!   r   r	   r   )r>   anyr   typer   Z	complex64)resr@   r   j)r   r
   negativer   r:   customize_result  s    
z#_tanhsinh.<locals>.customize_result)_tanhsinh_ivr>   ZerrstaterI   reshaper@   isinfrK   _initializebroadcast_toastype_transform_integralsrJ   r\   r   finfor   r   fullnanrQ   
empty_likeZ_EINPROGRESSintrN   _get_base_steprG   r   _loop)1fr   r   r   r   r   r	   r
   r   r   r   r   cZinf_aZinf_btempxsfsr@   r   r.   r-   r+   r,   r!   r"   rU   r   maxiterr   r   r   r   r#   r$   r%   r&   r'   r(   r)   r*   r7   Zres_work_pairsr;   rF   rV   rY   r`   r]   r   )r   r9   r   r
   r_   r   r:   	_tanhsinh   sx      
$
("
$ru   c                 C   s@   dt | j }t t d| d t j }|t }|| S )N   r   r   )r>   rh   ZtinyZarcsinhr   r   _N_BASE_STEPSrf   )r   ZfminZtmaxr9   r   r   r:   rm     s    	rm      c                 C   s   |d|   }t d|   }| dkr.t|d ntd|d d}|| }tjd }|t| }|t| }|t|d  }	dt|t|  }
| dkr|	d d n|	d |	d< |
|	fS Nr   r   r   )rw   r>   aranger   coshsinhexp)kr9   r   maxr^   ZjhZpi_2u1u2r5   r8   r   r   r:   _compute_pair  s    (
 r   c                 C   s   |t jkr*tdt _tdt _dgt _t jg}t jg}ttt jd | d D ]@}t	||\}}|
| |
| t j
t jd t|  qRt|t _t|t _|t _d S )Nr   r   r   )_pair_cacher9   r>   emptyr8   r5   indicesrangelenr   appendrW   )r~   r9   ZxjcsZwjsrT   r8   r5   r   r   r:   r     s    


r   c           	      C   sx   t tj| d ks|tjkr&t| | tj}tj}tj}|r@dn||  }|| d  }||| |||| |fS ry   )r   r   r   r9   r8   r5   rf   )	r~   r9   r1   r   r8   r5   r   startendr   r   r:   r2   .  s    
r2   c                 C   sj   || d }t j| |  | ||  | fdd}|| }t j||fdd}||k||kB }d||< ||fS )Nr   r   r<   r   )r>   rW   )r8   r5   r   r   alphar4   r   r   r   r:   r3   >  s    $r3   c           $      C   s  |j |j|j  }}}|j|j|j  }}}|jj| j|jj  }} }	|j	\}
}|
d|
d | \}}| 
d|
d |\}}|	
d|
d |\}}t| |dkB }t| |dkB }tj ||< tj|ddd}tj||ddd }tj||ddd }tj||ddd }||k}|| ||< || ||< || ||< tj||< tj|ddd}tj||ddd }tj||ddd }tj||ddd }||k }|| ||< || ||< || ||< | j} |jr|t| n|| }|jr|t| n|| }|jrtjntj}t|||||_t|tjd d f |j	} t|tjd d f |j	}!| | ||< |!| ||< |jr| t|j n| |j }"|jrtj|"t|j ddntj|"dd|j }#|||  |_ |_|_|||  |_|_|_|"|#fS )Nr   r   Tr=   Zkeepdimsr<   r   )r$   r%   r&   r'   r(   r)   r4   Tr5   r@   rb   r6   r>   rR   rJ   ZargmaxZtake_along_axisZargminr   rN   rO   maximumr*   re   rX   r   rA   r   sum)$rD   r7   r$   r%   r&   r'   r(   r)   r4   r5   n_xn_activeZxrZxlfrflwrZwlZ	invalid_rZ	invalid_lZirZxr_maxZfr_maxZwr_maxr^   ilZxl_minZfl_minZwl_minZflwl0Zfrwr0Z	magnitudeZfr0bZfl0brB   r   r   r   r:   r?   R  sV    

""r?   c                 C   s(  | j dks| jdkr,t| jtj}||fS tj}t| j}t	ddd}| j
jd dkrd| j }|| j  }| j|dd}|d d d d d |f |d| }| jrtj|fi |t| ntj|fi || }	tj|	| j
fdd| _
| j dkrt| jtj}||fS | j
jd dk rd| j }|| j d  }| jt| jdd}|d	d |f |d| }| jrtj|fi |t| ntj|fi || }
tj|
| j
fdd| _
| j
d
 }
| j
d }	| j}| jrt|}ttj| j|	| jd  gdd}ttj| j|
| jd  gdd}|tjt| jdd }| j}tj|d | d| ||gdd}t||t| j }nt| j|	 }t| j|
 }|tjt| jdd }| j}tj|t|t|  |d ||gdd}t||t| j }||| jjfS )Nr   r   Tr   r   r<   r   rv   .).).r   rZ   )r    r!   r>   Z	full_liker   rj   r   r   r   dictr   r@   r   rB   rb   r   r   rA   r   rW   r   rN   r   r   r*   r   rO   )r7   rj   r   r   Zaxis_kwargsr   r   Zfjwj_rlrB   rE   ZSnm2e1Zlog_e1Zd1Zd2Zd3r*   r   rM   r   r   r:   rL     sZ    


&"
$


$$".rL   c                 C   s   || k }|| | |  | |< ||< t | t |@ }d\| |< ||< t | }||  | |   | |< ||< t |}|  }d\| |< ||< | ||||||fS )N)r   r   r   )r>   rc   r6   )r   r   r_   r-   r+   r,   r.   r   r   r:   rg     s    

rg   c                 C   sb  d}t | st|d}t||\}}tt|sHtt|rPt|d}|dvrdt|t|}|d u r|rtj nd}|d ur|nd}t||dg}d}t	|j
tjst||rd}tt|rt|n.d	}t|dk stt|rt||d }|d u r&|n|d
 }td}|d u rN|d u rNd}|d u r\|n|}|d u rn|n|}d}t|||g}t	|j
tjrtt|rt|tj|kst|d}t|dk rt||tj\}}}t||}t|	s|	f}	d}|
dvr*t||d urFt |sFtd| |||||||||	|
|fS )N`f` must be callable.z1All elements of `a` and `b` must be real numbers.`log` must be True or False.   FTr           '`atol` and `rtol` must be real numbers.z/`atol` and `rtol` may not be positive infinity.z2`atol` and `rtol` must be non-negative and finite.r   l            
   z6`maxfun`, `maxlevel`, and `minlevel` must be integers.z:`maxfun`, `maxlevel`, and `minlevel` must be non-negative.z'`preserve_shape` must be True or False.z`callback` must be callable.)callable
ValueErrorr>   broadcast_arraysr[   Z	iscomplexrH   rJ   asarray
issubdtyper   floatingrP   rc   floatnumberallZisrealrf   Zint64miniterable)ro   r   r   r   r   r	   r
   r   r   r   r   r   message	rtol_tempparamsZBIGINTr   r   r:   ra     sn     
"


ra   c                 C   sR   t | } t| j}|| dkr@|| t j|t j | jdS tj	| |dS d S )Nr   )Z
fill_valuer   r<   )
r>   r   listr@   popri   rJ   r   r   rA   )rC   r=   r@   r   r   r:   
_logsumexpU  s    


r   c	              
   C   s  d}	t | st|	d}	t|||\}}}t|j|j|j}
t|
tjr\t|
tjrdt|	t	|}||k}t	||dk@ }||@ |@ }d}	|dvrt|	|d u r|rtj
 nd}|d ur|nd}t||dg}d}	t|jtjst|	|r,d}	tt|t|B rTt|	n(d	}	t|dk t	| B rTt|	|d }|d u rj|n|d
 }t|}||ks|dk rd}	t|	t|s|f}| |||||||||f
S )Nr   z:All elements of `a`, `b`, and `step` must be real numbers.r   r   r   r   r   z3`atol`, `rtol` may not be positive infinity or NaN.z3`atol`, and `rtol` must be non-negative and finite.r   z*`maxterms` must be a non-negative integer.)r   r   r>   r   Zresult_typer   r   r   ZcomplexfloatingrR   rJ   r   r   r[   rP   rQ   rl   r   )ro   r   r   stepr   r   maxtermsr   r   r   r   Zvalid_aZvalid_bZ
valid_stepvalid_abstepr   r   Zmaxterms_intr   r   r:   _nsum_iv`  sL    

r   r   i   c	              
      s  t | ||||||||	}	|	\
} }}}}
}}}}}tj| |f|dd}	|	\} }}}}}|d }t|| |}t|| |}t|
| }
t|| | }|||  }t|j	}tj
|rtj nd|dd }|du r|rdt| n|d }|||||||f}t|}t|}tjt|td}tjt|td}|d |k|
@  |d |k|
@ |
 }t r fd	d
|D }t| |  |  |  ||}	|	dd \| < | < |   |	d 7  < dt|    | < trbfdd
|D }t| | | | ||}	|	dd \|< |< |< |  |	d 7  < t|rtjtj ||< ||< d||< ||d ||d  }}||d ||d  }}t||||dk|dS )a  Evaluate a convergent sum.

    For finite `b`, this evaluates::

        f(a + np.arange(n)*step).sum()

    where ``n = int((b - a) / step) + 1``. If `f` is smooth, positive, and
    monotone decreasing, `b` may be infinite, in which case the infinite sum
    is approximated using integration.

    Parameters
    ----------
    f : callable
        The function that evaluates terms to be summed. The signature must be::

            f(x: ndarray, *args) -> ndarray

         where each element of ``x`` is a finite real and ``args`` is a tuple,
         which may contain an arbitrary number of arrays that are broadcastable
         with `x`. `f` must represent a smooth, positive, and monotone decreasing
         function of `x`; `_nsum` performs no checks to verify that these conditions
         are met and may return erroneous results if they are violated.
    a, b : array_like
        Real lower and upper limits of summed terms. Must be broadcastable.
        Each element of `a` must be finite and less than the corresponding
        element in `b`, but elements of `b` may be infinite.
    step : array_like
        Finite, positive, real step between summed terms. Must be broadcastable
        with `a` and `b`.
    args : tuple, optional
        Additional positional arguments to be passed to `f`. Must be arrays
        broadcastable with `a`, `b`, and `step`. If the callable to be summed
        requires arguments that are not broadcastable with `a`, `b`, and `step`,
        wrap that callable with `f`. See Examples.
    log : bool, default: False
        Setting to True indicates that `f` returns the log of the terms
        and that `atol` and `rtol` are expressed as the logs of the absolute
        and relative errors. In this case, the result object will contain the
        log of the sum and error. This is useful for summands for which
        numerical underflow or overflow would lead to inaccuracies.
    maxterms : int, default: 2**32
        The maximum number of terms to evaluate when summing directly. 
        Additional function evaluations may be performed for input
        validation and integral evaluation. 
    atol, rtol : float, optional
        Absolute termination tolerance (default: 0) and relative termination
        tolerance (default: ``eps**0.5``, where ``eps`` is the precision of
        the result dtype), respectively. Must be non-negative
        and finite if `log` is False, and must be expressed as the log of a
        non-negative and finite number if `log` is True.

    Returns
    -------
    res : _RichResult
        An instance of `scipy._lib._util._RichResult` with the following
        attributes. (The descriptions are written as though the values will be
        scalars; however, if `func` returns an array, the outputs will be

        arrays of the same shape.)
        success : bool
            ``True`` when the algorithm terminated successfully (status ``0``).
        status : int
            An integer representing the exit status of the algorithm.
            ``0`` : The algorithm converged to the specified tolerances.
            ``-1`` : Element(s) of `a`, `b`, or `step` are invalid
            ``-2`` : Numerical integration reached its iteration limit; the sum may be divergent.
            ``-3`` : A non-finite value was encountered.
        sum : float
            An estimate of the sum.
        error : float
            An estimate of the absolute error, assuming all terms are non-negative.
        nfev : int
            The number of points at which `func` was evaluated.

    See Also
    --------
    tanhsinh

    Notes
    -----
    The method implemented for infinite summation is related to the integral
    test for convergence of an infinite series: assuming `step` size 1 for
    simplicity of exposition, the sum of a monotone decreasing function is bounded by

    .. math::

        \int_u^\infty f(x) dx \leq \sum_{k=u}^\infty f(k) \leq \int_u^\infty f(x) dx + f(u)

    Let :math:`a` represent  `a`, :math:`n` represent `maxterms`, :math:`\epsilon_a`
    represent `atol`, and :math:`\epsilon_r` represent `rtol`.
    The implementation first evaluates the integral :math:`S_l=\int_a^\infty f(x) dx`
    as a lower bound of the infinite sum. Then, it seeks a value :math:`c > a` such
    that :math:`f(c) < \epsilon_a + S_l \epsilon_r`, if it exists; otherwise,
    let :math:`c = a + n`. Then the infinite sum is approximated as
    
    .. math::

        \sum_{k=a}^{c-1} f(k) + \int_c^\infty f(x) dx + f(c)/2

    and the reported error is :math:`f(c)/2` plus the error estimate of
    numerical integration. The approach described above is generalized for non-unit
    `step` and finite `b` that is too large for direct evaluation of the sum,
    i.e. ``b - a + 1 > maxterms``.

    References
    ----------
    [1] Wikipedia. "Integral test for convergence."
    https://en.wikipedia.org/wiki/Integral_test_for_convergence

    Examples
    --------
    Compute the infinite sum of the reciprocals of squared integers.
    
    >>> import numpy as np
    >>> from scipy.integrate._tanhsinh import _nsum
    >>> res = _nsum(lambda k: 1/k**2, 1, np.inf, maxterms=1e3)
    >>> ref = np.pi**2/6  # true value
    >>> res.error  # estimated error
    4.990014980029223e-07
    >>> (res.sum - ref)/ref  # true error
    -1.0101760641302586e-10
    >>> res.nfev  # number of points at which callable was evaluated
    1142
    
    Compute the infinite sums of the reciprocals of integers raised to powers ``p``.
    
    >>> from scipy import special
    >>> p = np.arange(2, 10)
    >>> res = _nsum(lambda k, p: 1/k**p, 1, np.inf, maxterms=1e3, args=(p,))
    >>> ref = special.zeta(p, 1)
    >>> np.allclose(res.sum, ref)
    True
    
    F)r   r   r   r   Ng      ?r   c                    s   g | ]}|  qS r   r   .0arg)i1r   r:   
<listcomp>L      z_nsum.<locals>.<listcomp>r   c                    s   g | ]}|  qS r   r   r   )i2r   r:   r   S  r   )r   r0   r#   successr"   )r   rK   rd   r>   re   rI   rf   floorrh   r   r   rJ   r   rk   rG   r   rl   Zonesr[   _directrR   _integral_boundrj   rb   r   )ro   r   r   r   r   r   r   r   r   tmpr   rr   rs   r@   r   Zntermsr   rU   	constantsSEr#   r"   Zi3Zargs_directZargs_indirectr   )r   r   r:   _nsum  sT     


r   Tc                 C   s0  |\}}}	}
}}}t |}t|| | | }t t|}|d d tjf |d d tjf |d d tjf   }}}dd |D }|tj||d|  }|||| d  k}tj||< | |g|R  }|
||< ||jdd }|rt|ddntj|dd}|rt	|t
|	 n
|	t| }|||fS )Nc                 S   s   g | ]}|d d t jf qS )Nr>   rX   r   r   r   r:   r     r   z_direct.<locals>.<listcomp>r   r   r   r<   )rl   r>   roundr   rX   rz   rj   r   r   rN   r   rO   )ro   r   r   r   r   r   r1   r   r   r   rU   _Zinclusive_adjustmentZstepsZ	max_stepsa2b2step2args2ksZi_nanrs   r"   r   r   r   r   r:   r   c  s    :
&r   c           &   	   C   s  |\}}}}}	}
}t jd|d}t| ||||
|	|d}t |
|jj}|r\t||	|j fn||	|j  }|jdk }t j||< |j}|dt j	f }|dt j	f }dd |D }|rt 
t |nd}t jdt d| |gg|d}t|}|||  }| |g|R  }t t j||d d t j	f kdd	|jd d
 }|| }|||  }t| |||||dd\}}}|t |O }d|t |< t j||< t| ||||
|	|d}|t t||f }| |g|R  } |d
7 }|r@t |}!||j|! || | | f}"t|"dd	}#||j|! || | | t jd  f}$t|$dd	j}%n<||j|  |d  | d  }#||j|  |d  | d  }%|j|  || < |#|%|||j | |j fS )Nr   r   )r   r   r   r   r   .c                 S   s   g | ]}|d t jf qS ).r   r   r   r   r:   r     r   z#_integral_bound.<locals>.<listcomp>r   r<   r   F)r1   r   rZ   )r>   r   ru   re   r/   r@   r   r#   rj   rX   r   log2rW   rz   r   minimumr   r   rP   r0   r   rN   r"   )&ro   r   r   r   r   r   r   r   r   r   r   r   r   ZlbZtolZi_skipr#   r   r   r   Zlog2maxtermsZn_stepsr"   r   Zfksntr~   leftZ
left_errorZ	left_nfevrightZfkZfbZlog_stepZS_termsr   ZE_termsr   r   r   r:   r     sN    $

 2
	
$r   )r   )T)!numpyr>   Zscipyr   Z(scipy._lib._elementwise_iterative_methodZ_libZ_elementwise_iterative_methodrK   Zscipy._lib._utilr   ru   Zfloat64rm   rw   r   r   r   r8   r5   r   r9   r2   r3   r?   rL   rg   ra   r   r   rl   r   r   r   r   r   r   r:   <module>   s@      @$UOH
6
 N
0