3
I4W^Å  ã               @   s   d dl Z G dd„ deƒZdS )é    Nc               @   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 <http://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| j ƒƒ‚|| _|| _|| _tj|| jtj	d�| _
tj| j
| jƒ | jrðtjƒ }| jd k	r€|j| jƒ n|jƒ  tjtjd| j| jf|d�tjdtjd| jƒƒƒ| _tjd| j| j| jf|d�jƒ }tj| j
|| jƒ ntj| jtj	d�| _| jjƒ | _d| _d S )Né   z<Supported range of dimensionality for SobolEngine is [1, {}])Údtypeé   )Ú	generatorr   )ÚMAXDIMÚ
ValueErrorÚformatÚseedÚscrambleÚ	dimensionÚtorchÚzerosÚMAXBITÚlongÚ
sobolstateZ_sobol_engine_initialize_state_Ú	GeneratorZmanual_seedÚmvÚrandintÚpowZarangeÚshiftZtrilZ_sobol_engine_scramble_ÚcloneÚquasiÚnum_generated)Úselfr   r   r   ÚgZltm© r   ú2/tmp/pip-build-e66j97b2/torch/torch/quasirandom.pyÚ__init__-   s(    

 zSobolEngine.__init__r   c             C   sR   t j| j|| j| j| j|d�\}| _|  j|7  _|dk	rN|j|ƒj|ƒ |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   ÚdrawL   s    zSobolEngine.drawc             C   s   | j j| jƒ d| _| S )zF
        Function to reset the ``SobolEngine`` to base state.
        r   )r   r    r   r   )r   r   r   r   Úresetb   s    zSobolEngine.resetc             C   s,   t j| 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_forwardj   s    	zSobolEngine.fast_forwardc             C   sT   dj | jƒg}| jr|dg7 }| jd k	r:|dj | jƒg7 }| jjd dj|ƒ d S )Nzdimension={}zscramble=Truezseed={}ú(z, ú))r
   r   r   r   Ú	__class__Ú__name__Újoin)r   Z
fmt_stringr   r   r   Ú__repr__w   s    

zSobolEngine.__repr__)FN)r*   Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   Zfloat32r$   r%   r&   r,   r   r   r   r   r      s   %
r   )r   Úobjectr   r   r   r   r   Ú<module>   s   