Re: Piecewise inside a Module or Block, I don't understand this behavior.

• To: mathgroup at smc.vnet.net
• Subject: [mg83296] Re: Piecewise inside a Module or Block, I don't understand this behavior.
• From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
• Date: Sat, 17 Nov 2007 05:15:22 -0500 (EST)
• Organization: The Open University, Milton Keynes, UK
• References: <fhjs9v\$4vg\$1@smc.vnet.net>

```W. Craig Carter wrote:

> I have a Piecewise function calculated in a module:
> Here is a simplifed example of something which has a
> behavior that puzzles me.
>
> a[c_, d_] :=
>   Module[{e, f}, e = d^2; f = c^2;
>    Return[Piecewise[{e, 0 < x < 1/2}, {f, 1/2 < x <= 1}]]];
> a[1,2] (*doen't return what I had anticipated*)
>
> (*However this does*)
> a[c_, d_] :=
> Module[{e, f}, e = d^2; f = c^2;
> Return[Piecewise[{d^2, 0 < x < 1/2}, {c^2, 1/2 < x <= 1}]]];
> a[1,2]

<snip>

First, note that, as posted, none of the above examples works: they both
return the same error message complaining about the erroneous structure
of the argument given to Piecewise.

In[1]:= a[c_, d_] := Module[{e, f}, e = d^2; f = c^2;
Return[Piecewise[{e, 0 < x < 1/2}, {f, 1/2 < x <= 1}]]];
a[1, 2]

a[c_, d_] := Module[{e, f}, e = d^2; f = c^2;
Return[Piecewise[{d^2, 0 < x < 1/2}, {c^2, 1/2 < x <= 1}]]];
a[1, 2]

During evaluation of In[1]:= Piecewise::pairs: The first argument
{4,0<x<1/2} of Piecewise is not a list of pairs. >>

Out[2]=
1          1
Piecewise[{e\$65, 0 < x < -}, {f\$65, - < x <= 1}]
2          2

During evaluation of In[1]:= Piecewise::pairs: The first argument
{4,0<x<1/2} of Piecewise is not a list of pairs. >>

Out[4]=
2          1     2  1
Piecewise[{2 , 0 < x < -}, {1 , - < x <= 1}]
2        2

The correct syntax for Piecewise is a list of lists. (Note that Return
is useless in these functions.) The following will work as expected.

In[5]:= a[c_, d_] := Module[{e, f}, e = d^2; f = c^2;
Piecewise[{{e, 0 < x < 1/2}, {f, 1/2 < x <= 1}}]];
a[1, 2]

a[c_, d_] := Module[{e, f}, e = d^2; f = c^2;
Piecewise[{{d^2, 0 < x < 1/2}, {c^2, 1/2 < x <= 1}}]];
a[1, 2]

Out[6]=
1       1
Piecewise[{{4, 0 < x < -}, {1, - < x <= 1}}]
2       2

Out[8]=
1       1
Piecewise[{{4, 0 < x < -}, {1, - < x <= 1}}]
2       2

Regards,
--
Jean-Marc

```

• Prev by Date: Re: Get list of function variables?
• Next by Date: Re: Does ColorFunction-> have different meanings to Plot3D and
• Previous by thread: Re: Piecewise inside a Module or Block, I don't understand this behavior.
• Next by thread: Re: Piecewise inside a Module or Block, I don't understand this behavior.