Complex numbers have two parts one that is a regular number (real) and another that is like a special extra part (imaginary). Floats, on the flip side, are straightforward numbers with decimals. So, complex numbers are a bit fancy with both real and imaginary bits, while float are just regular numbers with dots.
Python Complex to Float
Below are some ways and examples by which we can convert complex numbers to float in Python:
Convert Real Part to Float in Python
In this example, a complex number (3 + 4j) is assigned to the variable "complex_number." The real part of the complex number is then converted to a float.
Python3
complex_number = 3 + 4j
real_part_float = float(complex_number.real)
print("Complex Number:", complex_number)
print("Real Part as Float:", real_part_float)
OutputComplex Number: (3+4j)
Real Part as Float: 3.0
Converting Imaginary Part to Float in Python
In this example, a complex number (3 + 4j) is assigned to the variable "complex_number." The imaginary part of the complex number is then converted to a float.
Python3
complex_number = 3 + 4j
imag_part_float = float(complex_number.imag)
print("Complex Number:", complex_number)
print("Imaginary Part as Float:", imag_part_float)
OutputComplex Number: (3+4j)
Imaginary Part as Float: 4.0
Python Complex Matrix Multiplication with Float Output
In this example, two complex matrices, "matrix_a" and "matrix_b," are multiplied using NumPy dot product function. The resulting complex matrix is stored in "result_matrix," and its real part is extracted and converted to a float. The original matrices, the result matrix, and the real part as a float are then printed.
Python3
import numpy as np
matrix_a = np.array([[2 + 3j, 1 - 2j], [0.5 + 1j, -2]])
matrix_b = np.array([[1 - 1j, 2 + 2j], [3j, 4]])
result_matrix = np.dot(matrix_a, matrix_b)
real_part = float(result_matrix.real)
print("Matrix A:\n", matrix_a)
print("Matrix B:\n", matrix_b)
print("Result Matrix (Real Part as Float):\n", result_matrix)
print("Real Part as Float:", real_part)