2
$\begingroup$

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} }
$\endgroup$
8
  • $\begingroup$ You can expand your equation and then apply List. $\endgroup$
    – Spawn1701D
    May 16, 2013 at 17:32
  • $\begingroup$ Variables, e.g. Variables[x^3 + 6 x^2 y + 3 x y z + x z^2 + 1] yields {x, y, z} $\endgroup$
    – Artes
    May 16, 2013 at 17:32
  • $\begingroup$ If you want monomials there is e.g. MonomialList[a x + b y^2 + 3 c z^{1.3}] yielding {{1. a x, 1. b y^2, 3. c z^(13/10)}} $\endgroup$
    – Artes
    May 16, 2013 at 17:38
  • $\begingroup$ Hi guys, thank you very much for your help so far. Unfortunately I have had to update the question as I try your suggestions and see what additional flexibility I else I need. $\endgroup$
    – DJBunk
    May 16, 2013 at 17:41
  • 1
    $\begingroup$ So you need List @@ ((1 + a) x + (2.5 - b) y^2 + 3 c z^1.3) returning {(1 + a) x, (2.5 - b) y^2, 3 c z^1.3} $\endgroup$
    – Artes
    May 16, 2013 at 17:44

2 Answers 2

7
$\begingroup$

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]]
$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$
    – Mr.Wizard
    Oct 8, 2014 at 16:11
5
$\begingroup$

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.)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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