5

Consider an arbitrary expression expr. This expression may contain arbitrary data, like integers, variables, implicit functions etc. Now, I would like to determine if the variable a does or does not appear in expr (which means, a could be a factor, or a subscript, or a power, or a function argument, does not matter). If a appears - give back True or 1, if a does not appear, give back False or 0. Is there such a function in Mathematica? Or maybe one can implement it? Thanks for any suggestion!

Kagaratsch
  • 11,955
  • 4
  • 25
  • 72
  • 1
    What result would you like for expr = Integrate[f[a],{a,1,3}]? If True, then MemberQ is your function. If False (because a is a dummy variable, or scoped, if you prefer), then could use Internal`DependsOnQ[expr,a]. – Daniel Lichtblau Oct 27 '14 at 18:40
  • @Daniel Could you tell us a little more about DependsOnQ? Perhaps post an answer? By the way don't forget to use double back-ticks to offset code that itself contains a back-tick. (FIFY) – Mr.Wizard Oct 27 '14 at 18:55

3 Answers3

10
  • The functions you are looking for are MemberQ and FreeQ.
  • Both functions take a levelspec and the Option Heads, but the default value for each is different.

You can determine if expression x appears anywhere in a using:

MemberQ[a, x, {0, -1}, Heads -> True]

Or assuming the default option Heads -> True for FreeQ simply:

! FreeQ[a, x]
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
6

Sorry, after I posted the question, I thought of a trivial solution myself. In case if someone else will have a similar question, here it is:

FindVar[expr_, var_] := Module[{temp},
temp = Hold[expr] /. var -> 0;
If[temp === Hold[expr], False, True]
]
Kagaratsch
  • 11,955
  • 4
  • 25
  • 72
  • +1 for a rather clever attack of such a problem. :-) – Mr.Wizard Oct 26 '14 at 02:10
  • I confess I don't understand Mathematica syntax enough for this, but my untrained reading of it leads me to think it would say that x is not present in x-x. Is that a concern? Or are expressions like x-x and exp(x)/exp(x) not of concern? – alex.jordan Oct 26 '14 at 03:34
  • @alex.jordan I think you have a valid concern. However x - x already evaluates to 0 so if the expr is not held x does not appear inside it, if you see what I mean. Other cases may be more complex, however wrapping expr in Hold should fix most problems I believe. – Mr.Wizard Oct 26 '14 at 03:42
  • @Mr.Wizard I do understand, I just don't know if there are expressions that simplify a lot, possibly losing x, but are so complicated that Mathematica doesn't simplify them as much as actually possible. Still not knowing much yet about Mathematica, but if I understand what your answer is doing, it is looking at the parse tree of the expression, which seems like the real deal. There's also a whole separate issue of what would happen if you used FindVar[1/(x-1),x] and the expression is just not defined at x -> 1. – alex.jordan Oct 26 '14 at 04:06
  • 2
    You need not to replace var by a number. It is sufficient and more reliable to replace it with another variable or any other expression and then compare it with the original using ===. This technique become completely safe if you wrap the expression by Hold beforehand. – Alexey Popkov Oct 26 '14 at 09:56
  • I adjusted the above answer to what Alexey Popkov suggested. – Kagaratsch Oct 27 '14 at 17:27
4

[Posting as response per request.]

If the goal is to determine a "functional" dependency then the undocumented Internal`DependsOnQ might be a better choice. This function will weed out for example usage within dummy variables in definite Integrate. The indefinite case really should, and does, give dependence. Here is a quick example.

expri = Integrate[f[a], a];
exprd = Integrate[f[a], {a, 1, 3}];

In[230]:= Map[Internal`DependsOnQ[#, a] &, {expri, exprd}]

(* Out[230]= {True, False} *)
Daniel Lichtblau
  • 58,970
  • 2
  • 101
  • 199
  • Sometimes it doesn't work: like Internal`DependsOnQ[Limit[f[x], x -> 3], x] gives True when the answer should be False. I have a feeling that Internal`DependsOnQ relies on a preprogramed database of symbols which it checks, and Limit was left out by accident. – QuantumDot Dec 30 '17 at 17:55
  • @QuantumDot I'll see what I can find out about that example. Might be an issue along the lines you suggest. – Daniel Lichtblau Dec 31 '17 at 15:36
  • Hi @DanielLichtblau, any word on the issue of Limit with Internal`DependsOnQ? – QuantumDot Jan 04 '20 at 19:52
  • @QuantumDot Still an issue, I'm afraid. – Daniel Lichtblau Jan 04 '20 at 22:09