How to extract the coefficient from an expression

Here is the expression.

I want to extract the coefficient just as the table showing below.

I can do it by hand, but I expect a code that can automatically identify how many differenct terms there are ,what is its coefficient and show them all in a table.

this is the expression for Mma.

expr= (3 Subscript[1, b] Subscript[2, a])/8+1/8 E^(I [Theta]) Subscript[1, b] Subscript[2, a]+1/8 E^(2 I [Theta]) Subscript[1, b] Subscript[2, a]+3/8 E^(3 I [Theta]) Subscript[1, b] Subscript[2, a]-(3 Subscript[1, a] Subscript[2, b])/8+1/8 E^(I [Theta]) Subscript[1, a] Subscript[2, b]-1/8 E^(2 I [Theta]) Subscript[1, a] Subscript[2, b]+3/8 E^(3 I [Theta]) Subscript[1, a] Subscript[2, b]-(Sqrt[3] Subscript[3, a])/8-1/8 Sqrt[3] E^(I [Theta]) Subscript[3, a]+1/8 Sqrt[3] E^(2 I [Theta]) Subscript[3, a]+1/8 Sqrt[3] E^(3 I [Theta]) Subscript[3, a]+(Sqrt[3] Subscript[3, b])/8-1/8 Sqrt[3] E^(I [Theta]) Subscript[3, b]-1/8 Sqrt[3] E^(2 I [Theta]) Subscript[3, b]+1/8 Sqrt[3] E^(3 I [Theta]) Subscript[3, b]

I think of a example as below:

tab[expr_] := {#,
Coefficient[
expr, #]} & /@ (expr //
List @@ # & // # /. {Power[_, _] -> 1, Rational[_, _] -> 1,
E -> 1} & // Union) // Grid[#, Frame -> All] &

so, expr//tab will get the table above.

=================

1

 

Use Coefficient.
– Spawn1701D
Apr 25 ’13 at 11:25

  

 

It works. But how do I get the terms first?
– cmc
Apr 25 ’13 at 11:31

  

 

What do you mean?
– Spawn1701D
Apr 25 ’13 at 11:34

1

 

Does that make any sense? Do you want the terms of 3/8? Of Sqrt[3]/8? Of Exp[2 Pi]?
– bill s
Apr 25 ’13 at 11:48

1

 

not sure how your expressions look like, not sure what you dislike about your tab, here’s another approach, maybe it helps? Voilà: Grid[Transpose@{#, Coefficient[expr, #]} &@Union[Cases[expr, Times[c___ : 1, a : _Subscript, b :___Subscript] :> a b, 2]],Frame -> All]
– Pinguin Dirk
Apr 25 ’13 at 12:04

=================

1 Answer
1

=================

Let that you have the variables

vars= {Subscript[1, a],Subscript[1, b],Subscript[2, a],Subscript[2, b],Subscript[3, a],
Subscript[3, b]}

and

expr=(3 Subscript[1, b] Subscript[2, a])/8+1/8 E^(I [Theta]) Subscript[1, b] Subscript[2, a]+1/8 E^(2 I [Theta]) Subscript[1, b] Subscript[2, a]+3/8 E^(3 I [Theta]) Subscript[1, b] Subscript[2, a]-(3 Subscript[1, a] Subscript[2, b])/8+1/8 E^(I [Theta]) Subscript[1, a] Subscript[2, b]-1/8 E^(2 I [Theta]) Subscript[1, a] Subscript[2, b]+3/8 E^(3 I [Theta]) Subscript[1, a] Subscript[2, b]-(Sqrt[3] Subscript[3, a])/8-1/8 Sqrt[3] E^(I [Theta]) Subscript[3, a]+1/8 Sqrt[3] E^(2 I [Theta]) Subscript[3, a]+1/8 Sqrt[3] E^(3 I [Theta]) Subscript[3, a]+(Sqrt[3] Subscript[3, b])/8-1/8 Sqrt[3] E^(I [Theta]) Subscript[3, b]-1/8 Sqrt[3] E^(2 I [Theta]) Subscript[3, b]+1/8 Sqrt[3] E^(3 I [Theta]) Subscript[3, b]

to get the first column use the command

v = List@@Expand[Collect[expr, vars, 1 &]]

now things are easy:

({#,Coefficient[expr,#]}&/@v)// Grid[#, Frame -> All]&

You can tweak the Grid to your liking

  

 

it help me a lot. Thank you 1701D.
– cmc
Apr 25 ’13 at 12:10

  

 

@cmc to create the list vars you can use Cases if it is too long to do by hand.
– Spawn1701D
Apr 25 ’13 at 12:12