On Mar 15, 4:42 am, Dan Schult <dsch...@colgate.edu> wrote: > > It would be great if you could provide a concrete test case (with your > > expected result) to work on. You are welcome to open a new issue for > > this in our issue tracker. > > I'll try.... I've simplified my actual problem to get a shorter answer. > The equations come from circuits and the result we're looking for is > "p". > Change the circuit a little and "p" changes. > > import sympy > from sympy.abc import * > > all_eqn=[-c+c*a+s*a-m*C-m*P,\ > -H-f+m*P-L*y,\ > c+s-n,\ > p+y-c,\ > y-D*H/R] > > solve=[H,c,p,s,y] > > soln=sympy.solve(all_eqn,solve) > for key,val in soln.iteritems(): > print key,":",val > > Output is: > H : (-R*f + P*R*m)/(R + D*L) > p : (D*f + R*a*n - C*R*m - D*P*m - P*R*m + D*L*a*n - C*D*L*m - > D*L*P*m)/(R + D*L) > y : (-D*f + D*P*m)/(R + D*L) > c : a*n - C*m - P*m > s : n + C*m + P*m - a*n > > I'd like to have > p: a*n - C*m - P*m + (D*f - D*P*m)/(R+D*L) > > In other words, it would be nice to divide the numer by the denom > leaving any remainder > as a fraction. In yet other words, I'd like to factor the > denominator out of the numerator > to the extent possible. > > The expression: > cancel(collect(expand(val),solve)) > works in some cases, but not in others (I think because > e.g. C*R*m+C*D*L*m can't factor C*m easily unless you > tell sympy to factor C*m -- but I'm guessing here as I don't really > know) >
I noticed this, too, when working with your expression. >>> exec("%s=symbols('%s')" % (("n,P,R,D,m,f,a,C,L",)*2)) >>> eq=(D*f + R*a*n - C*R*m - D*P*m - P*R*m + D*L*a*n - C*D*L*m - D*L*P*m)/(R + >>> D*L) >>> cancel(collect(eq.expand(mul=1), >>> list(numer(eq).atoms(Symbol).difference(denom(eq).atoms(Symbol))))) a*n + (-D*P*m - P*R*m - D*L*P*m)/(R + D*L) - C*m + D*f/(R + D*L) I think the thing to do is to use polynomial division which is going to tell you exactly (as in a numerical division) what the whole and remainder parts are: >>> n,d = eq.as_numer_denom(); s = eq.atoms(Symbol); w,r = >>> div(Poly(n,*s),Poly(d, *s)) >>> print (w, r/d) (a*n - C*m - P*m, (D*f - D*P*m)/(R + D*L)) -- You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to sy...@googlegroups.com. To unsubscribe from this group, send email to sympy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sympy?hl=en.