
    ^MhF                         d dl mZmZ d dlmZ  G d d          Z G d de          Z G d de          Z G d	 d
e          Zd Z	ddZ
d ZdS )    )array_namespacexp_size)cached_propertyc                   "    e Zd ZdZddZddZdS )Rulea	  
    Base class for numerical integration algorithms (cubatures).

    Finds an estimate for the integral of ``f`` over the region described by two arrays
    ``a`` and ``b`` via `estimate`, and find an estimate for the error of this
    approximation via `estimate_error`.

    If a subclass does not implement its own `estimate_error`, then it will use a
    default error estimate based on the difference between the estimate over the whole
    region and the sum of estimates over that region divided into ``2^ndim`` subregions.

    See Also
    --------
    FixedRule

    Examples
    --------
    In the following, a custom rule is created which uses 3D Genz-Malik cubature for
    the estimate of the integral, and the difference between this estimate and a less
    accurate estimate using 5-node Gauss-Legendre quadrature as an estimate for the
    error.

    >>> import numpy as np
    >>> from scipy.integrate import cubature
    >>> from scipy.integrate._rules import (
    ...     Rule, ProductNestedFixed, GenzMalikCubature, GaussLegendreQuadrature
    ... )
    >>> def f(x, r, alphas):
    ...     # f(x) = cos(2*pi*r + alpha @ x)
    ...     # Need to allow r and alphas to be arbitrary shape
    ...     npoints, ndim = x.shape[0], x.shape[-1]
    ...     alphas_reshaped = alphas[np.newaxis, :]
    ...     x_reshaped = x.reshape(npoints, *([1]*(len(alphas.shape) - 1)), ndim)
    ...     return np.cos(2*np.pi*r + np.sum(alphas_reshaped * x_reshaped, axis=-1))
    >>> genz = GenzMalikCubature(ndim=3)
    >>> gauss = GaussKronrodQuadrature(npoints=21)
    >>> # Gauss-Kronrod is 1D, so we find the 3D product rule:
    >>> gauss_3d = ProductNestedFixed([gauss, gauss, gauss])
    >>> class CustomRule(Rule):
    ...     def estimate(self, f, a, b, args=()):
    ...         return genz.estimate(f, a, b, args)
    ...     def estimate_error(self, f, a, b, args=()):
    ...         return np.abs(
    ...             genz.estimate(f, a, b, args)
    ...             - gauss_3d.estimate(f, a, b, args)
    ...         )
    >>> rng = np.random.default_rng()
    >>> res = cubature(
    ...     f=f,
    ...     a=np.array([0, 0, 0]),
    ...     b=np.array([1, 1, 1]),
    ...     rule=CustomRule(),
    ...     args=(rng.random((2,)), rng.random((3, 2, 3)))
    ... )
    >>> res.estimate
     array([[-0.95179502,  0.12444608],
            [-0.96247411,  0.60866385],
            [-0.97360014,  0.25515587]])
     c                     t           )a  
        Calculate estimate of integral of `f` in rectangular region described by
        corners `a` and ``b``.

        Parameters
        ----------
        f : callable
            Function to integrate. `f` must have the signature::
                f(x : ndarray, \*args) -> ndarray

            `f` should accept arrays ``x`` of shape::
                (npoints, ndim)

            and output arrays of shape::
                (npoints, output_dim_1, ..., output_dim_n)

            In this case, `estimate` will return arrays of shape::
                (output_dim_1, ..., output_dim_n)
        a, b : ndarray
            Lower and upper limits of integration as rank-1 arrays specifying the left
            and right endpoints of the intervals being integrated over. Infinite limits
            are currently not supported.
        args : tuple, optional
            Additional positional args passed to ``f``, if any.

        Returns
        -------
        est : ndarray
            Result of estimation. If `f` returns arrays of shape ``(npoints,
            output_dim_1, ..., output_dim_n)``, then `est` will be of shape
            ``(output_dim_1, ..., output_dim_n)``.
        NotImplementedError)selffabargss        \/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/scipy/integrate/_rules/_base.pyestimatezRule.estimateC   s    B "!    c                     |                      ||||          }d}t          ||          D ] \  }}||                      ||||          z  }!| j                            ||z
            S )a-  
        Estimate the error of the approximation for the integral of `f` in rectangular
        region described by corners `a` and `b`.

        If a subclass does not override this method, then a default error estimator is
        used. This estimates the error as ``|est - refined_est|`` where ``est`` is
        ``estimate(f, a, b)`` and ``refined_est`` is the sum of
        ``estimate(f, a_k, b_k)`` where ``a_k, b_k`` are the coordinates of each
        subregion of the region described by ``a`` and ``b``. In the 1D case, this
        is equivalent to comparing the integral over an entire interval ``[a, b]`` to
        the sum of the integrals over the left and right subintervals, ``[a, (a+b)/2]``
        and ``[(a+b)/2, b]``.

        Parameters
        ----------
        f : callable
            Function to estimate error for. `f` must have the signature::
                f(x : ndarray, \*args) -> ndarray

            `f` should accept arrays `x` of shape::
                (npoints, ndim)

            and output arrays of shape::
                (npoints, output_dim_1, ..., output_dim_n)

            In this case, `estimate` will return arrays of shape::
                (output_dim_1, ..., output_dim_n)
        a, b : ndarray
            Lower and upper limits of integration as rank-1 arrays specifying the left
            and right endpoints of the intervals being integrated over. Infinite limits
            are currently not supported.
        args : tuple, optional
            Additional positional args passed to `f`, if any.

        Returns
        -------
        err_est : ndarray
            Result of error estimation. If `f` returns arrays of shape
            ``(npoints, output_dim_1, ..., output_dim_n)``, then `est` will be
            of shape ``(output_dim_1, ..., output_dim_n)``.
        r   )r   _split_subregionxpabs)	r   r   r   r   r   estrefined_esta_kb_ks	            r   estimate_errorzRule.estimate_errorf   su    V mmAq!T**(A.. 	< 	<HC4==Cd;;;KKw{{3,---r   Nr   )__name__
__module____qualname____doc__r   r   r   r   r   r   r      sH        : :x!" !" !" !"F1. 1. 1. 1. 1. 1.r   r   c                   6    e Zd ZdZd Zed             ZddZdS )	FixedRulea  
    A rule implemented as the weighted sum of function evaluations at fixed nodes.

    Attributes
    ----------
    nodes_and_weights : (ndarray, ndarray)
        A tuple ``(nodes, weights)`` of nodes at which to evaluate ``f`` and the
        corresponding weights. ``nodes`` should be of shape ``(num_nodes,)`` for 1D
        cubature rules (quadratures) and more generally for N-D cubature rules, it
        should be of shape ``(num_nodes, ndim)``. ``weights`` should be of shape
        ``(num_nodes,)``. The nodes and weights should be for integrals over
        :math:`[-1, 1]^n`.

    See Also
    --------
    GaussLegendreQuadrature, GaussKronrodQuadrature, GenzMalikCubature

    Examples
    --------

    Implementing Simpson's 1/3 rule:

    >>> import numpy as np
    >>> from scipy.integrate._rules import FixedRule
    >>> class SimpsonsQuad(FixedRule):
    ...     @property
    ...     def nodes_and_weights(self):
    ...         nodes = np.array([-1, 0, 1])
    ...         weights = np.array([1/3, 4/3, 1/3])
    ...         return (nodes, weights)
    >>> rule = SimpsonsQuad()
    >>> rule.estimate(
    ...     f=lambda x: x**2,
    ...     a=np.array([0]),
    ...     b=np.array([1]),
    ... )
     [0.3333333]
    c                     d | _         d S N)r   r   s    r   __init__zFixedRule.__init__   s    r   c                     t           r%   r
   r&   s    r   nodes_and_weightszFixedRule.nodes_and_weights   s    !!r   r   c           	          | j         \  }}| j        t          |          | _        t          ||||||| j                  S )aM  
        Calculate estimate of integral of `f` in rectangular region described by
        corners `a` and `b` as ``sum(weights * f(nodes))``.

        Nodes and weights will automatically be adjusted from calculating integrals over
        :math:`[-1, 1]^n` to :math:`[a, b]^n`.

        Parameters
        ----------
        f : callable
            Function to integrate. `f` must have the signature::
                f(x : ndarray, \*args) -> ndarray

            `f` should accept arrays `x` of shape::
                (npoints, ndim)

            and output arrays of shape::
                (npoints, output_dim_1, ..., output_dim_n)

            In this case, `estimate` will return arrays of shape::
                (output_dim_1, ..., output_dim_n)
        a, b : ndarray
            Lower and upper limits of integration as rank-1 arrays specifying the left
            and right endpoints of the intervals being integrated over. Infinite limits
            are currently not supported.
        args : tuple, optional
            Additional positional args passed to `f`, if any.

        Returns
        -------
        est : ndarray
            Result of estimation. If `f` returns arrays of shape ``(npoints,
            output_dim_1, ..., output_dim_n)``, then `est` will be of shape
            ``(output_dim_1, ..., output_dim_n)``.
        )r)   r   r   _apply_fixed_rule)r   r   r   r   r   nodesweightss          r   r   zFixedRule.estimate   sC    H /w7?%e,,DG Aq%$HHHr   Nr   )r   r   r    r!   r'   propertyr)   r   r   r   r   r#   r#      sc        % %N   " " X")I )I )I )I )I )Ir   r#   c                   L    e Zd ZdZd Zed             Zed             ZddZdS )	NestedFixedRulea  
    A cubature rule with error estimate given by the difference between two underlying
    fixed rules.

    If constructed as ``NestedFixedRule(higher, lower)``, this will use::

        estimate(f, a, b) := higher.estimate(f, a, b)
        estimate_error(f, a, b) := \|higher.estimate(f, a, b) - lower.estimate(f, a, b)|

    (where the absolute value is taken elementwise).

    Attributes
    ----------
    higher : Rule
        Higher accuracy rule.

    lower : Rule
        Lower accuracy rule.

    See Also
    --------
    GaussKronrodQuadrature

    Examples
    --------

    >>> from scipy.integrate import cubature
    >>> from scipy.integrate._rules import (
    ...     GaussLegendreQuadrature, NestedFixedRule, ProductNestedFixed
    ... )
    >>> higher = GaussLegendreQuadrature(10)
    >>> lower = GaussLegendreQuadrature(5)
    >>> rule = NestedFixedRule(
    ...     higher,
    ...     lower
    ... )
    >>> rule_2d = ProductNestedFixed([rule, rule])
    c                 0    || _         || _        d | _        d S r%   )higherlowerr   )r   r2   r3   s      r   r'   zNestedFixedRule.__init__  s    
r   c                 6    | j         | j         j        S t          r%   )r2   r)   r   r&   s    r   r)   z!NestedFixedRule.nodes_and_weights"  s    ;";00%%r   c                 6    | j         | j         j        S t          r%   )r3   r)   r   r&   s    r   lower_nodes_and_weightsz'NestedFixedRule.lower_nodes_and_weights)  s    :!://%%r   r   c                 >   | j         \  }}| j        \  }}| j        t          |          | _        | j                            ||gd          }	| j                            || gd          }
| j                            t          ||||	|
|| j                            S )a  
        Estimate the error of the approximation for the integral of `f` in rectangular
        region described by corners `a` and `b`.

        Parameters
        ----------
        f : callable
            Function to estimate error for. `f` must have the signature::
                f(x : ndarray, \*args) -> ndarray

            `f` should accept arrays `x` of shape::
                (npoints, ndim)

            and output arrays of shape::
                (npoints, output_dim_1, ..., output_dim_n)

            In this case, `estimate` will return arrays of shape::
                (output_dim_1, ..., output_dim_n)
        a, b : ndarray
            Lower and upper limits of integration as rank-1 arrays specifying the left
            and right endpoints of the intervals being integrated over. Infinite limits
            are currently not supported.
        args : tuple, optional
            Additional positional args passed to `f`, if any.

        Returns
        -------
        err_est : ndarray
            Result of error estimation. If `f` returns arrays of shape
            ``(npoints, output_dim_1, ..., output_dim_n)``, then `est` will be
            of shape ``(output_dim_1, ..., output_dim_n)``.
        Nr   axis)r)   r6   r   r   concatr   r+   )r   r   r   r   r   r,   r-   lower_nodeslower_weightserror_nodeserror_weightss              r   r   zNestedFixedRule.estimate_error0  s    D /w%)%A"]7?%e,,DGgnne[%9nBB-'@qIIw{{aA{M4QQ
 
 	
r   Nr   )	r   r   r    r!   r'   r.   r)   r6   r   r   r   r   r0   r0      sx        % %N  
 & & X& & & X&-
 -
 -
 -
 -
 -
r   r0   c                   D    e Zd ZdZd Zed             Zed             ZdS )ProductNestedFixeda`  
    Find the n-dimensional cubature rule constructed from the Cartesian product of 1-D
    `NestedFixedRule` quadrature rules.

    Given a list of N 1-dimensional quadrature rules which support error estimation
    using NestedFixedRule, this will find the N-dimensional cubature rule obtained by
    taking the Cartesian product of their nodes, and estimating the error by taking the
    difference with a lower-accuracy N-dimensional cubature rule obtained using the
    ``.lower_nodes_and_weights`` rule in each of the base 1-dimensional rules.

    Parameters
    ----------
    base_rules : list of NestedFixedRule
        List of base 1-dimensional `NestedFixedRule` quadrature rules.

    Attributes
    ----------
    base_rules : list of NestedFixedRule
        List of base 1-dimensional `NestedFixedRule` qudarature rules.

    Examples
    --------

    Evaluate a 2D integral by taking the product of two 1D rules:

    >>> import numpy as np
    >>> from scipy.integrate import cubature
    >>> from scipy.integrate._rules import (
    ...  ProductNestedFixed, GaussKronrodQuadrature
    ... )
    >>> def f(x):
    ...     # f(x) = cos(x_1) + cos(x_2)
    ...     return np.sum(np.cos(x), axis=-1)
    >>> rule = ProductNestedFixed(
    ...     [GaussKronrodQuadrature(15), GaussKronrodQuadrature(15)]
    ... ) # Use 15-point Gauss-Kronrod, which implements NestedFixedRule
    >>> a, b = np.array([0, 0]), np.array([1, 1])
    >>> rule.estimate(f, a, b) # True value 2*sin(1), approximately 1.6829
     np.float64(1.682941969615793)
    >>> rule.estimate_error(f, a, b)
     np.float64(2.220446049250313e-16)
    c                 t    |D ]&}t          |t                    st          d          '|| _        d | _        d S )Nz<base rules for product need to be instance ofNestedFixedRule)
isinstancer0   
ValueError
base_rulesr   )r   rD   rules      r   r'   zProductNestedFixed.__init__  sS     	4 	4DdO44 4  "3 4 4 44 %r   c                     t          d | j        D                       }| j        t          |          | _        | j                            t          d | j        D                       d          }||fS )Nc                 (    g | ]}|j         d          S r   r)   .0rE   s     r   
<listcomp>z8ProductNestedFixed.nodes_and_weights.<locals>.<listcomp>  s     CCC4T#A&CCCr   c                 (    g | ]}|j         d          S    rI   rJ   s     r   rL   z8ProductNestedFixed.nodes_and_weights.<locals>.<listcomp>  s     GGGt'*GGGr   r8   _cartesian_productrD   r   r   prodr   r,   r-   s      r   r)   z$ProductNestedFixed.nodes_and_weights  s    "CC4?CCC
 
 7?%e,,DG',,GGtGGG  	  
 
 g~r   c                     t          d | j        D                       }| j        t          |          | _        | j                            t          d | j        D                       d          }||fS )Nc                 (    g | ]}|j         d          S rH   r6   rK   cubatures     r   rL   z>ProductNestedFixed.lower_nodes_and_weights.<locals>.<listcomp>  s     QQQXX-a0QQQr   c                 (    g | ]}|j         d          S rN   rW   rX   s     r   rL   z>ProductNestedFixed.lower_nodes_and_weights.<locals>.<listcomp>  s     UUU1!4UUUr   rP   r8   rQ   rT   s      r   r6   z*ProductNestedFixed.lower_nodes_and_weights  s    "QQQQQ
 
 7?%e,,DG',,UUT_UUU  	  
 
 g~r   N)r   r   r    r!   r'   r   r)   r6   r   r   r   r@   r@   `  sd        ) )V     _"   _  r   r@   c                     t          |  } |j        | ddi}|                    |                    |d          dt	          |           f          }|S )NindexingijrP   r8   )r   meshgridreshapestacklen)arraysr   	arrays_ixresults       r   rR   rR     sU    	&	!BV3d33IZZ44r3v;;6GHHFMr   Nc              #      K   t                      z   dz   fdt           j        d                   D             }fdt          j        d                   D             }t          |          }t          |          }t          |j        d                   D ]}||df         ||df         fV  dS )a
  
    Given the coordinates of a region like a=[0, 0] and b=[1, 1], yield the coordinates
    of all subregions, which in this case would be::

        ([0, 0], [1/2, 1/2]),
        ([0, 1/2], [1/2, 1]),
        ([1/2, 0], [1, 1/2]),
        ([1/2, 1/2], [1, 1])
    N   c                 V    g | ]%}                     |         |         g          &S r   asarray)rK   ir   split_atr   s     r   rL   z$_split_subregion.<locals>.<listcomp>  s2    GGGBJJ!hqk*++GGGr   r   c                 V    g | ]%}                     |         |         g          &S r   rh   )rK   rj   r   rk   r   s     r   rL   z$_split_subregion.<locals>.<listcomp>  s2    HHHRZZ!ad+,,HHHr   .)r   rangeshaperR   )	r   r   r   rk   leftrighta_subb_subrj   s	   ````     r   r   r     s       
A		BEQ;GGGGGGU171:5F5FGGGDHHHHHHeAGAJ6G6GHHHEt$$Eu%%E5;q>"" + +AsFmU1c6]*****+ +r   c                 6   |j         }|                    ||          }|                    ||          }|j        dk    r|d d d f         }|j        d         }t	          |          }	t	          |          }
||	k    s||
k    rt          d| d|	 d|
           ||z
  }|dz   |dz  z  |z   }|                    ||          d|z  z  }||z  } | |g|R  }|                    |dgdg|j        dz
  z  R           }|                    ||z  d	|
          }|S )NrO   rP   z@rule and function are of incompatible dimension, nodes havendim z,, while limit of integration has ndima_ndim=z	, b_ndim=g      ?)dtyperf   r   )r9   rt   )	rt   astypendimrn   r   rC   rS   r_   sum)r   r   r   
orig_nodesorig_weightsr   r   result_dtype	rule_ndima_ndimb_ndimlengthsr,   weight_scale_factorr-   f_nodesweights_reshapedr   s                     r   r+   r+     sz   7L:|44J99\<88L !4(
 $IQZZFQZZFFi611 =!*= =#)= =4:= = > > 	> !eG !^#.2E ''''>>IM00GaooooGzz'B+L1#9I2J+L+LMM
 &&!G+!<&
H
HCJr   r%   )scipy._lib._array_apir   r   	functoolsr   r   r#   r0   r@   rR   r   r+   r   r   r   <module>r      s=   : : : : : : : : % % % % % %Q. Q. Q. Q. Q. Q. Q. Q.hXI XI XI XI XI XI XI XIvh
 h
 h
 h
 h
i h
 h
 h
VW W W W W W W Wt  + + + +2* * * * *r   