Re: negative pattern matching anyone?

*To*: mathgroup at smc.vnet.net*Subject*: [mg43775] Re: negative pattern matching anyone?*From*: Paul Abbott <paul at physics.uwa.edu.au>*Date*: Sat, 4 Oct 2003 02:04:53 -0400 (EDT)*Organization*: The University of Western Australia*References*: <blcqqj$p8h$1@smc.vnet.net> <blgio6$fvm$1@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

In article <blgio6$fvm$1 at smc.vnet.net>, Paul Abbott <paul at physics.uwa.edu.au> wrote: > > Then, is there a way to match both -3 and -a with the same pattern? > > Yes -- use Sign: > > h[x_] := Abs[-x] /; Sign[x] == -1 > h[x_] := x^2 I forgot to mention that also you need to declare a to be positive: h[x_] := Abs[-x] /; Sign[x] == -1 h[x_] := x^2 Sign[a] ^= 1; h[-3] 3 h[-a] Abs[a] h[a] a^2 To me it doesn't make sense for h[-x] to evaluate to Abs[x] unless x is positive and, a priori, this is not known. Indeed, it would probably be better to define Clear[h] h[x_] := Abs[-x] /; Sign[x] == -1 h[x_] := x^2 /; Sign[x] == 1 so that h[x] and h[-x] remain unevaluated unless Sign[x] is declared. Finally, what is the application? There are possibly other better ways to approach such problems. Cheers, Paul -- Paul Abbott Phone: +61 8 9380 2734 School of Physics, M013 Fax: +61 8 9380 1014 The University of Western Australia (CRICOS Provider No 00126G) 35 Stirling Highway Crawley WA 6009 mailto:paul at physics.uwa.edu.au AUSTRALIA http://physics.uwa.edu.au/~paul