Check individual terms after expanding

12 views
Skip to first unread message

artur zanon

unread,
Aug 4, 2022, 4:57:53 PM8/4/22
to sympy
Hi there! I have an issue and I found no solutions.
After using expand(), i'd like to gather the individual terms of my resulting equation.
Example:
Initial equation: x*(x + 2)
After expand(): x**2 + 2*x
I'd like a way to get the two resulting terms: x**2 and 2*x.
Using split() works most of the time, but when i have fractions, it does not.
Example: a/(a + 2) + b/(a + 2). Using split() here does not work.

Thanks!

Aaron Meurer

unread,
Aug 4, 2022, 5:02:32 PM8/4/22
to sy...@googlegroups.com
On Thu, Aug 4, 2022 at 2:57 PM artur zanon <turcat...@gmail.com> wrote:
>
> Hi there! I have an issue and I found no solutions.
> After using expand(), i'd like to gather the individual terms of my resulting equation.
> Example:
> Initial equation: x*(x + 2)
> After expand(): x**2 + 2*x
> I'd like a way to get the two resulting terms: x**2 and 2*x.
> Using split() works most of the time, but when i have fractions, it does not.

What do you mean by split()? There is no such SymPy function from what
I can see. Do you mean the string method str.split? You should not use
string functions to do symbolic manipulations on SymPy expressions.

The best way to get what you want is to use Add.make_args, like

>>> Add.make_args(x**2 + 2*x)
(x**2, 2*x)

Aaron Meurer

> Example: a/(a + 2) + b/(a + 2). Using split() here does not work.
>
> Thanks!
>
> --
> 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 view this discussion on the web visit https://groups.google.com/d/msgid/sympy/c6e2c084-469b-435d-8a2c-6a19bdb22e88n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages