Possible Duplicate:
Determine whether some expression contains a given symbol
Let's say I have some expression:
sample = {1, 1/q, f[m]}
And I want to check it if it has the symbol m in it.
My question: Is there a clean or fast way of checking if m is in the equation?
What I came up with so far:
Do[StringFreeQ[ToString[sample], "m"], {i, 300}] // AbsoluteTiming
(* {0.0120007, Null} *)
and
Do[(sample /. m -> Unique[]) === (sample /. m -> Unique[]), {i, 300}] // AbsoluteTiming
(* {0.0100005, Null} *)
These solutions work, but I feel like there might be a faster, functional way of doing this.
FreeQ? – Rojo Sep 27 '12 at 03:51MemberQandFreeQonly check on the first level? I checked this forMemberQbefore. – VF1 Sep 27 '12 at 03:52FreeQchecks all levels by default. Both can take a level specification anyway – Rojo Sep 27 '12 at 03:53FreeQ[{1, 1/q, D[m, t]}, m]– VF1 Sep 27 '12 at 03:53D[m, t]evaluates to0. If you want to check in held expressions, hold the evaluation withHoldorUnevaluated– Rojo Sep 27 '12 at 03:55