1
votes

I am trying to split up the multiplication of a dft matrix in to real and imaginary parts

from scipy.linalg import dft
improt numpy as np
# x is always real
x = np.ones(4)
W = dft(4)
Wx = W.dot(x)
Wxme = np.real(W).dot(x) + np.imag(W).dot(x)*1.0j

I would like that Wx and Wxme give the same value but they are not at all. I have narrowed down the bug a bit more:

In [62]: W[1]
Out[62]: 
array([  1.00000000e+00 +0.00000000e+00j,
         6.12323400e-17 -1.00000000e+00j,
        -1.00000000e+00 -1.22464680e-16j,  -1.83697020e-16 +1.00000000e+00j])

In [63]: np.sum(W[1])
Out[63]: (-2.2204460492503131e-16-1.1102230246251565e-16j)

In [64]: sum(W[1])
Out[64]: (-1.8369701987210297e-16-2.2204460492503131e-16j)

Why do sum and np.sum give different values ?? addition of complex numbers should not be anything but adding the real parts and the imaginary parts seperately right ??

Adding the by hand gives me the result I would expect as opposed to what numy gives me:

In [65]: 1.00000000e+00 + 6.12323400e-17 + -1.00000000e+00 + 1.83697020e-16
Out[65]: 1.8369702e-16

What am I missing ??

1
1 + -j - 1 + j = 0 - hpaulj

1 Answers

4
votes

Up to rounding error, these results are equal. The results have slightly different rounding error due to factors such as different summation order or different levels of precision used to represent intermediate results.