Re: Warning from Piecewise
- To: mathgroup at smc.vnet.net
- Subject: [mg61547] Re: [mg61523] Warning from Piecewise
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Sat, 22 Oct 2005 00:35:35 -0400 (EDT)
- References: <200510210438.AAA13446@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On 21 Oct 2005, at 13:38, Chris Rodgers wrote:
> Hi,
>
> When I try to set a delayed expression like this:
>
> Comm[Piecewise[A:{{_, _}..}], Piecewise[B:{{_, _}..}]] :=
> Comm[Testy[A], Testy2[B]]
>
> I get a warning from Piecewise that says:
>
> Piecewise::pairs: The first argument A : {{_, _} ..} of Piecewise
> is not
> a list of pairs.
>
> I thought that the HoldAll attribute on SetDelayed should prevent
> evaluation of the LHS argument, so where is this warning coming from?
>
> Can I ignore a warning like this, or should I instead use a different
> sort of pattern?
>
> Many thanks,
>
> Chris Rodgers.
>
>
Actually Set and SetDelayed are special functions and their behaviour
in this sort of situation is not determined by their attributes. You
can see the difference as follows. First, let us create a function H,
to which we give the Attribute HoldAll.
SetAttributes[H, HoldAll]
Let's also define a function f to be:
f[x_] := Print[x]
(There is a reason why I need another function that does the same
thing as Print).
Let's first try the following using a function G which evaluates its
arguments:
G[f[x=1+1],p]
From In[3]:=
2
Out[3]=
G(Null,p)
we can check that x has the value 2:
In[4]:=
x
Out[4]=
2
Now let's clear x and do the same thing with H:
In[5]:=
Clear[x]
In[6]:=
H[f[x=1+1],p]
Out[6]=
H(f(x=1+1),p)
In[7]:=
x
Out[7]=
x
No evaluation at all, as expected. Now let's again clear x and set H
to be SetDelayed:
In[8]:=
Clear[x]
In[9]:=
H=SetDelayed;
In[10]:=
H[f[x=1+1],p]
We get no output, and nothing gets printed, which means f was not
evaluated. However:
In[11]:=
x
Out[11]=
2
In other words, although SetDelayed does not evaluate the head of an
expression on the LHS it does evaluate its other parts.
Why does it behave in this way? I think the reason is related to the
way SetDelayed defines DownValues for symbols. Finally, you can avoid
your message by defining Comm as follows:
Comm[Unevaluated[Piecewise[A : {{_, _} ..}]], Unevaluated[
Piecewise[B : {{_, _} ..}]]] :=
Comm[Testy[A], Testy2[B]]
In fact however it does not really matter. If you evaluate the
definition with Unevaluated and one without and then check what
Mathematica knows about Comm with
?Comm
you will see exactly the same Output. So I think you are safe using
your function, provided of course the rest of your code that you did
not include is O.K.
Andrzej Kozlowski
Tokyo, Japan
- References:
- Warning from Piecewise
- From: Chris Rodgers <rodgers@physchem.NOSPAMox.aREMOVEc.uk>
- Warning from Piecewise