1
$\begingroup$

Here is the expression. the expression

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

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.

$\endgroup$
  • 1
    $\begingroup$ Use Coefficient. $\endgroup$ – Spawn1701D Apr 25 '13 at 11:25
  • $\begingroup$ It works. But how do I get the terms first? $\endgroup$ – cmc Apr 25 '13 at 11:31
  • $\begingroup$ What do you mean? $\endgroup$ – Spawn1701D Apr 25 '13 at 11:34
  • 1
    $\begingroup$ Does that make any sense? Do you want the terms of 3/8? Of Sqrt[3]/8? Of Exp[2 Pi]? $\endgroup$ – bill s Apr 25 '13 at 11:48
  • 1
    $\begingroup$ 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] $\endgroup$ – Pinguin Dirk Apr 25 '13 at 12:04
5
$\begingroup$

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

$\endgroup$
  • $\begingroup$ it help me a lot. Thank you 1701D. $\endgroup$ – cmc Apr 25 '13 at 12:10
  • $\begingroup$ @cmc to create the list vars you can use Cases if it is too long to do by hand. $\endgroup$ – Spawn1701D Apr 25 '13 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.