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: [mg83324] Re: Piecewise inside a Module or Block, I don't understand this behavior.
  • From: Albert <awnl at arcor.net>
  • Date: Sat, 17 Nov 2007 05:29:48 -0500 (EST)
  • Organization: Arcor
  • 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}]]];

just a sidenote: Return is not necessary since the last expression 
within the module is returned anyway. In fact Return sometimes behaves a 
little different than expected, so it is better not used if there is no 
reason to...

> 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]
> 
> (*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....
> 
Piecewise has attributes HoldAll, so the symbols e and f are not 
evaluated to d^2 and c^2. When using Piecewise@aAlt[] you are 
effectively doing Piecewise at {{...}} which you can also do directly 
within the module:

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

There are other tricks to achieve what you expected to happen, you just 
have to find a way to put the evaluated versions of e and f into the 
Piecewise function. A very handy candidate for this is With:

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

which I think is the cleanest way to achieve what you want.

If all this seems strange to you I suggest you learn more about the 
evaluation process within mathematica, good starting points are the 
tutorials listed at the bottom of guide/EvaluationControl in the 
Documentation-Center. This is an advanced topic but understanding it 
will probably be an eye opener and make you gain much more from 
mathematica's power...

hth,

albert


  • Prev by Date: Re: Re: Message: "Numerical interation converging too slowly"
  • Next by Date: classes.m
  • Previous by thread: Re: Piecewise inside a Module or Block, I don't understand this behavior.
  • Next by thread: BinaryWrite and Position in Mathematica 6