
    tKg                     D    d dl Z d dlZd dlmZmZ d dlmZ  G d d      Z	y)    N)assert_equalassert_allclose)	_iv_ratioc                   2   e Zd Zej                  j                  dg d      d        Zej                  j                  ddej                  dfej                  ddfg      d        Z	ej                  j                  d ej                  dd      ej                   ej                  ej                  g      ej                  j                  d ej                  e      j                    ej                  e      j                   ej                   ej                  ej                  g      d	               Zej                  j                  dd ej                  e      j"                  ej                  g      d
        Zej                  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                  j                  ddd ej&                   ej                  e      j"                         ej                  e      j"                  fg      d        Zej                  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y)TestIvRatiozv,x,r))   g6Z5Z?g&R͒U?)r   g?gZ?)r   g?gZr!?)r   g?g4e~u?)r   g}|@gG)ȿ?)Q@g}P?g1a?)r	   gj6i?gִN`?)r	   g:m@g9Ƭ7?)r	   g5T@g4+?)r	   gH%@gJ]?)EdL@g9L;w3@g'~V?)r
   g^s!iFE@g/X?)r
   gSR@g_8?)r
   gPT`@g )X?)r
   g>=s@g\h*?c                 6    t        t        ||      |dd       y)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 (1, 2.34, 56.789):
            xs = _sample(5, v=v)
            for x in xs:
                print(f"({v}, {x}, {float(iv_ratio_mp_float(v,x))}),")
        缉ؗҼ<r   rtolatolN)r   iv_ratioselfvxrs       e/home/alanp/www/video.onchill/myenv/lib/python3.12/site-packages/scipy/special/tests/test_iv_ratio.pytest_against_reference_valuesz)TestIvRatio.test_against_reference_values   s    V 	AA>    r   r   c                 0    t        t        ||      |       y)zIf exactly one of v or x is inf and the other is within domain,
        should return 0 or 1 accordingly.

        Also check that the function
        never returns -0.0.Nr   r   r   s       r   test_infzTestIvRatio.test_inf8   s     	Xa^Q'r   r   r   c                 L    t        t        ||      t        j                         y)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_nanD   s     	Xa^RVV,r   c                 \    t        t        |d      d       t        t        |d      d       y)z=If x is +/-0.0, return x to agree with the limiting behavior.g        g       Nr   )r   r   s     r   test_zero_xzTestIvRatio.test_zero_xM   s&     	Xa%s+Xa&-r   zv,x   )@xD{   c                 <    t        t        ||      d|z  |z         y)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.
        g      ?Nr   r   s      r   test_tiny_xzTestIvRatio.test_tiny_xS   s    " 	Xa^c!eQY/r   )r   g 7yAC)r$   g\)c=Hc                 0    t        t        ||      d       y)a9  If x is much greater 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 ~= 1.  Test against this asymptotic expression.
        g      ?Nr   r   s      r   test_huge_xzTestIvRatio.test_huge_xf   s     	Xa^S)r      c                 x    ||z  }|dt        j                  d|      z   z  }t        t        ||      |dd       y)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)r   hypotr   r   )r   r   r   texpecteds        r   test_huge_v_xzTestIvRatio.test_huge_v_xv   s:      EBHHQN*+Au1Er   N)__name__
__module____qualname__pytestmarkparametrizer   r   infr   	nextafterr   finfofloatsmallest_normalsmallest_subnormalr    maxr"   sqrtr'   r)   r/    r   r   r   r   	   s-   [[W ' "?#"?8 [[W	
BFFA	A' (	( [[S<2<<1#5w"OP[[SHBHHUO$C$C#C$,BHHUO$F$F#F$&FF7BFFBFF#< =-= Q-
 [[S1hbhhuo&9&9266"BC. D.
 [[U	
HBHHUO++,	
HBHHUO../	
HBHHUO..q01	%		a 	%		gbgghbhhuo&9&9:;% 	0	0 [[U	%$$	%xrxx':':;% 
	*
	* [[U	%		hbhhuo112	%		q	 ("((5/"5"56	%		hbhhuo11A56% 
F
Fr   r   )
r3   numpyr   numpy.testingr   r   scipy.special._ufuncsr   r   r   r>   r   r   <module>rB      s!      7 7F Fr   