Ask Your Question

Revision history [back]

Definitely we want that Sage reorders... We want A+B+A to become 2A + B...

I do not think it would be hard to manage a pretty print that put first the "positive" terms and then the negative ones (knowing that negative are considered as mul(x,-1)).

Here is a simple function that do it on the first level.

def pretty_print(E):
    if E.operator() == operator.add:
        pos = []
        neg = []
        for elt in E.operands():
            if elt.operator() == operator.mul and len(elt.operands()) == 2 and elt.operands()[1] < 0:
                neg.append(-elt)
            else:
                pos.append(elt)
    if pos and neg:
        print '+'.join(map(repr,pos)) + ' - (' + '+'.join(map(repr,neg)) + ')'

Then inside Sage

sage: var('x,y,z,t')
sage: pretty_print(y-x-3*z+t)
t+y - (x+3*z)

Definitely we want that Sage reorders... We want A+B+A to become 2A + B...

I do not think it would be hard to manage a pretty print that put first the "positive" terms and then the negative ones (knowing that negative are considered as mul(x,-1)).

Here is a simple function that do it on the first level.

def pretty_print(E):
    if E.operator() == operator.add:
        pos = []
        neg = []
        for elt in E.operands():
            if elt.operator() (elt.operator() == operator.mul and len(elt.operands()) elt.operands()[-1] < 0) or (elt.is_numeric() and elt < 0):
                neg.append(-elt)
            else:
                pos.append(elt)
        pos_str = ' + '.join(map(repr,pos))
        neg_str = ' + '.join(map(repr,neg))
        if len(neg) == 2 and elt.operands()[1] < 0:
                neg.append(-elt)
    print pos_str
        elif len(neg) == 1:
            print pos_str + ' - ' + neg_str
        else:
                pos.append(elt)
    if pos and neg:
        print '+'.join(map(repr,pos)) print ' + '.join(map(str,pos)) + ' - (' + '+'.join(map(repr,neg)) ' + '.join(map(str,neg)) + ')'

    else:
        print E

Then inside Sage

sage: var('x,y,z,t')
sage:     sage: pretty_print(x-y+z)
x + z - y
sage:     sage: pretty_print(z-x-y)
z - (x + y)
sage:     sage: pretty_print(y-x-3*z+t)
t+y - (x+3*z)
t + y - (x + 3*z)
sage: pretty_print(3*x*y - 2*z*t - 3)
3*x*y - (2*t*z + 3)

Definitely we want that Sage reorders... We want A+B+A to become 2A + B...

I do not think it would be hard to manage a pretty print that put first the "positive" terms and then the negative ones (knowing that negative are considered as mul(x,-1)).

Here is a simple function that do it on the first level.

def pretty_print(E):
    if E.operator() == operator.add:
        pos = []
        neg = []
        for elt in E.operands():
            if (elt.operator() == operator.mul and elt.operands()[-1] < 0) or (elt.is_numeric() and elt < 0):
                neg.append(-elt)
            else:
                pos.append(elt)
        pos_str = ' + '.join(map(repr,pos))
        neg_str = ' + '.join(map(repr,neg))
        if len(neg) == 0:
            print pos_str
        elif len(neg) == 1:
            print pos_str + ' - ' + neg_str
        else:
            print ' + '.join(map(str,pos)) + ' - (' + ' + '.join(map(str,neg)) + ')'

    else:
        print E

Then inside Sage

sage: var('x,y,z,t')
sage:     sage: pretty_print(x-y+z)
x + z - y
sage:     sage: pretty_print(z-x-y)
z - (x + y)
sage:     sage: pretty_print(y-x-3*z+t)
t + y - (x + 3*z)
sage: pretty_print(3*x*y - 2*z*t - 3)
3*x*y - (2*t*z + 3)