a
    BCCf%                     @  s.  d dl mZ d dlmZmZmZmZ d dlZd dl	mZ
 d dlZd dlZd dlmZ d dlmZ d dlmZmZ d dlmZ d dlmZmZmZ g d	ZdddZdddZG dd deZerd dlmZ G dd deZneZdddddZ e dd Z!e" e!_#dddZ$dd d!Z%ed"dd'd(Z&d)d* Z'dd+d,Z(dd-d.Z)d/d0 Z*dd
defd1d2Z+ed3d4dd
ded5d6d7Z,d8d8d9d8d:d;d<Z-d8d8d8d=d>d?Z.d8d8d8d=d@dAZ/dBd8dCdDdEZ0dd
dddFdGdHZ1ddIdJZ2dKdL Z3dMdN Z4dOdP Z5edQddTdUZ6d&dVd&d&gddWfd&dXg dYddZfdXd[g d\d]d^fdVd_g d`dadbfddcg dddedffd&dgg dhdidjfdkdlg dmdndofdpdqg drdsdtfdudvg dwdxdyfddzg d{d|d}fd~dg dddfd&dg dddfddg dddfdkdg dddfdZ7dddZ8dd Z9edddgZ:d[ddddddZ;dS )    )annotations)TYPE_CHECKINGCallableAnycastN)
namedtuple)roots_legendre)gammaln	logsumexp)
_rng_spawn)_NoValue_deprecate_positional_args_deprecated)
fixed_quad
quadraturerombergromb	trapezoidtrapzsimpssimpsoncumulative_trapezoidcumtrapznewton_cotesqmc_quadAccuracyWarningcumulative_simpson      ?c           
   
   C  s*  t | } |du r|}nRt |}|jdkr\t |}dg| j }|jd ||< ||}nt j||d}| j}tdg| }tdg| }tdd||< tdd||< z*|| t| | t|   d |}	W nR t	y$   t 
|}t 
| } t j|| t| | t|   d |}	Y n0 |	S )a  
    Integrate along the given axis using the composite trapezoidal rule.

    If `x` is provided, the integration happens in sequence along its
    elements - they are not sorted.

    Integrate `y` (`x`) along each 1d slice on the given axis, compute
    :math:`\int y(x) dx`.
    When `x` is specified, this integrates along the parametric curve,
    computing :math:`\int_t y(t) dt =
    \int_t y(t) \left.\frac{dx}{dt}\right|_{x=x(t)} dt`.

    Parameters
    ----------
    y : array_like
        Input array to integrate.
    x : array_like, optional
        The sample points corresponding to the `y` values. If `x` is None,
        the sample points are assumed to be evenly spaced `dx` apart. The
        default is None.
    dx : scalar, optional
        The spacing between sample points when `x` is None. The default is 1.
    axis : int, optional
        The axis along which to integrate.

    Returns
    -------
    trapezoid : float or ndarray
        Definite integral of `y` = n-dimensional array as approximated along
        a single axis by the trapezoidal rule. If `y` is a 1-dimensional array,
        then the result is a float. If `n` is greater than 1, then the result
        is an `n`-1 dimensional array.

    See Also
    --------
    cumulative_trapezoid, simpson, romb

    Notes
    -----
    Image [2]_ illustrates trapezoidal rule -- y-axis locations of points
    will be taken from `y` array, by default x-axis distances between
    points will be 1.0, alternatively they can be provided with `x` array
    or with `dx` scalar.  Return value will be equal to combined area under
    the red lines.

    References
    ----------
    .. [1] Wikipedia page: https://en.wikipedia.org/wiki/Trapezoidal_rule

    .. [2] Illustration image:
           https://en.wikipedia.org/wiki/File:Composite_trapezoidal_rule_illustration.png

    Examples
    --------
    Use the trapezoidal rule on evenly spaced points:

    >>> import numpy as np
    >>> from scipy import integrate
    >>> integrate.trapezoid([1, 2, 3])
    4.0

    The spacing between sample points can be selected by either the
    ``x`` or ``dx`` arguments:

    >>> integrate.trapezoid([1, 2, 3], x=[4, 6, 8])
    8.0
    >>> integrate.trapezoid([1, 2, 3], dx=2)
    8.0

    Using a decreasing ``x`` corresponds to integrating in reverse:

    >>> integrate.trapezoid([1, 2, 3], x=[8, 6, 4])
    -8.0

    More generally ``x`` is used to integrate along a parametric curve. We can
    estimate the integral :math:`\int_0^1 x^2 = 1/3` using:

    >>> x = np.linspace(0, 1, num=50)
    >>> y = x**2
    >>> integrate.trapezoid(y, x)
    0.33340274885464394

    Or estimate the area of a circle, noting we repeat the sample which closes
    the curve:

    >>> theta = np.linspace(0, 2 * np.pi, num=1000, endpoint=True)
    >>> integrate.trapezoid(np.cos(theta), x=np.sin(theta))
    3.141571941375841

    ``trapezoid`` can be applied along a specified axis to do multiple
    computations in one call:

    >>> a = np.arange(6).reshape(2, 3)
    >>> a
    array([[0, 1, 2],
           [3, 4, 5]])
    >>> integrate.trapezoid(a, axis=0)
    array([1.5, 2.5, 3.5])
    >>> integrate.trapezoid(a, axis=1)
    array([2.,  8.])
    N   r   axisr          @)npZ
asanyarrayndimdiffshapereshapeslicetuplesum
ValueErrorasarrayaddreduce)
yxdxr!   dr&   ndslice1slice2ret r7   W/var/www/html/django/DPS/env/lib/python3.9/site-packages/scipy/integrate/_quadrature.pyr      s,    f



*

0r   c                 C  s$   d}t j|tdd t| |||dS )z}An alias of `trapezoid`.

    `trapz` is kept for backwards compatibility. For new code, prefer
    `trapezoid` instead.
    zr'scipy.integrate.trapz' is deprecated in favour of 'scipy.integrate.trapezoid' and will be removed in SciPy 1.14.0   
stacklevel)r0   r1   r!   )warningswarnDeprecationWarningr   )r/   r0   r1   r!   msgr7   r7   r8   r      s    r   c                   @  s   e Zd ZdS )r   N)__name__
__module____qualname__r7   r7   r7   r8   r      s   r   )Protocolc                   @  s   e Zd ZU ded< dS )CacheAttributeszdict[int, tuple[Any, Any]]cacheN)r@   rA   rB   __annotations__r7   r7   r7   r8   rD      s   
rD   r   )funcreturnc                 C  s
   t t| S N)r   rD   rG   r7   r7   r8   cache_decorator   s    rK   c                 C  s,   | t jv rt j|  S t| t j| < t j|  S )zX
    Cache roots_legendre results to speed up calls of the fixed_quad
    function.
    )_cached_roots_legendrerE   r   )nr7   r7   r8   rL      s    

rL   r7      c                 C  sx   t |\}}t|}t|s*t|r2td|| |d  d | }|| d tj|| |g|R   dd dfS )a  
    Compute a definite integral using fixed-order Gaussian quadrature.

    Integrate `func` from `a` to `b` using Gaussian quadrature of
    order `n`.

    Parameters
    ----------
    func : callable
        A Python function or method to integrate (must accept vector inputs).
        If integrating a vector-valued function, the returned array must have
        shape ``(..., len(x))``.
    a : float
        Lower limit of integration.
    b : float
        Upper limit of integration.
    args : tuple, optional
        Extra arguments to pass to function, if any.
    n : int, optional
        Order of quadrature integration. Default is 5.

    Returns
    -------
    val : float
        Gaussian quadrature approximation to the integral
    none : None
        Statically returned value of None

    See Also
    --------
    quad : adaptive quadrature using QUADPACK
    dblquad : double integrals
    tplquad : triple integrals
    romberg : adaptive Romberg quadrature
    quadrature : adaptive Gaussian quadrature
    romb : integrators for sampled data
    simpson : integrators for sampled data
    cumulative_trapezoid : cumulative integration for sampled data
    ode : ODE integrator
    odeint : ODE integrator

    Examples
    --------
    >>> from scipy import integrate
    >>> import numpy as np
    >>> f = lambda x: x**8
    >>> integrate.fixed_quad(f, 0.0, 1.0, n=4)
    (0.1110884353741496, None)
    >>> integrate.fixed_quad(f, 0.0, 1.0, n=5)
    (0.11111111111111102, None)
    >>> print(1/9.0)  # analytical result
    0.1111111111111111

    >>> integrate.fixed_quad(np.cos, 0.0, np.pi/2, n=4)
    (0.9999999771971152, None)
    >>> integrate.fixed_quad(np.cos, 0.0, np.pi/2, n=5)
    (1.000000000039565, None)
    >>> np.sin(np.pi/2)-np.sin(0)  # analytical result
    1.0

    z8Gaussian quadrature is only available for finite limits.r   r"   r   r    N)rL   r#   realisinfr+   r*   )rG   abargsrM   r0   wr/   r7   r7   r8   r      s    >
r   Fc                   s&   |r fdd}n fdd}|S )ao  Vectorize the call to a function.

    This is an internal utility function used by `romberg` and
    `quadrature` to create a vectorized version of a function.

    If `vec_func` is True, the function `func` is assumed to take vector
    arguments.

    Parameters
    ----------
    func : callable
        User defined function.
    args : tuple, optional
        Extra arguments for the function.
    vec_func : bool, optional
        True if the function func takes vector arguments.

    Returns
    -------
    vfunc : callable
        A function that will take a vector argument and return the
        result.

    c                   s   | g R  S rI   r7   r0   rS   rG   r7   r8   vfunc*  s    zvectorize1.<locals>.vfuncc                   s   t | r| g R  S t | } | d g R  }t| }t|dt|}t j|f|d}||d< td|D ]}| | g R  ||< qr|S )Nr   dtyperX   r   )r#   isscalarr,   lengetattrtypeemptyrange)r0   Zy0rM   rX   outputirV   r7   r8   rW   -  s    

r7   )rG   rS   vec_funcrW   r7   rV   r8   
vectorize1  s    rc   z`scipy.integrate.quadrature` is deprecated as of SciPy 1.12.0and will be removed in SciPy 1.15.0. Please use`scipy.integrate.quad` instead."\O>2   Tr   c	                 C  s   t |ts|f}t| ||d}	tj}
tj}t|d |}t||d D ]D}t|	||d|d }t||
 }|}
||k s||t|
 k rF qqFt	j
d||f tdd |
|fS )aZ  
    Compute a definite integral using fixed-tolerance Gaussian quadrature.

    .. deprecated:: 1.12.0

          This function is deprecated as of SciPy 1.12.0 and will be removed
          in SciPy 1.15.0. Please use `scipy.integrate.quad` instead.

    Integrate `func` from `a` to `b` using Gaussian quadrature
    with absolute tolerance `tol`.

    Parameters
    ----------
    func : function
        A Python function or method to integrate.
    a : float
        Lower limit of integration.
    b : float
        Upper limit of integration.
    args : tuple, optional
        Extra arguments to pass to function.
    tol, rtol : float, optional
        Iteration stops when error between last two iterates is less than
        `tol` OR the relative change is less than `rtol`.
    maxiter : int, optional
        Maximum order of Gaussian quadrature.
    vec_func : bool, optional
        True or False if func handles arrays as arguments (is
        a "vector" function). Default is True.
    miniter : int, optional
        Minimum order of Gaussian quadrature.

    Returns
    -------
    val : float
        Gaussian quadrature approximation (within tolerance) to integral.
    err : float
        Difference between last two estimates of the integral.

    See Also
    --------
    romberg : adaptive Romberg quadrature
    fixed_quad : fixed-order Gaussian quadrature
    quad : adaptive quadrature using QUADPACK
    dblquad : double integrals
    tplquad : triple integrals
    romb : integrator for sampled data
    simpson : integrator for sampled data
    cumulative_trapezoid : cumulative integration for sampled data
    ode : ODE integrator
    odeint : ODE integrator

    Examples
    --------
    >>> from scipy import integrate
    >>> import numpy as np
    >>> f = lambda x: x**8
    >>> integrate.quadrature(f, 0.0, 1.0)
    (0.11111111111111106, 4.163336342344337e-17)
    >>> print(1/9.0)  # analytical result
    0.1111111111111111

    >>> integrate.quadrature(np.cos, 0.0, np.pi/2)
    (0.9999999999999536, 3.9611425250996035e-11)
    >>> np.sin(np.pi/2)-np.sin(0)  # analytical result
    1.0

    rb   r   r7   r   z-maxiter (%d) exceeded. Latest difference = %er9   r:   )
isinstancer)   rc   r#   infmaxr_   r   absr<   r=   r   )rG   rQ   rR   rS   tolrtolmaxiterrb   ZminiterrW   valerrrM   Znewvalr7   r7   r8   r   =  s"    I

r   c                 C  s   t | }|||< t|S rI   )listr)   )tra   valuelr7   r7   r8   tupleset  s    rt   c                 C  s&   d}t j|tdd t| ||||dS )zAn alias of `cumulative_trapezoid`.

    `cumtrapz` is kept for backwards compatibility. For new code, prefer
    `cumulative_trapezoid` instead.
    z'scipy.integrate.cumtrapz' is deprecated in favour of 'scipy.integrate.cumulative_trapezoid' and will be removed in SciPy 1.14.0r9   r:   r0   r1   r!   initial)r<   r=   r>   r   )r/   r0   r1   r!   rv   r?   r7   r7   r8   r     s    r   c                 C  s  t | } | j| dkr td|du r.|}nt |}|jdkrlt |}dg| j }d||< ||}n,t|jt| jkrtdnt j||d}|j| | j| d krtdt| j}tt	df| |t	dd}tt	df| |t	dd}	t j
|| | | |	   d	 |d}
|dur|dkr@tjd
tdd t |sTtdt|
j}d||< t jt j|||
jd|
g|d}
|
S )a  
    Cumulatively integrate y(x) using the composite trapezoidal rule.

    Parameters
    ----------
    y : array_like
        Values to integrate.
    x : array_like, optional
        The coordinate to integrate along. If None (default), use spacing `dx`
        between consecutive elements in `y`.
    dx : float, optional
        Spacing between elements of `y`. Only used if `x` is None.
    axis : int, optional
        Specifies the axis to cumulate. Default is -1 (last axis).
    initial : scalar, optional
        If given, insert this value at the beginning of the returned result.
        0 or None are the only values accepted. Default is None, which means
        `res` has one element less than `y` along the axis of integration.

        .. deprecated:: 1.12.0
            The option for non-zero inputs for `initial` will be deprecated in
            SciPy 1.15.0. After this time, a ValueError will be raised if
            `initial` is not None or 0.

    Returns
    -------
    res : ndarray
        The result of cumulative integration of `y` along `axis`.
        If `initial` is None, the shape is such that the axis of integration
        has one less value than `y`. If `initial` is given, the shape is equal
        to that of `y`.

    See Also
    --------
    numpy.cumsum, numpy.cumprod
    cumulative_simpson : cumulative integration using Simpson's 1/3 rule
    quad : adaptive quadrature using QUADPACK
    romberg : adaptive Romberg quadrature
    quadrature : adaptive Gaussian quadrature
    fixed_quad : fixed-order Gaussian quadrature
    dblquad : double integrals
    tplquad : triple integrals
    romb : integrators for sampled data
    ode : ODE integrators
    odeint : ODE integrators

    Examples
    --------
    >>> from scipy import integrate
    >>> import numpy as np
    >>> import matplotlib.pyplot as plt

    >>> x = np.linspace(-2, 2, num=20)
    >>> y = x
    >>> y_int = integrate.cumulative_trapezoid(y, x, initial=0)
    >>> plt.plot(x, y_int, 'ro', x, y[0] + 0.5 * x**2, 'b-')
    >>> plt.show()

    r   z,At least one point is required along `axis`.Nr   r   2If given, shape of x must be 1-D or the same as y.r    7If given, length of x along axis must be the same as y.r"   zThe option for values for `initial` other than None or 0 is deprecated as of SciPy 1.12.0 and will raise a value error in SciPy 1.15.0.r9   r:   z'`initial` parameter should be a scalar.rY   )r#   r,   r&   r+   r$   r%   r'   r[   rt   r(   cumsumr<   r=   r>   rZ   rp   concatenatefullrX   )r/   r0   r1   r!   rv   r2   r&   r3   r4   r5   resr7   r7   r8   r     sD    <





"


r   c              
   C  s  t | j}|d u rd}d}td f| }t||t|||}	t||t|d |d |}
t||t|d |d |}|d u rtj| |	 d| |
   | |  |d}||d 9 }ntj||d}t||t|||}t||t|d |d |}|| jtdd}|| jtdd}|| }|| }tj	||t
||dkd	}|d
 | |	 dtj	d|t
||dkd	  | |
 |tj	||t
||dkd	   | | d|    }tj||d}|S )Nr   r9   r         @r          @Fcopyoutwhereg      @r"   r   )r[   r&   r(   rt   r#   r*   r%   astypefloattrue_divide
zeros_like)r/   startstopr0   r1   r!   r3   step	slice_allslice0r4   r5   resulthZsl0Zsl1Zh0Zh1ZhsumZhprodZh0divh1tmpr7   r7   r8   _basic_simpson  sH    
&
	r   c                 C  s&   d}t j|tdd t| ||||dS )zyAn alias of `simpson`.

    `simps` is kept for backwards compatibility. For new code, prefer
    `simpson` instead.
    zp'scipy.integrate.simps' is deprecated in favour of 'scipy.integrate.simpson' and will be removed in SciPy 1.14.0r9   r:   r0   r1   r!   even)r<   r=   r>   r   )r/   r0   r1   r!   r   r?   r7   r7   r8   r   B  s    r   z1.14)versionr   c                C  sp  t | } t| j}| j| }|}|}d}	|durt |}t|jdkr|dg| }
|jd |
|< |j}d}	|t|
}nt|jt| jkrtd|j| |krtd|turtj	dt
dd |d dkrFd	}d	}tdf| }|tdfvr|nd
}|dvrtd|dkrlt||d}t||d}|durL|| ||  }|d| | | | |   7 }d}|d
kr:t| d|d |||}t||d}t||d}t||d}t j||gt jd}|dur.t||tddd}t||tddd}t t j||d}t j|| |dt j|| |dg}d|d d  d|d  |d   }d|d |d   }t j||t ||dkd}|d d d|d  |d   }d|d  }t j||t ||dkd}d|d d  }d|d  |d |d   }t j||t ||dkd}||| |  || |   || |   7 }|dv rt||d}t||d}|durv|| ||  }|d| | | | |   7 }t| d|d |||}|dv r"t||d}t||d}|dur|t| |t|  }|d| | | | |   7 }|t| d|d |||7 }|dkr<|d }|d }|| }nt| d|d |||}|	rl||}|S )ab  
    Integrate y(x) using samples along the given axis and the composite
    Simpson's rule. If x is None, spacing of dx is assumed.

    If there are an even number of samples, N, then there are an odd
    number of intervals (N-1), but Simpson's rule requires an even number
    of intervals. The parameter 'even' controls how this is handled.

    Parameters
    ----------
    y : array_like
        Array to be integrated.
    x : array_like, optional
        If given, the points at which `y` is sampled.
    dx : float, optional
        Spacing of integration points along axis of `x`. Only used when
        `x` is None. Default is 1.
    axis : int, optional
        Axis along which to integrate. Default is the last axis.
    even : {None, 'simpson', 'avg', 'first', 'last'}, optional
        'avg' : Average two results:
            1) use the first N-2 intervals with
               a trapezoidal rule on the last interval and
            2) use the last
               N-2 intervals with a trapezoidal rule on the first interval.

        'first' : Use Simpson's rule for the first N-2 intervals with
                a trapezoidal rule on the last interval.

        'last' : Use Simpson's rule for the last N-2 intervals with a
               trapezoidal rule on the first interval.

        None : equivalent to 'simpson' (default)

        'simpson' : Use Simpson's rule for the first N-2 intervals with the
                  addition of a 3-point parabolic segment for the last
                  interval using equations outlined by Cartwright [1]_.
                  If the axis to be integrated over only has two points then
                  the integration falls back to a trapezoidal integration.

                  .. versionadded:: 1.11.0

        .. versionchanged:: 1.11.0
            The newly added 'simpson' option is now the default as it is more
            accurate in most situations.

        .. deprecated:: 1.11.0
            Parameter `even` is deprecated and will be removed in SciPy
            1.14.0. After this time the behaviour for an even number of
            points will follow that of `even='simpson'`.

    Returns
    -------
    float
        The estimated integral computed with the composite Simpson's rule.

    See Also
    --------
    quad : adaptive quadrature using QUADPACK
    romberg : adaptive Romberg quadrature
    quadrature : adaptive Gaussian quadrature
    fixed_quad : fixed-order Gaussian quadrature
    dblquad : double integrals
    tplquad : triple integrals
    romb : integrators for sampled data
    cumulative_trapezoid : cumulative integration for sampled data
    cumulative_simpson : cumulative integration using Simpson's 1/3 rule
    ode : ODE integrators
    odeint : ODE integrators

    Notes
    -----
    For an odd number of samples that are equally spaced the result is
    exact if the function is a polynomial of order 3 or less. If
    the samples are not equally spaced, then the result is exact only
    if the function is a polynomial of order 2 or less.

    References
    ----------
    .. [1] Cartwright, Kenneth V. Simpson's Rule Cumulative Integration with
           MS Excel and Irregularly-spaced Data. Journal of Mathematical
           Sciences and Mathematics Education. 12 (2): 1-9

    Examples
    --------
    >>> from scipy import integrate
    >>> import numpy as np
    >>> x = np.arange(0, 10)
    >>> y = np.arange(0, 10)

    >>> integrate.simpson(y, x=x)
    40.5

    >>> y = np.power(x, 3)
    >>> integrate.simpson(y, x=x)
    1640.5
    >>> integrate.quad(lambda x: x**3, 0, 9)[0]
    1640.25

    >>> integrate.simpson(y, x=x, even='first')
    1644.5

    r   Nr   rw   rx   zWThe 'even' keyword is deprecated as of SciPy 1.11.0 and will be removed in SciPy 1.14.0r9   r:   g        r   )avglastfirstr   z>Parameter 'even' must be 'simpson', 'avg', 'last', or 'first'.r         ?   rY   r       r   r~   )r   r   )r   r   r   r"   )r#   r,   r[   r&   r'   r)   r+   r   r<   r=   r>   r(   rt   r   Zfloat64r%   Zsqueezer   r   )r/   r0   r1   r!   r   r3   NZlast_dxZfirst_dxZreturnshapeZshapexZ	saveshapern   r   r   r4   r5   Zslice3r   Zhm2Zhm1ZdiffsnumZdenalphabetaetar7   r7   r8   r   O  s    i









$ (






r   z
np.ndarrayz.Callable[[np.ndarray, np.ndarray], np.ndarray])r/   r1   integration_funcrH   c                 C  s   || |}|| ddddf |ddddf ddddf }t |j}|d  d7  < t|}|ddddf |ddddf< |ddddf |ddddf< |d |d< tj|dd}|S )zCalculate cumulative sum of Simpson integrals.
    Takes as input the integration function to be used. 
    The integration_func is assumed to return the cumulative sum using
    composite Simpson's rule. Assumes the axis of summation is -1.
    .Nr   r   r9   ).r   r    )rp   r&   r#   r^   ry   )r/   r1   r   Zsub_integrals_h1Zsub_integrals_h2r&   Zsub_integralsr|   r7   r7   r8   #_cumulatively_sum_simpson_integralsH  s    

4

  r   )r/   r1   rH   c                 C  sd   |dddf }| dddf }| dddf }| dddf }|d d| d	 d|  |d	   S )
zCalculate the Simpson integrals for all h1 intervals assuming equal interval
    widths. The function can also be used to calculate the integral for all
    h2 intervals by reversing the inputs, `y` and `dx`.
    .Nr   r   r   r9   r   rN      r7   )r/   r1   r2   f1f2f3r7   r7   r8   #_cumulative_simpson_equal_intervalsa  s
    r   c                 C  s   |dddf }|dddf }| dddf }| dddf }| dddf }|| }|| }|| }	||	 }
d| }d|
 | }|
 }|d || ||  ||   S )	zCalculate the Simpson integrals for all h1 intervals assuming unequal interval
    widths. The function can also be used to calculate the integral for all
    h2 intervals by reversing the inputs, `y` and `dx`.
    .Nr   r   r   r9   r   r   r7   )r/   r1   Zx21Zx32r   r   r   Zx31Zx21_x31Zx21_x32Zx21x21_x31x32Zcoeff1Zcoeff2Zcoeff3r7   r7   r8   %_cumulative_simpson_unequal_intervalso  s    r   znpt.ArrayLike)arrrH   c                 C  s,   t | } t | jt jr(| jtdd} | S )NFr   )r#   r,   Z
issubdtyperX   integerr   r   )r   r7   r7   r8   _ensure_float_array  s    
r   ru   c             
   C  s0  t | } | }| j}zt| |d} W nB tyf } z*d| d| j d}t||W Y d}~n
d}~0 0 | jd dk rt||||dd}	t|	|d}	n|dur6t |}d}|j|ks|jd	krt||| kst||jd	krt	|| jnt||d}tj
|dd
}t|dkr(tdt| |t}	nrt |}t|||| d	 }
t||d	}d}|jdks|j|kst|t	||
}t||d}t| |t}	|durt |}t||d	}d}|jdks|j|kst|t	||}t||d}|	|7 }	tj||	fdd
}	t|	d|}	|	S )a,  
    Cumulatively integrate y(x) using the composite Simpson's 1/3 rule.
    The integral of the samples at every point is calculated by assuming a 
    quadratic relationship between each point and the two adjacent points.

    Parameters
    ----------
    y : array_like
        Values to integrate. Requires at least one point along `axis`. If two or fewer
        points are provided along `axis`, Simpson's integration is not possible and the
        result is calculated with `cumulative_trapezoid`.
    x : array_like, optional
        The coordinate to integrate along. Must have the same shape as `y` or
        must be 1D with the same length as `y` along `axis`. `x` must also be
        strictly increasing along `axis`.
        If `x` is None (default), integration is performed using spacing `dx`
        between consecutive elements in `y`.
    dx : scalar or array_like, optional
        Spacing between elements of `y`. Only used if `x` is None. Can either 
        be a float, or an array with the same shape as `y`, but of length one along
        `axis`. Default is 1.0.
    axis : int, optional
        Specifies the axis to integrate along. Default is -1 (last axis).
    initial : scalar or array_like, optional
        If given, insert this value at the beginning of the returned result,
        and add it to the rest of the result. Default is None, which means no
        value at ``x[0]`` is returned and `res` has one element less than `y`
        along the axis of integration. Can either be a float, or an array with
        the same shape as `y`, but of length one along `axis`.

    Returns
    -------
    res : ndarray
        The result of cumulative integration of `y` along `axis`.
        If `initial` is None, the shape is such that the axis of integration
        has one less value than `y`. If `initial` is given, the shape is equal
        to that of `y`.

    See Also
    --------
    numpy.cumsum
    cumulative_trapezoid : cumulative integration using the composite 
        trapezoidal rule
    simpson : integrator for sampled data using the Composite Simpson's Rule

    Notes
    -----

    .. versionadded:: 1.12.0

    The composite Simpson's 1/3 method can be used to approximate the definite 
    integral of a sampled input function :math:`y(x)` [1]_. The method assumes 
    a quadratic relationship over the interval containing any three consecutive
    sampled points.

    Consider three consecutive points: 
    :math:`(x_1, y_1), (x_2, y_2), (x_3, y_3)`.

    Assuming a quadratic relationship over the three points, the integral over
    the subinterval between :math:`x_1` and :math:`x_2` is given by formula
    (8) of [2]_:
    
    .. math::
        \int_{x_1}^{x_2} y(x) dx\ &= \frac{x_2-x_1}{6}\left[\
        \left\{3-\frac{x_2-x_1}{x_3-x_1}\right\} y_1 + \
        \left\{3 + \frac{(x_2-x_1)^2}{(x_3-x_2)(x_3-x_1)} + \
        \frac{x_2-x_1}{x_3-x_1}\right\} y_2\\
        - \frac{(x_2-x_1)^2}{(x_3-x_2)(x_3-x_1)} y_3\right]

    The integral between :math:`x_2` and :math:`x_3` is given by swapping
    appearances of :math:`x_1` and :math:`x_3`. The integral is estimated
    separately for each subinterval and then cumulatively summed to obtain
    the final result.
    
    For samples that are equally spaced, the result is exact if the function
    is a polynomial of order three or less [1]_ and the number of subintervals
    is even. Otherwise, the integral is exact for polynomials of order two or
    less. 

    References
    ----------
    .. [1] Wikipedia page: https://en.wikipedia.org/wiki/Simpson's_rule
    .. [2] Cartwright, Kenneth V. Simpson's Rule Cumulative Integration with
            MS Excel and Irregularly-spaced Data. Journal of Mathematical
            Sciences and Mathematics Education. 12 (2): 1-9

    Examples
    --------
    >>> from scipy import integrate
    >>> import numpy as np
    >>> import matplotlib.pyplot as plt
    >>> x = np.linspace(-2, 2, num=20)
    >>> y = x**2
    >>> y_int = integrate.cumulative_simpson(y, x=x, initial=0)
    >>> fig, ax = plt.subplots()
    >>> ax.plot(x, y_int, 'ro', x, x**3/3 - (x[0])**3/3, 'b-')
    >>> ax.grid()
    >>> plt.show()

    The output of `cumulative_simpson` is similar to that of iteratively
    calling `simpson` with successively higher upper limits of integration, but
    not identical.

    >>> def cumulative_simpson_reference(y, x):
    ...     return np.asarray([integrate.simpson(y[:i], x=x[:i])
    ...                        for i in range(2, len(y) + 1)])
    >>>
    >>> rng = np.random.default_rng(354673834679465)
    >>> x, y = rng.random(size=(2, 10))
    >>> x.sort()
    >>>
    >>> res = integrate.cumulative_simpson(y, x=x)
    >>> ref = cumulative_simpson_reference(y, x)
    >>> equal = np.abs(res - ref) < 1e-15
    >>> equal  # not equal when `simpson` has even number of subintervals
    array([False,  True, False,  True, False,  True, False,  True,  True])

    This is expected: because `cumulative_simpson` has access to more
    information than `simpson`, it can typically produce more accurate
    estimates of the underlying integral over subintervals.

    r   z`axis=z$` is not valid for `y` with `y.ndim=z`.Nr   )r1   r!   rv   z_If given, shape of `x` must be the same as `y` or 1-D with the same length as `y` along `axis`.r   r    r   z$Input x must be strictly increasing.zkIf provided, `dx` must either be a scalar or have the same shape as `y` but with only 1 point along `axis`.zpIf provided, `initial` must either be a scalar or have the same shape as `y` but with only 1 point along `axis`.)r   r&   r#   Zswapaxes
IndexErrorr$   r+   r   r[   Zbroadcast_tor%   anyr   r   rt   r   rz   )r/   r0   r1   r!   rv   Z
original_yZoriginal_shapeemessager|   Zfinal_dx_shapeZalt_input_dx_shapeZalt_initial_input_shaper7   r7   r8   r     sd    { 

&
r   c              	   C  s  t | } t| j}| j| }|d }d}d}||k rH|dK }|d7 }q.||krXtdi }	tdf| }
t|
|d}t|
|d}|t j|td }| | | |  d | |	d< |
}| } }}td|d D ]}|dL }t||t|||}|dL }d	|	|d df || | j	|d
   |	|df< td|d D ]J}|	||d f }|||	|d |d f  dd| > d   |	||f< q4|d }q|r|t 
|	d std nz|d }W n ttfy   d}Y n0 z|d }W n ttfy   d}Y n0 d||f }d}t|dt| ddd t|d D ]8}t|d D ]}t||	||f  dd qBt  q2tdt|  |	||f S )a  
    Romberg integration using samples of a function.

    Parameters
    ----------
    y : array_like
        A vector of ``2**k + 1`` equally-spaced samples of a function.
    dx : float, optional
        The sample spacing. Default is 1.
    axis : int, optional
        The axis along which to integrate. Default is -1 (last axis).
    show : bool, optional
        When `y` is a single 1-D array, then if this argument is True
        print the table showing Richardson extrapolation from the
        samples. Default is False.

    Returns
    -------
    romb : ndarray
        The integrated result for `axis`.

    See Also
    --------
    quad : adaptive quadrature using QUADPACK
    romberg : adaptive Romberg quadrature
    quadrature : adaptive Gaussian quadrature
    fixed_quad : fixed-order Gaussian quadrature
    dblquad : double integrals
    tplquad : triple integrals
    simpson : integrators for sampled data
    cumulative_trapezoid : cumulative integration for sampled data
    ode : ODE integrators
    odeint : ODE integrators

    Examples
    --------
    >>> from scipy import integrate
    >>> import numpy as np
    >>> x = np.arange(10, 14.25, 0.25)
    >>> y = np.arange(3, 12)

    >>> integrate.romb(y)
    56.0

    >>> y = np.sin(np.power(x, 2.5))
    >>> integrate.romb(y)
    -0.742561336672229

    >>> integrate.romb(y, show=True)
    Richardson Extrapolation Table for Romberg Integration
    ======================================================
    -0.81576
     4.63862  6.45674
    -1.10581 -3.02062 -3.65245
    -2.57379 -3.06311 -3.06595 -3.05664
    -1.34093 -0.92997 -0.78776 -0.75160 -0.74256
    ======================================================
    -0.742561336672229  # may vary

    r   r   z=Number of samples must be one plus a non-negative power of 2.Nr   rY   r"   )r   r   r   r    r9   zE*** Printing table only supported for integrals of a single data set.rN      z%%%d.%dfz6Richardson Extrapolation Table for Romberg Integration=
)sepend r   )r#   r,   r[   r&   r+   r(   rt   r   r_   r*   rZ   print	TypeErrorr   )r/   r1   r!   showr3   ZNsampsZNintervrM   kRr   r   Zslicem1r   Zslice_Rr   r   r   ra   jprevZpreciswidthZformstrtitler7   r7   r8   r   F  s`    =



08




r   c                 C  s   |dkrt dn||dkr6d| |d | |d   S |d }t|d |d  | }|d d|  }||t|  }tj| |dd}|S dS )aU  
    Perform part of the trapezoidal rule to integrate a function.
    Assume that we had called difftrap with all lower powers-of-2
    starting with 1. Calling difftrap only returns the summation
    of the new ordinates. It does _not_ multiply by the width
    of the trapezoids. This must be performed by the caller.
        'function' is the function to evaluate (must accept vector arguments).
        'interval' is a sequence with lower and upper limits
                   of integration.
        'numtraps' is the number of trapezoids to use (must be a
                   power-of-2).
    r   z#numtraps must be > 0 in difftrap().r   r   r9   r    N)r+   r   r#   aranger*   )functionintervalZnumtrapsZnumtosumr   ZloxZpointssr7   r7   r8   	_difftrap  s    
r   c                 C  s   d| }|| |  |d  S )z
    Compute the differences for the Romberg quadrature corrections.
    See Forman Acton's "Real Computing Made Real," p 143.
    r}   r   r7   )rR   cr   r   r7   r7   r8   _romberg_diff  s    r   c                 C  s   d }}t dt| dd t d| t d t dd  tt|D ]b}t d	d
| |d |d  d|  f dd t|d D ]}t d|| |  dd qt d qDt d t d|| | dd t dd
t|d  d d d S )Nr   zRomberg integration ofr   r   from z%6s %9s %9s)ZStepsZStepSizeZResultsz%6d %9fr9   r   r"   z%9fzThe final result isafterzfunction evaluations.)r   reprr_   r[   )r   r   resmatra   r   r7   r7   r8   _printresmat  s    
,
r   z`scipy.integrate.romberg` is deprecated as of SciPy 1.12.0and will be removed in SciPy 1.15.0. Please use`scipy.integrate.quad` instead.`sbO>
   c	              	   C  sP  t |st |rtdt| ||d}	d}
||g}|| }t|	||
}|| }|gg}t j}|d }td|d D ]}|
d9 }
|t|	||
7 }|| |
 g}t|D ]"}|t|| || |d  q|| }||d  }|r|| t	|| }||k s||t	| k r q:|}qvt
jd||f tdd |rLt|	|| |S )aI  
    Romberg integration of a callable function or method.

    .. deprecated:: 1.12.0

          This function is deprecated as of SciPy 1.12.0 and will be removed
          in SciPy 1.15.0. Please use `scipy.integrate.quad` instead.

    Returns the integral of `function` (a function of one variable)
    over the interval (`a`, `b`).

    If `show` is 1, the triangular array of the intermediate results
    will be printed. If `vec_func` is True (default is False), then
    `function` is assumed to support vector arguments.

    Parameters
    ----------
    function : callable
        Function to be integrated.
    a : float
        Lower limit of integration.
    b : float
        Upper limit of integration.

    Returns
    -------
    results : float
        Result of the integration.

    Other Parameters
    ----------------
    args : tuple, optional
        Extra arguments to pass to function. Each element of `args` will
        be passed as a single argument to `func`. Default is to pass no
        extra arguments.
    tol, rtol : float, optional
        The desired absolute and relative tolerances. Defaults are 1.48e-8.
    show : bool, optional
        Whether to print the results. Default is False.
    divmax : int, optional
        Maximum order of extrapolation. Default is 10.
    vec_func : bool, optional
        Whether `func` handles arrays as arguments (i.e., whether it is a
        "vector" function). Default is False.

    See Also
    --------
    fixed_quad : Fixed-order Gaussian quadrature.
    quad : Adaptive quadrature using QUADPACK.
    dblquad : Double integrals.
    tplquad : Triple integrals.
    romb : Integrators for sampled data.
    simpson : Integrators for sampled data.
    cumulative_trapezoid : Cumulative integration for sampled data.
    ode : ODE integrator.
    odeint : ODE integrator.

    References
    ----------
    .. [1] 'Romberg's method' https://en.wikipedia.org/wiki/Romberg%27s_method

    Examples
    --------
    Integrate a gaussian from 0 to 1 and compare to the error function.

    >>> from scipy import integrate
    >>> from scipy.special import erf
    >>> import numpy as np
    >>> gaussian = lambda x: 1/np.sqrt(np.pi) * np.exp(-x**2)
    >>> result = integrate.romberg(gaussian, 0, 1, show=True)
    Romberg integration of <function vfunc at ...> from [0, 1]

    ::

       Steps  StepSize  Results
           1  1.000000  0.385872
           2  0.500000  0.412631  0.421551
           4  0.250000  0.419184  0.421368  0.421356
           8  0.125000  0.420810  0.421352  0.421350  0.421350
          16  0.062500  0.421215  0.421350  0.421350  0.421350  0.421350
          32  0.031250  0.421317  0.421350  0.421350  0.421350  0.421350  0.421350

    The final result is 0.421350396475 after 33 function evaluations.

    >>> print("%g %g" % (2*result, erf(1)))
    0.842701 0.842701

    z5Romberg integration only available for finite limits.rf   r   r   r9   z,divmax (%d) exceeded. Latest difference = %er:   )r#   rP   r+   rc   r   rh   r_   appendr   rj   r<   r=   r   r   )r   rQ   rR   rS   rk   rl   r   Zdivmaxrb   rW   rM   r   ZintrangeZordsumr   r   ro   last_rowra   rowr   Z
lastresultr7   r7   r8   r     s@    ] 

r   r9      r   )r   r   r   Z   r   )r   r   r   r   r   P   -   )       r   r   r   ii  i   )   K   re   re   r   r   ii@/     ))         i  r   r   r   iix  r   iC  )    +    r   r   r   r   i	i  r   i_7  )	     ` )  iDr   r   r   r   ii?# 	   i ^ )
)  }=  8  K    r   r   r   r   r   ii  ip )>  < sB( :ih r   r   r   r   r   iii0	   i 0)I"!  jmi r   r   r   r   r   r   l&	 l    7 iR0P ) @ 7@!!Nd7ipRr   r   r   r   r   r   i<ic]    l    `5]v)   v[O    =H/54 +w    "- Mp:    {> $MY( r  r  r  r  r  r   r   l`: l    @	Al   @d@* )i`p`*o   Fg! f    \a LR l   @` r  r  r
  r	  r  r  r  lx= l   7-)r   r9   r   r   rN   r   r   r   r   r   r   r   r      c                 C  s  z<t | d }|r"t|d } ntt| dkr:d}W n( tyd   | }t|d } d}Y n0 |r|tv rt| \}}}}}|tj|td | }|t|| fS | d dks| d |krt	d| t| }	d|	 d }
t|d }|
|ddtj
f  }tj|}tdD ]}d| ||| }qd|ddd d  }|dddddf ||d  }|d dkr|r||d	  }|d }n||d  }|d }|t|	| | }|d }|t| t| }t|}||| fS )
a  
    Return weights and error coefficient for Newton-Cotes integration.

    Suppose we have (N+1) samples of f at the positions
    x_0, x_1, ..., x_N. Then an N-point Newton-Cotes formula for the
    integral between x_0 and x_N is:

    :math:`\int_{x_0}^{x_N} f(x)dx = \Delta x \sum_{i=0}^{N} a_i f(x_i)
    + B_N (\Delta x)^{N+2} f^{N+1} (\xi)`

    where :math:`\xi \in [x_0,x_N]`
    and :math:`\Delta x = \frac{x_N-x_0}{N}` is the average samples spacing.

    If the samples are equally-spaced and N is even, then the error
    term is :math:`B_N (\Delta x)^{N+3} f^{N+2}(\xi)`.

    Parameters
    ----------
    rn : int
        The integer order for equally-spaced data or the relative positions of
        the samples with the first sample at 0 and the last at N, where N+1 is
        the length of `rn`. N is the order of the Newton-Cotes integration.
    equal : int, optional
        Set to 1 to enforce equally spaced data.

    Returns
    -------
    an : ndarray
        1-D array of weights to apply to the function at the provided sample
        positions.
    B : float
        Error coefficient.

    Notes
    -----
    Normally, the Newton-Cotes rules are used on smaller integration
    regions and a composite rule is used to return the total integral.

    Examples
    --------
    Compute the integral of sin(x) in [0, :math:`\pi`]:

    >>> from scipy.integrate import newton_cotes
    >>> import numpy as np
    >>> def f(x):
    ...     return np.sin(x)
    >>> a = 0
    >>> b = np.pi
    >>> exact = 2
    >>> for N in [2, 4, 6, 8, 10]:
    ...     x = np.linspace(a, b, N + 1)
    ...     an, B = newton_cotes(N, 1)
    ...     dx = (b - a) / N
    ...     quad = dx * np.sum(an * f(x))
    ...     error = abs(quad - exact)
    ...     print('{:2d}  {:10.9f}  {:.5e}'.format(N, quad, error))
    ...
     2   2.094395102   9.43951e-02
     4   1.998570732   1.42927e-03
     6   2.000017814   1.78136e-05
     8   1.999999835   1.64725e-07
    10   2.000000001   1.14677e-09

    r   rY   r   r   z1The sample positions must start at 0 and end at Nr9   Nr"   r~   )r[   r#   r   allr%   	Exception_builtincoeffsarrayr   r+   ZnewaxisZlinalginvr_   dotmathlogr	   exp)Zrnequalr   nadavinbdbanyitiZnvecCZCinvra   ZvecZaiBNpowerp1Zfacr7   r7   r8   r     sF    A
$

r   c              
     s  t tdsddlm} |t_ntj}t s8d}t|t| }t| }t	||\}}|j
d }	z || d  W n2 ty }
 zd}t||
W Y d }
~
n
d }
~
0 0 z t||gj  }W nL ty }
 z2d|
 d}tj|d	d
  fdd}W Y d }
~
n
d }
~
0 0 t|}||kr@d}t|t|}||kr`d}t||d u rx|j|	}nt||jjsd}t||j|j
d krd}t|t|dd }|j|}|dvrd}t||||||||||f	S )Nqmcr   )statsz`func` must be callable.r9   z`func` must evaluate the integrand at points within the integration range; e.g. `func( (a + b) / 2)` must return the integrand at the centroid of the integration volume.zAException encountered when attempting vectorized call to `func`: z. For better performance, `func` should accept two-dimensional array `x` with shape `(len(a), n_points)` and return an array of the integrand value at each of the `n_points.r   r:   c                   s   t j d| dS )Nr   )r!   r   )r#   Zapply_along_axisrU   rJ   r7   r8   rW   N  s    z_qmc_quad_iv.<locals>.vfuncz`n_points` must be an integer.z!`n_estimates` must be an integer.z8`qrng` must be an instance of scipy.stats.qmc.QMCEngine.z`qrng` must be initialized with dimensionality equal to the number of variables in `a`, i.e., `qrng.random().shape[-1]` must equal `a.shape[0]`.rng_seed>   FTz*`log` must be boolean (`True` or `False`).)hasattrr   Zscipyr%  callabler   r#   Z
atleast_1dr   broadcast_arraysr&   r  r+   r  Tr<   r=   Zint64r$  ZHaltonrg   Z	QMCEnginer2   r\   Z_qmcZcheck_random_state)rG   rQ   rR   n_pointsn_estimatesqrngr  r%  r   dimr   rW   Zn_points_intZn_estimates_intr&  rngr7   rJ   r8   _qmc_quad_iv'  s^    

 "





r0  QMCQuadResultintegralstandard_errori   )r,  r+  r-  r  c             	     s  t | |||||}|\	} }}}}}}}	ddd}
dfdd	 d fdd		d fd
d	}t||krd}tj|dd t|rtj nddS ||k }d|jdd }|| ||  ||< ||< t|| }|| }t	}t
|j}tD ]V}||}|	j|||j}| |}|
|||||< t|f d|| i|j}q  ||}||||d}|r|dk r|tjd  n|| }t||S )a  
    Compute an integral in N-dimensions using Quasi-Monte Carlo quadrature.

    Parameters
    ----------
    func : callable
        The integrand. Must accept a single argument ``x``, an array which
        specifies the point(s) at which to evaluate the scalar-valued
        integrand, and return the value(s) of the integrand.
        For efficiency, the function should be vectorized to accept an array of
        shape ``(d, n_points)``, where ``d`` is the number of variables (i.e.
        the dimensionality of the function domain) and `n_points` is the number
        of quadrature points, and return an array of shape ``(n_points,)``,
        the integrand at each quadrature point.
    a, b : array-like
        One-dimensional arrays specifying the lower and upper integration
        limits, respectively, of each of the ``d`` variables.
    n_estimates, n_points : int, optional
        `n_estimates` (default: 8) statistically independent QMC samples, each
        of `n_points` (default: 1024) points, will be generated by `qrng`.
        The total number of points at which the integrand `func` will be
        evaluated is ``n_points * n_estimates``. See Notes for details.
    qrng : `~scipy.stats.qmc.QMCEngine`, optional
        An instance of the QMCEngine from which to sample QMC points.
        The QMCEngine must be initialized to a number of dimensions ``d``
        corresponding with the number of variables ``x1, ..., xd`` passed to
        `func`.
        The provided QMCEngine is used to produce the first integral estimate.
        If `n_estimates` is greater than one, additional QMCEngines are
        spawned from the first (with scrambling enabled, if it is an option.)
        If a QMCEngine is not provided, the default `scipy.stats.qmc.Halton`
        will be initialized with the number of dimensions determine from
        the length of `a`.
    log : boolean, default: False
        When set to True, `func` returns the log of the integrand, and
        the result object contains the log of the integral.

    Returns
    -------
    result : object
        A result object with attributes:

        integral : float
            The estimate of the integral.
        standard_error :
            The error estimate. See Notes for interpretation.

    Notes
    -----
    Values of the integrand at each of the `n_points` points of a QMC sample
    are used to produce an estimate of the integral. This estimate is drawn
    from a population of possible estimates of the integral, the value of
    which we obtain depends on the particular points at which the integral
    was evaluated. We perform this process `n_estimates` times, each time
    evaluating the integrand at different scrambled QMC points, effectively
    drawing i.i.d. random samples from the population of integral estimates.
    The sample mean :math:`m` of these integral estimates is an
    unbiased estimator of the true value of the integral, and the standard
    error of the mean :math:`s` of these estimates may be used to generate
    confidence intervals using the t distribution with ``n_estimates - 1``
    degrees of freedom. Perhaps counter-intuitively, increasing `n_points`
    while keeping the total number of function evaluation points
    ``n_points * n_estimates`` fixed tends to reduce the actual error, whereas
    increasing `n_estimates` tends to decrease the error estimate.

    Examples
    --------
    QMC quadrature is particularly useful for computing integrals in higher
    dimensions. An example integrand is the probability density function
    of a multivariate normal distribution.

    >>> import numpy as np
    >>> from scipy import stats
    >>> dim = 8
    >>> mean = np.zeros(dim)
    >>> cov = np.eye(dim)
    >>> def func(x):
    ...     # `multivariate_normal` expects the _last_ axis to correspond with
    ...     # the dimensionality of the space, so `x` must be transposed
    ...     return stats.multivariate_normal.pdf(x.T, mean, cov)

    To compute the integral over the unit hypercube:

    >>> from scipy.integrate import qmc_quad
    >>> a = np.zeros(dim)
    >>> b = np.ones(dim)
    >>> rng = np.random.default_rng()
    >>> qrng = stats.qmc.Halton(d=dim, seed=rng)
    >>> n_estimates = 8
    >>> res = qmc_quad(func, a, b, n_estimates=n_estimates, qrng=qrng)
    >>> res.integral, res.standard_error
    (0.00018429555666024108, 1.0389431116001344e-07)

    A two-sided, 99% confidence interval for the integral may be estimated
    as:

    >>> t = stats.t(df=n_estimates-1, loc=res.integral,
    ...             scale=res.standard_error)
    >>> t.interval(0.99)
    (0.0001839319802536469, 0.00018465913306683527)

    Indeed, the value reported by `scipy.stats.multivariate_normal` is
    within this range.

    >>> stats.multivariate_normal.cdf(b, mean, cov, lower_limit=a)
    0.00018430867675187443

    Fc                 S  s(   |rt | t| S t| | S d S rI   )r
   r#   r  r*   )
integrandsdAr  r7   r7   r8   sum_product  s    zqmc_quad.<locals>.sum_productc                   s$   |rt | t  S t| S d S rI   )r
   r#   r  mean)	estimatesr  )r,  r7   r8   r7    s    zqmc_quad.<locals>.meanNr   c                   s|   |p | |}|rjt | |\} }t | |t jd  f}t|dd}t dtd| t |   S t j| |dS d S )N              ?r   r    r   r9   )ddof)r#   r)  Zvstackpir
   rO   r  std)r8  mr:  r  tempr%   )r7  r,  r7   r8   r<    s    zqmc_quad.<locals>.stdc                   sJ   |p | |}|p | |d|d}|r8|dt   S |t  S d S )Nr   )r:  r  r   )r#   r  sqrt)r8  r=  r   r  r7  r,  r<  r7   r8   sem  s
    zqmc_quad.<locals>.semz^A lower limit was equal to an upper limit, so the value of the integral is zero by definition.r9   r:   r   r    seed)r=  r  r9  )F)F)Nr   F)NNF)r0  r#   r   r<   r=   r1  rh   r*   prodZzerosr   r/  r_   randomr$  scaler*  r]   Z
_init_quadr;  )rG   rQ   rR   r,  r+  r-  r  rS   r/  r%  r6  rA  r   Zi_swapsignAr5  r8  Zrngsra   sampler0   r4  r2  r3  r7   r@  r8   r   t  s6    n


 
&r   )Nr   r   )Nr   r   )r7   rN   )r7   F)r7   rd   rd   re   Tr   )Nr   r   N)Nr   r   N)r   r   F)r7   r   r   Fr   F)r   )<
__future__r   typingr   r   r   r   numpyr#   Znumpy.typingZnptr  r<   collectionsr   Zscipy.specialr   r	   r
   Zscipy._lib._utilr   Zscipy._lib.deprecationr   r   r   __all__r   r   Warningr   rC   rD   rK   rL   dictrE   r   rc   r   rt   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r0  r1  r   r7   r7   r7   r8   <module>   s   
 


G
-  [

k' y 9
 	   






#
mJ