
    (phQI                         S SK rS SKJrJr  S SKJrJrJr  S SK	J
r
  \r\rS rS rS rSS jrSS	 jrSS
 jrSS jrSS jrS rg)    N)fftifft)gammaincinvndtrndtri)primes_from_2_toc                     [        5       n[        [        [        R                  " U 5      5      S-   5       H4  nX-  (       d   UR                  U5        X-  n X-  (       d  M   U S:X  d  M4    O   U S:w  a  UR                  U 5        [        U5      $ )zLReturn a sorted list of the unique prime factors of a positive integer.
       )setr   intnpsqrtaddsorted)nfactorsps      E/var/www/html/venv/lib/python3.13/site-packages/scipy/stats/_qmvnt.py_factorize_intr   .   su     eGc"''!*o125KKNGA 55 6 3 	AvA'?    c                     U S-
  n[        U5      n[        U5      nSnSnXS:  aI  XU   -  n[        [        U5      [        U5      [        U 5      5      nUS:X  a  US-  nSnOUS-  nXS:  a  MI  U$ )zCompute a primitive root of the prime number `p`.

Used in the CBC lattice construction.

References
----------
.. [1] https://en.wikipedia.org/wiki/Primitive_root_modulo_n
r
      r   )r   lenpowr   )r   pmr   r   rkdrds           r   _primitive_rootr    >   s     
QBR GGA	A	A
%!*QQQ(7FAAFA % Hr   c                    [        US-   5      nUS   n[        R                  " U 5      n[        R                  " SS[        R                  " U S-
  5      -  /5      nSnSn[        R                  " SU S-   5      nUS-
  S-  n[        U5      n	[        R                  " U[        S9n
[        US-
  5       H  nXU   -  U-  XS-   '   M     [        R                  " X-
  U
5      n
X-  nX-  U-
  S-   n[        U5      n[        SU 5       H|  n[        R                  " US	US-    S	S	S2   XS-   U S	S	S2   /5      nXSUS-
     XOS-
     U-  -   -  n[        U[        U5      -  5      R                  R                  5       nX   X'   M~     Xq-  nXQ4$ )
a;  Compute a QMC lattice generator using a Fast CBC construction.

Parameters
----------
n_dim : int > 0
    The number of dimensions for the lattice.
n_qmc_samples : int > 0
    The desired number of QMC samples. This will be rounded down to the
    nearest prime to enable the CBC construction.

Returns
-------
q : float array : shape=(n_dim,)
    The lattice generator vector. All values are in the open interval
    ``(0, 1)``.
actual_n_qmc_samples : int
    The prime number of QMC samples that must be used with this lattice,
    no more, no less.

References
----------
.. [1] Nuyens, D. and Cools, R. "Fast Component-by-Component Construction,
       a Reprise for Different Kernels", In H. Niederreiter and D. Talay,
       editors, Monte-Carlo and Quasi-Monte Carlo Methods 2004,
       Springer-Verlag, 2006, 371-385.
r
         ?g?r   r   dtypegUUUUUU?N)r   r   oneshstackaranger    r   rangeminimumr   r   realargmin)n_dimn_qmc_samplesprimesbtgmqwzmgpermjpncfcs	reordereds                    r   _cbc_latticer>   Y   s   8 ma/0F2JM	B	C		%!) 445	6B	A	A
		!UQYA		q A&A 771C D1q5\7{m3U ::m*D1D		B
"wA	QB1e_IIdqsGDbDMc!HTrTN
 	 AaC2c7Y../c!f""))+w  	
Ar   c                    [        U5      nSn	US:X  a  [        U5      [        U5      -
  n
SnO[        XhS-  5      nSn
SnSnX:  av  X:  aq  [        [        R
                  " S5      U-  5      nU " XX#4SU0UD6u  pnX-  n	SSX-  S-  -   -  nU
UX-
  -  -  n
[        R
                  " U5      U-  nX:  a  X:  a  Mq  XU	4$ )	aP  Automatically rerun the integration to get the required error bound.

Parameters
----------
func : callable
    Either :func:`_qmvn` or :func:`_qmvt`.
covar, low, high : array
    As specified in :func:`_qmvn` and :func:`_qmvt`.
rng : Generator, optional
    default_rng(), yada, yada
error : float > 0
    The desired error bound.
limit : int > 0:
    The rough limit of the number of integration points to consider. The
    integration will stop looping once this limit has been *exceeded*.
**kwds :
    Other keyword arguments to pass to `func`. When using :func:`_qmvt`, be
    sure to include ``nu=`` as one of these.

Returns
-------
prob : float
    The estimated probability mass within the bounds.
est_error : float
    3 times the standard error of the batch estimates.
n_samples : int
    The number of integration points actually used.
r   r
   gV瞯<i          r#   r   rng)r   phiminroundr   r   )funccovarlowhighrA   errorlimitkwdsr   	n_samplesprob	est_errormieipiniwts                    r   _qautorT      s    : 	E
AIAv4y3s8#	D!	I$5rwwqzB'BbDDtDJBBOIR^a//0BB")$$Db(I I$5 I%%r   c                    [        XU5      u  pxn	UR                  S   n
US   n[        US   U-  5      n[        U	S   U-  5      nUnX-
  nSnSn[        U
S-
  [	        X-  S5      5      u  nn[
        R                  " U
S-
  U45      n[
        R                  " U5      S-   n[        U5       GH3  n[
        R                  " UU5      n[
        R                  " UU5      nUR                  5       n[        SU
5       H  nUUS-
     U-  UR                  5       -   nUUR                  [        5      -  n[        SU-  S-
  5      n[        UUU-  -   5      UUS-
  SS24'   UUSU24   USU2SS24   -  nUUU4   n[        UU   U-
  U-  5      n[        U	U   U-
  U-  5      nX-
  nUU-  nM     UR!                  5       U-
  US-   -  nUU-  nUS-
  U-  US-   -  X-  -   nGM6     S[
        R"                  " U5      -  nUU-  nUUU4$ )aI  Multivariate normal integration over box bounds.

Parameters
----------
m : int > n_batches
    The number of points to sample. This number will be divided into
    `n_batches` batches that apply random offsets of the sampling lattice
    for each batch in order to estimate the error.
covar : (n, n) float array
    Possibly singular, positive semidefinite symmetric covariance matrix.
low, high : (n,) float array
    The low and high integration bounds.
rng : Generator, optional
    default_rng(), yada, yada
lattice : 'cbc' or callable
    The type of lattice rule to use to construct the integration points.
n_batches : int > 0, optional
    The number of QMC batches to apply.

Returns
-------
prob : float
    The estimated probability mass within the bounds.
est_error : float
    3 times the standard error of the batch estimates.
r   r   r   r@   r
   r   N   )_permuted_choleskyshaperB   r>   maxr   zerosr(   r)   fullcopyrandomastyper   absphinvmeanr   )r5   rF   rG   rH   rA   lattice	n_batchescholohir   ctr:   r   cidcirM   	error_varr2   r.   y	i_samplesr8   dcpvir4   xr<   rN   rL   s                                  r   _qmvnrr      s-   6 %U6KCR		!A	TBBqEBJABqEBJA	
B
&CDI#AE3q~q+ABA}
!a%'(A		-(1,I9GGM2&WW]C(WWYq!A!a%9$szz|3A#AAEAIAAF
+Aa!eQhKArrE
Qrr1uX%AQTBRUQY"$%ARUQY"$%ABbB  WWY!a%(	Ui'1q51AE9	+ . BGGI&&I	)II%%r   c                    ^^^	^
^^^^ [        XU5      u  mmmTR                  S   mTS-
  mTS   n[        TS   U-  5      n[        TS   U-  5      nUm	UT	-
  m
UU	U
UUUUU4S jnUT4$ )a  Transform the multivariate normal integration into a QMC integrand over
a unit hypercube.

The dimensionality of the resulting hypercube integration domain is one
less than the dimensionality of the original integrand. Note that this
transformation subsumes the integration bounds in order to account for
infinite bounds. The QMC integration one does with the returned integrand
should be on the unit hypercube.

Parameters
----------
covar : (n, n) float array
    Possibly singular, positive semidefinite symmetric covariance matrix.
low, high : (n,) float array
    The low and high integration bounds.
use_tent : bool, optional
    If True, then use tent periodization. Only helpful for lattice rules.

Returns
-------
integrand : Callable[[NDArray], NDArray]
    The QMC-integrable integrand. It takes an
    ``(n_qmc_samples, ndim_integrand)`` array of QMC samples in the unit
    hypercube and returns the ``(n_qmc_samples,)`` evaluations of at these
    QMC points.
ndim_integrand : int
    The dimensionality of the integrand. Equal to ``n-1``.
r   r
   rV   c                  T  > [        U 5      n[        [        R                  " U S   5      5      nUT:X  d   e[        R                  " X45      n[        R                  " UT5      n[        R                  " UT5      nUR                  5       n[        ST5       H  nT(       a  [        SXS-
     -  S-
  5      nOXS-
     n[        XHU-  -   5      X7S-
  S S 24'   TUS U24   US U2S S 24   -  n	TXw4   n
[        TU   U	-
  U
-  5      n[        TU   U	-
  U
-  5      nX-
  nXe-  nM     U$ )Nr   r
   r   )
r   r   
atleast_1dr[   r\   r]   r)   r`   ra   rB   )zsndim_qmcr.   rl   r:   rn   ro   rp   rq   r<   rh   r   re   ri   rj   rg   rf   r   ndim_integranduse_tents               r   	integrand%_mvn_qmc_integrand.<locals>.integrand8  s-   r7BMM"Q%01>)))HHh./GGM2&WW]C(WWYq!ABsGa(sGF
+A!eQhKArrE
Qrr1uX%AQTBRUQY"$%ARUQY"$%ABB  	r   )rX   rY   rB   )rF   rG   rH   ry   rh   r:   r   rz   re   ri   rj   rg   rf   r   rx   s      `    @@@@@@@r   _mvn_qmc_integrandr|     s}    : %U6KCR		!AUN	TBBqEBJABqEBJA	
B
b&C . n$$r   c           
      ~   [        S[        R                  " U5      5      n[        R                  " U[        R                  S9n[        R                  " U[        R                  S9n[        X#U-  XH-  5      u  pnU	R                  S   nSnSn[        U[        X-  S5      5      u  nn[        R                  " U5      S-   n[        U5       GH'  n[        R                  " U5      n[        R                  " UU45      n[        U5       GH  nUU   U-  UR                  5       -   nUUR                  [        5      -  n[        SU-  S-
  5      nUS:X  aD  US:  a'  [        R                  " S[!        US-  U5      -  5      nOz[        R"                  " U5      nOc[%        WUW-  -   5      n[        R&                  " SS9   UUS	2S	S	24==   U	US	2US-
  4   S	S	2[        R(                  4   U-  -  ss'   S	S	S	5        UUS	S	24   n[        R                  " U5      n[        R                  " U5      n[        R&                  " SS9   U
U   W-  U-
  nUU   U-  U-
  nS	S	S	5        SUWS
:  '   SUWS
:  '   [        U5      S:  n [        U5      S:  n![+        UU    5      UU '   [+        UU!   5      UU!'   UU-
  nUU-  nGM     UR-                  5       U-
  US-   -  nUU-  nUS-
  U-  US-   -  UU-  -   nGM*     S[        R                  " U5      -  n"UU-  n#UU"U#4$ ! , (       d  f       GN.= f! , (       d  f       N= f)a  Multivariate t integration over box bounds.

Parameters
----------
m : int > n_batches
    The number of points to sample. This number will be divided into
    `n_batches` batches that apply random offsets of the sampling lattice
    for each batch in order to estimate the error.
nu : float >= 0
    The shape parameter of the multivariate t distribution.
covar : (n, n) float array
    Possibly singular, positive semidefinite symmetric covariance matrix.
low, high : (n,) float array
    The low and high integration bounds.
rng : Generator, optional
    default_rng(), yada, yada
lattice : 'cbc' or callable
    The type of lattice rule to use to construct the integration points.
n_batches : int > 0, optional
    The number of QMC batches to apply.

Returns
-------
prob : float
    The estimated probability mass within the bounds.
est_error : float
    3 times the standard error of the batch estimates.
n_samples : int
    The number of samples actually used.
r#   r$   r   r@   r
   r   ignore)invalidNi	   rW   )rZ   r   r   asarrayfloat64rX   rY   r>   r(   r)   r&   r[   r^   r_   r   r`   r   	ones_likera   errstatenewaxisrB   rb   )$r5   nurF   rG   rH   rA   rc   rd   snre   rf   rg   r   rM   rk   r2   r.   rm   r8   ro   r<   rp   r4   rq   r   r:   rn   rl   sir   loishiislo_maskhi_maskrN   rL   s$                                       r   _qmvtr   R  s   > 
S"''"+	B
**S


+C::d"**-D$U"Hdi@KCR		!ADI#As1>1'=>A}		-(1,I9WW]#HHa'(qA!y 3::</A#AAEAIA Av 6KQ$: :;AQA!a"f*%[[2ab!eHABAIq"**} = AAH 31a4B&A&AX.!uqy2~!uqy2~ / AdRiLAdRiL$i!mG$i!mGT']+AgJT']+AgJQB"HBG L WWY!a%(	Ui'1q51AE9	W Z BGGI&&I	)II%%5 32 /.s   ?6LL.
L+.
L<c           
      *   [         R                  " U [         R                  S9n[         R                  " U[         R                  S9n[         R                  " U[         R                  S9nUR                  S   nUR                  Xw4:w  a  [	        S5      eUR                  U4:w  d  UR                  U4:w  a  [	        S5      e[         R
                  " [         R                  " [         R                  " U5      S5      5      nSXS:H  '   XX-  nXh-  nXH-  nXHSS2[         R                  4   -  n[         R                  " U5      n	[         R
                  " S[         R                  -  5      n
[        U5       GH  nUS	-   U-  nUnSnSnSnSnSn[        X5       H  nUUU4   U:  d  M  [         R
                  " UUU4   5      nUS:  a  UUSU24   U	SU -  nUU   U-
  U-  nUU   U-
  U-  n[        U5      [        U5      -
  nUU::  d  Mu  UnUnUnUnUnM     X:  a  XKU4   XMU4'   [        U[         R                  USU24   [         R                  USU24   5        [        U[         R                  US	-   S2U4   [         R                  US	-   S2U4   5        [        U[         R                  US	-   U2U4   [         R                  XS	-   U24   5        [        X[U5        [        XkU5        X:  a  XX4'   SXKUS	-   S24'   [        US	-   U5       H=  nUUU4==   U-  ss'   UUUS	-   US	-   24==   UUU4   XKS	-   US	-   2U4   -  -  ss'   M?     [        U5      U:  aC  [         R                   " U* U-  S-  5      [         R                   " U* U-  S-  5      -
  X-  -  X'   OUU-   S-  X'   US
:  a  UX'   O
US:  a  UX'   XKSUS	-   24==   U-  ss'   X[==   U-  ss'   Xk==   U-  ss'   GM  SXKS2U4'   X[   Xk   -   S-  X'   GM     XEU4$ )a+  Compute a scaled, permuted Cholesky factor, with integration bounds.

The scaling and permuting of the dimensions accomplishes part of the
transformation of the original integration problem into a more numerically
tractable form. The lower-triangular Cholesky factor will then be used in
the subsequent integration. The integration bounds will be scaled and
permuted as well.

Parameters
----------
covar : (n, n) float array
    Possibly singular, positive semidefinite symmetric covariance matrix.
low, high : (n,) float array
    The low and high integration bounds.
tol : float, optional
    The singularity tolerance.

Returns
-------
cho : (n, n) float array
    Lower Cholesky factor, scaled and permuted.
new_low, new_high : (n,) float array
    The scaled and permuted low and high integration bounds.
r$   r   z!expected a square symmetric arrayzLexpected integration boundaries the same dimensions as the covariance matrixr@   r#   Nr   r
   i
   )r   arrayr   rY   
ValueErrorr   maximumdiagr   r[   rQ   r)   rB   _swap_slicess_r`   exp)rF   rG   rH   tolre   new_lonew_hir   rn   rl   sqtpr   epkimckdemr<   lo_mhi_mrp   ri   lo_ihi_ides                           r   rX   rX     s   4 ((5


+CXXc,FXXd"**-F		!A
yyQF<==||tv||t3'
 	

 
BGGCL#.	/BBSyM
LF
LFICamC
A771ruu9D1X1umqA1a4y3WWSAY'q5ArrE
QrU*Aq	A+q	A+YT*9BCDDB  6d)CBKbeeBFmRUU1bqb5\:beeBFGRK0"%%Q
2CDbeeAE"HaK0"%%E"H2EFB'B'8I C1q56	N1q5!_AqD	R	Aq1uQU{N#s1a4y31uQU{A~3F'FF# % 3x#~q 01BFFD54<!;K4LL% tq(#:ADBYAD6AE6	Nb NIOIIOICAJI	)Q.ADe f r   c                 X    X   R                  5       nX   R                  5       X'   X0U'   g )N)r]   )rq   slc1slc2ts       r   r   r     s%    	AgllnAGdGr   )gMbP?i'  )cbcr   )F)g|=)numpyr   	scipy.fftr   r   scipy.specialr   r   r   scipy.stats._qmcr   rB   ra   r   r    r>   rT   rr   r|   r   rX   r    r   r   <module>r      sY   F   2 2 -  68|/&j@&L=%@W&tcLr   