[Date Index] [Thread Index] [Author Index]
Re: negative pattern matching anyone?
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