
    Ǆg                         d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZm	Z	 dgZ
d Zg dZg dZg d	Zg d
ZeegZeegZddZej&                  j(                  d        Z G d de      Zy)    N)constraints)Distribution)broadcast_alllazy_propertyVonMisesc                 r    t        |      }|j                         }|r|j                         | |z  z   }|r|S N)listpop)ycoefresults      e/home/mcse/projects/flask_80/flask-venv/lib/python3.12/site-packages/torch/distributions/von_mises.py
_eval_polyr      s7    :DXXZF
a&j( 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                 V   |dk(  s|dk(  sJ | dz  }||z  }t        |t        |         }|dk(  r| j                         |z  }|j                         }d| z  }| d| j                         z  z
  t        |t        |         j                         z   }t        j                  | dk  ||      }|S )zX
    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    C   s    
 A:!## 	
DA	AAq+e,-Ez%IIKE 	qAaeeg
1k%.@ A E E GGE[[T5%0FMr   c                 6   t        j                  |j                  t         j                  | j                        }|j                         st        j                  d|j                  z   | j                  | j                        }|j                         \  }}}t        j                  t        j                  |z        }	d||	z  z   ||	z   z  }
|||
z
  z  }|d|z
  z  |z
  dkD  ||z  j                         dz   |z
  dk\  z  }|j                         r>t        j                  ||dz
  j                         |
j!                         z  |      }||z  }|j                         s|t        j                  z   | z   dt        j                  z  z  t        j                  z
  S )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_sampler?   Z   s<   ;;qwwejjDDhhjJJtagg~SYYszzJXXZ
BIIdggl#aJN3Z!^,A;#q(a"f\\^a-?!-Cq-HI::<FR#XOO$5$@!DA&=D hhj K#!dgg+.88r   c                   2    e Zd ZdZej
                  ej                  dZej
                  ZdZ	d fd	Z
d Zed        Zed        Zed        Z ej"                          ej$                         fd	       Z fd
Zed        Zed        Zed        Z xZS )r   aX  
    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
    )r3   r4   Fc                     t        ||      \  | _        | _        | j                  j                  }t	        j
                         }t        |   |||       y r	   )r   r3   r4   r(   r   Sizesuper__init__)selfr3   r4   validate_argsbatch_shapeevent_shape	__class__s         r   rD   zVonMises.__init__   sB    '4S-'H$$$hhnnjjlk=Ar   c                     | j                   r| j                  |       | j                  t        j                  || j
                  z
        z  }|t        j                  dt        j                  z        z
  t        | j                  d      z
  }|S )Nr&   r   r   )
_validate_args_validate_sampler4   r   r-   r3   r.   r   r/   r    )rE   valuelog_probs      r   rO   zVonMises.log_prob   sy    !!%(%%		%$((2B(CChhq477{#$%d&8&8BC 	
 r   c                 T    | j                   j                  t        j                        S r	   )r3   tor   doublerE   s    r   _loczVonMises._loc   s    xx{{5<<((r   c                 T    | j                   j                  t        j                        S r	   )r4   rQ   r   rR   rS   s    r   _concentrationzVonMises._concentration   s    !!$$U\\22r   c                     | j                   }ddd|dz  z  z   j                         z   }|d|z  j                         z
  d|z  z  }d|dz  z   d|z  z  }d|z  |z   }t        j                  |dk  ||      S )Nr      r&   gh㈵>)rV   sqrtr   r   )rE   kappataurho_proposal_r_proposal_r_taylors         r   r]   zVonMises._proposal_r   s    ##1q5!8|#))++a#g^^%%!e)436za#g.Y.{{54<);[IIr   c                 B   | j                  |      }t        j                  || j                  j                  | j
                  j                        }t        | j                  | j                  | j                  |      j                  | j
                  j                        S )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   emptyrT   r#   r3   r$   r?   rV   r]   rQ   )rE   sample_shaper(   r   s       r   samplezVonMises.sample   sn     $$\2KKTYY__TXX__M IIt**D,<,<a

"TXX^^
	r   c                    	 t         |   |      S # t        $ rh | j                  j	                  d      }| j
                  j                  |      }| j                  j                  |      } t        |       |||      cY S w xY w)NrL   )rF   )rC   expandNotImplementedError__dict__getr3   r4   type)rE   rG   rF   r3   r4   rI   s        r   re   zVonMises.expand   s{    	O7>+.." 	O MM--.>?M((//+.C ..55kBM4:c=NN		Os    A.BBc                     | j                   S )z8
        The provided mean is the circular one.
        r3   rS   s    r   meanzVonMises.mean   s    
 xxr   c                     | j                   S r	   rk   rS   s    r   modezVonMises.mode   s    xxr   c                     dt        | j                  d      t        | j                  d      z
  j                         z
  S )z<
        The provided variance is the circular one.
        r   rK   r   )r    r4   exprS   s    r   variancezVonMises.variance   s>     '(:(:!D)$*<*<AFGce		
r   r	   )__name__
__module____qualname____doc__r   realpositivearg_constraintssupporthas_rsamplerD   rO   r   rT   rV   r]   r   no_gradrB   rc   re   propertyrl   rn   rq   __classcell__)rI   s   @r   r   r   j   s    " *..AUAUVOGKB	 ) ) 3 3 J J U]]_",%**,   O     

 

r   )r   )r.   r   	torch.jit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_tracingr?   r    r   r   <module>r      s       + 9 B ,

 ~.~.. 9 9i
| i
r   