5
$\begingroup$

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!

$\endgroup$
2
  • 1
    $\begingroup$ 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]. $\endgroup$ Oct 27, 2014 at 18:40
  • $\begingroup$ @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) $\endgroup$
    – Mr.Wizard
    Oct 27, 2014 at 18:55

3 Answers 3

10
$\begingroup$
  • 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]
$\endgroup$
6
$\begingroup$

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]
]
$\endgroup$
6
  • $\begingroup$ +1 for a rather clever attack of such a problem. :-) $\endgroup$
    – Mr.Wizard
    Oct 26, 2014 at 2:10
  • $\begingroup$ 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? $\endgroup$ Oct 26, 2014 at 3:34
  • $\begingroup$ @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. $\endgroup$
    – Mr.Wizard
    Oct 26, 2014 at 3:42
  • $\begingroup$ @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. $\endgroup$ Oct 26, 2014 at 4:06
  • 2
    $\begingroup$ 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. $\endgroup$ Oct 26, 2014 at 9:56
4
$\begingroup$

[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} *)
$\endgroup$
4
  • $\begingroup$ 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. $\endgroup$
    – QuantumDot
    Dec 30, 2017 at 17:55
  • $\begingroup$ @QuantumDot I'll see what I can find out about that example. Might be an issue along the lines you suggest. $\endgroup$ Dec 31, 2017 at 15:36
  • $\begingroup$ Hi @DanielLichtblau, any word on the issue of Limit with Internal`DependsOnQ? $\endgroup$
    – QuantumDot
    Jan 4, 2020 at 19:52
  • $\begingroup$ @QuantumDot Still an issue, I'm afraid. $\endgroup$ Jan 4, 2020 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.