[Date Index]
[Thread Index]
[Author Index]
Re: Defining a function P[A_ | B_]:=f[A,B] overriding Alternatives
*To*: mathgroup at smc.vnet.net
*Subject*: [mg19221] Re: Defining a function P[A_ | B_]:=f[A,B] overriding Alternatives
*From*: Eckhard Hennig <hennig at itwm.uni-kl.de>
*Date*: Wed, 11 Aug 1999 02:06:42 -0400
*Organization*: ITWM
*References*: <7oojvg$i35@smc.vnet.net>
*Sender*: owner-wri-mathgroup at wolfram.com
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: How do I make my notebook act like ...**
Next by Date:
**Re: How to suppress the beep in version 4?**
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**
| |