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.