o
    Ö­j|'  ã                   @   sP   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„ dƒZG dd„ dƒZdS )é    N)Úassert_equalÚassert_allclose)Ú	_iv_ratioÚ_iv_ratio_cc                   @   sØ  e Zd Zej dg d¢¡dd„ ƒZej ddejdfejddfg¡dd„ ƒZ	ej d	d
ej ej
ejg¡ej de e¡j e e¡j ej ej
ejg¡dd„ ƒƒZej d	dde e¡jejg¡dd„ ƒZej dde e¡jfde e¡jfde e¡jd fde e¡jdfe e¡je e e¡j¡fg¡dd„ ƒZej ddde e e¡j¡e e¡jfg¡dd„ ƒZej de e¡je e¡jfe e¡jd e e¡jfe e¡je e¡jd fg¡dd„ ƒZdS )ÚTestIvRatioúv,x,r))ç      à?çUUUUUUÅ?g.a0R#Å?)r   çUUUUUUÕ?gˆ<)ª“Ô?)r   r   góŠV×S“Ý?)r   çUUUUUUå?gPò]k(¦â?)r   ç¬ªªªªªê?gjDŒ­Õå?)é   ç6Z£5Z£Õ?g&RÍ’®UÅ?)r   çªªªªªªæ?g„‘ùÂZÕ?)r   ç«ªªªªªò?gZrò!à?)r   çÝÝÝÝÝÝý?gŽ4e~u–å?)r   ç}ðÁ|@gG)È¿ë?)ç¸…ëQ¸@ç}ôõ¦P…é?gâ1ÖÜaÅ?)r   çj�6Ðiû?gÖ´N¶`�Õ?)r   ç Ó:m @g9Æ¬Ü7à?)r   çí5’¦T@g÷4¸+ç�å?)r   ç¿ŽÔëH½%@gü¬ñJ]Ðê?)ç¢E¶óýdL@ç€9LŒ;w3@g¤ñž'~VÅ?)r   çä^s!iFE@gì¥Õ/°XÕ?)r   çÙÎ÷SãR@g“_®8à?)r   çP²ÂTø`@g¿ø )Xå?)r   çÿ>ÝïÓ=s@g\h*¬ê?c                 C   ó   t t||ƒ|d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Úr© r*   ú^/var/www/html/CropPilot/venv/lib/python3.10/site-packages/scipy/special/tests/test_iv_ratio.pyÚtest_against_reference_values   s   0z)TestIvRatio.test_against_reference_valuesr   r   c                 C   ó   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%   r*   r*   r+   Útest_inf@   ó   zTestIvRatio.test_infr'   ç\�Âõ(\ß?r(   c                 C   ó   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(   r*   r*   r+   Útest_nanI   ó   zTestIvRatio.test_nanr   c                 C   s$   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.ç        ç       €Nr/   ©r&   r'   r*   r*   r+   Útest_zero_xR   ó   zTestIvRatio.test_zero_xúv,xé   ©ç@Œµx¯Dé{   c                 C   s   t t||ƒd| | ƒ 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   Nr/   r7   r*   r*   r+   Útest_tiny_xX   s   zTestIvRatio.test_tiny_x©r   g €à7yÃAC©rB   g¥\Ãñ)c=Hc                 C   s   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.
        ç      ð?Nr/   r7   r*   r*   r+   Útest_huge_xk   s   zTestIvRatio.test_huge_xé   c                 C   s6   || }|dt  d|¡  }tt||ƒ|d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)r5   Úhypotr   r$   ©r&   r'   r(   ÚtÚexpectedr*   r*   r+   Útest_huge_v_x{   s   zTestIvRatio.test_huge_v_xN©Ú__name__Ú
__module__Ú__qualname__ÚpytestÚmarkÚparametrizer,   r5   Úinfr0   r6   ÚfinfoÚfloatÚsmallest_normalÚsmallest_subnormalr8   Úmaxr=   ÚsqrtrD   rH   rN   r*   r*   r*   r+   r      sJ    


þ
þ
ú
ý
ýr   c                   @   sØ  e Zd Zej dg d¢¡dd„ ƒZej ddejdfejddfg¡dd„ ƒZ	ej d	d
ej ej
ejg¡ej de e¡j e e¡j ej ej
ejg¡dd„ ƒƒZej d	dde e¡jejg¡dd„ ƒZej dde e¡jfde e¡jfde e¡jd fde e¡jdfe e¡je e e¡j¡fg¡dd„ ƒZej ddde e e¡j¡e e¡jfg¡dd„ ƒZej de e¡je e¡jfe e¡jd e e¡jfe e¡je e¡jd fg¡dd„ ƒZdS )ÚTestIvRatioCr   ))r   r	   g{´çs+·ê?)r   r
   gÿ»aë*¶å?)r   r   g†ºTV6á?)r   r   g`D)¯³Ú?)r   r   g,wç¤ÒUÔ?)r   r   gv«L[”ªê?)r   r   g>7öžRå?)r   r   gLá¼Ùß?)r   r   gä–5ÓÔ?)r   r   gäZ³ß ñÃ?)r   r   gˆ3zÊˆ§ê?)r   r   g•¥Ø¤O?å?)r   r   gŽ÷s¦F�ß?)r   r   g–�¨1ÄÔ?)r   r   gL9ÔŠ¾Ä?)r   r   g—Cv`ªê?)r   r   g
-è§Så?)r   r   gÚ@É£Žùß?)r   r   g‚þ­OÕ?)r   r   g�^V³ßOÅ?c                 C   r   )z8The reference values are one minus those of TestIvRatio.çVçž¯Ò<r   r!   N©r   Ú
iv_ratio_cr%   r*   r*   r+   r,   ’   s   z*TestIvRatioC.test_against_reference_valuesr   r   c                 C   r-   r.   ©r   r`   r%   r*   r*   r+   r0   ¬   r1   zTestIvRatioC.test_infr'   r2   r(   c                 C   r3   r4   )r   r`   r5   r6   r7   r*   r*   r+   r8   µ   r9   zTestIvRatioC.test_nanr   c                 C   s$   t t|dƒdƒ t t|dƒdƒ dS )zIf x is +/-0.0, return 1.r:   rG   r;   Nra   r<   r*   r*   r+   r=   ¾   r>   zTestIvRatioC.test_zero_xr?   r@   rA   c                 C   s    t t||ƒdd| |  ƒ 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.
        rG   r   Nra   r7   r*   r*   r+   rD   Ä   s    zTestIvRatioC.test_tiny_xrE   rF   c                 C   s"   t t||ƒ|d | d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   r^   r   r!   Nr_   r7   r*   r*   r+   rH   ×   s   "zTestIvRatioC.test_huge_xrI   c                 C   s:   || }d|dt  d|¡   }tt||ƒ|d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)r5   rJ   r   r`   rK   r*   r*   r+   rN   ç   s   zTestIvRatioC.test_huge_v_xNrO   r*   r*   r*   r+   r]   �   sJ    


þ
þ
ú
ý
ýr]   )rS   Únumpyr5   Únumpy.testingr   r   Úscipy.special._ufuncsr   r$   r   r`   r   r]   r*   r*   r*   r+   Ú<module>   s    