7
$\begingroup$

Is there a function that can extract a list of variables in an expression? For example, assume we have an expression

x^2+y^3+z

This expression has variables x, y and z. The result should be

{x, y, z}

. Is there a way to get this?

$\endgroup$
  • 13
    $\begingroup$ Variables command should work Variables[x^2 + y^3 + z] $\endgroup$ – user59583 Dec 18 '18 at 9:07
  • 1
    $\begingroup$ What about x[1]^2 + x[2]^3 + x[3]? Variables works, but none of the present answer do... $\endgroup$ – Michael E2 Aug 14 at 13:48
7
$\begingroup$

For polynomial expressions @Buddha_the_Scientist's suggestion Variables will work. For more general expressions

expr = x^2 + y^3 + z
DeleteDuplicates@Cases[expr, _Symbol, ∞]

Should do the trick in most situations.

$\endgroup$
  • $\begingroup$ Since all symbols are on level -1, you can use {-1} , instead of \[infinity]. $\endgroup$ – Fred Simons Dec 18 '18 at 11:14
  • $\begingroup$ Might want to include only symbols in the "Global`" context. I'd probably use Union instead of DeleteDuplicates to get them in canonical order. $\endgroup$ – Michael E2 Dec 18 '18 at 16:37
4
$\begingroup$

The undocumented Integrate`getAllVariables is a somewhat more robust version of Variables. It has a required second argument that specifies a variable to be excluded from the output. It just goes to show that internal functions are not always defined with the general user in mind.

Integrate`getAllVariables[x^2 + y^3 + z, {}] (* delete {} from output: *)
Variables[x^2 + y^3 + z]                     (*   {} can't happen anyway *)
(*
  {x, y, z}
  {x, y, z}
*)

A case Variables does not handle:

Integrate`getAllVariables[{x[0]'[t] + a t == 0, 
  y[1] == Sin[b[t]] x[0][t]^2}, {}]
Variables[{x[0]'[t] + a t == 0, y[1] == Sin@b[t] x[0][t]^2}]
(*
  {a, t, b[t], y[1], x[0][t]}
  {}
*)

Note how b[t] is treated differently than Sin[t], etc.:

Integrate`getAllVariables[a + b[t] - c[t + s] + x^y, {}]
Integrate`getAllVariables[a + Sin[t] - Cos[t + s] + x^y, {}]
(*
  {a, x, y, b[t], c[s + t]}
  {a, s, t, x, y}
*)

The second argument is useful in problems in which there is a principal independent variable and you want to get all the others. Excluding more than one, however, can only be achieved by hacking:

Integrate`getAllVariables[x^2 + y^3 + z, x]
(*  {y, z}  *)

xc /: {xc[a__]} := {a};  (* except the variables a.. *)
Integrate`getAllVariables[x^2 + y^3 + z, xc[x, y]]
(*  {z}  *)
$\endgroup$
  • $\begingroup$ It is remarkable that Integrate`getAllVariables excludes symbols with NumericFunction attribute. $\endgroup$ – Shadowray Aug 14 at 15:46
2
$\begingroup$

I like the following approach x):

expr = x^2 + y^3 + z;    
Select[DeleteDuplicates@Level[expr, Depth@expr], Head[#] == Symbol &]

the result is:

{x, y, z}
$\endgroup$
1
$\begingroup$

Listing the expression might be an alternative for this simple example

List @@ (x^8 + y^3 + z) /. a_Symbol^n_ -> a
$\endgroup$
  • $\begingroup$ How about 2 x^y - z? $\endgroup$ – Shadowray Aug 14 at 12:28
  • $\begingroup$ Then i prefer Cases[List @@ (2 x^y - z), _Symbol, Infinity]//Union $\endgroup$ – user49047 Aug 14 at 13:06
  • $\begingroup$ List does not matter anymore Cases[(2 x^y - z), _Symbol, Infinity]//Union $\endgroup$ – user49047 Aug 14 at 13:08

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.