
    9i                         S SK r S SKJr  S SKrS SKrS SKJr  S SKJr  S SKJ	r	  S SK
JrJr  S/rS r/ S	Qr/ S
Qr/ SQr/ SQr\\/r\\/rSS jr\R,                  R.                  S 5       r " S S\	5      rg)    N)Optional)Tensor)constraints)Distribution)broadcast_alllazy_propertyVonMisesc                     [        U5      nUR                  5       nU(       a  UR                  5       X-  -   nU(       a  M  U$ N)listpop)ycoefresults      ]/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/torch/distributions/von_mises.py
_eval_polyr      s7    :DXXZF
aj( $M    )g      ?g$@g03@g,?N?g2t?gIx?gtHZr?)	 e3E?g-5?gՒ+Hub?gJNYgTPÂ?g'gZ?gUL+ߐg;^p?)      ?gY?g(z?g*O?gZ9?g.h?gӰ٩=5?)	r   g.kg?VmgtZOZ?g<Q g'8`?gP⥝gqJ:N?g;PJ4qc                 Z   US:X  d  US:X  d   eU S-  nX"-  n[        U[        U   5      nUS:X  a  U R                  5       U-  nUR                  5       nSU -  nU SU R                  5       -  -
  [        U[        U   5      R                  5       -   n[
        R                  " U S:  X45      nU$ )zL
Returns ``log(I_order(x))`` for ``x > 0``,
where `order` is either 0 or 1.
r      g      @r   )r   _COEF_SMALLabslog_COEF_LARGEtorchwhere)xorderr   smalllarger   s         r   _log_modified_bessel_fnr"   E   s    
 A:!## 	
DA	Aq+e,-Ez%IIKE 	qAaeeg
1k%.@ A E E GGE[[T50FMr   c                 D   [         R                  " UR                  [         R                  U R                  S9nUR                  5       (       Gd  [         R                  " SUR                  -   U R                  U R                  S9nUR                  5       u  pgn[         R                  " [        R                  U-  5      n	SX)-  -   X)-   -  n
XU
-
  -  nUSU-
  -  U-
  S:  X-  R                  5       S-   U-
  S:  -  nUR                  5       (       a=  [         R                  " XS-
  R                  5       U
R!                  5       -  U5      nXL-  nUR                  5       (       d  GM  U[        R                  -   U -   S[        R                  -  -  [        R                  -
  $ )Ndtypedevice)   r      r   r   )r   zerosshapeboolr&   allrandr%   unbindcosmathpir   anyr   signacos)locconcentration
proposal_rr   doneuu1u2u3zfcaccepts                r   _rejection_samplerA   \   s4   ;;qwwejjDDhhjjJJtagg~SYYszzJXXZ
IIdggl#JN3!^,A;#q(af\\^a-?!-Cq-HI::<<F#XOO$5$@!DA=D hhjj K#!dgg+.88r   c            	         ^  \ rS rSrSr\R                  \R                  S.r\R                  r	Sr
 SS\S\S\\   S	S4U 4S
 jjjrS r\S	\4S j5       r\S	\4S j5       r\S	\4S j5       r\R*                  " 5       \R,                  " 5       4S j5       rSU 4S jjr\S	\4S j5       r\S	\4S j5       r\S	\4S j5       rSrU =r$ )r	   l   a(  
A circular von Mises distribution.

This implementation uses polar coordinates. The ``loc`` and ``value`` args
can be any real number (to facilitate unconstrained optimization), but are
interpreted as angles modulo 2 pi.

Example::
    >>> # xdoctest: +IGNORE_WANT("non-deterministic")
    >>> m = VonMises(torch.tensor([1.0]), torch.tensor([1.0]))
    >>> m.sample()  # von Mises distributed with loc=1 and concentration=1
    tensor([1.9777])

:param torch.Tensor loc: an angle in radians.
:param torch.Tensor concentration: concentration parameter
)r5   r6   FNr5   r6   validate_argsreturnc                    > [        X5      u  U l        U l        U R                  R                  n[        R
                  " 5       n[        TU ]  XEU5        g r   )r   r5   r6   r*   r   Sizesuper__init__)selfr5   r6   rD   batch_shapeevent_shape	__class__s         r   rI   VonMises.__init__   s@     (5S'H$$$hhnnjjl=Ar   c                 &   U R                   (       a  U R                  U5        U R                  [        R                  " XR
                  -
  5      -  nU[        R                  " S[        R                  -  5      -
  [        U R                  SS9-
  nU$ )Nr(   r   r   )
_validate_args_validate_sampler6   r   r/   r5   r0   r   r1   r"   )rJ   valuelog_probs      r   rT   VonMises.log_prob   sw    !!%(%%		%((2B(CChhq477{#$%d&8&8BC 	
 r   c                 T    U R                   R                  [        R                  5      $ r   )r5   tor   doublerJ   s    r   _locVonMises._loc   s    xx{{5<<((r   c                 T    U R                   R                  [        R                  5      $ r   )r6   rW   r   rX   rY   s    r   _concentrationVonMises._concentration   s    !!$$U\\22r   c                     U R                   nSSSUS-  -  -   R                  5       -   nUSU-  R                  5       -
  SU-  -  nSUS-  -   SU-  -  nSU-  U-   n[        R                  " US:  XT5      $ )Nr      r(   gh㈵>)r]   sqrtr   r   )rJ   kappataurho_proposal_r_proposal_r_taylors         r   re   VonMises._proposal_r   s    ##1q5!8|#))++a#g^^%%!e)436za#g.Y.{{54<);IIr   c                 <   U R                  U5      n[        R                  " X R                  R                  U R
                  R                  S9n[        U R                  U R                  U R                  U5      R                  U R
                  R                  5      $ )a  
The sampling algorithm for the von Mises distribution is based on the
following paper: D.J. Best and N.I. Fisher, "Efficient simulation of the
von Mises distribution." Applied Statistics (1979): 152-157.

Sampling is always done in double precision internally to avoid a hang
in _rejection_sample() for small values of the concentration, which
starts to happen for single precision around 1e-4 (see issue #88443).
r$   )_extended_shaper   emptyrZ   r%   r5   r&   rA   r]   re   rW   )rJ   sample_shaper*   r   s       r   sampleVonMises.sample   sl     $$\2KKYY__TXX__M IIt**D,<,<a

"TXX^^
	r   c                   >  [         TU ]  U5      $ ! [         ad    U R                  R	                  S5      nU R
                  R                  U5      nU R                  R                  U5      n[        U 5      " XEUS9s $ f = f)NrQ   )rD   )rH   expandNotImplementedError__dict__getr5   r6   type)rJ   rK   	_instancerD   r5   r6   rM   s         r   ro   VonMises.expand   sw    	O7>+.." 	O MM--.>?M((//+.C ..55kBM:cNN		Os    A+B ?B c                     U R                   $ )z(
The provided mean is the circular one.
r5   rY   s    r   meanVonMises.mean   s    
 xxr   c                     U R                   $ r   rw   rY   s    r   modeVonMises.mode   s    xxr   c                 v    S[        U R                  SS9[        U R                  SS9-
  R                  5       -
  $ )z,
The provided variance is the circular one.
r   rP   r   )r"   r6   exprY   s    r   varianceVonMises.variance   s>     '(:(:!D)$*<*<AFGce		
r   )r6   r5   r   )__name__
__module____qualname____firstlineno____doc__r   realpositivearg_constraintssupporthas_rsampler   r   r+   rI   rT   r   rZ   r]   re   r   no_gradrG   rl   ro   propertyrx   r{   r   __static_attributes____classcell__)rM   s   @r   r	   r	   l   sB   " *..AUAUVOGK )-		B	B 	B  ~		B
 
	B 	B	 )f ) ) 3 3 3 JV J J ]]_"'**,   O f   f   

& 

 

r   )r   )r0   typingr   r   	torch.jitr   torch.distributionsr    torch.distributions.distributionr   torch.distributions.utilsr   r   __all__r   _I0_COEF_SMALL_I0_COEF_LARGE_I1_COEF_SMALL_I1_COEF_LARGEr   r   r"   jitscript_if_tracingrA   r	    r   r   <module>r      s         + 9 B ,

 ~.~.. 9 9n
| n
r   