2

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.

Needs["Notation`"]

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.

Chen Stats Yu
  • 4,986
  • 2
  • 24
  • 50

1 Answers1

3

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} *)

and

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

and

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

Edit

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]]
Mike Honeychurch
  • 37,541
  • 3
  • 85
  • 158
  • 2
    But E is not considered to be a variable by Variables. Maybe add something like Function[z, Context[z] =!= "System`"] or some such. – Rolf Mertig Oct 15 '14 at 21:33
  • 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 – Mike Honeychurch Oct 15 '14 at 21:41
  • @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. – Chen Stats Yu Oct 15 '14 at 21:43
  • @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. – Mike Honeychurch Oct 15 '14 at 21:48
  • @MikeHoneychurch A little off topic, I have been using (learning) Maple since 2006. Tried MMA for a couple of times. Most of the time Maple is sufficient for my study and research. But recently, I found that Maple does not have it's own Global optimization tools. MMA is much better and FASTER for my problems in particular. So I have decided to have a more "serious" go at it. If you had a look at my other post (link given in the post above),ideally I really need to have TWO sets of variables, with subscripts. But at the moment, MMA sucks! It also sucks in solving PDEs. – Chen Stats Yu Oct 15 '14 at 21:54
  • @ChenStatsYu I think it is unfair to say it sucks at PDE solving. While i used to solve PDEs day in day out I have not done so for 10 years. There are still some problems that Mma cannot solve so you have to roll your own -- which I did cause NDSolve couldn't do it. Also NDSolve is an incredibly complex function to learn if you want to delve into it. Perhaps you should post a question if you are having a problem solving a PDE. – Mike Honeychurch Oct 15 '14 at 22:02
  • 1
    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. – Rolf Mertig Oct 15 '14 at 22:03
  • @MikeHoneychurch Yes, I posted. MMA9 couldn't solve it. MMA 10 fails. But Maple can. Btw, it's not numerical, it's symbolic. And the actual problem is way too big to display or state in a short post. But I did post a very simple PDE, here. I have nothing against MMA, but it's not good enough at most of the aspects for what I want to do at the moment. I do like it's flexibility and portability. – Chen Stats Yu Oct 15 '14 at 22:08
  • @ChenStatsYu if Maple works for you and you understand it then stick to it. The time needed to switch to a rival product is rarely worth it if you are proficient in one of the other Ms – Mike Honeychurch Oct 15 '14 at 22:10
  • @MikeHoneychurch But like I said, whole problem needs a lot more. Maple can't do Global optimization (without it's 3rd party GOT tools box), but MMA can, and MMA is much faster. However, MMA fails at the PDE part, and the ability to setup my variables. – Chen Stats Yu Oct 15 '14 at 22:13
  • @ChenStatsYu in that case use Maple but use Mma for the stuff that you don't feel that Maple does well, ... or cannot do – Mike Honeychurch Oct 15 '14 at 22:14
  • @MikeHoneychurch That's preciously what I am trying to do. In the post above, I am trying to setup the problem. Being able to identify the variables, therefore the number of variables, is just a middle step, for what I am trying to do. – Chen Stats Yu Oct 15 '14 at 22:17