-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathhammersley.m
48 lines (45 loc) · 1.2 KB
/
hammersley.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function S = hammersley(D,N)
% HAMMERSLEY - Hammersley quasi-random sequence
%
% S = HAMMERSLEY(D,N) Generates N numbers from a D-dimensional
% Hammersley quasirandom sequence using successive primes
% as bases except for the last dimension which has the form
% [1:N]'/N-1/2/N (where the last term modifies usual Hammersley
% sequence to produce sequence in open interval (0,1)). The
% matrix S is of size DxN.
%
% Copyright (c) 2008 Aki Vehtari
% This software is distributed under the GNU General Public
% Licence (version 3 or later); please refer to the file
% Licence.txt, included with the software, for details.
S=zeros(N,D);
% Last column is easy
S(:,D)=([1:N]'/N)-1/(2*N);
% Matlab's PRIMES returns primes smaller than given value,
% but we need D-1 primes
pn=2*D;
p=primes(pn);
while(numel(p)<D-1)
pn=2*pn;
p=primes(pn);
end
P=p(1:D-1);
for k=1:D-1 % dimensions
pk=P(k);
for j=1:N % points in sequence
bj=j;
n = max(1,round(log2(bj+1)/log2(pk)));
while pk.^n <= bj
n = n + 1;
end
b=zeros(1,n);
b(n)=rem(bj,pk);
while bj && n>1
n=n-1;
bj=floor(bj/pk);
b(n)=rem(bj,pk);
end
S(j,k)=sum(fliplr(b)./pk.^[1:numel(b)]);
end
end
S = S';