Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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} *)


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.

share|improve this question

marked as duplicate by R. M. Sep 27 '12 at 3:59

This question was marked as an exact duplicate of an existing question.

What about FreeQ? – Rojo Sep 27 '12 at 3:51
FreeQ - but thats too short for an answer ;-) – user21 Sep 27 '12 at 3:52
@Rojo Don't MemberQ and FreeQ only check on the first level? I checked this for MemberQ before. – VF1 Sep 27 '12 at 3:52
No, FreeQ checks all levels by default. Both can take a level specification anyway – Rojo Sep 27 '12 at 3:53
Counterexample: FreeQ[{1, 1/q, D[m, t]}, m] – VF1 Sep 27 '12 at 3:53
up vote 0 down vote accepted

How about:

sample = {1, 1/q, ff[f[m]]};
FreeQ[sample, m]
(* False *)
share|improve this answer

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