我正在使用 Matlablinspace
函数和范围:
运算符来获得等距的向量,但我意外地收到了不等距的数字。我的代码如下:
format long
x1 = linspace(3,5,20);
diff(x1)
x2 = 3:0.1:5;
diff(x2)
向量差 ( diff
) 的输出如下:
x1
0.105263157894737
0.105263157894737
0.105263157894737
0.105263157894737
0.105263157894737
0.105263157894737
0.105263157894737
0.105263157894737
0.105263157894737
0.105263157894737
0.105263157894737
0.105263157894737
0.105263157894736
0.105263157894737
0.105263157894737
0.105263157894736
0.105263157894737
0.105263157894736
0.105263157894737
x2
0.100000000000000
0.100000000000000
0.100000000000000
0.100000000000000
0.100000000000000
0.100000000000000
0.100000000000000
0.100000000000000
0.100000000000000
0.100000000000000
0.100000000000000
0.100000000000001
0.100000000000000
0.100000000000001
0.100000000000000
0.100000000000000
0.100000000000001
0.100000000000000
0.100000000000001
0.100000000000000
为了解决这个问题,我通过以下代码使用 Kahan 求和方法:
dx = 2/19;
x3 = zeros(size(x1));
x3(1) = 0;
partial_sum = 0;
c = 0.0;
for k=2:20,
y = dx - c;
t = partial_sum + y;
c = (t - partial_sum) - y;
partial_sum = t;
x3(k) = partial_sum;
end
diff(x3)
我现在正在获得一个等距向量
0.105263157894737
0.105263157894737
0.105263157894737
0.105263157894737
0.105263157894737
0.105263157894737
0.105263157894737
0.105263157894737
0.105263157894737
0.105263157894737
0.105263157894737
0.105263157894737
0.105263157894737
0.105263157894737
0.105263157894737
0.105263157894737
0.105263157894737
0.105263157894737
0.105263157894737
但是,这种方法是“顺序的”。是否有人知道 Kahan 求和方法的矢量化或并行实现以提高效率或将其移植到(CUDA)并行减少?
先感谢您。