Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Function to detect presence of a variable in

  • To: mathgroup at smc.vnet.net
  • Subject: [mg109398] Re: Function to detect presence of a variable in
  • From: DrMajorBob <btreat1 at austin.rr.com>
  • Date: Sun, 25 Apr 2010 06:24:22 -0400 (EDT)

The default levelspec for Position is {0,Infinity}, but level 0 doesn't  
work (in any way that makes sense):

expr = x;
Position[expr, x, {0, Infinity}]

{{}}

FAIL! This contradicts, I think, tutorial/LevelsInExpressions, where Help  
says:

"You can think of levels in expressions in terms of trees. The level of a  
particular part in an expression is simply the distance down the tree at  
which that part appears, with the top of the tree considered as level 0."

x is obviously the top of TreeForm[x]... but according to Position, x  
appears nowhere in x, at all.

Cases works, however.

expr = 1/x;
Length@Cases[expr, x, {0, Infinity}]

1

expr = 1*x;
Length@Cases[expr, x, {0, Infinity}]

1

Bobby

On Sat, 24 Apr 2010 03:01:50 -0500, <carlos at colorado.edu> wrote:

> 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?
>


-- 
DrMajorBob at yahoo.com


  • Prev by Date: Re: Stop mathematica reordering expression during matrix multiply
  • Next by Date: Re: Stop mathematica reordering expression during matrix
  • Previous by thread: Re: Function to detect presence of a variable in
  • Next by thread: Re: Function to detect presence of a variable in