帕斯卡中的高斯
Gaussian in pascal
我尝试将代码直接从 java 源代码移植到 Pascal,但是它抛出了 运行 时间错误。
怎样才能得到合适的高斯曲线?帕斯卡内置函数怎么样?
原始源代码:
synchronized public double nextGaussian() {
// See Knuth, ACP, Section 3.4.1 Algorithm C.
if (haveNextNextGaussian) {
haveNextNextGaussian = false;
return nextNextGaussian;
} else {
double v1, v2, s;
do {
v1 = 2 * nextDouble() - 1; // between -1 and 1
v2 = 2 * nextDouble() - 1; // between -1 and 1
s = v1 * v1 + v2 * v2;
} while (s >= 1 || s == 0);
double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s);
nextNextGaussian = v2 * multiplier;
haveNextNextGaussian = true;
return v1 * multiplier;
}
}
第一次尝试 pascal 端口(抛出 运行时间错误):
function log (n : double) : double;
begin
result := ln(n) / ln(10);
end;
var hgauss : boolean;
var ngauss : double;
function gauss() : double;
var x1, x2, w : double;
begin
if hgauss then
begin
result := ngauss;
hgauss := false;
end else
begin
repeat
x1 := 2.0 * rand() - 1.0;
x2 := 2.0 * rand() - 1.0;
w := x1 * x1 + x2 * x2;
until w >= 1.0;
w := sqrt( (-2.0 * log( w ) ) / w );
result := x1 * w;
ngauss := x2 * w;
hgauss := true;
end;
end;
这里的浮点运算无效:
w := sqrt((-2.0 * log( w ) ) / w);
第二次转换尝试(运行s 但我不确定数学是否正确):
function log (n : double) : double;
begin
result := ln(n) / ln(10);
end;
var hgauss : boolean;
var ngauss : double;
function gauss() : double;
var x1, x2, w, num : double;
begin
if hgauss then
begin
result := ngauss;
hgauss := false;
end else
begin
repeat
x1 := 2.0 * rand() - 1.0;
x2 := 2.0 * rand() - 1.0;
w := x1 * x1 + x2 * x2;
until w >= 1.0;
num := -2.0 * log( w ) / w;
w := sqrt(abs(num));
if num < 0 then w := -w;
result := x1 * w;
ngauss := x2 * w;
hgauss := true;
end;
end;
你从 Java 到 Pascal 的端口有一个重要部分有问题
do {...} while (s >= 1 || s == 0);
应该翻译成
repeat {...} until ((s<1) and (s<>0));
所以你有错误的终止条件。 Java 如果 0 < s < 1
则终止循环,但如果 w >= 1
.
则循环结束
如果 w > 1
你有 -2*ln(w) < 0
并且浮点异常来自对负数求平方根!
对于大多数 Pascal 版本,您对标准函数的命名是不寻常的,
IMO 应该是
repeat
x1 := 2.0 * random - 1.0;
x2 := 2.0 * random - 1.0;
w := x1 * x1 + x2 * x2;
until (w<1.0) and (w>0.0);
w := sqrt(-2.0*ln(w)/w);
result := x1 * w;
ngauss := x2 * w;
请注意,您确实必须使用 ln
而不是您自制的以 10 为底的对数 log
。使用的方法是Marsaglia's polar method.
rand()
在 [0,1)
范围内 ( 0 <= rand() < 1
)
所以 2.0 * rand() - 1.0
在 [-1,1)
范围内
所以 x1
和 x2
在 [-1,1)
范围内
所以 w := x1 * x1 + x2 * x2
在 [0,2)
范围内
并且在 sqrt( -2.0 * ln( w ) / w )
中 w
是正数
所以自然对数:ln(w) 应该是负数
所以 w 应该在 (0,1)
范围内
这样循环就不会退出 until (w > 0.0)and (w < 1.0);
工作示例代码(使用 SCAR Divi 3.41.00):
program New;
var hgauss : boolean;
var ngauss : double;
function gauss() : double;
var x1, x2, w : double;
begin
if hgauss then
begin
result := ngauss;
hgauss := false;
end else
begin
repeat
x1 := 2.0 * rand() - 1.0;
x2 := 2.0 * rand() - 1.0;
w := x1 * x1 + x2 * x2;
until (w > 0.0)and (w < 1.0);
w := sqrt( -2.0 * ln( w ) / w );
result := x1 * w;
ngauss := x2 * w;
hgauss := true;
end;
end;
begin
writeln( gauss() );
writeln( gauss() );
end.
我尝试将代码直接从 java 源代码移植到 Pascal,但是它抛出了 运行 时间错误。
怎样才能得到合适的高斯曲线?帕斯卡内置函数怎么样?
原始源代码:
synchronized public double nextGaussian() {
// See Knuth, ACP, Section 3.4.1 Algorithm C.
if (haveNextNextGaussian) {
haveNextNextGaussian = false;
return nextNextGaussian;
} else {
double v1, v2, s;
do {
v1 = 2 * nextDouble() - 1; // between -1 and 1
v2 = 2 * nextDouble() - 1; // between -1 and 1
s = v1 * v1 + v2 * v2;
} while (s >= 1 || s == 0);
double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s);
nextNextGaussian = v2 * multiplier;
haveNextNextGaussian = true;
return v1 * multiplier;
}
}
第一次尝试 pascal 端口(抛出 运行时间错误):
function log (n : double) : double;
begin
result := ln(n) / ln(10);
end;
var hgauss : boolean;
var ngauss : double;
function gauss() : double;
var x1, x2, w : double;
begin
if hgauss then
begin
result := ngauss;
hgauss := false;
end else
begin
repeat
x1 := 2.0 * rand() - 1.0;
x2 := 2.0 * rand() - 1.0;
w := x1 * x1 + x2 * x2;
until w >= 1.0;
w := sqrt( (-2.0 * log( w ) ) / w );
result := x1 * w;
ngauss := x2 * w;
hgauss := true;
end;
end;
这里的浮点运算无效:
w := sqrt((-2.0 * log( w ) ) / w);
第二次转换尝试(运行s 但我不确定数学是否正确):
function log (n : double) : double;
begin
result := ln(n) / ln(10);
end;
var hgauss : boolean;
var ngauss : double;
function gauss() : double;
var x1, x2, w, num : double;
begin
if hgauss then
begin
result := ngauss;
hgauss := false;
end else
begin
repeat
x1 := 2.0 * rand() - 1.0;
x2 := 2.0 * rand() - 1.0;
w := x1 * x1 + x2 * x2;
until w >= 1.0;
num := -2.0 * log( w ) / w;
w := sqrt(abs(num));
if num < 0 then w := -w;
result := x1 * w;
ngauss := x2 * w;
hgauss := true;
end;
end;
你从 Java 到 Pascal 的端口有一个重要部分有问题
do {...} while (s >= 1 || s == 0);
应该翻译成
repeat {...} until ((s<1) and (s<>0));
所以你有错误的终止条件。 Java 如果 0 < s < 1
则终止循环,但如果 w >= 1
.
如果 w > 1
你有 -2*ln(w) < 0
并且浮点异常来自对负数求平方根!
对于大多数 Pascal 版本,您对标准函数的命名是不寻常的, IMO 应该是
repeat
x1 := 2.0 * random - 1.0;
x2 := 2.0 * random - 1.0;
w := x1 * x1 + x2 * x2;
until (w<1.0) and (w>0.0);
w := sqrt(-2.0*ln(w)/w);
result := x1 * w;
ngauss := x2 * w;
请注意,您确实必须使用 ln
而不是您自制的以 10 为底的对数 log
。使用的方法是Marsaglia's polar method.
rand()
在 [0,1)
范围内 ( 0 <= rand() < 1
)
所以 2.0 * rand() - 1.0
在 [-1,1)
范围内
所以 x1
和 x2
在 [-1,1)
范围内
所以 w := x1 * x1 + x2 * x2
在 [0,2)
范围内
并且在 sqrt( -2.0 * ln( w ) / w )
中 w
是正数
所以自然对数:ln(w) 应该是负数
所以 w 应该在 (0,1)
范围内
这样循环就不会退出 until (w > 0.0)and (w < 1.0);
工作示例代码(使用 SCAR Divi 3.41.00):
program New;
var hgauss : boolean;
var ngauss : double;
function gauss() : double;
var x1, x2, w : double;
begin
if hgauss then
begin
result := ngauss;
hgauss := false;
end else
begin
repeat
x1 := 2.0 * rand() - 1.0;
x2 := 2.0 * rand() - 1.0;
w := x1 * x1 + x2 * x2;
until (w > 0.0)and (w < 1.0);
w := sqrt( -2.0 * ln( w ) / w );
result := x1 * w;
ngauss := x2 * w;
hgauss := true;
end;
end;
begin
writeln( gauss() );
writeln( gauss() );
end.