Skip to content Skip to sidebar Skip to footer

Why Can't My Python Code Return The Real Part Of A Complex Number?

I am having trouble extracting the real part of a complex number in my Python code. According to what I see, the program seems to recognize the whole complex number as real? impor

Solution 1:

The s object is a type of sympy.core.add.Add you should use sympy to extract specific parts:

>>> sympy.re(s)
16.3617836069885
>>> sympy.im(s)
-0.632549329011278

or convert them to a suitable format for the numpy

>>> np.complex(s)
(16.361783606988478-0.6325493290112776j)
>>> np.real(np.complex(s))
16.361783606988478
>>> np.imag(np.complex(s))
-0.6325493290112776

Convert from sympy.core.add.Add to numpy complex number


Post a Comment for "Why Can't My Python Code Return The Real Part Of A Complex Number?"