pythonarraysnumpypointer-aliasing

Does NumPy handle 1:1 aliasing of complex-number operations correctly?


Say a and b are disjoint 1D complex NumPy arrays, and I do numpy.multiply(a, b, b).
Is b guaranteed to contain the same values as I would get via b[:] = numpy.multiply(a, b)?

I haven't actually been able to produce incorrect results, but I don't know if I'm just being lucky with my particular compilation or platform or if I can actually rely on that, hence the question.

Notice that with float (i.e. real numbers) the answer would obviously yes, since a reasonable implementation can't make it fail, but with complex numbers it's easy for the cross-multiply operation to give incorrect results by writing the real part and then reading an imaginary part:

# say the real part is at [0] and the imaginary part is at [1] and c is the product of a & b
c[0] = a[0] * b[0] - a[1] * b[1]
c[1] = a[0] * b[1] + a[1] * b[0]  # if c[0] overlaps a[0] or b[0] then this is wrong

Solution

  • Yes. Complex values ought to be treated atomically. If it doesn't work like that, then it's a bug that we'll fix.