Ask Your Question

Revision history [back]

One way to do that is to factor the numerator and then the denominator:

sage: print('{}/({})'.format(f.numerator().factor(), f.denominator().factor()))
(x - 1)^2/(x + 1)

One could turn this into a function with a special case for no denominator:

def pretty_print_factor(f):
    d = f.denominator()
    if d == 1:
        print(f.factor())
    else:
        n = f.numerator().factor()
        print('{}/({})'.format(n, d.factor()))

Using this function, we get:

sage: pretty_print_factor(f)
(x - 1)^2/(x + 1)

One way to do that is to factor the numerator and then the denominator:

sage: print('{}/({})'.format(f.numerator().factor(), print('({})/({})'.format(f.numerator().factor(), f.denominator().factor()))
(x ((x - 1)^2/(x 1)^2)/(x + 1)

One could turn this into a function with a special case for no denominator:

def pretty_print_factor(f):
    d = f.denominator()
    if d == 1:
        print(f.factor())
    else:
        n = f.numerator().factor()
        print('{}/({})'.format(n, print('({})/({})'.format(n, d.factor()))

Using this function, we get:

sage: pretty_print_factor(f)
(x ((x - 1)^2/(x 1)^2)/(x + 1)

The function could be improved to leave out parenthesis when not needed.