Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Pattern matching

  • To: mathgroup at smc.vnet.net
  • Subject: [mg116555] Re: Pattern matching
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Sat, 19 Feb 2011 05:14:56 -0500 (EST)

_.  represents an argument that can be omitted and replaced with the Default

rule = f[u + x_.] -> x;

{f[u + 5], f[u + 0], f[u]} /. rule

{5, 0, 0}

rule2 = f[a_. * u + x_.] -> a*x;

{f[3 u + 5], f[u + 5], f[3 u + 0], f[u + 0], f[3 u], f[u]} /. rule2

{15, 5, 0, 0, 0, 0}

Default /@ {Plus, Times}

{0, 1}


Bob Hanlon

---- =?ISO-8859-1?Q?=22Andr=E9_M=2E_Grabinski=22?= at smc.vnet.net wrote: 

=============
Hi everyone,

hopefully someone of you could answer me a simple question concerning
patterns in Mathematica 8. Consider the following minimal example,


In[1]:= rule = f[u + x_] -> x;
        f[u + 5] /. rule
        f[u + 0] /. rule
        f[u] /. rule

Out[1]= 5

Out[2]= f[u]

Out[3]= f[u]


Now the question: in which way do I need to modify "rule" in order to
get "0" as result for "Out[3]"? Of course I could add a second rule,
i.e. "rule = {f[u + x_] -> x,f[u] -> 0}", but I feel like there should
be a more elegant way than to take care of each and every exception
(okay, in this minimal example this wouldn't matter but I'm just
interested in the general way of setting up patterns).

Thank you very much in advance,

A.G.



  • Prev by Date: Re: Pattern matching
  • Next by Date: Re: Approximation
  • Previous by thread: Re: Pattern matching
  • Next by thread: Re: Pattern matching