o
    hH                     @   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 d dlm	Z	m
Z
 dg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)Tensor)constraints)Distribution)broadcast_alllazy_propertyVonMisesc                 C   s,   t |}| }|r| | |  }|s
|S N)listpop)ycoefresult r   q/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/torch/distributions/von_mises.py
_eval_poly   s   r   )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                 C   s   |dks
|dks
J | d }|| }t |t| }|dkr#|  | }| }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   smalllarger   r   r   r   _log_modified_bessel_fnD   s   "r   c                 C   s   t j|jt j| jd}| slt jd|j | j| jd}| \}}}t 	t
j| }	d||	  ||	  }
|||
  }|d|  | dk||  d | dkB }| rht ||d  |
  |}||B }| r|t
j |  dt
j  t
j 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acceptr   r   r   _rejection_sample[   s   ,
r<   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d	efd
dZed	efddZed	efddZe e fddZd fdd	Zed	efddZed	efddZed	efddZ  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
    )r0   r1   FNc                    s6   t ||\| _| _| jj}t }t ||| d S r   )r   r0   r1   r%   r   Sizesuper__init__)selfr0   r1   validate_argsbatch_shapeevent_shape	__class__r   r   r?      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_args_validate_sampler1   r   r*   r0   r+   r   r,   r   )r@   valuelog_probr   r   r   rJ      s   
zVonMises.log_probreturnc                 C      | j tjS r   )r0   tor   doubler@   r   r   r   _loc      zVonMises._locc                 C   rL   r   )r1   rM   r   rN   rO   r   r   r   _concentration   rQ   zVonMises._concentrationc                 C   sh   | j }ddd|d     }|d|   d|  }d|d  d|  }d| | }t|dk ||S )Nr      r#   gh㈵>)rR   sqrtr   r   )r@   kappataurho_proposal_r_proposal_r_taylorr   r   r   rX      s   zVonMises._proposal_rc                 C   s@   |  |}tj|| jj| jjd}t| 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   emptyrP   r    r0   r!   r<   rR   rX   rM   )r@   sample_shaper%   r   r   r   r   sample   s   

zVonMises.samplec                    sX   zt  |W S  ty+   | jd}| j|}| j|}t| |||d Y S w )NrG   )rA   )r>   expandNotImplementedError__dict__getr0   r1   type)r@   rB   	_instancerA   r0   r1   rD   r   r   r^      s   zVonMises.expandc                 C      | j S )z8
        The provided mean is the circular one.
        r0   rO   r   r   r   mean   s   zVonMises.meanc                 C   rd   r   re   rO   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   rF   r   )r   r1   exprO   r   r   r   variance   s   zVonMises.variancer   )__name__
__module____qualname____doc__r   realpositivearg_constraintssupporthas_rsampler?   rJ   r   r   rP   rR   rX   r   no_gradr=   r]   r^   propertyrf   rg   ri   __classcell__r   r   rD   r   r   k   s,    		)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   r   r   <module>   s&   		

