This carries on the discussion here and here.

I had mistaken Variable[] to give you ALL variables in an expression, but apparently, it is not meant for that.


Symbolize[ParsedBoxWrapper[SubscriptBox["x", "_"]]]
Symbolize[ParsedBoxWrapper[SubscriptBox["y", "_"]]]
var1 = Table[ToExpression["Subscript[x, " <> ToString[i] <> "]"], {i, 10}]
var2 = Table[ToExpression["Subscript[y, " <> ToString[i] <> "]"], {i, 10}]

expr = var1*Exp[var2]^var1

Variables[Level[expr, 1]]
Variables[Level[expr, -1]]

I have tried to Level it our with many different values. But none seem to be working.

Basically, I am looking for an equivalent command in Mathematica to the Maple indets.

  • $\begingroup$ What is the desired result? $\endgroup$ – Kuba Oct 15 '14 at 20:00
  • 2
    $\begingroup$ Cases[expr, Subscript[__], Infinity] // Union $\endgroup$ – Bob Hanlon Oct 15 '14 at 20:27
  • $\begingroup$ @Kuba The desired result would be all the variables considered, i.e. everything in var1 and var2. $\endgroup$ – Chen Stats Yu Oct 15 '14 at 22:18

The problem you are having is to do with the way Symbolize works ...I think, specifically I am pretty sure the actual symbolized expression has to be typeset in your cell rather than referring to it via another variable. Someone else might like to explain that more elegantly.

Firstly you don't need to use strings:

Clear[x, y, var1, var2];

var1 = Table[Subscript[x, i], {i, 10}]
var2 = Table[Subscript[y, i], {i, 10}]

expr = var1*Exp[var2]^var1

Next step is to see what the problem is:

Head /@ var1
(* {Subscript, Subscript, Subscript, Subscript, Subscript, Subscript,
Subscript, Subscript, Subscript, Subscript} *)


AtomQ /@ var1
{False, False, False, False, False, False, False, False, False, False}

So for your subscripted variables to be "scraped" in some way from expr you need them to be recognised as symbols and as atoms. I am not 100% sure on why there is a difference -- probably something obvious I have overlooked -- but in any case consider the following:

enter image description here

so therefore try:

enter image description here


Union@Cases[expr1, _Symbol, \[Infinity]]
(* {E, Subscript[x, 1], Subscript[x, 10], Subscript[x, 2], Subscript[x, \
3], Subscript[x, 4], Subscript[x, 5], Subscript[x, 6], Subscript[x, \
7], Subscript[x, 8], Subscript[x, 9], Subscript[y, 1], Subscript[y, \
10], Subscript[y, 2], Subscript[y, 3], Subscript[y, 4], Subscript[y, \
5], Subscript[y, 6], Subscript[y, 7], Subscript[y, 8], Subscript[y, 9]
 } *)

enter image description here


As per Rolfs suggestion to screen out system variables:

Union@Cases[expr1, z_Symbol /; Context[z] =!= "System`", \[Infinity]]

or alternatively if everything is in a global context only:

Union@Cases[expr1, z_Symbol /; Context[z] === "Global`", \[Infinity]]
| improve this answer | |
  • 2
    $\begingroup$ But E is not considered to be a variable by Variables. Maybe add something like Function[z, Context[z] =!= "System`"] or some such. $\endgroup$ – Rolf Mertig Oct 15 '14 at 21:33
  • $\begingroup$ Yes good idea @RolfMertig. Do you have a more elegant explanation of the way Symbolize works? My attempted comment in my answer is rather clumsy. But it is still breakfast time here and my brain is foggy $\endgroup$ – Mike Honeychurch Oct 15 '14 at 21:41
  • $\begingroup$ @Mike I share the same option with @RolfMertig, E should not be a variable. Just to comment on the Variable[] function, I still don't get it, why Mathematica does not have a "straightforward" function, returns all variables in a general expression. Instead, MMA decided that "variables" are only important in "polynomials". Just so weird. For the example above, I have made it more "complicated" than the actual problem I need, but again, I can make it more complicated, just to make a particular solution fail. But the Maple indets() command, always give you what you would expect. $\endgroup$ – Chen Stats Yu Oct 15 '14 at 21:43
  • $\begingroup$ @ChenStatsYu Mma is really poor with subscripted variables ..surprising for a software that many would say is the best computer algebra software. Mathcad is great for subscripted variable, easily the best on the market, but of course it has other deficiencies relative to Mma. As a Maple user you would know that Maple also is much better than Mma for subscripted variables, but not as good as Mathcad. $\endgroup$ – Mike Honeychurch Oct 15 '14 at 21:48
  • 1
    $\begingroup$ Do not use Subscript and do not use Notation for anything non-trivial. What I sometimes do is something like MakeBoxes[x1, _] := InterpretationBox[SubscriptBox["x", 1], x1] and then you can input x1 and get displayed a subscripted x1 and it also behaves nice w.r.t. to InputForm and Variables etc. $\endgroup$ – Rolf Mertig Oct 15 '14 at 22:03

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.