Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #10493

Re: is Head[] part of the expression?

From "A. Retey" <albert.retey@gmail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: is Head[] part of the expression?
Date 2012-06-15 19:31 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jrg2j1$sjo$1@smc.vnet.net> (permalink)
References <jrep1p$nrj$1@smc.vnet.net>

Show all headers | View raw


Hi,

> 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

I think you get fooled by the fact that Position does, by default, search in all levels and includes Heads. This is what probably shades some light on this:

Position[{1,2,3},_]

to get the expected behaviour, use an explicit level specification and Heads option, e.g.:

Position[{1, 2, 3}, _, {1}, Heads -> False]

Whether Heads are looked at and which levels are looked at by default depends on the function you use, there is no common convention that they all behave the same. This seems o.k. since there is a different useful default depending on what these functions do. I found it saved me from many surprises and made my code more robust to include explicit level specifications as much as I can...

hth,

albert

Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

is Head[] part of the expression? "Nasser M. Abbasi" <nma@12000.org> - 2012-06-15 07:42 +0000
  Re: is Head[] part of the expression? "Nasser M. Abbasi" <nma@12000.org> - 2012-06-15 19:35 +0000
  Re: is Head[] part of the expression? "A. Retey" <albert.retey@gmail.com> - 2012-06-15 19:31 +0000
  fyi, updated my Mathematica HOWTO for basic engineering examples "Nasser M. Abbasi" <nma@12000.org> - 2012-06-18 00:04 +0000
  Re: is Head[] part of the expression? Szabolcs Horvát <szhorvat@gmail.com> - 2012-06-18 09:49 +0000
  fyi, updated my Mathematica HOWTO for basic engineering examples "Nasser M. Abbasi" <nma@12000.org> - 2012-06-17 07:56 +0000
  fyi, updated my Mathematica HOWTO for basic engineering examples "Nasser M. Abbasi" <nma@12000.org> - 2012-06-17 20:46 +0000

csiph-web