math - Factoring fractions in sympy -
how ask sympy factor x**2 - 3/2*x + 1/2 (x-1)*(x-1/2)? 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, factor expression [3], product arguments [5], , multiply them [6]. multiplication in step [6] calls evaluation routines simplify expression, not point of expanding it.
Comments
Post a Comment