extract terms and their coefficients from an expression

704 views
Skip to first unread message

tvn

unread,
Mar 30, 2013, 6:54:20 PM3/30/13
to sage-s...@googlegroups.com
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.  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}



Dima Pasechnik

unread,
Mar 30, 2013, 11:49:55 PM3/30/13
to sage-s...@googlegroups.com
On 2013-03-30, tvn <nguyent...@gmail.com> 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)

Simon King

unread,
Mar 31, 2013, 6:53:03 PM3/31/13
to sage-s...@googlegroups.com
Hi!

On 2013-03-31, Dima Pasechnik <dim...@gmail.com> wrote:
> On 2013-03-30, tvn <nguyent...@gmail.com> 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

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?

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.

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.

Cheers,
Simon


tvn

unread,
Apr 1, 2013, 10:49:05 AM4/1/13
to sage-s...@googlegroups.com

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


Dima Pasechnik

unread,
Apr 2, 2013, 11:04:28 PM4/2/13
to sage-s...@googlegroups.com
On 2013-04-01, tvn <nguyent...@gmail.com> wrote:
> ------=_Part_2931_5871514.1364827746074
> Content-Type: text/plain; charset=ISO-8859-1
something like this:

sage: b = (3*x^2 + a*x + 4)
sage: [t.coeffs(x) for t in b.operands()]
[[[a, 1]], [[3, 2]], [[4, 0]]]
Reply all
Reply to author
Forward
0 new messages