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


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

is Head[] part of the expression?

Started by"Nasser M. Abbasi" <nma@12000.org>
First post2012-06-15 07:42 +0000
Last post2012-06-17 20:46 +0000
Articles 7 — 3 participants

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


Contents

  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

#10473 — is Head[] part of the expression?

From"Nasser M. Abbasi" <nma@12000.org>
Date2012-06-15 07:42 +0000
Subjectis Head[] part of the expression?
Message-ID<jrep1p$nrj$1@smc.vnet.net>
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

[toc] | [next] | [standalone]


#10492

From"Nasser M. Abbasi" <nma@12000.org>
Date2012-06-15 19:35 +0000
Message-ID<jrg2pn$sn3$1@smc.vnet.net>
In reply to#10473
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] | [prev] | [next] | [standalone]


#10493

From"A. Retey" <albert.retey@gmail.com>
Date2012-06-15 19:31 +0000
Message-ID<jrg2j1$sjo$1@smc.vnet.net>
In reply to#10473
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

[toc] | [prev] | [next] | [standalone]


#10512 — fyi, updated my Mathematica HOWTO for basic engineering examples

From"Nasser M. Abbasi" <nma@12000.org>
Date2012-06-18 00:04 +0000
Subjectfyi, updated my Mathematica HOWTO for basic engineering examples
Message-ID<jrlrba$cqr$1@smc.vnet.net>
In reply to#10473
fyi;

I've updated my HOWTO basic examples in Mathematica and <that_other_one>.

I updated many Mathematica examples to use the new V 8.0 control
systems functions (nice functions btw, easy to use). Before I had
those examples written using the control systems application. I also
put the code in the page now. Before I had them as screen shots.

Made few corrections and general clean up.

Here is the link, hope you like it.

http://12000.org/my_notes/mma_matlab_control/index.htm

I learn better by seeing an example of how to do something.
So, every-time I solve a problem, I try to document it so
that I do not have to re-remember how to do it each time.

Nothing advanced. Just simple basic examples. Things I run
across while doing my school HW's. Will be updated on regular
basis.

--Nasser

[toc] | [prev] | [next] | [standalone]


#10535

FromSzabolcs Horvát <szhorvat@gmail.com>
Date2012-06-18 09:49 +0000
Message-ID<jrmtjf$h78$1@smc.vnet.net>
In reply to#10473
Position does look at the head even when you use == or !=.  The 
difference between x==0 and x===0 is that x==0 will not evaluate to 
either True or False when x has no value while x===0 will evaluate to False.

On 2012.06.15. 9:42, Nasser M. Abbasi wrote:
> 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
>


-- 
Szabolcs Horvát
Visit Mathematica.SE:  http://mathematica.stackexchange.com/


[toc] | [prev] | [next] | [standalone]


#10536 — fyi, updated my Mathematica HOWTO for basic engineering examples

From"Nasser M. Abbasi" <nma@12000.org>
Date2012-06-17 07:56 +0000
Subjectfyi, updated my Mathematica HOWTO for basic engineering examples
Message-ID<jrk2je$8je$1@smc.vnet.net>
In reply to#10473
fyi;

I've updated my HOWTO basic examples in Mathematica and <that_other_one>.

I updated many Mathematica examples to use the new V 8.0 control
systems functions (nice functions btw, easy to use). Before I had
those examples written using the control systems application. I also
put the code in the page now. Before I had them as screen shots.

Made few corrections and general clean up.

Here is the link, hope you like it.

http://12000.org/my_notes/mma_matlab_control/index.htm

I learn better by seeing an example of how to do something.
So, every-time I solve a problem, I try to document it so
that I do not have to re-remember how to do it each time.

Nothing advanced. Just simple basic examples. Things I run
across while doing my school HW's. Will be updated on regular
basis.

--Nasser

[toc] | [prev] | [next] | [standalone]


#10539 — fyi, updated my Mathematica HOWTO for basic engineering examples

From"Nasser M. Abbasi" <nma@12000.org>
Date2012-06-17 20:46 +0000
Subjectfyi, updated my Mathematica HOWTO for basic engineering examples
Message-ID<jrlfmc$c2o$1@smc.vnet.net>
In reply to#10473
fyi;

I've updated my HOWTO basic examples in Mathematica and <that_other_one>.

I updated many Mathematica examples to use the new V 8.0 control
systems functions (nice functions btw, easy to use). Before I had
those examples written using the control systems application. I also
put the code in the page now. Before I had them as screen shots.

Made few corrections and general clean up.

Here is the link, hope you like it.

http://12000.org/my_notes/mma_matlab_control/index.htm

I learn better by seeing an example of how to do something.
So, every-time I solve a problem, I try to document it so
that I do not have to re-remember how to do it each time.

Nothing advanced. Just simple basic examples. Things I run
across while doing my school HW's. Will be updated on regular
basis.

--Nasser

[toc] | [prev] | [standalone]


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


csiph-web