
    VhH                         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 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)Tensor)constraints)Distribution)broadcast_alllazy_propertyVonMisesc                 r    t        |      }|j                         }|r|j                         | |z  z   }|r|S N)listpop)ycoefresults      M/home/dcms/DCMS/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!   D   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@   [   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                   X    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efd       Zedefd       Zedefd	       Z ej$                          ej&                         fd
       Zd fd	Zedefd       Zedefd       Zedef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
    )r4   r5   Fc                     t        ||      \  | _        | _        | j                  j                  }t	        j
                         }t        |   |||       y r
   )r   r4   r5   r)   r   Sizesuper__init__)selfr4   r5   validate_argsbatch_shapeevent_shape	__class__s         r   rE   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_sampler5   r   r.   r4   r/   r   r0   r!   )rF   valuelog_probs      r   rP   zVonMises.log_prob   sy    !!%(%%		%$((2B(CChhq477{#$%d&8&8BC 	
 r   returnc                 T    | j                   j                  t        j                        S r
   )r4   tor   doublerF   s    r   _loczVonMises._loc   s    xx{{5<<((r   c                 T    | j                   j                  t        j                        S r
   )r5   rS   r   rT   rU   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㈵>)rX   sqrtr   r   )rF   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   emptyrV   r$   r4   r%   r@   rX   r_   rS   )rF   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)NrM   )rG   )rD   expandNotImplementedError__dict__getr4   r5   type)rF   rH   	_instancerG   r4   r5   rJ   s         r   rg   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.
        r4   rU   s    r   meanzVonMises.mean   s    
 xxr   c                     | j                   S r
   rn   rU   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   rL   r   )r!   r5   exprU   s    r   variancezVonMises.variance   s>     '(:(:!D)$*<*<AFGce		
r   r
   )__name__
__module____qualname____doc__r   realpositivearg_constraintssupporthas_rsamplerE   rP   r   r   rV   rX   r_   r   no_gradrC   re   rg   propertyro   rq   rt   __classcell__)rJ   s   @r   r   r   k   s   " *..AUAUVOGKB	 )f ) ) 3 3 3 JV J J U]]_",%**,   O f   f   

& 

 

r   )r   )r/   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_tracingr@   r    r   r   <module>r      s        + 9 B ,

 ~.~.. 9 9i
| i
r   