Skip to content Skip to sidebar Skip to footer

Sympy Simplify Eliminate Imaginary Numbers

I'm trying to get the cosine similarity between convolved vectors. Because I'm using fast fourier transform, I am using complex numbers. In the calculation of the cosine similarity

Solution 1:

If you know the imaginary part is 0 then you can just take the real part of the evaluation, else use "chop=True" with caution to discard relatively small imaginary parts:

>>> q
(-1.53283653303955 + 6.08703605256546e-17*I)/(sqrt(5.69974497311137 + 
5.55111512312578e-17*I)*sqrt(14.2393958011541 - 3.46944695195361e-18*I))
>>> q.n(chop=True)
-0.170146237401735
>>> re(q.n())
-0.170146237401735

Post a Comment for "Sympy Simplify Eliminate Imaginary Numbers"