Re: Patterns with conditions
- To: mathgroup at smc.vnet.net
- Subject: [mg116929] Re: Patterns with conditions
- From: Peter Pein <petsie at dordos.net>
- Date: Fri, 4 Mar 2011 03:42:37 -0500 (EST)
- References: <iknsln$kcn$1@smc.vnet.net>
Am 03.03.2011 12:05, schrieb ?erých Jakub: > Dear Mathematica group, > I'm playing with function definitions and patterns based multiple definition of the function. I have defined this function: > sinc[x_ /; x == 0] := 1; > sinc[x_] := Sin[\[Pi] x]/(\[Pi] x); > > (I know, that Mathematica has Sinc function defined, it's just the test.) > > It works fine for let's say sinc[\[Pi]], even for sinc[0]. But if I define the table: > > tab = {0, \[Pi]/3, \[Pi]/2, \[Pi] 2/3, \[Pi], \[Pi] 4/3, \[Pi] 5/3, > 2 \[Pi]}; > > and I let my function evaluate the results sinc[tab], it returns error messages: > Power::infy: Infinite expression 1/0 encountered.>> and > Infinity::indet: Indeterminate expression 0 ComplexInfinity encountered.>> > > I can understand, that "tab" doesn't fit to pattern condition /; x==0, also I know, that it is possible to Map my function to table > Map[sinc, tab] and it works fine. > > I can imagine solution with IF[x==0,1, Sin[\[Pi] x]/(\[Pi] x), but my question is: Is it possible to make my function fully Listable using just pattern conditions? > > Thanks for responses > > Jakub > > P.S. Code in one block for easy copying: > > sinc[x_ /; x == 0] := 1; > sinc[x_] := Sin[\[Pi] x]/(\[Pi] x); > tab = {0, \[Pi]/3, \[Pi] 2/3, \[Pi], \[Pi] 4/3, \[Pi] 5/3, 2 \[Pi]}; > sinc[\[Pi]] > sinc[0] > sinc[tab] > Map[sinc, tab] > Hi, if you want to get the behaviour of Sinc, you should drop the \[Pi]s in the definition. In Version 8 works: In[1]:= sinc[x_] := 1 /; x == 0; sinc[x_] := Sin[x]/x; SetAttributes[sinc, Listable]; In[4]:= sinc[Range[0, 2, 1/3]*Pi] Out[4]= {1, (3*Sqrt[3])/(2*Pi), (3*Sqrt[3])/(4*Pi), 0, -((3*Sqrt[3])/(8*Pi)), -((3*Sqrt[3])/(10*Pi)), 0} And you're right: any list is != 0. That is the reason for the attribute Listable. hth, Peter