I wouldn't bother checking. If the expression is not a fraction, yes the denom term will be 1, but the code I suggested will do nothing to it.
>>> (-x).could_extract_minus_sign() True >>> n,d = (-x).as_numer_denom() >>> n -x >>> d 1 >>> -n/-d -x One thing you might want to consider is that as_numer_denom() seems to pull together Adds with a common denominator >>> (x-y).as_numer_denom() (x - y, 1) >>> (x-y/(1-x)).as_numer_denom() (-y + x⋅(1 - x), 1 - x) >>> (x-y/(1-x)).could_extract_minus_sign() True >>> n, d = (x-y/(1-x)).as_numer_denom() >>> -n/-d -(y - x*(1 - x))/(1 - x) >>> print simplify((x-y/(1-x))) (x - y - x**2)/(1 - x) >>> ((x - y - x**2)/(1 - x)).could_extract_minus_sign() False I would recommend putting the code at the end of simplify. As you can see, the above looks much nicer if you run it through simplify first. Aaron Meurer On Jun 8, 2009, at 12:10 PM, Ryan Krauss wrote: > I like that. I am glad to write a patch if this is the desired > behavior of simplify. Does there need to be a check for whether or > not something is a fraction first? Is checking for denom == 1 the > best way to do that? > > In [146]: a,b = x2_tf_ds.as_numer_denom() > > In [147]: a > Out[147]: Gc*k > > In [148]: b > Out[148]: Gc*k + k*m1*s**2 + k*m2*s**2 + m1*m2*s**4 > > In [149]: b.as_numer_denom() > Out[149]: (Gc*k + k*m1*s**2 + k*m2*s**2 + m1*m2*s**4, 1) > > Ryan > > On Mon, Jun 8, 2009 at 12:45 PM, Aaron S. Meurer > <[email protected]> wrote: > > You might look into could_extract_minus_sign: > >>> sympify('-Gc*k/(-Gc*k - k*m1*s**2 - k*m2*s**2 - > m1*m2*s**4)').could_extract_minus_sign() > True > >>> sympify('Gc*k/(-Gc*k - k*m1*s**2 - k*m2*s**2 - > m1*m2*s**4)').could_extract_minus_sign() > False > > Perhaps simplify() should do > > if expr.could_extract_minus_sign(): > n, d = expr.as_numer_denom() > expr = sympify(-1)*n/(sympify(-1)*d) > > or some similar. > > > Aaron Meurer > On Jun 8, 2009, at 11:36 AM, Ryan Krauss wrote: > > > So, I have yet another simplification question. What is the correct > > way to factor a minus one out of this: > > > > In [108]: type(x2_tf_ds) > > Out[108]: <class 'sympy.core.mul.Mul'> > > > > In [109]: x2_tf_ds > > Out[109]: -Gc*k/(-Gc*k - k*m1*s**2 - k*m2*s**2 - m1*m2*s**4) > > > > This didn't work: > > In [110]: sympy.simplify(x2_tf_ds) > > Out[110]: -Gc*k/(-Gc*k - k*m1*s**2 - k*m2*s**2 - m1*m2*s**4) > > > > This works, and is fine with me. But I wonder if there is a better > > way: > > In [111]: a,b = x2_tf_ds.as_numer_denom() > > > > In [112]: -a/-b > > Out[112]: Gc*k/(Gc*k + k*m1*s**2 + k*m2*s**2 + m1*m2*s**4) > > > > > > Thanks, > > > > Ryan > > > > > > > > > > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
