Services & Resources / Wolfram Forums / MathGroup Archive
-----

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: [mg83341] Re: Piecewise inside a Module or Block, I don't understand this behavior.
  • From: Peter Pein <petsie at dordos.net>
  • Date: Sat, 17 Nov 2007 05:38:40 -0500 (EST)
  • References: <fhjs9v$4vg$1@smc.vnet.net>

Hi Craig,

have a closer look at the syntax of Piecewise[] and remember LISP.

 Similar to the double parentheses in
(cond ((condition1 action1) ... (t default)))
in LISP, you should write
Piecewise[{{action_1, condition_1},...,{action_n, condition_n}},default]
in Mathematica.

That's the way I memorized the double curly braces

Peter

W. Craig Carter schrieb:
> 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*)
> 
> (*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: Re: Basic Locator question in a Manipulate...
  • Next by Date: Re: Get list of function variables?
  • 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.