On Fri, Feb 22, 2013 at 6:46 PM, David Mashburn <[email protected]> wrote: > Chris, > > Thanks! I took your suggestion of using "as_independent" and made a function > that now behaves the way I described in the first post: > > import sympy > from sympy import S,N,sqrt,Poly > x,y = S('x'),S('y') > > def GetCoefficientsOfFreeVariables(eq): > if eq.__class__==sympy.Mul: > coeffs,vars = zip(eq.as_independent(*eq.free_symbols,as_Add=False)) > elif eq.__class__==sympy.Add: > coeffs,vars = zip(*(i.as_independent(*i.free_symbols,as_Add=False) > for i in eq.args)) > return coeffs,vars
You might want to add an else clause to this. What if you just pass GetCoefficientsOfFreeVariables(x)? Aaron Meurer > > p = 4*sqrt(97)*x/97 - 2*sqrt(85)*y/85 > p2 = 4*sqrt(97)*x/97 > p3 = 4*sqrt(97)*x/97 - 2*sqrt(85)*y/85 + 4*sqrt(2)*x*y > > GetCoefficientsOfFreeVariables(p) > -> ((4*sqrt(97)/97, -2*sqrt(85)/85), (x, y)) > > GetCoefficientsOfFreeVariables(p2) > -> ((4*sqrt(97)/97,), (x,)) > > GetCoefficientsOfFreeVariables(p3) > -> ((4*sqrt(97)/97, -2*sqrt(85)/85, 4*sqrt(2)), (x, y, x*y)) > > > I have not tested this for speed, but I see no reason it would be any slower > than as_coefficient_dict. Is there anything else I could do to improve / > generalize this function? > > Thanks, > -David > > > On Friday, February 22, 2013 11:57:06 AM UTC-6, smichr wrote: >> >> On Fri, Feb 22, 2013 at 10:31 PM, David Mashburn >> <[email protected]> wrote: >> > Hello everyone, >> > >> > I have been using the method "as_coeff_dictionary" to get fast access to >> > the >> > coefficients of simple multi-variable linear polynomials (a*x+b*y+... >> > where >> > x,y are symbols and a,b are numerical). >> > >> > For the most part it works great, but I ran into a couple of quirks with >> > this approach. First of all, "as_coeff_dictionary" only treats floating >> > point or integer values as coefficients, and any irrational or more >> >> The definition of coefficient in sympy is generally a Number (not >> number). For one, 'is_Number' is true and the other, 'is_number' is >> True. >> >> > complicated pure numerical expressions become part of the "variable" >> > instead. I can live with this by applying N first, but ideally a method >> > that >> > could split off any numerical factors from the true variables would be >> > preferable to me. >> >> Perhaps this will suffice? >> >> >>> eq >> 2*sqrt(3)*x**2 >> >>> eq.as_independent(*eq.free_symbols, as_Add=False) >> (2*sqrt(3), x**2) >> >> The as_Add argument being False forces the expression to be treated as a >> Mul. >> >> > >> > More insidious than this (at least to my way of looking at it) is that >> > passing a Mul object instead of an Add results in things like >> > {<number>*x : >> > 1} instead of {x:<number>}. >> >> The method treates everything like a product so the keys are always >> the factors -- if a Add is sent, it treats that as the single factor. >> This way you always know that the expression can be rebuilt by >> multiplying the the keys raised to their exponents. >> >> The above method I showed is pretty straight forward. Whether >> something else is better or not depends on what you want to do with >> the pieces. > > -- > You received this message because you are subscribed to the Google Groups > "sympy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/sympy?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > > -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sympy?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
