
    (ph|'                     \    S SK r S SKrS SKJrJr  S SKJrJ	r
   " S S5      r " S S5      rg)    N)assert_equalassert_allclose)	_iv_ratio_iv_ratio_cc                      \ rS rSr\R
                  R                  S/ SQ5      S 5       r\R
                  R                  SS\R                  S4\R                  SS4/5      S 5       r
\R
                  R                  SS	\R                  * \R                  \R                  /5      \R
                  R                  S
\R                  " \5      R                  * \R                  " \5      R                  * \R                  * \R                  \R                  /5      S 5       5       r\R
                  R                  SSS\R                  " \5      R"                  \R                  /5      S 5       r\R
                  R                  SS\R                  " \5      R                  4S\R                  " \5      R                  4S\R                  " \5      R                  S-  4S\R                  " \5      R"                  S4\R                  " \5      R"                  \R&                  " \R                  " \5      R"                  5      4/5      S 5       r\R
                  R                  SSS\R&                  " \R                  " \5      R"                  5      \R                  " \5      R"                  4/5      S 5       r\R
                  R                  S\R                  " \5      R"                  \R                  " \5      R"                  4\R                  " \5      R"                  S-  \R                  " \5      R"                  4\R                  " \5      R"                  \R                  " \5      R"                  S-  4/5      S 5       rSrg)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                 .    [        [        X5      USSS9  g)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       T/var/www/html/venv/lib/python3.13/site-packages/scipy/special/tests/test_iv_ratio.pytest_against_reference_values)TestIvRatio.test_against_reference_values   s    ` 	A>    r   r   c                 .    [        [        X5      U5        gzaIf 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_infTestIvRatio.test_inf@   s     	Xa^Q'r0   r*   \(\?r+   c                 J    [        [        X5      [        R                  5        gz]If 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_nanTestIvRatio.test_nanI   s     	Xa^RVV,r0   r   c                 \    [        [        US5      S5        [        [        US5      S5        g)z?If x is +/-0.0, return x to ensure iv_ratio is an odd function.               Nr3   r)   r*   s     r-   test_zero_xTestIvRatio.test_zero_xR   s&     	Xa%s+Xa&-r0   v,x   @xD{   c                 :    [        [        X5      SU-  U-  5        g)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 R ~= x/2v.  Test against this asymptotic expression.
r   Nr3   r;   s      r-   test_tiny_xTestIvRatio.test_tiny_xX   s    " 	Xa^c!eQY/r0   r   g 7yACrG   g\)c=Hc                 .    [        [        X5      S5        g)a  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.
      ?Nr3   r;   s      r-   test_huge_xTestIvRatio.test_huge_xk   s     	Xa^S)r0      c                 p    X!-  nUS[         R                  " SU5      -   -  n[        [        X5      USSS9  g)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)r9   hypotr   r'   r)   r*   r+   texpecteds        r-   test_huge_v_xTestIvRatio.test_huge_v_x{   s6      EBHHQN*+u1Er0    N__name__
__module____qualname____firstlineno__pytestmarkparametrizer.   r9   infr4   r:   finfofloatsmallest_normalsmallest_subnormalr<   maxrB   sqrtrJ   rP   rX   __static_attributes__rZ   r0   r-   r   r      s   [[W ' ,?-,?8 [[W	
BFFA	A' (	(
 [[S4"&&"&&"&&"AB[[SBHHUO$C$C#C$&HHUO$F$F#F$&FF7BFFBFF#< =-= C-
 [[S3288E?+>+>"GH. I.
 [[U	
BHHUO++,	
BHHUO../	
BHHUO..q01	%		a 	%		bggbhhuo&9&9:;% 	0	0 [[U	%$$	%rxx':':;% 
	*
	* [[U	%		bhhuo112	%		q	 "((5/"5"56	%		bhhuo11A56% 
F
Fr0   r   c                      \ rS rSr\R
                  R                  S/ SQ5      S 5       r\R
                  R                  SS\R                  S4\R                  SS4/5      S 5       r
\R
                  R                  SS	\R                  * \R                  \R                  /5      \R
                  R                  S
\R                  " \5      R                  * \R                  " \5      R                  * \R                  * \R                  \R                  /5      S 5       5       r\R
                  R                  SSS\R                  " \5      R"                  \R                  /5      S 5       r\R
                  R                  SS\R                  " \5      R                  4S\R                  " \5      R                  4S\R                  " \5      R                  S-  4S\R                  " \5      R"                  S4\R                  " \5      R"                  \R&                  " \R                  " \5      R"                  5      4/5      S 5       r\R
                  R                  SSS\R&                  " \R                  " \5      R"                  5      \R                  " \5      R"                  4/5      S 5       r\R
                  R                  S\R                  " \5      R"                  \R                  " \5      R"                  4\R                  " \5      R"                  S-  \R                  " \5      R"                  4\R                  " \5      R"                  \R                  " \5      R"                  S-  4/5      S 5       rSrg)TestIvRatioC   r
   ))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                 .    [        [        X5      USSS9  g)z8The reference values are one minus those of TestIvRatio.V瞯<r   r$   Nr   
iv_ratio_cr(   s       r-   r.   *TestIvRatioC.test_against_reference_values   s    0 	
1(!%a@r0   r   r   c                 .    [        [        X5      U5        gr2   r   rq   r(   s       r-   r4   TestIvRatioC.test_inf   s     	Z%q)r0   r*   r6   r+   c                 J    [        [        X5      [        R                  5        gr8   )r   rq   r9   r:   r;   s      r-   r<   TestIvRatioC.test_nan   s     	Z%rvv.r0   r   c                 \    [        [        US5      S5        [        [        US5      S5        g)zIf x is +/-0.0, return 1.r?   rO   r@   Nrt   rA   s     r-   rB   TestIvRatioC.test_zero_x   s&     	Z3'-Z4(#.r0   rD   rE   rF   c                 @    [        [        X5      SSU-  U-  -
  5        g)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.
rO   r   Nrt   r;   s      r-   rJ   TestIvRatioC.test_tiny_x   s    " 	Z%sCE19}5r0   rL   rM   c                 :    [        [        X5      US-
  U-  SSS9  g)a#  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   ro   r   r$   Nrp   r;   s      r-   rP   TestIvRatioC.test_huge_x   s     	
1(1S5!)%aHr0   rR   c                 v    X!-  nSUS[         R                  " SU5      -   -  -
  n[        [        X5      USSS9  g)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)r9   rT   r   rq   rU   s        r-   rX   TestIvRatioC.test_huge_v_x   s<      EqAA.//
1((QGr0   rZ   Nr[   rZ   r0   r-   rl   rl      s    [[W ' ,A-,A [[W	
BFFA	A' *	*
 [[S4"&&"&&"&&"AB[[SBHHUO$C$C#C$&HHUO$F$F#F$&FF7BFFBFF#< =/= C/
 [[S3288E?+>+>"GH/ I/
 [[U	
BHHUO++,	
BHHUO../	
BHHUO..q01	%		a 	%		bggbhhuo&9&9:;% 	6	6 [[U	%$$	%rxx':':;% 
	I
	I [[U	%		bhhuo112	%		q	 "((5/"5"56	%		bhhuo11A56% 
H
Hr0   rl   )r`   numpyr9   numpy.testingr   r   scipy.special._ufuncsr   r'   r   rq   r   rl   rZ   r0   r-   <module>r      s2      7AF AFHiH iHr0   