3
Oéd©0  ã               @   s$   d Z ddlZdgZG dd„ dƒZdS )a  
  Licensing:
    This code is distributed under the MIT license.


  Authors:
    Original FORTRAN77 version of i4_sobol by Bennett Fox.
    MATLAB version by John Burkardt.
    PYTHON version by Corrado Chisari

    Original Python version of is_prime by Corrado Chisari

    Original MATLAB versions of other functions by John Burkardt.
    PYTHON versions by Corrado Chisari

    Original code is available from
    http://people.sc.fsu.edu/~jburkardt/py_src/sobol/sobol.html

  Modifications:
    Wrapped into Python class [30.10.2017]
é    NÚSobolc               @   s6   e Zd Zdd„ Zddd„Zdd„ Zdd	„ Zd
d„ ZdS )r   c             C   sF   d | _ d | _d | _d | _d | _d | _d | _d | _d | _d | _	d | _
d S )N)ÚatmostÚdim_maxÚdim_num_saveÚinitializedÚlastqÚlog_maxÚmaxcolÚpolyÚrecipdÚ	seed_saveÚv)Úself© r   úV/var/www/html/virt/lib64/python3.6/site-packages/scipy/optimize/_shgo_lib/sobol_seq.pyÚ__init__   s    zSobol.__init__é   c             C   sL   t j||ft jƒ}x4t|ƒD ](}|| }| j||ƒ\||d|…f< }qW |S )a;  
        i4_sobol_generate generates a Sobol dataset.

        Parameters:
          Input, integer dim_num, the spatial dimension.
          Input, integer N, the number of points to generate.
          Input, integer SKIP, the number of initial points to skip.

          Output, real R(M,N), the points.
        r   )ÚnpÚfullÚnanÚrangeÚi4_sobol)r   Údim_numÚnÚskipÚrÚjÚseedZ	next_seedr   r   r   Úi4_sobol_generate*   s
     zSobol.i4_sobol_generatec             C   s0   t j|ƒ}d}x|dkr*|d7 }|d }qW |S )aË  
        i4_bit_hi1 returns the position of the high 1 bit base 2 in an integer.

        Example:
          +------+-------------+-----
          |    N |      Binary | BIT
          +------|-------------+-----
          |    0 |           0 |   0
          |    1 |           1 |   1
          |    2 |          10 |   2
          |    3 |          11 |   2
          |    4 |         100 |   3
          |    5 |         101 |   3
          |    6 |         110 |   3
          |    7 |         111 |   3
          |    8 |        1000 |   4
          |    9 |        1001 |   4
          |   10 |        1010 |   4
          |   11 |        1011 |   4
          |   12 |        1100 |   4
          |   13 |        1101 |   4
          |   14 |        1110 |   4
          |   15 |        1111 |   4
          |   16 |       10000 |   5
          |   17 |       10001 |   5
          | 1023 |  1111111111 |  10
          | 1024 | 10000000000 |  11
          | 1025 | 10000000001 |  11

        Parameters:
          Input, integer N, the integer to be measured.
          N should be nonnegative. If N is nonpositive,
          the value will always be 0.

          Output, integer BIT, the number of bits base 2.
        r   r   é   )r   Úfloor)r   r   ÚiÚbitr   r   r   Ú
i4_bit_hi1<   s    %

zSobol.i4_bit_hi1c             C   s8   d}t j|ƒ}x$|d|d  kr2|d7 }|d }qW |S )ay  
        I4_BIT_LO0 returns the position of the low 0 bit base 2 in an integer.

        Example:
          +------+------------+----
          |    N |     Binary | BIT
          +------+------------+----
          |    0 |          0 |   1
          |    1 |          1 |   2
          |    2 |         10 |   1
          |    3 |         11 |   3
          |    4 |        100 |   1
          |    5 |        101 |   2
          |    6 |        110 |   1
          |    7 |        111 |   4
          |    8 |       1000 |   1
          |    9 |       1001 |   2
          |   10 |       1010 |   1
          |   11 |       1011 |   3
          |   12 |       1100 |   1
          |   13 |       1101 |   2
          |   14 |       1110 |   1
          |   15 |       1111 |   5
          |   16 |      10000 |   1
          |   17 |      10001 |   2
          | 1023 | 1111111111 |   1
          | 1024 | 0000000000 |   1
          | 1025 | 0000000001 |   1

        Parameters:
          Input, integer N, the integer to be measured.
          N should be nonnegative.

          Output, integer BIT, the position of the low 1 bit.
        r   r   )r   r    )r   r   r"   r!   r   r   r   Ú
i4_bit_lo0h   s    $
zSobol.i4_bit_lo0c          )   C   s¼  | j dkrd| _ dL| _| j  s*|| jkrxd| _ d| _dM| _d| _dN| _tj| j| jfƒ| _tjddddddddddddddddddddddddddddddddddddddddg(ƒ| jdd…df< tjddddddddddddddddddddddddddddddddddddddg&ƒ| jdd…df< tjdd	ddddd	d	ddddddd	ddd	ddddd	ddddd	ddd	ddd	dddg%ƒ| jdd…df< tjddd
dddddd
d	dddddd	dddd
dd
dddd	ddddd	dddd
g#ƒ| jd	d…df< tjd
ddddddddddddddddddd	ddddd	dddddd
dd
g!ƒ| jdd…df< tjdddd	dddddddddddddd
dddddddddgƒ| jdd…d	f< tjddddddddd d!d"dd#d$ddd%d&d'd(d)gƒ| jdd…d*f< tjdddgƒ| jdd…df< ddddddddd#d+d&d,dd-d.d/d0d1dd2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFg(| _	d| j d | _
| j| j
ƒ| _d| jdd| j…f< || jkrV|| _xXtd|d ƒD ]D}| j	|d  }d}|d }x |dkrÜ|d }|d7 }q¾W | j	|d  }tj|ƒ}x6t|ddOƒD ]&}|d }|d| k||d < |}qW x°t|d | jd ƒD ]˜}| j|d || d f }	d}
x\td|d ƒD ]J}|
d9 }
||d  rvtjt|	ƒt|
| j|d || d f  ƒƒ}	qvW |	| j|d |d f< qDW qœW d}
xPt| jd ddPƒD ]:}|
d9 }
| jd|…|d f |
 | jd|…|d f< qþW dGd|
  | _tj|ƒ| _ttj|ƒƒ}|dk rrd}d}
|dkrtj|ƒ| _nb|| jd kr®| j|ƒ}
nD|| jkrVd| _tj|ƒ| _x|tt| jƒt|ƒƒD ]d}| j|ƒ}
xRtd|d ƒD ]@}tjt| j|d  ƒt| j|d |
d f ƒƒ| j|d < q W qâW | j|ƒ}
nœ| jd |k ròx€tt| jd ƒt|ƒƒD ]d}| j|ƒ}
xRtd|d ƒD ]@}tjt| j|d  ƒt| j|d |
d f ƒƒ| j|d < qžW q€W | j|ƒ}
| j|
k r,tdHƒ tdIƒ tdJ| j ƒ tdK|
 ƒ dS tj|ƒ}xntd|d ƒD ]\}| j|d  | j ||d < tjt| j|d  ƒt| j|d |
d f ƒƒ| j|d < qFW || _|d7 }||gS )Qaó  
        i4_sobol generates a new quasirandom Sobol vector with each call.

        Discussion:
          The routine adapts the ideas of Antonov and Saleev.

        Reference:
          Antonov, Saleev,
          USSR Computational Mathematics and Mathematical Physics,
          Volume 19, 1980, pages 252 - 256.

          Paul Bratley, Bennett Fox,
          Algorithm 659:
          Implementing Sobol's Quasirandom Sequence Generator,
          ACM Transactions on Mathematical Software,
          Volume 14, Number 1, pp. 88-100, 1988.

          Bennett Fox,
          Algorithm 647:
          Implementation and Relative Efficiency of Quasirandom
          Sequence Generators,
          ACM Transactions on Mathematical Software,
          Volume 12, Number 4, pp. 362-376, 1986.

          Ilya Sobol,
          USSR Computational Mathematics and Mathematical Physics,
          Volume 16, pp. 236-242, 1977.

          Ilya Sobol, Levitan,
          The Production of Points Uniformly Distributed in a Multidimensional
          Cube (in Russian),
          Preprint IPM Akad. Nauk SSSR,
          Number 40, Moscow 1976.

        Parameters:
          Input, integer DIM_NUM, the number of spatial dimensions.
          DIM_NUM must satisfy 1 <= DIM_NUM <= 40.

          Input/output, integer SEED, the "seed" for the sequence.
          This is essentially the index in the sequence of the quasirandom
          value to be generated. On output, SEED has been set to the
          appropriate next value, usually simply SEED+1.
          If SEED is less than 0 on input, it is treated as though it were 0.
          An input value of 0 requests the first (0th) element of the sequence.

          Output, real QUASI(DIM_NUM), the next quasirandom vector.
        Nr   r   é(   é   é   r   é   é   é	   é   é   é   é   é   é   é   é   é   é   é   é   é%   é!   é'   é?   é1   és   é)   éO   éw   éK   éI   éi   é;   éA   éq   é=   éY   é-   ék   é   é/   é7   éC   éa   é[   ém   ég   éƒ   éÁ   é‰   é‘   é   éñ   é   é¹   é§   éå   é«   éÕ   é¿   éý   éË   éÓ   éï   é÷   i  iq  i+  g      ð?zI4_SOBOL - Fatal error!z  Too many calls!z  MAXCOL = %d
z  L =      %d
éÿÿÿÿrd   rd   rd   rd   )r   r   r   r   r   r   Zzerosr   Z	transposer
   r   r#   r	   r   Zbitwise_xorÚintr   r   r    r$   Úprint)r   r   r   r!   r   ÚmZincludÚkZj2ZnewvZlseedZ	seed_tempZquasir   r   r   r   “   sò    2
(((
((((

*"2


B
B

<zSobol.i4_sobolN)r   )Ú__name__Ú
__module__Ú__qualname__r   r   r#   r$   r   r   r   r   r   r      s
   
,+)Ú__doc__Znumpyr   Ú__all__r   r   r   r   r   Ú<module>   s   