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


Groups > comp.soft-sys.math.mathematica > #10540 > unrolled thread

Re: is Head[] part of the expression?

Started byDana DeLouis <dana01@me.com>
First post2012-06-19 07:17 +0000
Last post2012-06-20 07:49 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Re: is Head[] part of the expression? Dana DeLouis <dana01@me.com> - 2012-06-19 07:17 +0000
    Re: is Head[] part of the expression? Peter Breitfeld <phbrf@t-online.de> - 2012-06-20 07:49 +0000

#10540 — Re: is Head[] part of the expression?

FromDana DeLouis <dana01@me.com>
Date2012-06-19 07:17 +0000
SubjectRe: is Head[] part of the expression?
Message-ID<jrp91u$qbf$1@smc.vnet.net>
> 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...@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


[toc] | [next] | [standalone]


#10557

FromPeter Breitfeld <phbrf@t-online.de>
Date2012-06-20 07:49 +0000
Message-ID<jrrva8$4qd$1@smc.vnet.net>
In reply to#10540
A way to get all functions with hat a specific option without yielding
errors could be:


Options[WhoHasOption]={Full->False};
WhoHasOption[option_, context_String : "System`*", OptionsPattern[]] :=
Module[{hA, langQ},
  langQ = OptionValue[Full] === True;
  hA = ToExpression /@ 
    Quiet[Select[Names[context], MemberQ[ToExpression[#1,
          StandardForm, Options[Unevaluated[#1]] &][[All, 1]], 
        option] &]];
  If[langQ, {#, Options[#, option]} & /@ hA, hA]
]

usage is:

WhoHasOption[Heads]

If you want to see the setting of this Option, call:

WhoHasOption[Heads,Full->True]

If you want the functions of a specific context only, call:

WhoHasOption[Heads,"context`"]

Dana DeLouis wrote:

>> 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...@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
>
>
>

-- 
_________________________________________________________________
Peter Breitfeld | Bad Saulgau, Germany | http://www.pBreitfeld.de

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web