U
    d                  	   @   s   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d Z
ddd	d
dddgZdddddddddg	ZdddddddgZdddd d!d"d#d$d%g	ZeegZeegZd,d&d'Zejjd(d) ZG d*d+ d+eZdS )-    N)constraints)Distribution)broadcast_alllazy_propertyc                 C   s*   t |}| }|r&| | |  }q|S N)listpop)yZcoefresult r   A/tmp/pip-unpacked-wheel-ua33x9lu/torch/distributions/von_mises.py
_eval_poly
   s
    r   g      ?g$@g03@g,?N?g2t?gIx?gtHZr?g e3E?g-5?gՒ+Hub?gJNYgTPÂ?g'gZ?gUL+ߐg;^p?      ?gY?g(z?g*O?gZ9?g.h?gӰ٩=5?g.kg?VmgtZOZ?g<Q g'8`?gP⥝gqJ:N?g;PJ4qc                 C   s   |dks|dkst | d }|| }t|t| }|dkrF|  | }| }d|  }| d|    t|t|   }t| dk ||}|S )zX
    Returns ``log(I_order(x))`` for ``x > 0``,
    where `order` is either 0 or 1.
    r      g      @r   )AssertionErrorr   _COEF_SMALLabslog_COEF_LARGEtorchwhere)xorderr	   smallZlarger
   r   r   r   _log_modified_bessel_fn   s    "r   c                 C   s   t j|jt j| jd}| st jd|j | j| jd}| \}}}t 	t
j| }	d||	  ||	  }
|||
  }|d|  | dk||  d | dkB }| rt ||d  |
  |}||B }q|t
j |  dt
j  t
j S )Ndtypedevice)   r      r   r   )r   zerosshapeboolr   allZrandr   Zunbindcosmathpir   anyr   signacos)locconcentrationZ
proposal_rr   doneuu1u2u3zfcacceptr   r   r   _rejection_sample4   s    ,
r5   c                       s   e Zd ZdZejejdZejZdZ	d fdd	Z
dd Ze e fd	d
Z fddZedd Zedd Zedd Z  ZS )VonMisesa"  
    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::
        >>> m = dist.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
    )r*   r+   FNc                    s   t ||\| _| _| jj}t }ddd| jd     }|d|   d| j  }d|d  d|  | _tt	| 
||| d S )Nr      r   )r   r*   r+   r!   r   Sizesqrt_proposal_rsuperr6   __init__)selfr*   r+   validate_argsbatch_shapeZevent_shapetaurho	__class__r   r   r<   X   s    zVonMises.__init__c                 C   sL   | j r| | | jt|| j  }|tdtj  t	| jdd }|S )Nr   r   r   )
_validate_argsZ_validate_sampler+   r   r$   r*   r%   r   r&   r   )r=   valuelog_probr   r   r   rG   d   s
    
"zVonMises.log_probc                 C   s6   |  |}tj|| jj| jjd}t| j| j| j|S )z
        The sampling algorithm for the von Mises distribution is based on the following paper:
        Best, D. J., and Nicholas I. Fisher.
        "Efficient simulation of the von Mises distribution." Applied Statistics (1979): 152-157.
        r   )	Z_extended_shaper   emptyr*   r   r   r5   r+   r:   )r=   Zsample_shaper!   r   r   r   r   samplek   s    
zVonMises.samplec                    sb   zt t| |W S  tk
r\   | jd}| j|}| j|}t| |||d Y S X d S )NrE   )r>   )	r;   r6   expandNotImplementedError__dict__getr*   r+   type)r=   r?   r>   r*   r+   rB   r   r   rJ   v   s    zVonMises.expandc                 C   s   | j S )z8
        The provided mean is the circular one.
        r*   r=   r   r   r   mean   s    zVonMises.meanc                 C   s   | j S r   rO   rP   r   r   r   mode   s    zVonMises.modec                 C   s$   dt | jddt | jdd   S )z<
        The provided variance is the circular one.
        r   rD   r   )r   r+   exprP   r   r   r   variance   s    zVonMises.variance)N)__name__
__module____qualname____doc__r   realZpositiveZarg_constraintsZsupportZhas_rsampler<   rG   r   Zno_gradr8   rI   rJ   propertyrQ   rR   r   rT   __classcell__r   r   rB   r   r6   D   s   
	

r6   )r   )r%   r   Z	torch.jitZtorch.distributionsr   Z torch.distributions.distributionr   Ztorch.distributions.utilsr   r   r   Z_I0_COEF_SMALLZ_I0_COEF_LARGEZ_I1_COEF_SMALLZ_I1_COEF_LARGEr   r   r   ZjitZscript_if_tracingr5   r6   r   r   r   r   <module>   s4   
   
   

