
    ^Mh|'                     j    d dl Z d dlZd dlmZmZ d dlmZm	Z
  G d d          Z G d d          ZdS )    N)assert_equalassert_allclose)	_iv_ratio_iv_ratio_cc                      e Zd Zej                            dg d          d             Zej                            ddej        dfej        ddfg          d             Z	ej                            ddej         ej
        ej        g          ej                            d	 ej        e          j          ej        e          j         ej         ej
        ej        g          d
                         Zej                            ddd ej        e          j        ej        g          d             Zej                            dd ej        e          j        fd ej        e          j        fd ej        e          j        dz  fd ej        e          j        df ej        e          j         ej         ej        e          j                  fg          d             Zej                            ddd ej         ej        e          j                   ej        e          j        fg          d             Zej                            d ej        e          j         ej        e          j        f ej        e          j        dz   ej        e          j        f ej        e          j         ej        e          j        dz  fg          d             ZdS )TestIvRatiov,x,r))      ?UUUUUU?g.a0R#?)r
   UUUUUU?g<)?)r
   r
   gVS?)r
   UUUUUU?gP]k(?)r
   笪?gjD?)   6Z5Z?g&R͒U?)r   窪?gZ?)r   竪?gZr!?)r   ?g4e~u?)r   }|@gG)ȿ?)Q@}P?g1a?)r   j6i?gִN`?)r   :m@g9Ƭ7?)r   5T@g4+?)r   翎H%@gJ]?)EdL@9L;w3@g'~V?)r   ^s!iFE@g/X?)r   SR@g_8?)r   PT`@g )X?)r   >=s@g\h*?c                 H    t          t          ||          |dd           dS )a  The reference values are computed using mpmath as follows.

        from mpmath import mp
        mp.dps = 100

        def iv_ratio_mp(v, x):
            return mp.besseli(v, x) / mp.besseli(v - 1, x)

        def _sample(n, *, v):
            '''Return n positive real numbers x such that iv_ratio(v, x) are
            roughly evenly spaced over (0, 1).  The formula is taken from [1].

            [1] Banerjee A., Dhillon, I. S., Ghosh, J., Sra, S. (2005).
                "Clustering on the Unit Hypersphere using von Mises-Fisher
                Distributions."  Journal of Machine Learning Research,
                6(46):1345-1382.
            '''
            r = np.arange(1, n+1) / (n+1)
            return r * (2*v-r*r) / (1-r*r)

        for v in (0.5, 1, 2.34, 56.789):
            xs = _sample(5, v=v)
            for x in xs:
                print(f"({v}, {x}, {float(iv_ratio_mp(v,x))}),")
        缉ؗҼ<r   rtolatolN)r   iv_ratioselfvxrs       a/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/scipy/special/tests/test_iv_ratio.pytest_against_reference_valuesz)TestIvRatio.test_against_reference_values   s*    ` 	AA>>>>>>    r   r   c                 B    t          t          ||          |           dS ziIf exactly one of v or x is inf and the other is within domain,
        should return 0 or 1 accordingly.Nr   r&   r'   s       r,   test_infzTestIvRatio.test_inf@   s"     	Xa^^Q'''''r.   r)   \(\?r*   c                 V    t          t          ||          t          j                   dS zeIf at least one argument is out of domain, or if v = x = inf,
        the function should return nan.N)r   r&   npnanr(   r)   r*   s      r,   test_nanzTestIvRatio.test_nanI   s$     	Xa^^RV,,,,,r.   r
   c                 ~    t          t          |d          d           t          t          |d          d           dS )z?If x is +/-0.0, return x to ensure iv_ratio is an odd function.               Nr1   r(   r)   s     r,   test_zero_xzTestIvRatio.test_zero_xR   s>     	Xa%%s+++Xa&&-----r.   v,x   @xD{   c                 N    t          t          ||          d|z  |z             dS )a9  If x is much less than v, the bounds

                    x                                 x
        --------------------------- <= R <= -----------------------
        v-0.5+sqrt(x**2+(v+0.5)**2)         v-1+sqrt(x**2+(v+1)**2)

        collapses to R ~= x/2v.  Test against this asymptotic expression.
        r
   Nr1   r8   s      r,   test_tiny_xzTestIvRatio.test_tiny_xX   s*    " 	Xa^^c!eQY/////r.   r   g 7yACrB   g\)c=Hc                 B    t          t          ||          d           dS )aA  If x is much greater than v, the bounds

                    x                                 x
        --------------------------- <= R <= ---------------------------
        v-0.5+sqrt(x**2+(v+0.5)**2)         v-0.5+sqrt(x**2+(v-0.5)**2)

        collapses to R ~= 1.  Test against this asymptotic expression.
              ?Nr1   r8   s      r,   test_huge_xzTestIvRatio.test_huge_xk   s"     	Xa^^S)))))r.      c                     ||z  }|dt          j        d|          z   z  }t          t          ||          |dd           dS )a  If both x and v are very large, the bounds

                    x                                 x
        --------------------------- <= R <= -----------------------
        v-0.5+sqrt(x**2+(v+0.5)**2)         v-1+sqrt(x**2+(v+1)**2)

        collapses to R ~= x/(v+sqrt(x**2+v**2).  Test against this asymptotic
        expression, and in particular that no numerical overflow occurs during
        intermediate calculations.
        r   r"   r   r#   N)r6   hypotr   r&   r(   r)   r*   texpecteds        r,   test_huge_v_xzTestIvRatio.test_huge_v_x{   sK      EBHQNN*+Au1EEEEEEr.   N__name__
__module____qualname__pytestmarkparametrizer-   r6   infr2   r7   finfofloatsmallest_normalsmallest_subnormalr9   maxr>   sqrtrE   rJ   rQ    r.   r,   r   r      sY       [W ' ' '  ,? ?- ,?8 [W	
BFA	A'  ( (	 (
 [S4"&"&"&"ABB[SHBHUOO$C#C$,BHUOO$F#F$&F7BFBF#< = =- -= = CB-
 [S3828E??+>"GHH. . IH.
 [U	
HBHUOO+,	
HBHUOO./	
HBHUOO.q01	%	a 	%	gbghbhuoo&9::;%  	0 	0 	0 [U	%$	%	%xrx':;%  
	* 	* 
	* [U	%	hbhuoo12	%	q	 ("(5//"56	%	hbhuoo1A56%  
F F 
F F Fr.   r   c                      e Zd Zej                            dg d          d             Zej                            ddej        dfej        ddfg          d             Z	ej                            ddej         ej
        ej        g          ej                            d	 ej        e          j          ej        e          j         ej         ej
        ej        g          d
                         Zej                            ddd ej        e          j        ej        g          d             Zej                            dd ej        e          j        fd ej        e          j        fd ej        e          j        dz  fd ej        e          j        df ej        e          j         ej         ej        e          j                  fg          d             Zej                            ddd ej         ej        e          j                   ej        e          j        fg          d             Zej                            d ej        e          j         ej        e          j        f ej        e          j        dz   ej        e          j        f ej        e          j         ej        e          j        dz  fg          d             ZdS )TestIvRatioCr	   ))r
   r   g{s+?)r
   r   ga*?)r
   r
   gTV6?)r
   r   g`D)?)r
   r   g,wU?)r   r   gvL[?)r   r   g>7R?)r   r   gL?)r   r   g5?)r   r   gZ ?)r   r   g3zʈ?)r   r   gؤO??)r   r   gsF?)r   r   g1?)r   r   gL9Ԋ?)r   r   gCv`?)r   r   g
-S?)r   r   g@ɣ?)r   r   gO?)r   r    g^VO?c                 H    t          t          ||          |dd           dS )z8The reference values are one minus those of TestIvRatio.V瞯<r   r#   Nr   
iv_ratio_cr'   s       r,   r-   z*TestIvRatioC.test_against_reference_values   s+    0 	
1a((!%a@@@@@@r.   r   r   c                 B    t          t          ||          |           dS r0   r   rf   r'   s       r,   r2   zTestIvRatioC.test_inf   s$     	Z1%%q)))))r.   r)   r3   r*   c                 V    t          t          ||          t          j                   dS r5   )r   rf   r6   r7   r8   s      r,   r9   zTestIvRatioC.test_nan   s&     	Z1%%rv.....r.   r
   c                 ~    t          t          |d          d           t          t          |d          d           dS )zIf x is +/-0.0, return 1.r;   rI   r<   Nrh   r=   s     r,   r>   zTestIvRatioC.test_zero_x   s>     	Z3''---Z4((#.....r.   r?   r@   rA   c                 T    t          t          ||          dd|z  |z  z
             dS )a=  If x is much less than v, the bounds

                    x                                 x
        --------------------------- <= R <= -----------------------
        v-0.5+sqrt(x**2+(v+0.5)**2)         v-1+sqrt(x**2+(v+1)**2)

        collapses to 1-R ~= 1-x/2v.  Test against this asymptotic expression.
        rI   r
   Nrh   r8   s      r,   rE   zTestIvRatioC.test_tiny_x   s0    " 	Z1%%sCE19}55555r.   rF   rG   c                 T    t          t          ||          |dz
  |z  dd           dS )aK  If x is much greater than v, the bounds

                    x                                 x
        --------------------------- <= R <= ---------------------------
        v-0.5+sqrt(x**2+(v+0.5)**2)         v-0.5+sqrt(x**2+(v-0.5)**2)

        collapses to 1-R ~= (v-0.5)/x.  Test against this asymptotic expression.
        r
   rd   r   r#   Nre   r8   s      r,   rJ   zTestIvRatioC.test_huge_x   s3     	
1a((1S5!)%aHHHHHHr.   rK   c                     ||z  }d|dt          j        d|          z   z  z
  }t          t          ||          |dd           dS )a  If both x and v are very large, the bounds

                    x                                 x
        --------------------------- <= R <= -----------------------
        v-0.5+sqrt(x**2+(v+0.5)**2)         v-1+sqrt(x**2+(v+1)**2)

        collapses to 1 - R ~= 1 - x/(v+sqrt(x**2+v**2).  Test against this
        asymptotic expression, and in particular that no numerical overflow
        occurs during intermediate calculations.
        r   r"   r   r#   N)r6   rM   r   rf   rN   s        r,   rQ   zTestIvRatioC.test_huge_v_x   sR      EqAA.//
1a(((QGGGGGGr.   NrR   r`   r.   r,   rb   rb      s_       [W ' ' '  ,A A- ,A [W	
BFA	A'  * *	 *
 [S4"&"&"&"ABB[SHBHUOO$C#C$,BHUOO$F#F$&F7BFBF#< = =/ /= = CB/
 [S3828E??+>"GHH/ / IH/
 [U	
HBHUOO+,	
HBHUOO./	
HBHUOO.q01	%	a 	%	gbghbhuoo&9::;%  	6 	6 	6 [U	%$	%	%xrx':;%  
	I 	I 
	I [U	%	hbhuoo12	%	q	 ("(5//"56	%	hbhuoo1A56%  
H H 
H H Hr.   rb   )rV   numpyr6   numpy.testingr   r   scipy.special._ufuncsr   r&   r   rf   r   rb   r`   r.   r,   <module>rq      s         7 7 7 7 7 7 7 7       AF AF AF AF AF AF AF AFHiH iH iH iH iH iH iH iH iH iHr.   