I'm using the Matlab linspace
function and the range :
operator to obtain equally spaced vectors, but I'm unespectedly receiving unequally spaced numbers. My code is the following:
format long
x1 = linspace(3,5,20);
diff(x1)
x2 = 3:0.1:5;
diff(x2)
and the outputs of the vectors differences (diff
) are the following:
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
To solve this problem, I'm using Kahan summation approach by the following code:
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)
I'm now obtaining an equispaced vector
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
However, this approach is "sequential". Is anyone aware of a vectorized or parallel implementation of the Kahan summation approach to improve the efficiency or porting it to a (CUDA) parallel reduction?
Thank you in advance.