Re: Re: negative pattern matching anyone?
- To: mathgroup at smc.vnet.net
- Subject: [mg43935] Re: [mg43933] Re: negative pattern matching anyone?
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Tue, 14 Oct 2003 01:07:32 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
How about this:
holdTimes[x_, y_] := If[x y != 0, With[{u = Abs[x], v = Abs[y]},
x y HoldForm[u v]/Abs[x y]], 0]
Everything you demand seems to work:
In[2]:=
holdTimes[3,-2]
Out[2]=
-(3 2)
In[3]:=
holdTimes[-3,-2]
Out[3]=
3 2
I added:
In[4]:=
holdTimes[-2,0]
Out[4]=
0
although you can easily modify it to return
- (2 0)
if you prefer.
Andrzej Kozlowski
Yokohama, Japan
http://www.mimuw.edu.pl/~akoz/
http://platon.c.u-tokyo.ac.jp/andrzej/
On Monday, October 13, 2003, at 05:04 PM, Paolo Bientinesi wrote:
> Paul Abbott wrote:
>>
>> Finally, what is the application? There are possibly other better ways
>> to approach such problems.
>>
>
> Thanks again for the responses.
>
> I have to say that I was searching for a single pattern to match
> both the cases just for elegance, not for strict need.
>
> Anyway the particular problem I'm dealing with is somewhat unnatural:
> I am working with HoldForms, say:
>
> holdTimes[x_,y_]:=HoldForm[x y]
>
> so that
>
> holdTimes[3,-2] returns
>
> 3 (-2)
>
> but what I am particularly interested in is that the product x y is
> not evaluated, while the sign of the operation can be resolved
> (this to avoid situations like -(-(-(-(.... ).
> So I would like holdTimes to behave like
>
> holdTimes[3,-2]
> =
> -(3 2)
>
> and
>
> holdTimes[-3,-a]
> =
> 3 a
>
>
> Unfortunately the definitions
>
> holdTimes[-x_,-y_]:=holdTimes[x,y]
> holdTimes[-x_,y_]:=-holdTimes[x,y]
> holdTimes[x_,-y_]:=-holdTimes[x,y]
> holdTimes[x_,y_]:=holdForm[x y]
>
> don't work, as
>
> holdTimes[3, -4]
> =
> 3 (-4)
>
> and
>
> holdTimes[-3,-4]
> =
> -3 (-4)
>
> but notice that
>
> holdTimes[-a, -b]
> =
> a b
>
>
> --
> Paolo
>
> pauldj at cs.utexas.edu paolo.bientinesi at iit.cnr.it
>
>
>