MathGroup Archive 1999

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

Search the Archive

Re: Defining a function P[A_ | B_]:=f[A,B] overriding Alternatives

  • To: mathgroup at smc.vnet.net
  • Subject: [mg19258] Re: Defining a function P[A_ | B_]:=f[A,B] overriding Alternatives
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Thu, 12 Aug 1999 01:24:17 -0400
  • References: <7oojvg$i35@smc.vnet.net> <7or3jp$o6@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Eckhard:
Following your idea  n the posting below:

p[HoldPattern[Alternatives][A_ , B_]] := {A, B}

DownValues[p]

{HoldPattern[p[HoldPattern[Alternatives][A_, B_]]] :> {A, B}}

p[a | b]

{a, b}

Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565



Eckhard Hennig <hennig at itwm.uni-kl.de> wrote in message
news:7or3jp$o6 at smc.vnet.net...
>
> Peltio schrieb in Nachricht <7oojvg$i35 at smc.vnet.net>...
> >Is there a general way to call a function with arguments separated not by
> >commas but by user defined symbols (particularly Mathematica sacred
> symbols)
> >?
> >_________________________________________________
> >
> >
> >Here's what I mean:
> >I was willing to define a function for the calculation of the conditional
> >probabilty, via the Bayes formula, and I wanted to define it in such a
way
> >that the function call would be:
> >
> >p[A|B]
> >
> >but Mathematica correctly interpreted the lhs of the definition
> >p[A_|B_] as if there were two alternative arguments.
> >I can unprotect Alternatives and clear it and that would work,
> >
> >Unprotect[Alternatives];
> >Clear[Alternatives];
> >p[A_|B_]:= {A,B}
> >
> >p[A|B]
> >    {A,B}
> >
> >but, is there a less traumatic way to achieve this without fully
> sacrificing
> >Alternatives? It would be fine to avoid its evaluation only when inside a
> >[ ] function, by means of upvalues. But the following does not seems to
> >work:
> >
> >Unprotect[Alternatives];
> >Alternatives /: p[A_|B_]:={A,B}
> >
> >And yet I wanted to be able to define my function without showing the
> >Alternatives /: part. Is there a way to inhibit the Alternatives
evaluation
> >inside p[] in a single statement (that could be hidden in a
initialization
> >cell)?
>
>
> After defining p[A_ | B_] you can manipulate the DownValues of p as
follows
> to prevent interpretation of the Alternatives pattern.
>
>
> In[1]:=
> p[A_ | B_] := {A, B}
>
> In[2]:=
> DownValues[p] = DownValues[p] /.
>   Alternatives -> HoldPattern[Alternatives];
>
> In[3]:=
> p[C | D]
>
> Out[3]=
> {C, D}
>
>
> However, this requires applying the replacement rule from In[2] after
> defining the pattern p[A_|B_]. The following piece of code performs this
> replacement automatically; it can be put into an initialization cell. Note
> that the SetDelayed pattern only works for the symbol p. The variable
> SetDelayedFlag prevents infinite recursion.
>
> In[1]:=
> SetDelayedFlag = True;
> Unprotect[SetDelayed];
> SetDelayed[x_p, y_] /; SetDelayedFlag :=
>   Block[{SetDelayedFlag = False},
>     x := y;
>     DownValues[p] = DownValues[p] /.
>       {HoldPattern[Alternatives] -> HoldPattern[Alternatives],
>        Alternatives -> HoldPattern[Alternatives]};
> ];
> Protect[SetDelayed];
>
> In[2]:=
> p[ A_ | B_] := {A, B}
>
> (* No explicit manipulation of DownValues necessary here. *)
>
> In[3]:=
> p[C | D]
>
> Out[3]=
> {C, D}
>
> -- Eckhard
>
> -----------------------------------------------------------
> Dipl.-Ing. Eckhard Hennig      mailto:hennig at itwm.uni-kl.de
> Institut fuer Techno- und Wirtschaftsmathematik e.V. (ITWM)
> Erwin-Schroedinger-Strasse,  67663 Kaiserslautern,  Germany
>   Voice: +49-(0)631-205-3126    Fax: +49-(0)631-205-4139
>     http://www.itwm.uni-kl.de/as/employees/hennig.html
>
>      ITWM - Makers of Analog Insydes for Mathematica
>         http://www.itwm.uni-kl.de/as/products/ai
> -----------------------------------------------------------
>
>
>
>



  • Prev by Date: Re: Cleanslate -- where to find it?
  • Next by Date: Re: Mathematica Link for Excel and Excel 2000
  • Previous by thread: Re: Defining a function P[A_ | B_]:=f[A,B] overriding Alternatives
  • Next by thread: Re: Defining a function P[A_ | B_]:=f[A,B] overriding Alternatives