I'm having the hardest time working if I have a sum of products, with coefficients out the front, like
expr = 1 + 2 a[0] b[1] + 2 a[0] b[1] c[3] + c[6] + 5 d[3, 4]
How do I get a list of terms with their coefficients, like
{ {1, 1}, {2, a[0] b[1]}, {2, a[0] b[1] c[3]}, {1, c[6]}, {5, d[3,4]} }?
or even
{ {1, 1}, {2, {a[0], b[1]}}, {2, {a[0],b[1],c[3]} }, {1, c[6]}, {5, d[3,4]}}?
I've tried:
Table[{Coefficient[expr, i], i}, {i, Variables[expr]}]
(* {{2 b[1] + 2 b[1] c[3], a[0]}, {2 a[0] + 2 a[0] c[3], b[1]}, {2 a[0] b[1], c[3]}, {1, c[6]}, {5, d[3, 4]}} *)
But this returns coefficients for the "individual" variables a[0]
, b[1]
, c[1]
, c[3]
, d[3,4]
, not the variables as they are grouped in the sum.
I also tried manually picking through the sum, without much success
{expr[[2,1]], expr[[2,2;;-1]]} (* want {2, a[0] b[1]} *)
(* {2, a[0],b[1] } *) (* YAY! *)
{expr[[4,1]], expr[[4,2;;-1]]} (* want {1, c[6]} *)
(* {6, c[]} *) (* Oh no... *)