Re: is Head[] part of the expression?
- To: mathgroup at smc.vnet.net
- Subject: [mg126897] Re: is Head[] part of the expression?
- From: "Nasser M. Abbasi" <nma at 12000.org>
- Date: Fri, 15 Jun 2012 15:31:14 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <jrep1p$nrj$1@smc.vnet.net> <699305e5-6342-4cae-bdb2-775ef14b951c@googlegroups.com>
- Reply-to: nma at 12000.org
On 6/15/2012 11:53 AM, A. Retey wrote:
> to get the expected behaviour, use an explicit level specification and Heads option, e.g.:
>
> Position[{1, 2, 3}, _, {1}, Heads -> False]
>
That is good option to use. But it does not seem to be accepted
by all functions:
--------------------------------
mat = {{0, 0, 1}, {10, 0, 2}, {3, 4, 0}};
sp = SparseArray[mat];
pos = sp["NonzeroPositions", Heads -> False] (*does NOT work*)
---------------------------------
but
------------------------------------
pos=sp["NonzeroPositions"] (*works*)
Out[46]= {{1,3},{2,1},{2,3},{3,1},{3,2}}
---------------------------------
and
--------------------------
v1 = {a, b, c};
v2 = {e, f, g};
Outer[Times, v1, v2, Heads -> False] (*error*)
-----------------------
and
-------------------------------------
A = {{50, 75, 0}, {50, 0, 100}, {0, 75, 100},
{75, 100, 0}, {0, 75,100}, {0, 75, 100}};
ReplacePart[A,
DeleteDuplicates[
Position[A, _?(# != 0 &)], (#1[[1]] == #2[[1]] &)] -> -1,
Heads -> False] (*ok*)
-----------------------------------
> 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
>
So, I guess the rule of thumb I'll add to my cheat sheet is like this:
1. Add Heads -> False always (unless I really want to look at Head, not likely)
2. If the Mathematica function does not like Heads option,
then do not use it (make a notes of such functions, should
not be many of them)
I just think this now adds a bit more complexity if one has to
worry about the Head of the expression being part of the
expression. I think Head of expression should not be
looked at, unless by explicit call or explicit option.
Thanks to all the replies.
thanks,
--Nasser