Search code examples
arrayspython-3.xcomplex-numbers

Creating a complex 2D-array from two 2D-real-arrays ? - Python 3


Consider the following two real 2D-array

real = [x_00,x_01,....,x_0m]
       [x_10,x_11,....,x_1m]
       ......
       [x_n0,x_n1,....,x_nm]

imag = [y_00,y_01,....,y_0m]
       [y_10,y_11,....,y_1m]
       ......
       [y_n0,y_n1,....,y_nm]

Im looking for a fast method (in my case n = 10**7) to convert the imag array to a complex one and add it to the reall array

z = x + i*imag

In the end i want a complex array with the following output (basic matrix addition):

z = [x_00 + i * y_00, x_01 + i * y_01, ... , x_0m + i*y_0m]
     ....
    [x_n0 + i * y_n0, x_n1 + i * y_n1, ... , x_nm + i*y_nm]

Example Code:

real = np.array([[1,2],[3,4]])
imag = np.array([[1,2],[3,4]])

Looking for output:

z = np.array([[1+1j, 2+2j],[3+3j, 4+4j]])

Solution

  • It is simply

    z=real+1j*imag
    

    (using your np.arrays)