MathGroup Archive 1998

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

Search the Archive

RE: are nested patterns imposs




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. |

You are redundent if you say----( pat=v*_.|2 ). The (v*2) case was
already covered by (v_.). It's even more usless to say ( pat=v*_.|0)
because (v*0) evaluates to (0).

You said  (  pat=u*_.+(v*_.|0)  ).
Using your pattern consider ( MatchQ[u*5+v*0, pat] ). Before the pattern
matching is done (u*5+v*0) is evaluated to give (5 u). Then the pattern
matcher is looking for either (v) or (v times something). The pattern
doesn't match because (v) is nowhere to be found.

I think that explains why  MatchQ[u*5, pat] was  False.

Here is the way I would do what you have in mind. In[1]:=
pat=(u*_.+v_.)|(u_.);

In[2]:=
MatchQ[u*5+v*10,pat]

Out[2]=
True

In[3]:=
MatchQ[u*5,pat]
Out[3]=
True

BTW:  Both of the above would also evaluate to True if I used (
pat=(u*_+v*_)|(u*_)   ).
But this pattern insist that (u) and (v) are multiplied by something.
When we use (u*_.)  we are saying that a simple (u) is to be considered
the  product (u*1).

Ted Ersek
 




  • Prev by Date: RE: algebraic solutions
  • Next by Date: Re: FindRoot accuracy/precision
  • Prev by thread: sound with 2.2
  • Next by thread: THE ANSWERS: how to pull real numbers out of a linear funktion