Alternatively, a function can be used to toggle between the two representations:


def mixed(r):
    if isinstance(r, Integer):
        return r
    if isinstance(r, Rational):
        s = sign(r)
        p, q = s*r.p, r.q
        w, x = divmod(p, q)
        return '%s %s/%s' % (s*w, x, q) if w else r
    elif isinstance(r, str):
        try:
            a, b = [Rational(s) for s in r.split()]
            if a < 0:
                return a - b
            else:
                return a + b
        except:
            pass
    return r

R = Rational

>>> R(5,3)
5/3
>>> mixed(_)
'1 2/3'
>>> mixed(_)
5/3
>>> R(2,3)
2/3
>>> mixed(_)
2/3
>>> mixed(_)
2/3
>>> [mixed(i/S(3)) for i in range(10,16)]
['3 1/3', '3 2/3', 4, '4 1/3', '4 2/3', 5]
>>> [mixed(i) for i in _]
[10/3, 11/3, 4, 13/3, 14/3, 5]

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to