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

*To*: mathgroup at smc.vnet.net*Subject*: [mg83308] 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:21:33 -0500 (EST)*References*: <fhjs9v$4vg$1@smc.vnet.net> <473DA496.5050509@gmail.com>

On Nov 16, 2007 4:10 PM, W. Craig Carter <ccarter at mit.edu> wrote: > > >> 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*) > > > > All this is just about a syntax error you made. > > > > First, note that, as posted, none of the above examples works: they both > > Cher Jean-Marc, > I don't get an error when I cut and paste this expression. > Math6.01 > > And, the posted work-around works just fine: > 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] > > Note, that this seems to be peculiar to Piecewise: e.g, No. > a[c_, d_] := > Module[{e, f}, e = d^2; f = c^2; > Return[MyUnAssignedFunction[{e, 0 < x < 1/2}, {f, 1/2 < x <= 1}]]]; > ` a[1,2] > > works as I hoped. Yes, because MyUnAssignedFunction being not defined, it does not expect or check for any particular argument structure. > PS: My goal here was to write a function that made a calculation of the > bounds, and then returned a piecewise function of x that had the bounds > as explicitly calculated. I have clearly understood what you tried to achieve. I gave you the reason why the syntax you used erroneous and I also gave you the fixed code (i.e. using the correct syntax for Piecewise, no trick or work-around involve here) that returns a piecewise function with its bounds evaluated. So, the following expression returns a piecewise function with its bounds correctly evaluated, doesn't it? In[1]:= 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] Out[2]= Piecewise[{{4, 0 < x < 1/2}, {1, Inequality[1/2, Less, x, LessEqual, 1]}}] The above result is identical to what is returned by the function aAlt (with "work-around"). Indeed, aAlt[1, 2] returns the list of pairs {{4, 0 < x < 1/2}, {1, 1/2 < x <= 1}} which is exactly the format required by Piecewise. Your original code passed the sequence {4, 0 < x < 1/2}, {1, 1/2 < x <= 1} (two separate lists, and not one list of lists) to Piecewise. In other words, {{4, 0 < x < 1/2}, {1, 1/2 < x <= 1}} is not the same as {4, 0 < x < 1/2}, {1, 1/2 < x <= 1}, and In[10]:= Piecewise[{{4, 0 < x < 1/2}, {1, 1/2 < x <= 1}}] Out[10]= \[Piecewise] { {4, 0 < x < 1/2}, {1, 1/2 < x <= 1} } is syntactically correct, whereas In[11]:= Piecewise[{4, 0 < x < 1/2}, {1, 1/2 < x <= 1}] During evaluation of In[11]:= Piecewise::pairs: The first argument \ {4,0<x<1/2} of Piecewise is not a list of pairs. >> Out[11]= Piecewise[{4, 0 < x < 1/2}, {1, 1/2 < x <= 1}] is not. You can get the code I used as well as a pdf file at http://homepages.nyu.edu/~jmg336/mathematica/piecewisemodule.nb http://homepages.nyu.edu/~jmg336/mathematica/piecewisemodule.pdf I hope this clarifies the issue. Best regards, -- Jean-Marc