I found something strange while working with complex matrices in python using numpy. I'll just make a short example to explain it:
This code is working totally fine:
import numpy as np
a = np.zeros((2, 2))
b = np.array([[1j, 1j], [1j, 2]])
a = a + b
print(a)
with output
[[0.+1.j 0.+1.j]
[0.+1.j 0.+1.j]]
But if I change the a = a + b to a += b (how I usually do it), it's giving me an error.
import numpy as np
a = np.zeros((2, 2))
b = np.array([[1j, 1j], [1j, 1j]])
a += b
print(a)
with error:
numpy.core._exceptions.UFuncTypeError: Cannot cast ufunc 'add' output from dtype('complex128') to dtype('float64') with casting rule 'same_kind'
Where does this come from? I just want to understand.