I'm looking for a nice way to split an expression into a list of the terms (i.e. the addends).
The desired behavior in:
terms[a^2 + a^-2 + c]
(* Out: {a^2, a^-2, c} *)
terms[a*b*c]
(* Out: {a*b*c} *)
I've tried a few solutions. MonomialList doesn't work if the expression is not a polynomial. I've also tried
Level[a + b + c, 1]
(* Out: {a, b, c} *)
But the problem is it breaks down for monomials.
Level[a, 1]
(* Out: {} *)
Level[a*b*c, 1]
(* Out: {a, b, c} *)
The desired output would be {a}
and {a b c}
respectively.
Thank you in advance!
terms[a*b*c]
but terms here area
andb
andc
. Unless you are using your own definition of what aterm
is. So you can useList @@ expr
to obtain the terms.expr = a^2 + a^-2 + c; List @@ expr
gives{1/a^2, a^2, c}
andexpr = a*b*c; List @@ expr
gives{a, b, c}
etc.. $\endgroup$expr = a; List @@ expr
returns justa
rather than{a}
. $\endgroup$