Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complex numbers with fractions

Tags:

python

So I need to hold something like this:

complex(fractions.Fraction(1, 3), fractions.Fraction(1, 3))

but python is changing fraction to floating point, and since I don't want to use floating point and stay with fractions is there any other implementation of complex numbers that can do what I want? or maybe there is a possibile way to do a small change to current implementation?

like image 810
apologiessirnoclue Avatar asked Oct 28 '22 13:10

apologiessirnoclue


1 Answers

You could just use sympy.

from sympy import sympify, I, re, im, expand

z = sympify(1)/3+I/3

Notice that I had to use sympify(1) since otherwise 1/3 would evaluate to a float rather than a sympy expression. Now you can do calculations with complex numbers like so

re(z) -> 1/3
im(z) -> 1/3
z*z= -> (1/3+𝑖/3)**2
expand(z*z) -> (2*i)/9
like image 128
user2640045 Avatar answered Nov 13 '22 06:11

user2640045