> >> 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 
>
> Note that this is not a symbolic expression, but a "proper" polynomial. 
> But the original question was about symbolic expressions. Perhaps the 
> original poster has a reason for using symbolic expressions? 
>

Right, my question is on expressions.  That's how I store my data.  In 
short, I just want something that is like Sympy's as_coefficients_dict for 
expressions  (I've put its documentation at the end of this msg).  However, 
based on Dima's feedbacks above, I've written something like this  for the 
input expression  p  

vs = p.variables()
pr_p = PolynomialRing(ZZ,vs, None if len(vs) > 1 else 1)(p)
cs = pr_p.coefficients()
ts = map(SR, pr_p.monomials())
return cs,ts




 

>
> Perhaps the following does what is requested? 
>   sage: var('x','a') 
>   (x, a) 
>   sage: b = (3*x + a*x + 4) 
>   sage: b.operands() 
>   [a*x, 3*x, 4] 
>
> It would probably not be too dificult to extract the constant 
> coefficient of each operand. 
>


How do I do get the coefficients of these operands ?   


 

>
> It all depends on what answer you would expect. Say, if you present an 
> equal polynomial symbolic expression by 
>   sage: c = (3+a)*x + 4 
> Then you get 
>   sage: c.operands() 
>   [(a + 3)*x, 4] 
>
 Is this what you want? Or do you want that an automatic expansion into a 

> sum takes place? In this case it might really be better to use 
> polynomials. 
>


I don't need automatic expansion,  if the input is (3+a)*x + 4, then I want 
the output to be something like what sympy's as_coefficients_dict() would 
give

In [44]: ((3+x)*y+4).as_coefficients_dict()
Out[44]: defaultdict(<type 'int'>, {1: 4, y*(x + 3): 1})

 


Doc of sympy's as_coefficients_dict()


Return a dictionary mapping terms to their Rational coefficient.
        Since the dictionary is a defaultdict, inquiries about terms which
        were not present will return a coefficient of 0. If an expression is
        not an Add it is considered to have a single term.

        Examples
        ========

        >>> from sympy.abc import a, x
        >>> (3*x + a*x + 4).as_coefficients_dict()
        {1: 4, x: 3, a*x: 1}
        >>> _[a]
        0
        >>> (3*a*x).as_coefficients_dict()
        {a*x: 3}





> Cheers, 
> Simon 
>
>
>

-- 
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