
    Џkh                     X    d dl Z d dlZd dlmZ d dlmZmZ d dlmZ  G d de      Z	d Z
y)    N)cached_property)array_namespace	np_compat)NestedFixedRulec                   8    e Zd ZdZddZed        Zed        Zy)GenzMalikCubaturea  
    Genz-Malik cubature.

    Genz-Malik is only defined for integrals of dimension >= 2.

    Parameters
    ----------
    ndim : int
        The spatial dimension of the integrand.

    xp : array_namespace, optional
        The namespace for the node and weight arrays. Default is None, where NumPy is
        used.

    Attributes
    ----------
    higher : Cubature
        Higher-order rule.

    lower : Cubature
        Lower-order rule.

    References
    ----------
    .. [1] A.C. Genz, A.A. Malik, Remarks on algorithm 006: An adaptive algorithm for
        numerical integration over an N-dimensional rectangular region, Journal of
        Computational and Applied Mathematics, Volume 6, Issue 4, 1980, Pages 295-302,
        ISSN 0377-0427, https://doi.org/10.1016/0771-050X(80)90039-X.

    Examples
    --------
    Evaluate a 3D integral:

    >>> import numpy as np
    >>> from scipy.integrate import cubature
    >>> from scipy.integrate._rules import GenzMalikCubature
    >>> def f(x):
    ...     # f(x) = cos(x_1) + cos(x_2) + cos(x_3)
    ...     return np.sum(np.cos(x), axis=-1)
    >>> rule = GenzMalikCubature(3) # Use 3D Genz-Malik
    >>> a, b = np.array([0, 0, 0]), np.array([1, 1, 1])
    >>> rule.estimate(f, a, b) # True value 3*sin(1), approximately 2.5244
     np.float64(2.5244129547230862)
    >>> rule.estimate_error(f, a, b)
     np.float64(1.378269656626685e-06)
    Nc                     |dk  rt        d      |dk7  s|dk7  rt        d      || _        || _        || _        |t
        }t        |j                  d            | _        y )N   z1Genz-Malik cubature is only defined for ndim >= 2      zKGenz-Malik cubature is currently only supportedfor degree=7, lower_degree=5r   )	
ValueErrorNotImplementedErrorndimdegreelower_degreer   r   emptyxp)selfr   r   r   r   s        ^/var/www/teggl/fontify/venv/lib/python3.12/site-packages/scipy/integrate/_rules/_genz_malik.py__init__zGenzMalikCubature.__init__;   sn    !8PQQQ;,!+% 'E F F 	(:B!"((1+.    c                    t        j                  d      }t        j                  d      }t        j                  d      }t        j                  d      }t        j                  d| j                  z  gt        |fd| j                  dz
  z  z         t        | fd| j                  dz
  z  z         t        |fd| j                  dz
  z  z         t        | fd| j                  dz
  z  z         t        ||fd| j                  dz
  z  z         t        || fd| j                  dz
  z  z         t        | | fd| j                  dz
  z  z         t        j                  || f| j                        	      }dd| j                  dz   z  | j                  z  z   d| j                  z  z   }| j                  j                  t        t        |       | j                  j                        }| j                  j                  || j                  |f      }|j                  }d| j                  z  d	d
| j                  z  z
  d| j                  dz  z  z   z  dz  }d| j                  z  dz  dz  }	d| j                  z  dd| j                  z  z
  z  dz  }
d| j                  z  dz  }d}| j                  j                  | j                  j                  |gdz  | j                  j                        | j                  j                  |	gd| j                  z  z  | j                  j                        | j                  j                  |
gd| j                  z  z  | j                  j                        | j                  j                  |gd| j                  dz
  z  | j                  z  z  | j                  j                        | j                  j                  |gd| j                  z  z  | j                  j                        g      }||fS )NPuPu??g5P?r      r
   )repeatdtypei2  i#  i  iL  i  i  i  gSgQτ?gbM?)mathsqrt	itertoolschainr   _distinct_permutationsproductr   asarraylistzipfloat64reshapeTconcat)r   l_2l_3l_4l_5its
nodes_sizenodesw_1w_2w_3w_4w_5weightss                 r   nodes_and_weightsz#GenzMalikCubature.nodes_and_weightsL   s`    iioiioiioiioooDII"C6DDIIM,B#BC"SD7TTYY]-C#CD"C6DDIIM,B#BC"SD7TTYY]-C#CD"C:		A0F#FG"C#;Q1G#GH"SD3$<$$))a-2H#HIsSDk$))<

 !tyy1}-		9:Q		\I
cO''//   

 		:'>?
  		\ed499n4diil8JKLuT 	 $))|s"4'$))|sTYY 67%?$))|,''..GGOOSEAITWW__O=GGOOSEQ]3477??OKGGOOSEQ]3477??OKGGOOdii!m,tyy89ggoo   GGOOSEQ		\2$''//OJ	"
 	 g~r   c                    t        j                  d      }t        j                  d      }t        j                  d      }t        j                  d| j                  z  gt        |fd| j                  dz
  z  z         t        | fd| j                  dz
  z  z         t        |fd| j                  dz
  z  z         t        | fd| j                  dz
  z  z         t        ||fd| j                  dz
  z  z         t        || fd| j                  dz
  z  z         t        | | fd| j                  dz
  z  z               }dd| j                  dz   z  | j                  z  z   }| j                  j                  t        t        |       | j                  j                        }| j                  j                  || j                  |f      }|j                  }d| j                  z  dd| j                  z  z
  d	| j                  dz  z  z   z  dz  }d| j                  z  d
z  }d| j                  z  dd| j                  z  z
  z  dz  }	d| j                  z  dz  }
| j                  j                  | j                  j                  |gdz  | j                  j                        | j                  j                  |gd| j                  z  z  | j                  j                        | j                  j                  |	gd| j                  z  z  | j                  j                        | j                  j                  |
gd| j                  dz
  z  | j                  z  z  | j                  j                        g      }||fS )Nr   r   r   r   r
   r   i  i  2   gQA!?i	  d   i  g>쎡?)r    r!   r"   r#   r   r$   r   r&   r'   r(   r)   r*   r+   r,   )r   r-   r.   r/   r1   r2   r3   r4   r5   r6   r7   r9   s               r   lower_nodes_and_weightsz)GenzMalikCubature.lower_nodes_and_weights   s    iioiioiioooDII"C6DDIIM,B#BC"SD7TTYY]-C#CD"C6DDIIM,B#BC"SD7TTYY]-C#CD"C:		A0F#FG"C#;Q1G#GH"SD3$<$$))a-2H#HI	
 !tyy1}-		9:
S#YtwwG		:'>? $))|c$))m 3bAo EFL$))|	*$))|c$))m 34t;$))|)''..GGOOSEAITWW__O=GGOOSEQ]3477??OKGGOOSEQ]3477??OKGGOOdii!m,tyy89ggoo  	"
  g~r   )r   r   N)__name__
__module____qualname____doc__r   r   r:   r>    r   r   r   r      s6    -^/" 5 5n + +r   r   c              #   (  K   t        |       }t        |      }	 t        |       t        |dz
  dd      D ]  }||   ||dz      k  s n yt        |dz
  |d      D ]  }||   ||   k  s n |   ||   c||<   ||<   |d||z
  d   ||dz   d xw)zM
    Find the number of distinct permutations of elements of `iterable`.
    r
   r   N)sortedlentuplerange)iterableitemssizeijs        r   r$   r$      s      8Eu:D
El taxR( 	AQx%A,&	  taxB' 	AQx%("	 #1XuQxa%(JQtVBJ'acd+ s   AB	"B,&B)r    r"   	functoolsr   scipy._lib._array_apir   r   scipy.integrate._rulesr   r   r$   rC   r   r   <module>rR      s(      % < 2e eP(r   