a
    àñhy  ã                   @   s(   d dl Z d dlmZ G dd„ deƒZdS )é    N)ÚOptionalc                   @   sL   e Zd ZdZdZdZddd„Zddejfd	d
„Z	dd„ Z
dd„ Zdd„ ZdS )ÚSobolEngineaÌ  
    The :class:`torch.quasirandom.SobolEngine` is an engine for generating
    (scrambled) Sobol sequences. Sobol sequences are an example of low
    discrepancy quasi-random sequences.

    This implementation of an engine for Sobol sequences is capable of
    sampling sequences up to a maximum dimension of 1111. It uses direction
    numbers to generate these sequences, and these numbers have been adapted
    from `here <https://web.maths.unsw.edu.au/~fkuo/sobol/joe-kuo-old.1111>`_.

    References:
      - Art B. Owen. Scrambling Sobol and Niederreiter-Xing points.
        Journal of Complexity, 14(4):466-489, December 1998.

      - I. M. Sobol. The distribution of points in a cube and the accurate
        evaluation of integrals.
        Zh. Vychisl. Mat. i Mat. Phys., 7:784-802, 1967.

    Args:
        dimension (Int): The dimensionality of the sequence to be drawn
        scramble (bool, optional): Setting this to ``True`` will produce
                                   scrambled Sobol sequences. Scrambling is
                                   capable of producing better Sobol
                                   sequences. Default: ``False``.
        seed (Int, optional): This is the seed for the scrambling. The seed
                              of the random number generator is set to this,
                              if specified. Otherwise, it uses a random seed.
                              Default: ``None``

    Examples::

        >>> soboleng = torch.quasirandom.SobolEngine(dimension=5)
        >>> soboleng.draw(3)
        tensor([[0.5000, 0.5000, 0.5000, 0.5000, 0.5000],
                [0.7500, 0.2500, 0.7500, 0.2500, 0.7500],
                [0.2500, 0.7500, 0.2500, 0.7500, 0.2500]])
    é   iW  FNc           	      C   s<  || j ks|dk r$td| j › d�ƒ‚|| _|| _|| _t d¡}tj|| j|tj	d�| _
t | j
| j¡ | j�r
d }| jd ur’t ¡ }| | j¡ tjd| j| jf||d�}t |t dtjd| j|d	�¡¡| _| j| j| jf}tjd|||d� ¡ }t | j
|| j¡ ntj| j|tj	d�| _| jjtjd
�| _d| _d S )Né   z9Supported range of dimensionality for SobolEngine is [1, ú]Úcpu)ÚdeviceÚdtypeé   )r   Ú	generatorr   )r   )Zmemory_format)ÚMAXDIMÚ
ValueErrorÚseedÚscrambleÚ	dimensionÚtorchr   ÚzerosÚMAXBITÚlongÚ
sobolstateZ_sobol_engine_initialize_state_Ú	GeneratorZmanual_seedÚrandintÚmvÚpowZarangeÚshiftZtrilZ_sobol_engine_scramble_ÚcloneZcontiguous_formatÚquasiÚnum_generated)	Úselfr   r   r   r   ÚgZ
shift_intsZltm_dimsZltm© r    úQ/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/torch/quasirandom.pyÚ__init__.   s.    ÿ


$zSobolEngine.__init__r   c                 C   sR   t j| j|| j| j| j|d�\}| _|  j|7  _|durN| |¡ |¡ |S |S )at  
        Function to draw a sequence of :attr:`n` points from a Sobol sequence.
        Note that the samples are dependent on the previous samples. The size
        of the result is :math:`(n, dimension)`.

        Args:
            n (Int, optional): The length of sequence of points to draw.
                               Default: 1
            out (Tensor, optional): The output tensor
            dtype (:class:`torch.dtype`, optional): the desired data type of the
                                                    returned tensor.
                                                    Default: ``torch.float32``
        )r	   N)r   Z_sobol_engine_drawr   r   r   r   Z
resize_as_Úcopy_)r   ÚnÚoutr	   Úresultr    r    r!   ÚdrawO   s    
ÿzSobolEngine.drawc                 C   s   | j  | j¡ d| _| S )zF
        Function to reset the ``SobolEngine`` to base state.
        r   )r   r#   r   r   )r   r    r    r!   Úresete   s    zSobolEngine.resetc                 C   s,   t  | j|| j| j| j¡ |  j|7  _| S )a  
        Function to fast-forward the state of the ``SobolEngine`` by
        :attr:`n` steps. This is equivalent to drawing :attr:`n` samples
        without using the samples.

        Args:
            n (Int): The number of steps to fast-forward by.
        )r   Z_sobol_engine_ff_r   r   r   r   )r   r$   r    r    r!   Úfast_forwardm   s    	zSobolEngine.fast_forwardc                 C   sT   d| j › �g}| jr|dg7 }| jd ur:|d| j› �g7 }| jjd d |¡ d S )Nz
dimension=zscramble=Truezseed=ú(z, ú))r   r   r   Ú	__class__Ú__name__Újoin)r   Z
fmt_stringr    r    r!   Ú__repr__z   s    

zSobolEngine.__repr__)FN)r-   Ú
__module__Ú__qualname__Ú__doc__r   r   r"   r   Úfloat32r'   r(   r)   r/   r    r    r    r!   r      s   %
!r   )r   Útypingr   Úobjectr   r    r    r    r!   Ú<module>   s   