Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2012

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

Search the Archive

Re: is Head[] part of the expression?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg126946] Re: is Head[] part of the expression?
  • From: Dana DeLouis <dana01 at me.com>
  • Date: Tue, 19 Jun 2012 03:15:55 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com

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

Hi.  Here's my attempt at finding such functions.
In help for the function 'Names, there is an example for finding functions with certain Attributes.
I could not find one for Options.
Here's the best I came up with for now.
I get a warning message with the output.
Does anyone have a better idea for this code ??   Thanks.

Here's my attempt.
Find functions that have the option  Heads -> True/False

NamesWithOption[hd_]:=Module[{SysNames,Opt,Pos},
 SysNames = Names["System`*"];
 Opt=Options/@ToExpression[SysNames];
 Pos=Position[Opt,hd,\[Infinity]][[All,1]];
 SysNames[[Pos]]//ToExpression
]


NamesWithOption[Heads]

Options::opmix: Cannot mix streams and non-streams in {Courier,10.}.
ToExpression::notstrbox: FEPrivate`FrontEndResourceString[GetFEKernelInit] is not .. . etc

Output.. ..

{Apply,Cases,Count,DeleteCases,Depth,FreeQ,Level,
  Map,MapAll,MapIndexed,MemberQ,Position,Replace,ReplacePart,Scan}


Does anyone know of a better way to get this list ?
Thanks.


For the Op, one can temporarily change the default behavior...

Options[Position]
{Heads->True}

SetOptions[Position,Heads->False]
{Heads->False}

Options[Position]
{Heads->False}


= = = = = = = = = =
Dana DeLouis
Mac & Math 8
= = = = = = = = = =


On Jun 15, 3:35 pm, "Nasser M. Abbasi" <n... at 12000.org> wrote:
> 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





  • Prev by Date: NMinimize a complicated built function
  • Next by Date: Clear All Output
  • Previous by thread: Re: is Head[] part of the expression?
  • Next by thread: Re: is Head[] part of the expression?