You should read this
. It's much easier to read your post if you use code tags to denote any Python from math or anything else. And is g supposed to return a string? A special object?
What you're doing isn't just about fractions, it's about working with polynomials by using vectors to describe the coefficients. Numpy or scipy might have functionality for that, although the Fraction class in fractions is for straight numbers (although you can indeed get a numerator and denominator out of a Fraction object). Also, you're not just working with polynomials, it looks like you even want to factor them. I couldn't find factoring functions with a very quick Google search. There might be other modules for it, or you might me able to add the functionality on top of numpy.