MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Patterns with conditions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg116900] Re: Patterns with conditions
  • From: Albert Retey <awnl at gmx-topmail.de>
  • Date: Fri, 4 Mar 2011 03:37:22 -0500 (EST)
  • References: <iknsln$kcn$1@smc.vnet.net>

Am 03.03.2011 12:05, schrieb =8Aer=FDch 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?

whether a function is listable or not depends on its attributes, so no,
you cannot enforce it with pattern conditions, but of course you can do
so by setting the attribute:

SetAttributes[sinc, Listable]
sinc[x_ /; x == 0] := 1;
sinc[x_] := Sin[\[Pi] x]/(\[Pi] x);

which will make your examples work as intended. Note that the reason for
your original definition to somtimes be listable is that for arbitrary
arguments it would call Sin - which is listable.

hth,

albert


  • Prev by Date: Re: Prepending Data to a File
  • Next by Date: Re: Patterns with conditions
  • Previous by thread: Re: Patterns with conditions
  • Next by thread: Re: Patterns with conditions