On 2013-03-30, tvn <[email protected]> wrote:
> ------=_Part_1723_8861826.1364684060099
> Content-Type: text/plain; charset=ISO-8859-1
>
> In sympy there's a method call as_coeffficients_dict()  that returns all 
> the terms and their coefficients from an expression.  I am if I can do 
> something like this in Sage.  
you can do a similar thing, except that the monomials are encoded by
their exponents

sage: R.<x,a> = ZZ[]
sage: p=x^2*a-a^2+a-4
sage: p.dict()
{(0, 0): -4, (0, 1): 1, (0, 2): -1, (2, 1): 1}
sage: 

That is, each term "(i,j): t" corresponds to monimial
x^i a^j having non-zero coefficient t.

The conversion is easy, by getting the variables of p as follows:
sage: p.variables()
(x, a)


For examples
>
>
>
>         >>> (3*x + a*x + 4).as_coefficients_dict()
>         {1: 4, x: 3, a*x: 1}
>
>         >>> (3*a*x).as_coefficients_dict()
>         {a*x: 3}
>
>        >>> (x-oo).as_coefficients_dict()     #can also deal with infinity  
>        {1: -oo, x: 1}
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" 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/sage-support?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to