Re: An argument pattern problem: ranges and lists

• To: mathgroup at smc.vnet.net
• Subject: [mg52993] Re: [mg52973] An argument pattern problem: ranges and lists
• From: Bob Hanlon <hanlonr at cox.net>
• Date: Sun, 19 Dec 2004 06:14:36 -0500 (EST)
• Sender: owner-wri-mathgroup at wolfram.com

```Use TagSet ( /: )

pauli /: f[pauli[x_,y_]] := x+y /; 0<=y<=4;
pauli /: g[pauli[x1_,y1_], pauli[x2_,y2_]] :=
(x1+y1)*(x2+y2) /; 0<=y1<=4 && 0<=y2<=4;

{f[pauli[a,1]],f[pauli[a,5]]}

{a + 1, f[pauli[a, 5]]}

{g[pauli[a,1],pauli[b,2]],
g[pauli[a,1],pauli[b,5]]}

{(a + 1)*(b + 2), g[pauli[a, 1], pauli[b, 5]]}

Bob Hanlon

>
> From: Simon Anders <simon.anders at uibk.ac.at>
To: mathgroup at smc.vnet.net
> Date: 2004/12/18 Sat AM 03:59:53 EST
> To: mathgroup at smc.vnet.net
> Subject: [mg52993] [mg52973] An argument pattern problem: ranges and lists
>
> Hi,
>
> I am trying to use argument patterns to ensure that my function is only
> applied onto arguments which have the format
>     pauli [_, _]
> where the second argument should be an integer between 0 and 4.
>
> So I defined a type
>     pauliT = HoldPattern [pauli [_, s_Integer] /; s>=0 && s<=4];
>
> and declared my function as follows:
>    f [p:pauliT] := DoSomething
>
> This works fine, but another function takes a pair of arguments
>    g [p:{pauliT,pauliT}] := DoSomethingElse
>
> This is never called, because the pattern cannot be matched.
>
> I observed:
>
>    pauliT = HoldPattern [pauli [_, s_Integer] /; s>=0 && s<=4];
>    MatchQ[{pauli[1, 2], pauli[1, 3]}, {pauliT, pauliT}]
>    --> False
>
> The problem seems to be that I named the second argument 's', because if
> I delete this name, it matches:
>
>    pauliT = HoldPattern [pauli [_, s_Integer]];
>    MatchQ[{pauli[1, 2], pauli[1, 3]}, {pauliT, pauliT}]
>    --> False
>
>    pauliT = HoldPattern [pauli [_, _Integer]];
>    MatchQ[{pauli[1, 2], pauli[1, 3]}, {pauliT, pauliT}]
>    --> True
>
> So, how do I specify the range condition correctly?
>