2

Is there a command that takes an equation as an input and creates an output with a list of terms in the original equation? Something like:

Input: Foo[ (1+a) x+ (2.5-b) y^2+ 3 c z^{1.3}]

Output: { (1+a) x ,  (2.5-b) y^2 ,  3 c z^{1.3} }
DJBunk
  • 683
  • 1
  • 8
  • 18

2 Answers2

7

What you are asking to do, it seems, is to replace the Plus Head, with the List Head. The Apply function, shorthanded as @@, will do what you want:

Input: expr = Foo[a + b + c];

Now we can get just the a+b+c with First:

Input: expr2 = First@expr;

Check out FullForm to get rid of shorthanded notation:

Input: FullForm[expr2]
Output: Plus[a,b,c]

And finally, we can turn Plus into List with Apply:

Input: List@@expr2
Output: {a,b,c}

All in one line:

Input: List@@First[Foo[a+b+c]]
Ian Hincks
  • 1,859
  • 13
  • 21
  • I am fairly certain that Foo was given as the example of the function itself, meaning that Foo[a + b + c] should directly evaluate to {a, b, c}. Nevertheless +1. – Mr.Wizard Oct 08 '14 at 16:11
5

Examining the structure of the expression with TreeForm

TreeForm@Foo[(1 + a) x + (2.5 - b) y^2 + 3 c z^{1.3}]

TreeForm@Foo[(1 + a) x + (2.5 - b) y^2 + 3 c z^{1.3}]

shows us another way:

Level[Foo[(1 + a) x + (2.5 - b) y^2 + 3 c z^{1.3}], {3}]

(* {(1 + a) x, (2.5 - b) y^2, 3 c z^1.3} *)

(Simply count the depth of the function arguments from the Head of the expression.)

Aky
  • 2,719
  • 12
  • 19