I have expressions like these:
expr1 = -2 a Z[-2] - 2 b Z[-1] - 2 Z[-1] + a Z[-1]
expr2 = -2 a Z[-2] - 2 b Z[-1] - 2 Z[-1] + a Z[-1] + c Z[0]
expr3 = -2 a Z[-2] - 2 c Z[-1] - 2 Z[-1] + a Z[-1] + c Z[0]
and I want to find out whether an expression contains as coefficient a
, b
and c
. So what I want is a function f[expr]
, such that
f[expr1] (* false because it only contains a and b *)
f[expr2] (* true because it contains a, b and c *)
f[expr3] (* false because it only contains a and c *)
The only way I can think of is converting it to a string and do a string-search, but I'm sure there must be a more clever way. I was trying to use Case[]
, but I was not able to find a solution yet.
Edit:
There are several different solutions, I tested them on speed. Run each of the functions 100.000 times (with one argument only, in case the {a,b,c}
-list was a function-argument, it was replaced to be constant - for fair comparison) with the expr1
, expr2
, expr3
expressions.
Results:
- Dr. belisarius: 11.3443515 sec
- Bill: 2.0469818 sec
- eldo's 1st: 3.6095660 sec
- eldo's 2nd: 2.4532568 sec
- Suba Thomas: 6.8753645 sec
- (Algohi: My Mathmatica9 does not support
SubsetQ
unfortunatly.)
The fastest solution will get the acceptance-reward, after roughly 24hours of the original questions. Thanks for the infos and the fun :-)
CoefficientList
(reference.wolfram.com/language/ref/CoefficientList.html) or this tutorial at extracting pieces of algebraic expressions (reference.wolfram.com/language/tutorial/…) $\endgroup$