MathGroup Archive 1998

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

Search the Archive

Re: are nested patterns impossible?



Thomas Lemm wrote:
> 
> I try to create a pattern describing a linear combination of a list of
> variables. Be those variables u and v, the pattern should match for
> both
> 
> u*k+v*l (k,l constants)
> 
> and
> 
> u*k     (coefficient of v == 0)
> 
> But when I program it in using nested patterns
> 
> In[82]:=pat=u*_.+(v*_.|0)
> Out[82]=(v _.|0)+u _.
> 
> In[84]:=MatchQ[u*5+v*10,pat]
> Out[84]=True
> 
> In[86]:=MatchQ[u*5,pat]
> Out[86]=False
> 
> The last case does not match, which leads to the question:  what did I
> program and how can I program the thing I intended to?
> 
> I already know about the possibility making a pattern like
> 
> u*_.+v*_.|u*_.
> 
> But this is not the way I want to program this example as I would like
> to extend this case to many more than two variables u,v.
> 
> Thomas Lemm


Thomas:
Something along the following lines?

In[1]:=
pat2=HoldPattern[Plus[(u|v|w)*_. ... ]]|(u|v|w)*_.|0;

In[2]:=
MatchQ[v*10+u +3w,pat2]
MatchQ[u*5,pat2]
MatchQ[0,pat2]

Out[2]=
True

Out[3]=
True

Out[4]=
True


-- 
Allan Hayes
Mathematica Training and Consulting
Leicester, UK
hay@haystack.demon.co.uk
http://www.haystack.demon.co.uk
voice: +44 (0)116 271 4198
fax: +44 (0)116 271 8642




  • Prev by Date: Re: Simple question
  • Next by Date: Re: combine two vectors to form matrix
  • Prev by thread: Re: are nested patterns impossible?
  • Next by thread: Assignment to submatrices