Function to detect presence of a variable in expression
- To: mathgroup at smc.vnet.net
- Subject: [mg109380] Function to detect presence of a variable in expression
- From: carlos at colorado.edu
- Date: Sat, 24 Apr 2010 04:01:50 -0400 (EDT)
I would like to have a function k = CountMeIn[expr,var] that returns the number of times var appears in expr, eg. CountMeIn[(1/x)^x,x] returns 2. var could be a list, in which case k would be a conforming list: k = CountMeIn[(2/x)^(x*w^4*z),{x,y,w,z}] should return {2,0,1,1}. expr is never a list. Tried to use Position for this, but the behavior is finicky: Position[1/x,x] returns {{1,1}} but Position[x,x] or Position[1*x,x] returns {{}} (why?) May be expr should be converted to {expr} inside the function for safety?