
    e!h|'                     \    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y)    N)assert_equalassert_allclose)	_iv_ratio_iv_ratio_cc                      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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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)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                 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 (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       i/var/www/html/diagnosisapp-backend/venv/lib/python3.12/site-packages/scipy/special/tests/test_iv_ratio.pytest_against_reference_valuesz)TestIvRatio.test_against_reference_values   s    ` 	AA>    r   r   c                 0    t        t        ||      |       y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                 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_nanI   s     	Xa^RVV,r.   r
   c                 \    t        t        |d      d       t        t        |d      d       y)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                 <    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.
        r
   Nr1   r8   s      r,   test_tiny_xzTestIvRatio.test_tiny_xX   s    " 	Xa^c!eQY/r.   r   g 7yACrB   g\)c=Hc                 0    t        t        ||      d       y)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                 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)r6   hypotr   r&   r(   r)   r*   texpecteds        r,   test_huge_v_xzTestIvRatio.test_huge_v_x{   s:      EBHHQN*+Au1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      s"   [[W ' ,?-,?8 [[W	
BFFA	A' (	(
 [[S4"&&"&&"&&"AB[[SHBHHUO$C$C#C$,BHHUO$F$F#F$&FF7BFFBFF#< =-= C-
 [[S38288E?+>+>"GH. I.
 [[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   c                      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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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)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                 6    t        t        ||      |dd       y)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                 0    t        t        ||      |       yr0   r   rf   r'   s       r,   r2   zTestIvRatioC.test_inf   s     	Z1%q)r.   r)   r3   r*   c                 L    t        t        ||      t        j                         yr5   )r   rf   r6   r7   r8   s      r,   r9   zTestIvRatioC.test_nan   s     	Z1%rvv.r.   r
   c                 \    t        t        |d      d       t        t        |d      d       y)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                 B    t        t        ||      dd|z  |z  z
         y)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   s!    " 	Z1%sCE19}5r.   rF   rG   c                 B    t        t        ||      |dz
  |z  dd       y)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   s!     	
1a(1S5!)%aHr.   rK   c                 ~    ||z  }d|dt        j                  d|      z   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 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   s@      EqAA.//
1a((QGr.   NrR   r`   r.   r,   rb   rb      s&   [[W ' ,A-,A [[W	
BFFA	A' *	*
 [[S4"&&"&&"&&"AB[[SHBHHUO$C$C#C$,BHHUO$F$F#F$&FF7BFFBFF#< =/= C/
 [[S38288E?+>+>"GH/ I/
 [[U	
HBHHUO++,	
HBHHUO../	
HBHHUO..q01	%		a 	%		gbgghbhhuo&9&9:;% 	6	6 [[U	%$$	%xrxx':':;% 
	I
	I [[U	%		hbhhuo112	%		q	 ("((5/"5"56	%		hbhhuo11A56% 
H
Hr.   rb   )rV   numpyr6   numpy.testingr   r   scipy.special._ufuncsr   r&   r   rf   r   rb   r`   r.   r,   <module>rq      s2      7AF AFHiH iHr.   