Re: Patterns with conditions
- To: mathgroup at smc.vnet.net
- Subject: [mg116959] Re: Patterns with conditions
- From: Maxim <m.r at inbox.ru>
- Date: Sun, 6 Mar 2011 05:40:13 -0500 (EST)
- References: <iknsln$kcn$1@smc.vnet.net>
On Mar 3, 5:05 am, =A9er=FDch Jakub <Ser... at panska.cz> wrote: > Dear Mathematica group, > I'm playing with function definitions and patterns based multiple definit= ion 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 defin= e 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 me= ssages: > 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] Note that making the function Listable may not give the optimal performance. Even though functions like Sinc are Listable, packed arrays are a special case and Sinc[Range[1., 10.]] doesn't get expanded into {Sinc[1.], Sinc[2.], ...}. Instead, it only calls Sinc once and then loops over the list internally without having to go through the main evaluator for every element in the list. So it might be better to have a non-listable function with a definition for f[x_List] and to try to make the most out of vectorized operations. But creating a definition without removable singularities is not trivial; even if we have Sinc[z], it's not obvious how to represent Sinc'[z]. Creating new objects Sinc[1, z], etc. for the derivatives doesn't seem particularly elegant. It could be possible to have something like Meromorphic[Sin[z]/z]; then if a given value is not a zero of the denominator, the function can be evaluated normally, and for potential singularities it can always be done by using the power series since the numerator and the denominator are holomorphic. So -- theoretically -- that might be possible. Maxim Rytin m.r at inbox.ru