a
    –¥»aã  ã                   @   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
g d¢Zg d¢Zg d	¢Zg d
¢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
   úW/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/torch/distributions/von_mises.pyÚ
_eval_poly
   s
    r   )g      ð?g§Œ$æþ@g¤0”¸3¸@g,–Ç?ØNó?gª2çt´Ñ?gýåIˆ¨x¢?gtHÅZ×Ãr?)	ç Þe3EˆÙ?g�-ÿ¥5‹?gÛÕ’+Hub?gJ‰NÁÐY¿gTÑPŠóÃ‚?gÇý'•¿gãZåðæüš?gULÊ+ß�¿gˆ;ý^p?)ç      à?g§ì‘Yÿì?g(„«�Ézà?gê*öúOÃ?gZƒµ9›?gœå.™•³h?gÓ°­Ù©=5?)	r   g¾þÍ.k¤¿g?Œ”V¨m¿g–tZØOÖZ?gÜ<¯Q …¿gÙ'8©`—?gP“þâ¥�¿gqœ©J:N’?g;PÈJ£4q¿c                 C   sŽ   |dks|dksJ ‚| 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   )r   Ú_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�}| ¡ sÒt 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    ,
r4   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‡  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Úsuperr5   Ú__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   rF   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   r4   r*   r9   )r<   Zsample_shaper    r   r
   r
   r   Úsamplek   s    
zVonMises.samplec                    s`   zt t| ƒ |¡W S  tyZ   | j d¡}| j |¡}| j |¡}t| ƒ|||d� Y S 0 d S )NrD   )r=   )	r:   r5   ÚexpandÚNotImplementedErrorÚ__dict__Úgetr)   r*   Útype)r<   r>   r=   r)   r*   rA   r
   r   rI   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$   dt | jdd�t | jdd�  ¡  S )z<
        The provided variance is the circular one.
        r   rC   r   )r   r*   ÚexprN   r
   r
   r   Úvariance†   s    ÿzVonMises.variance)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚrealZpositiveZarg_constraintsZsupportZhas_rsampler;   rF   r   Zno_gradr7   rH   rI   ÚpropertyrO   r   rQ   Ú__classcell__r
   r
   rA   r   r5   D   s   
	
r5   )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_tracingr4   r5   r
   r
   r
   r   Ú<module>   s    

