Re: Patterns with conditions
- To: mathgroup at smc.vnet.net
- Subject: [mg116923] Re: Patterns with conditions
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Fri, 4 Mar 2011 03:41:32 -0500 (EST)
On 3/3/11 at 5:58 AM, Serych at panska.cz wrote:
>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);
>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:
<snip>
>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.
This is a big hint. Use SetAttributes to give your function the
attribute listable, i.e.,
sinc[x_ /; x == 0] := 1;
sinc[x_] := Sin[Pi*x]/(Pi*x);
SetAttributes[sinc, Listable]
tab = {0, Pi/3, Pi*(2/3), Pi, Pi*(4/3), Pi*(5/3), 2*Pi};
sinc[tab]
And you will find you will get the result you want with no error message