Skip to content
Advertisement

Solving linear equation of complex matrices

I have a linear equation of a form ax=b where b is a complex matrix, and a is a real matrix. I’m expecting solution x to be a complex vector. When trying the numpy.linalg.solve(a,b) function, I’m getting this warning:

ComplexWarning: Casting complex values to real discards the imaginary part

How do I solve this equation to preserve the complex numbers?

Advertisement

Answer

Split b into its real and imaginary components and solve the real/imaginary parts separately
Ax = b = A(xr+xi) = br + bi
A @ xr = br, A @ xi = bi

br = np.real(b)
bi = np.imag(b)
A_inv = np.linalg.inv(A)
xr = A_inv @ br
xi = A_inv @ bi
x = xr + 1.j*xi

EDIT The way you want to do it:

x = np.linalg.solve(A, np.real(b)) + 1.j*np.linalg.solve(A, np.imag(b))
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement