Using the .coeff to get the coefficient of an expression using sympy parser

133 views
Skip to first unread message

Abdul-Azeez Isiaq

unread,
Mar 24, 2014, 8:07:25 AM3/24/14
to sy...@googlegroups.com
I am quite new to python and am using the sympy library to work on some mathematical code using the parser input. The problem is that it parses the coefficient of x as the constant when there is no constant in the expression. Below is a simple example of what am trying to explain.
>>> parse_expr("2x", transformations=transformations)
2x
>>> eqn= parse_expr("2x", transformations=transformations)
>>> eqn.coeff('x',0)
2
>>> eqn.coeff('x')
2
]
 At least am very sure eqn.coeff('x',0) which is a constant is eqn  to 0  in this equation but the compiler says it is 2.

Aaron Meurer

unread,
Mar 24, 2014, 9:40:41 PM3/24/14
to sy...@googlegroups.com
Yeah, that looks like a wrong result to me. It's because it's using
as_independent(x), but it should only do that if it's an Add. I think
it should be an easy fix if you want to give it a shot.

Aaron Meurer
> --
> 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 sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/16ac5b41-fb76-494d-b407-5f1087447c4a%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages