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!
Asked
Active
Viewed 1,652 times
5
Kagaratsch
- 11,955
- 4
- 25
- 72
3 Answers
10
- The functions you are looking for are
MemberQandFreeQ. - 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]
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
-
-
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
xis not present inx-x. Is that a concern? Or are expressions likex-xandexp(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 - xalready evaluates to0so if theexpris not heldxdoes not appear inside it, if you see what I mean. Other cases may be more complex, however wrappingexprinHoldshould 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 usedFindVar[1/(x-1),x]and the expression is just not defined atx -> 1. – alex.jordan Oct 26 '14 at 04:06 -
2You 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
-
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]givesTruewhen the answer should beFalse. I have a feeling thatInternal`DependsOnQrelies on a preprogramed database of symbols which it checks, andLimitwas 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
LimitwithInternal`DependsOnQ? – QuantumDot Jan 04 '20 at 19:52 -
expr = Integrate[f[a],{a,1,3}]? IfTrue, thenMemberQis your function. IfFalse(becauseais a dummy variable, or scoped, if you prefer), then could useInternal`DependsOnQ[expr,a]. – 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) – Mr.Wizard Oct 27 '14 at 18:55