MathGroup Archive 2012

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

Search the Archive

Re: part of the expression?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg126910] Re: part of the expression?
  • From: Andrzej Kozlowski <akozlowski at gmail.com>
  • Date: Sun, 17 Jun 2012 03:57:18 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <201206150741.DAA24416@smc.vnet.net>

Generally the Head is always examined unless you set the options 
Heads->False:

In[1]:= v = {1, 0, 3};

In[2]:= Position[v, x_ /; Not[SameQ[x, 0]]]

{{0},{1},{3},{}}

Position[v, x_ /; Not[SameQ[x, 0]], Heads -> False]

{{1},{3},{}}

However, what distinguishes the different examples you gave is the fact 
that in some cases certain tests do not yield either True or False. The 
results of such tests are ignored.

Thus when your test is x/;Unequal[x,1] Mathematica applies it only to  
those parts of the expression for which the test yields True or False 
and omits both the Head (List) and the entire expression since
List==0
and
{1,2,3}==0

return neither True nor False. Note that, however,

 Position[v, x_ /; (x != {1, 2})]

{{}}

Why? Mathematica only perfumed the test {1,2,3}=={1,2}, which returned

{1, 2, 3} == {1, 2}

False

and did perform the tests List=={1,2} and 1=={1,2} etc, as these yield neither True nor False.

In general then testing for position only tests that yield either True or False are performed, thus the Head of the expression is tested only if the test is of the kind that can yield True or False. (Of course if you call Position with the option Heads->False, the Head will be ignored in all cases).

So Mathematica seems pretty smart here. How does it know when not to perform tests? This is not entirely clear to me. It is interesting to compare these two equivalent tests:

Position[v, x_ /; Unequal[x,1]]

{{2}, {3}}

Position[v, x_ /;  ! Equal[x,1]]

{{2}, {3}}

The outcomes are the same, but Trace results are quite different:

Trace[Position[v,x_/;x!=1]]
=
{{v,{1,0,3}},Position[{1,0,3},x_/;x!=1],{1!=1,False},{0!=1,True},{3!=1,True},{{2},{3}}}


Trace[Position[v,x_/;!x==1]]
=
{{v,{1,0,3}},Position[{1,0,3},x_/;!x==1],{!List==1,List!=1},{{1==1,True},!True,False},{{0==1,False},!False,True},{{3==1,False},!=
False,True},{!{1,0,3}==1,{1,0,3}!=1},{{2},{3}}}

Somehow in the first case there were no comparisons with List and the entire expression at all (or at least Trace does not show them). In the second case such comparisons were made but because they returned neither True nor False they were ignored. Why there is this difference I do not know.

Andrzej Kozlowski



On 15 Jun 2012, at 09:41, Nasser M. Abbasi wrote:

> I am now in a state of little confusion about when Head[]
> of a list (the value at index 0) is 'looked' at when
> passing a list to a function to process the list.
>
> For example, below I have a list, and then Position[]
> is passed this list to find the nonzero elements in
> the list.
>
> Depending on the test for zero used, different
> results show up. Sometime the Head[] of the list
> is examined (because I get zero index) and sometime
> it is not.
>
> So, my question, what is the rule-of-thumb on this?
> I am making a cheat sheet for Mathematica, and
> wanted to make a simple rules to remember.
>
>
> -- case 1 -----
> v={1,0,3};
> Position[v,x_/;x!=0]
>
> Out[68]= {{1},{3}}   (*what I expected*)
> -----------------------
>
> --- case 2 --------------------
> Position[v,x_/;Not[PossibleZeroQ[x]]]
>
> Out[69]= {{0},{1},{3}}
> ----------------------------
>
> ----- case 3 ---------------------
> Position[v,x_/;Not[SameQ[x,0]]]
>
> Out[70]= {{0},{1},{3},{}}
> --------------------------------
>
> ---- case 4--------------
> Position[v, x_ /; Not[SameQ[x, 0]], 2]
>
> Out[71]= {{0},{1},{3}}
> ---------------------------
>
> thanks,
>
> --Nasser
>




  • Prev by Date: Re: Varying a constant in an ODE to Manipulate solution
  • Next by Date: Re: Parenthesis reduction
  • Previous by thread: Re: part of the expression?
  • Next by thread: Re: is Head[] part of the expression?