实现四维Halton序列
Halton序列的伪代码可以在here找到。我写了一个函数来做这件事,但由于某种原因检查了第四维Halton序列的Matlab结果,我的数字不匹配,我不知道为什么。这里是我的代码:实现四维Halton序列
double Halton_Seq(int index, double base){ double f = 1, r;
while(index > 0){
f = f/base;
r = r + f*(fmod(index,base));
index = index/base;
}
return r;
}
以下是前10个结果,我得到:
1 0.25
0.5
0.75
0.0625
0.3125
0.5625
0.8125
0.125
0.375
这里是前10个结果MATLAB得到:
Columns 1 through 2 0 0.5000
Columns 3 through 4
0.2500 0.7500
Columns 5 through 6
0.1250 0.6250
Columns 7 through 8
0.3750 0.8750
Columns 9 through 10
0.0625 0.5625
回答:
你忘了初始化r
在第2行。
r = 0;
double Halton_Seq(int index, int base){ double f = 1, r = 0;
while(index > 0){
f = f/base;
r = r + f* (index% base);
index = index/base;
}
return r;
}
// Output for 10 (base 2)
0.000000
0.500000
0.250000
0.750000
0.125000
0.625000
0.375000
0.875000
0.062500
0.562500
以上是 实现四维Halton序列 的全部内容, 来源链接: utcz.com/qa/259687.html