Re: Conditional SetDelayed with Replacement
- To: mathgroup at smc.vnet.net
- Subject: [mg92859] Re: Conditional SetDelayed with Replacement
- From: yatesd at mac.com
- Date: Wed, 15 Oct 2008 05:38:37 -0400 (EDT)
- References: <gcv7d5$e1i$1@smc.vnet.net>
Others have shown how to create what you want using With (and that
technique is very important), but the type of function you are
creating here is also a good candidate for Piecewise:
f[x_] := Evaluate[Piecewise[Transpose[{Thread[x < Range[5]^2],
Range[5]}]]]
where I have put in the Evaluate so that the Piecewise function does
not have to be created each time f is called.
Information[f] shows you that the definition stored for f is now
f[x_]:=Piecewise[{{x<1,1},{x<4,2},{x<9,3},{x<16,4},{x<25,5}},0]
You could also use Table instead of the Thread and Transpose used
above:
f[x_]:=Evaluate[Piecewise[Table[{x<i^2,i},{i,5}]]]
gives the same result, and is probably easier to read.
Regards,
Derek