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!
- The functions you are looking for are
MemberQ
andFreeQ
. - 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]
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]
]
-
$\begingroup$ +1 for a rather clever attack of such a problem. :-) $\endgroup$ – Mr.Wizard Oct 26 '14 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 inx-x
. Is that a concern? Or are expressions likex-x
andexp(x)/exp(x)
not of concern? $\endgroup$ – alex.jordan Oct 26 '14 at 3:34 -
$\begingroup$ @alex.jordan I think you have a valid concern. However
x - x
already evaluates to0
so if theexpr
is not heldx
does not appear inside it, if you see what I mean. Other cases may be more complex, however wrappingexpr
inHold
should fix most problems I believe. $\endgroup$ – Mr.Wizard Oct 26 '14 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 usedFindVar[1/(x-1),x]
and the expression is just not defined atx -> 1
. $\endgroup$ – alex.jordan Oct 26 '14 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$ – Alexey Popkov Oct 26 '14 at 9:56
[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} *)
-
$\begingroup$ Sometimes it doesn't work: like
Internal`DependsOnQ[Limit[f[x], x -> 3], x]
givesTrue
when the answer should beFalse
. I have a feeling thatInternal`DependsOnQ
relies on a preprogramed database of symbols which it checks, andLimit
was left out by accident. $\endgroup$ – QuantumDot Dec 30 '17 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$ – Daniel Lichtblau Dec 31 '17 at 15:36
-
$\begingroup$ Hi @DanielLichtblau, any word on the issue of
Limit
withInternal`DependsOnQ
? $\endgroup$ – QuantumDot Jan 4 '20 at 19:52 -
$\begingroup$ @QuantumDot Still an issue, I'm afraid. $\endgroup$ – Daniel Lichtblau Jan 4 '20 at 22:09
expr = Integrate[f[a],{a,1,3}]
? IfTrue
, thenMemberQ
is your function. IfFalse
(becausea
is a dummy variable, or scoped, if you prefer), then could useInternal`DependsOnQ[expr,a]
. $\endgroup$ – Daniel Lichtblau Oct 27 '14 at 18:40DependsOnQ
? 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 '14 at 18:55