MathGroup Archive 2007

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

Search the Archive

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

  • To: mathgroup at smc.vnet.net
  • Subject: [mg83284] Re: Piecewise inside a Module or Block, I don't understand this behavior.
  • From: Szabolcs Horvát <szhorvat at gmail.com>
  • Date: Sat, 17 Nov 2007 05:09:04 -0500 (EST)
  • References: <fhjs9v$4vg$1@smc.vnet.net>

W. Craig Carter wrote:
> Hello,
> 
> 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*)

You did not explain what kind of behaviour you expected ... I do not see 
anything wrong with the *behaviour* of this example.  But I would like 
to note that Piecewise is used incorrectly (it should be Piecewise[{{e, 
0 < x < 1/2}, {f, 1/2 < x <= 1}}]), 'x' is not defined, Return[] is not 
needed at the end of Module[], and ';' is not needed at the end of a := 
definition.

This is what I get:

In[1]:=
a[c_, d_] := Module[{e, f}, e = d^2; f = c^2;
      Return[Piecewise[{e, 0 < x < 1/2}, {f, Inequality[1/2, Less, x,
          LessEqual, 1]}]]];
a[1, 2] (* original *)

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]= Piecewise[{e$65, 0 < x < 1/2}, {f$65, Inequality[1/2, Less, x,
     LessEqual, 1]}]


In[3]:=
a[c_, d_] := Module[{e, f}, e = d^2; f = c^2;
     Piecewise[{{e, 0 < x < 1/2}, {f, Inequality[1/2, Less, x,
         LessEqual, 1]}}]]
a[1, 2] (* corrected version *)

Out[4]= Piecewise[{{4, 0 < x < 1/2}, {1, Inequality[1/2, Less, x, 
LessEqual,
      1]}}]

In[5]:=
x = 0.3;
a[c_, d_] := Module[{e, f}, e = d^2; f = c^2;
     Piecewise[{{e, 0 < x < 1/2}, {f, Inequality[1/2, Less, x,
         LessEqual, 1]}}]]
a[1, 2] (* 'x' is defined *)

Out[7]= 4

(Sorry about the ugly InpuForm ... just paste it into Mathematic and 
press CTRL-SHIFT-N to convert the inequalities to a more readable form.)

Szabolcs


> (*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]
> 
> (*The same goes for Block, and putting an explicit Evaluate
> inside the local function*)
> 
> (*This behavior seems to be unique to Piecewise*)
> a[c_, d_] :=
> Module[{e, f}, e = d^2; f = c^2;
> Return[MyFunction[{e, 0 < x < 1/2}, {f, 1/2 < x <= 1}]]];
> a[1,2]
> 
> I can't find anything about Piecewise, or in Module and
> Block documentation that gives me a hint.
> 
> Anyone know what is going on?
> 
> Thanks, Craig
> 
> PS: I have a work-around:
> 
> aAlt[c_, d_] :=
> Module[{e, f}, e = d^2; f = c^2;
> {{e, 0 < x < 1/2}, {f, 1/2 < x <= 1}}];
> Piecewise@aAlt[1,2]
> 
> but, I am still curious....
> 


  • Prev by Date: Creating a Slideshow with Mathematica6!
  • Next by Date: data structures in Mathematica
  • Previous by thread: 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.