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


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

Re: is Head[] part of the expression?

From "Nasser M. Abbasi" <nma@12000.org>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: is Head[] part of the expression?
Date 2012-06-15 19:35 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jrg2pn$sn3$1@smc.vnet.net> (permalink)
References <jrep1p$nrj$1@smc.vnet.net> <699305e5-6342-4cae-bdb2-775ef14b951c@googlegroups.com>

Show all headers | View raw


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

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