1

I have an algebraic expression

(l[d,1]/(-l[d,1]+m[dd,1] p[dd]+m[du,1] p[du])+l[d,2]/(-l[d,2]+m[dd,2] p[dd]+m[ud,2] p[ud])+l[u,1]/(-l[u,1]+m[uu,1] p[uu])+l[u,2]/(-l[u,2]+m[uu,2] p[uu]))/(l[d,1]+l[d,2]+l[u,1]+l[u,2])

and I want to get a list of all the variables that are present in it excluding the mathematical operators , e.g., {l[d,1], m[dd,1], p[dd], ...}. That is, replace all the heads that are Plus, Power or Times by the head List, Flatten the resulting list and remove the duplicates. How do I do that?

Pisto
  • 344
  • 1
  • 8
  • Short answer,Ma thematic does not have such a function. Unlike Maple, you can use 'indets'. But sometimes, you can try Variables[] with Level[] to get an answer in Mathematica. I have asked the same thing before, and there is no unified answer that can give you a satisfied answer if you are only looking for "variables", not in Mathematica. – Chen Stats Yu Apr 10 '15 at 08:17
  • also see http://mathematica.stackexchange.com/questions/61131/get-all-variables-in-an-expression-with-variables – Chen Stats Yu Apr 10 '15 at 08:24

3 Answers3

2
f = (l[d, 1]/(-l[d, 1] + m[dd, 1] p[dd] + m[du, 1] p[du]) + l[d, 2]/(-l[d, 2] + m[dd, 2] p[dd] + m[ud, 2] p[ud]) + l[u, 1]/(-l[u, 1] + m[uu, 1] p[uu]) + l[u, 2]/(-l[u, 2] + m[uu, 2] p[uu]))/(l[d, 1] + l[d, 2] + l[u, 1] + l[u, 2])

vars = Variables[f]

Output

{l[d, 1], l[d, 2], l[u, 1], l[u, 2], m[dd, 1], p[dd], m[du, 1], p[du],  m[dd, 2], m[ud, 2], p[ud], m[uu, 1], p[uu], m[uu, 2]}

Honestly speaking, I have seen before a similar answer using the Variable here, but can't find it. If someone find it please post it on comment. I would be glad to add the reference.

Sumit
  • 15,912
  • 2
  • 31
  • 73
1
expr=(l[d, 1]/(-l[d, 1] + m[dd, 1] p[dd] + m[du, 1] p[du]) + l[d, 2]/(-l[d, 2] + m[dd, 2] p[dd] + m[ud, 2] p[ud]) + l[u, 1]/(-l[u, 1] + m[uu, 1] p[uu]) + l[u, 2]/(-l[u, 2] + m[uu, 2] p[uu]))/(l[d, 1] + l[d, 2] + l[u, 1] + l[u, 2])

Variables[Level[expr, 2]]

Again, there is no such single function in Mathematica, but you can try combinations of functions like 'Variables' and 'Level'. If you have Maple, indets is the one you are looking for.

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

Adapting the answer provided here:

varslist = Select[Intersection@
  Flatten[Cases[#, _?AtomQ, {0, Infinity}] & /@ Variables[orig]],
  Not@NumberQ[#] &]

{d, dd, du, u, ud, uu}

where orig is your original expression.

TransferOrbit
  • 3,547
  • 13
  • 26