How do I ask sympy to factor x**2 - 3/2*x + 1/2 into (x-1)*(x-1/2)? The closest I've gotten is:
>>> (x**2 - Rational(3/2)*x + Rational(1/2)).factor()
(x - 1)*(2*x - 1)/2
A simple way:
In [1]: expr = (x**2 - Rational(3/2)*x + Rational(1/2))
In [2]: expr
Out[2]:
2 3*x 1
x - --- + -
2 2
In [3]: fe = factor(expr)
In [4]: fe
Out[4]:
(x - 1)*(2*x - 1)
-----------------
2
In [5]: fe.args
Out[5]: (1/2, x - 1, 2*x - 1)
In [6]: fe.args[0]*fe.args[2]*fe.args[1]
Out[6]: (x - 1)*(x - 1/2)
That is, you factor the expression [3], then get the product arguments [5], and multiply them [6]. The multiplication in step [6] calls some evaluation routines which slightly simplify the expression, but not to the point of expanding it.