Re: [sympy] obtaining coefficients and terms of a given expression

1,757 views
Skip to first unread message

Aaron Meurer

unread,
Nov 28, 2012, 2:01:59 AM11/28/12
to sy...@googlegroups.com
On Tue, Nov 27, 2012 at 8:48 PM, ThanhVu Nguyen
<nguyent...@gmail.com> wrote:
> Hi, I came from the Sage (sagemath) background and am trying to use Sympy
> for my new project instead.
>
> I am wondering how to obtain the coefs and terms of a given expression in
> sympy. For example, given the expression a**2+b+5*c+2, I want to get the
> coefs [1,1,5,2] and the terms [a^2,b,c,1].

If you are just working with polynomials, the easiest way is to use
the Poly class, and use the all_coeffs method. I forget what the best
way otherwise is. You can use .coeff, but it doesn't generalize to
getting the constant term from more than one variable. as_independent
does it, but that only works correctly if the expression is an Add.

>
> Also, does Sympy has an equivalence to the class "Expression" of Sage ?
> By expression I mean symbols x,y,z, .. and operations over symbols x > y , x
> + 5 . Sympy seems to treat these separately, e.g. x,y,z are Symbols, x
> + 5 is Add, x > y is StrictGreaterThan. Perhaps all of these seperate
> classes inherit a common class ?

Yes, Expr is the superclass of all of these. And Basic is the
superclass of Expr. There are some classes that are Basic but not
Expr, which are classes that aren't symbolic expressions. Actually,
StrictInequality should be one of these, but it isn't.

Aaron Meurer

>
> Thanks,
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/sympy/-/5f-YQchcnjwJ.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to
> sympy+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sympy?hl=en.

Chris Smith

unread,
Nov 28, 2012, 2:22:03 AM11/28/12
to sy...@googlegroups.com

If you are just working with polynomials, the easiest way is to use
the Poly class, and use the all_coeffs method.  I forget what the best
way otherwise is.  You can use .coeff, but it doesn't generalize to
getting the constant term from more than one variable.  as_independent
does it, but that only works correctly if the expression is an Add.


What do you mean in this case, Aaron? 

Aaron Meurer

unread,
Nov 28, 2012, 2:39:24 AM11/28/12
to sy...@googlegroups.com
In [73]: x.as_independent(x)
Out[73]: (1, x)

If you want the constant term, 0, then this is the wrong method.

Aaron Meurer

>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.

Chris Smith

unread,
Nov 28, 2012, 3:41:29 AM11/28/12
to sy...@googlegroups.com

In [73]: x.as_independent(x)
Out[73]: (1, x)

If you want the constant term, 0, then this is the wrong method.


The as_Add flag was added to allow for this:

>>> x.as_independent(x, as_Add=False)
(0, x)


IMO, the "deps" flag should be added into as_coeff_Add and as_coeff_Mul and as_independent should be deprecated.

ThanhVu Nguyen

unread,
Nov 28, 2012, 8:50:45 PM11/28/12
to sy...@googlegroups.com


On Wednesday, November 28, 2012 2:01:59 AM UTC-5, Aaron Meurer wrote:
On Tue, Nov 27, 2012 at 8:48 PM, ThanhVu Nguyen
<nguyent...@gmail.com> wrote:
> Hi, I came from the Sage (sagemath) background and am trying to use Sympy
> for my new project instead.
>
> I am wondering how to obtain the coefs and terms of a given expression in
> sympy.  For example, given  the expression  a**2+b+5*c+2,  I want to get the
> coefs [1,1,5,2] and the terms [a^2,b,c,1].

If you are just working with polynomials, the easiest way is to use
the Poly class, and use the all_coeffs method.

Do you mean convert my expression to Poly and use the all_coefs method ?  I did that but it doesn't support multvariate poly  
Poly(a**2+b+5*c+2).all_coeffs()
PolynomialError:  ..


Chris Smith

unread,
Nov 28, 2012, 9:42:08 PM11/28/12
to sy...@googlegroups.com
Do you want something like this, then:

>>> eq=a**2+b+5*c+2
>>> eq.as_coefficients_dict()
defaultdict(<type 'int'>, {1: 2, c: 5, b: 1, a**2: 1})
>>> dict(_)
{1: 2, c: 5, b: 1, a**2: 1}

Aaron Meurer

unread,
Nov 28, 2012, 9:51:03 PM11/28/12
to sy...@googlegroups.com
On Wed, Nov 28, 2012 at 6:50 PM, ThanhVu Nguyen
<nguyent...@gmail.com> wrote:
>
>
> On Wednesday, November 28, 2012 2:01:59 AM UTC-5, Aaron Meurer wrote:
>>
>> On Tue, Nov 27, 2012 at 8:48 PM, ThanhVu Nguyen
>> <nguyent...@gmail.com> wrote:
>> > Hi, I came from the Sage (sagemath) background and am trying to use
>> > Sympy
>> > for my new project instead.
>> >
>> > I am wondering how to obtain the coefs and terms of a given expression
>> > in
>> > sympy. For example, given the expression a**2+b+5*c+2, I want to get
>> > the
>> > coefs [1,1,5,2] and the terms [a^2,b,c,1].
>>
>> If you are just working with polynomials, the easiest way is to use
>> the Poly class, and use the all_coeffs method.
>
>
> Do you mean convert my expression to Poly and use the all_coefs method ? I
> did that but it doesn't support multvariate poly
> Poly(a**2+b+5*c+2).all_coeffs()
> PolynomialError: ..

Oh I guess you just want expr.coeffs() then, or expr.terms().

Aaron Meurer

>
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/sympy/-/thgKwRaQ5wsJ.

ThanhVu Nguyen

unread,
Nov 28, 2012, 11:19:52 PM11/28/12
to sy...@googlegroups.com

>
> Do you mean convert my expression to Poly and use the all_coefs method ?  I
> did that but it doesn't support multvariate poly
> Poly(a**2+b+5*c+2).all_coeffs()
> PolynomialError:  ..

Oh I guess you just want expr.coeffs() then, or expr.terms().


expr.coeffs() works ok but expr.terms() returns 
In [30]: Poly(a**2+b+5*c+2).terms()
Out[30]: [((2, 0, 0), 1), ((0, 1, 0), 1), ((0, 0, 1), 5), ((0, 0, 0), 2)]

Not a,b,c,1  as I want.  However, the as_coefficients_dict() works well (and I don't have to convert it to a Poly) .  Thanks    
 
Reply all
Reply to author
Forward
0 new messages