
    xf                       d dl mZ d dlmZmZmZmZ d dl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 g dZdgd
ZdgdZ G d de      Zerd dlmZ  G d de      ZneZdhdZed        Z e       e_        didZdjdZ	 	 dkdZ d Z!dldZ"dldZ#d Z$dldZ%dldZ&dmdZ'd  Z(d! Z)d" Z*	 	 dnd#Z+dd$ddgd	d%fdd&g d'd	d(fd&d)g d*d+d,fd$d-g d.d/d0fdd1g d2d3d4fdd5g d6d7d8fd9d:g d;d<d=fd>d?g d@dAdBfdCdDg dEdFdGfddHg dIdJdKfdLdMg dNdOdPfddQg dRdSdTfdUdVg dWdXdYfd9dZg d[d\d]fd^Z,dod_Z-d` Z. edadbdcg      Z/d)dddddedfZ0y)p    )annotations)TYPE_CHECKINGCallableAnycastN)
namedtuple)roots_legendre)gammaln	logsumexp)
_rng_spawn)
fixed_quad
quadraturerombergromb	trapezoidtrapzsimpssimpsoncumulative_trapezoidcumtrapznewton_cotesqmc_quadAccuracyWarningc                    t        t        d      rt        j                  | |||      S t        j                  | |||      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.])
    r   xdxaxis)hasattrnpr   r   yr   r   r   s       =/usr/lib/python3/dist-packages/scipy/integrate/_quadrature.pyr   r      s9    P r;||Ar55xxQ2D11    c                     t        | |||      S )z}An alias of `trapezoid`.

    `trapz` is kept for backwards compatibility. For new code, prefer
    `trapezoid` instead.
    r   )r   r"   s       r$   r   r      s     Q!..r%   c                      e Zd Zy)r   N)__name__
__module____qualname__ r%   r$   r   r      s    r%   r   )Protocolc                      e Zd ZU ded<   y)CacheAttributeszdict[int, tuple[Any, Any]]cacheN)r(   r)   r*   __annotations__r+   r%   r$   r.   r.      s    ))r%   r.   c                "    t        t        |       S N)r   r.   )funcs    r$   cache_decoratorr4      s    &&r%   c                    | 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_legendrer/   r	   )ns    r$   r6   r6      sK     	"(((%++A..&4Q&7  #!''**r%      c                ,   t        |      \  }}t        j                  |      }t        j                  |      st        j                  |      rt	        d      ||z
  |dz   z  dz  |z   }||z
  dz  t        j
                  | | |g| z  d      z  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   N)r6   r!   realisinf
ValueErrorsum)r3   abargsr7   r   wr#   s           r$   r   r      s    | "!$DAq

A	xx{bhhqk * + 	+	
1qsC!AaC9rvvaQ.R88$>>r%   Fc                *     |r fd}|S  f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                     | g S r2   r+   )r   rC   r3   s    r$   vfunczvectorize1.<locals>.vfunc  s    >D>!r%   c                B   t        j                  |       r	 | g S t        j                  |       }  | d   g }t        |       }t	        |dt        |            }t        j                  |f|      }||d<   t        d|      D ]  } | |   g ||<    |S )Nr   dtyperI   r:   )r!   isscalarasarraylengetattrtypeemptyrange)r   y0r7   rI   outputirC   r3   s         r$   rG   zvectorize1.<locals>.vfunc  s    {{1~A~~%

1Aad"T"BAABb2EXXqd%0FF1I1a[ . 1--q	.Mr%   r+   )r3   rC   vec_funcrG   s   ``  r$   
vectorize1rV      s     2 	"  L	 Lr%   r:   c	                   t        |t              s|f}t        | ||      }	t        j                  }
t        j                  }t        |dz   |      }t        ||dz         D ]?  }t        |	||d|      d   }t        ||
z
        }|}
||k  s||t        |
      z  k  s< |
|fS  t        j                  d||fz  t               |
|fS )a  
    Compute a definite integral using fixed-tolerance Gaussian quadrature.

    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

    rU   r:   r+   r   z-maxiter (%d) exceeded. Latest difference = %e)
isinstancetuplerV   r!   infmaxrQ   r   abswarningswarnr   )r3   rA   rB   rC   tolrtolmaxiterrU   miniterrG   valerrr7   newvals                 r$   r   r   #  s    B dE"wtTH5E
&&C
&&C'!)W%G7GAI& 
E1aQ/2&*o9d3s8m+
 8O
 	;wnL	 8Or%   c                8    t        |       }|||<   t        |      S r2   )listrZ   )trT   valuels       r$   tuplesetrl   x  s    QAAaD8Or%   c                "    t        | ||||      S )zAn alias of `cumulative_trapezoid`.

    `cumtrapz` is kept for backwards compatibility. For new code, prefer
    `cumulative_trapezoid` instead.
    )r   r   r   initial)r   )r#   r   r   r   rn   s        r$   r   r     s      Q2D'JJr%   c                   t        j                  |       } ||}nt        j                  |      }|j                  dk(  r<t        j                  |      }dg| j                  z  }d||<   |j	                  |      }nMt        |j                        t        | j                        k7  rt        d      t        j                  ||      }|j                  |   | j                  |   dz
  k7  rt        d      t        | j                        }t        t        d      f|z  |t        dd            }t        t        d      f|z  |t        dd            }	t        j                  || |   | |	   z   z  dz  |      }
|st        j                  |      st        d      t        |
j                        }d||<   t        j                  t        j                  |||
j                  	      |
g|      }
|
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.
        Typically this value should be 0. 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.

    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
    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()

    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;   z'`initial` parameter should be a scalar.rJ   )r!   rL   ndimdiffreshaperM   shaper?   rl   slicecumsumrK   rh   concatenatefullrI   )r#   r   r   r   rn   dru   ndslice1slice2ress              r$   r   r     s   n 	

1AyJJqM66Q;
AC!&&LEE$K		% A\S\) * + + %A774=AGGDMA-- * + + 
QWWBuT{nR'uQ~>FuT{nR'uT2?F
))A6QvY./#5D
AC{{7#FGGSYYdnnbggeWCIIFL"&( Jr%   c                   t        | j                        }|d}d}t        d       f|z  }t        ||t        |||            }	t        ||t        |dz   |dz   |            }
t        ||t        |dz   |dz   |            }|3t	        j
                  | |	   d| |
   z  z   | |   z   |      }||dz  z  }|S t	        j                  ||      }t        ||t        |||            }t        ||t        |dz   |dz   |            }||   j                  t        d      }||   j                  t        d      }||z   }||z  }t	        j                  ||t	        j                  |      |dk7  	      }|d
z  | |	   dt	        j                  d|t	        j                  |      |dk7  	      z
  z  | |
   |t	        j                  ||t	        j                  |      |dk7  	      z  z  z   | |   d|z
  z  z   z  }t	        j
                  ||      }|S )Nr      r:         @r<         @F)copyoutwhereg      @r;         ?)rM   ru   rv   rl   r!   r@   rs   astypefloattrue_divide
zeros_like)r#   startstopr   r   r   r{   step	slice_allslice0r|   r}   resulthsl0sl1h0h1hsumhprodh0divh1tmps                         r$   _basic_simpsonr     s#   	QWWB}Dtr!IiuUD$'?@FiuU1Wd1fd'CDFiuU1Wd1fd'CDFy&	C&	M1AfI=DI"s(, M% GGAD!y$eT4(@Ay$eAgtAvt(DEsV]]5u]-sV]]5u]-BwR..RR]]2->bAgN3h!F)W46MM'4J6=l"D DE F)t')~~dE:<--:M<AQJ(H(H I	I F)sW}56 7 $'Mr%   c                "    t        | ||||      S )zyAn alias of `simpson`.

    `simps` is kept for backwards compatibility. For new code, prefer
    `simpson` instead.
    )r   r   r   even)r   )r#   r   r   r   r   s        r$   r   r     s     1bt$77r%   c           	     	   t        j                  |       } t        | j                        }| j                  |   }|}|}d}	|t        j                  |      }t        |j                        dk(  rAdg|z  }
|j                  d   |
|<   |j                  }d}	|j	                  t        |
            }n6t        |j                        t        | j                        k7  rt        d      |j                  |   |k7  rt        d      |t        j                  dt        d       |dz  dk(  r/d	}d	}t        d      f|z  }||nd
}|dvrt        d      |dk(  r=t        ||d      }t        ||d      }|||   ||   z
  }|d|z  | |   | |   z   z  z  }d}|d
k(  rt        | d|dz
  |||      }t        ||d      }t        ||d      }t        ||d      }t        j                  ||g      }|t        ||t        ddd            }t        ||t        ddd            }t        j                  t        j                  ||            }t        j                   ||   |      t        j                   ||   |      g}d|d   dz  z  d|d   z  |d   z  z   }d|d   |d   z   z  }t        j"                  ||t        j$                  |      |dk7        }|d   dz  d|d   z  |d   z  z   }d|d   z  }t        j"                  ||t        j$                  |      |dk7        }d|d   dz  z  }d|d   z  |d   |d   z   z  }t        j"                  ||t        j$                  |      |dk7        }||| |   z  || |   z  z   || |   z  z
  z  }|dv rNt        ||d      }t        ||d      }|||   ||   z
  }|d|z  | |   | |   z   z  z  }t        | d|dz
  |||      }|dv rct        ||d      }t        ||d      }||t        |         |t        |         z
  }|d|z  | |   | |   z   z  z  }|t        | d|dz
  |||      z  }|dk(  r
|dz  }|dz  }||z   }nt        | d|dz
  |||      }|	r|j	                        }|S )a  
    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.13.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
    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)
    40.5

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

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

    r   Nr:   rp   rq   zWThe 'even' keyword is deprecated as of SciPy 1.11.0 and will be removed in SciPy 1.13.0r   
stacklevelg        r   )avglastfirstr   z>Parameter 'even' must be 'simpson', 'avg', 'last', or 'first'.r         ?   r<      r   r   )r   r   )r   r   r   r;   )r!   rL   rM   ru   rt   rZ   r?   r^   r_   DeprecationWarningrv   rl   r   asfarrayfloat64rs   squeezer   r   )r#   r   r   r   r   r{   Nlast_dxfirst_dxreturnshapeshapex	saveshaperd   r   r   r|   r}   slice3r   hm2hm1diffsnumdenalphabetaetas                              r$   r   r     s   N 	

1A	QWWB	AGHK}JJqMqww<1S2XF771:F4LIK		%-(A\S\) * + +774=A * + + &1	
 	1uz4[NR'	 'tY::- 
 6 ir2Fir2F}F)ai/3=AfI&	$9::C D9#Aq!A#q"d;Fir2Fir2Fir2FRH%A}y$b"a0@Ay$b$0BC

27714#89ZZc
6ZZc
68 adai-!ad(QqT/1Cqtad{#CNNMM#&Qh	E A$!)cAaDj1Q4//Cad(C>>MM#&Qh	D adai-Cad(adQqTk*C..MM#&Qh	C eAfIoQvY6QvYFFF ##ir2Fir2F}F)ai/3w;&	!F) 344C#Aq!A#q"d;F?"iq1Fiq1F}U6]+af.>>3x<61V9!455CnQ1Q32t<<F5=3JCcMF#1ac1b$7IIi Mr%   c           
        t        j                  |       } t        | j                        }| j                  |   }|dz
  }d}d}||k  r|dz  }|dz  }||k  r||k7  rt	        d      i }	t        d      f|z  }
t        |
|d      }t        |
|d      }|t        j                  |t              z  }| |   | |   z   dz  |z  |	d<   |
}|x}x}}t        d|dz         D ]  }|dz  }t        ||t        |||            }|dz  }d	|	|dz
  df   || |   j                  |
      z  z   z  |	|df<   t        d|dz         D ]0  }|	||dz
  f   }|||	|dz
  |dz
  f   z
  dd|z  z  dz
  z  z   |	||f<   2 |dz  } |rt        j                  |	d         st        d       n	 |d   }	 |d   }d||fz  }d}t        |dt        |      z  dd       t        |dz         D ]4  }t        |dz         D ]  }t        ||	||f   z  d        t                6 t        dt        |      z         |	||f   S # t        t        f$ r d}Y w xY w# t        t        f$ r d}Y w xY w)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   rJ   r;   )r   r   r   r<   r   zE*** Printing table only supported for integrals of a single data set.r8      z%%%d.%dfz6Richardson Extrapolation Table for Romberg Integration=
)sepend r   )r!   rL   rM   ru   r?   rv   rl   r   rQ   r@   rK   print	TypeError
IndexError)r#   r   r   showr{   NsampsNintervr7   kRr   r   slicem1r   slice_Rr   r   r   rT   jprevpreciswidthformstrtitles                            r$   r   r     s   z 	

1A	QWWBWWT]FQhG	A	A
g+	a	Q g+ 	G| 4 5 	5 	At#Iiq)Fy$+G"**Ru--A6QwZ',Q.AfIG!!E!D41ac] !7D%tT*BC
AaC8q7T)B'BBC1a&	q!A# 	GAa1X;DQ!QqSz] 2ac
A~FFAq!fI	G 	
S {{1V9% + ,aQ !E6?2GLE%s5z)t>1Q3Z qs 8A'Aq!fI-378 #E
"#aV9! z*  z* s$   H. "I .IIIIc                "   |dk  rt        d      |dk(  rd | |d          | |d         z   z  S |dz  }t        |d   |d   z
        |z  }|d   d|z  z   }||t        j                  |      z  z   }t        j                   | |      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   r   r<   )r?   r   r!   aranger@   )functionintervalnumtrapsnumtosumr   loxpointsss           r$   	_difftrapr     s     1}>??	QHXa[)(8A;*??@@A:(1+hqk)*83qkC!G#q299X...FF8F#!,r%   c                (    d|z  }||z  | z
  |dz
  z  S )z
    Compute the differences for the Romberg quadrature corrections.
    See Forman Acton's "Real Computing Made Real," p 143.
    r   r   r+   )rB   cr   r   s       r$   _romberg_diffr     s$    
 q&C!GaK#)$$r%   c                   dx}}t        dt        |       d       t        d|       t        d       t        ddz         t        t        |            D ]Z  }t        d	d
|z  |d   |d   z
  d|z  z  fz  d       t        |dz         D ]  }t        d||   |   z  d        t        d       \ t        d       t        d||   |   d       t        dd
t        |      dz
  z  dz   d       y )Nr   zRomberg integration ofr   r   from z%6s %9s %9s)StepsStepSizeResultsz%6d %9fr   r:   r;   z%9fzThe final result isafterzfunction evaluations.)r   reprrQ   rM   )r   r   resmatrT   r   s        r$   _printresmatr     s    IA	
"DN<	&(	"I	-:
:;3v; i1a4(1+hqk"9BE!BCCMqs 	3A%6!9Q<(c2	3b		
 
"I	
137	'1s6{1}%a')@Ar%   c	           
        t        j                  |      st        j                  |      rt        d      t        | ||      }	d}
||g}||z
  }t	        |	||
      }||z  }|gg}t         j
                  }|d   }t        d|dz         D ]  }|
dz  }
|t	        |	||
      z  }||z  |
z  g}t        |      D ]'  }|j                  t        ||   ||   |dz                ) ||   }||dz
     }|r|j                  |       t        ||z
        }||k  s||t        |      z  k  r n#|} t        j                  d||fz  t               |rt        |	||       |S )a
  
    Romberg integration of a callable function or method.

    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.rX   r:   r   r   z,divmax (%d) exceeded. Latest difference = %e)r!   r>   r?   rV   r   r[   rQ   appendr   r]   r^   r_   r   r   )r   rA   rB   rC   r`   ra   r   divmaxrU   rG   r7   r   intrangeordsumr   r   re   last_rowrT   rowr   
lastresults                         r$   r   r     s   j 
xx{bhhqk . / 	/x9E	A1vH1uHuh*FFhZF
&&CayH1fQh 	Q)E8Q//& 1$%q 	@AJJ}Xa[#a&!A#>?	@Qac]
MM#&:%&9dS[00 	:fc]J	 UHf-Mr%   r      r   )r:      r:   Z   r   )r:   r   r   r:   r   P   -   )       r   r   r   ii  i   )   K   2   r   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:   r   r   r   r8   r   r   r   r	  
   r  r   r!     c                v   	 t        |       dz
  }|rt        j                  |dz         } n-t        j                  t        j                  |       dk(        rd}|rH|t        v r@t        |   \  }}}}}|t        j                  |t              z  |z  }|t        |      |z  fS | d   dk7  s| d   |k7  rt        d      | t        |      z  }	d|	z  dz
  }
t        j                  |dz         }|
|ddt        j                  f   z  }t        j                  j                  |      }t        d      D ](  }d|z  |j                  |      j                  |      z
  }* d|ddd   dz   z  }|dddddf   j                  |      |dz  z  }|dz  dk(  r|r||d	z   z  }|dz   }n||dz   z  }|dz   }|t        j                  |	|z  |      z
  }|dz   }|t        j                   |      z  t#        |      z
  }t        j$                  |      }|||z  fS # t
        $ r  | }t        j                  |dz         } d}Y w xY w)
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:   rJ   r   r   z1The sample positions must start at 0 and end at Nr   Nr;   r   )rM   r!   r   allrs   	Exception_builtincoeffsarrayr   r?   newaxislinalginvrQ   dotmathlogr
   exp)rnequalr   nadavinbdbanyitinvecCCinvrT   vecaiBNpowerp1facs                        r$   r   r   x  s;   B	GAI1Q3BVVBGGBK1$%E n$+A.BB"((2U++b059R<
1
2! ) * 	*	eAhB	
R!B99QqS>D
d1bjj=!!A99==D1X .v--.
cc1
C	a1f		#	!b&	)B	A
"X!"X!	bffRY#	#B	qB

gbk
)C
((3-Cr#v:G  YYqs^s   AH %H87H8c           	         t        t        d      sddlm} |t        _        nt        j                  }t	               sd}t        |      t        j                  |      j                         }t        j                  |      j                         }t        j                  ||      \  }}|j                  d   }		   ||z   dz         	   t        j                  ||g      j                          }t        j"                  |      }||k7  rd}t        |      t        j"                  |      }||k7  rd}t        |      ||j$                  j'                  |	      }n-t)        ||j$                  j*                        sd}t        |      |j,                  |j                  d   k7  rd}t        |      t/        |dd       }|j0                  j3                  |      }|dvrd}t        |      |||||||||f	S # t        $ r}
d}t        |      |
d }
~
ww xY w# t        $ r-}
d|
 d}t        j                   |d	
        fd}Y d }
~
Vd }
~
ww xY w)Nqmcr   )statsz`func` must be callable.r   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                4    t        j                  d|       S )Nr   )r   arr)r!   apply_along_axis)r   r3   s    r$   rG   z_qmc_quad_iv.<locals>.vfunc  s    &&t"!<<r%   z`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`).)r    r   scipyrS  callabler   r!   
atleast_1dr   broadcast_arraysru   r4  r?   r6  Tr^   r_   int64rR  HaltonrY   	QMCEnginerz   rN   _qmccheck_random_state)r3   rA   rB   n_pointsn_estimatesqrngr<  rS  messagedimerG   n_points_intn_estimates_intrW  rngs   `               r$   _qmc_quad_ivrk    s7    8U#D>,   	aA
aAq!$DAq
''!*C)a!eq[=RXXq!f  88H%L<2  hh{+Oo%5  |yy$eii112L  vvH !!tZ.H
**
'
'
1C
->  1ac3NNe  )) !q()  	=S !,,
 	g!,	= 	=	=s0   ;G9 
)H 9	HHH	I!"I		IQMCQuadResultintegralstandard_errori   )rc  rb  rd  r<  c          	        t        | |||||      }|\	  } }}}}}}}	dd}
dfd	dfd	dfd	}t        j                  ||k(        r=d}t        j                  |d       t        |rt        j                   d      S dd      S ||k  }d	|j                  d	
      z  }||   ||   c||<   ||<   t        j                  ||z
        }||z  }t        j                        }t        |j                        }t              D ]o  }|j                  |      }|	j                  j                  |||      j                   } | |      } |
|||      ||<    t#        |      dd||   i|j$                  }q  ||      } ||||      }|r|dk  r|t        j&                  dz  z   n||z  }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

    c                x    |r!t        |       t        j                  |      z   S t        j                  | |z        S r2   )r   r!   r<  r@   )
integrandsdAr<  s      r$   sum_productzqmc_quad.<locals>.sum_product  s0    Z(266":5566*r/**r%   c                t    |r!t        |       t        j                        z
  S t        j                  |       S r2   )r   r!   r<  mean)	estimatesr<  rc  s     r$   ru  zqmc_quad.<locals>.mean  s.    Y'"&&*===779%%r%   r   c                l   |xs	  | |      }|rt        j                  | |      \  } }t        j                  | |t         j                  dz  z   f      }t	        |d      }t        j
                  dt	        d|z        t        j                  |z
        z
  z        S t        j                  | |      S )N              ?r   r<   r   r   )ddof)r!   r[  vstackpir   r=   r<  std)rv  mry  r<  temprs   ru  rc  s         r$   r|  zqmc_quad.<locals>.std  s    %i%..y!<LIq99iRUURZ89DT*D773)AH"5$&FF;+=$>#? @ A A 66)$//r%   c                    |xs	  | |      }|xs  | |d|      }|r|dt        j                        z  z
  S |t        j                        z  S )Nr:   )ry  r<  r   )r!   r<  sqrt)rv  r}  r   r<  ru  rc  r|  s       r$   semzqmc_quad.<locals>.sem  sY    %i%3Ys3s266+....rww{+++r%   z^A lower limit was equal to an upper limit, so the value of the integral is zero by definition.r   r   r   r<   seed)r}  r<  rx  )F)Nr   F)NNFr+   )rk  r!   anyr^   r_   rl  r[   r@   prodzerosr   rj  rQ   randomrR  scaler\  rO   
_init_quadr{  )r3   rA   rB   rc  rb  rd  r<  rC   rj  rS  rs  r  re  i_swapsignArr  rv  rngsrT   sampler   rq  rm  rn  ru  r|  s      `                     @@r$   r   r   2  s   \ aHk4ED?C<D!Q+tS#u+&	0, 
vva1f~<g!,bffWA66A66UF&**"*%&DV9aiAfIqy
AA	
XB%Idhh,D; ;X& IIOOFAq)++!W
":r37	! tDz:tAw:$//:; Is#HhC8N'*taxx"%%("htmH>22r%   )Nr   r   )r3   r   returnr.   )r+   r8   )r+   F)r+   "\O>r  r   Tr:   )Nr   r   N)r   r   F)r+   `sbO>r  Fr0  F)r   )1
__future__r   typingr   r   r   r   numpyr!   r;  r^   collectionsr   scipy.specialr	   r
   r   scipy._lib._utilr   __all__r   r   Warningr   r,   r.   r4   r6   dictr/   r   rV   r   rl   r   r   r   r   r   r   r   r   r   r   r5  r   rk  rl  r   r+   r%   r$   <module>r     s3   " 5 5    " ( , '*k2`/	g 	   *( * O' 	+ 	+  $v  D?N*Z HJ&'RjKZz"N8tnsB4%B" FK %vh 	
!QqE"R	!GBr	!Ib	"^Bs#	#!$u-	#'40	%7fE	%?f	% #V-	
6 7	)	 
H ()4l	D 
G 016	@ 
\ 3 5@		 
J 8 :E			7 FjZGOT ?Z9I,JK )*Dtr3r%   