Re: Question about pattern matching
- To: mathgroup at smc.vnet.net
- Subject: [mg34086] Re: [mg34057] Question about pattern matching
- From: "Dr. Reinhard Simonovits" <Reinhard.Simonovits at uni-graz.at>
- Date: Thu, 2 May 2002 03:49:27 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Dear Mike, Your question is a very good one and typical in computer algebra systems. There are several general ways to tackle your problem of substitution: (kg m^2) /(s^2) /. (kg m)/(s^2) -> n I would recommend the first one. Note: All calculations are done with Mathematica 3.0.1 (1) Let Mathematica do everything for you. - The Solve solution In[1]:= Clear[kg,ms,s]; kg m^2 /s^2 /.Solve[ kg m == s^2 n,kg][[1]] Out[1]= m n Put your substitution in an equation, calculate a variable and /. does the substitution of the result of the Solve command for you. Here you solve the equation for kg. Depending on which variable you are solving your equation, the substitution may vary a little. Here you solve the equation for s. In[2]:= kg m^2 /s^2 /.Solve[ kg m == s^2 n,s][[1]] Out[2]= m n The result ist the same as from above. Here you solve the equation for m. In[3]:= kg m^2 /s^2 /.Solve[ kg m == s^2 n,m][[1]]//InputForm Out[3]//InputForm= (n^2*s^2)/kg m is quadratically, so another (*correct*) result is displayed. Now, you can use the Solve trick for more complicated expressions: Example 1 Substitution in the term kg^2 m^4 /s^7 IIn[7]:= kg^2 m^4 /s^7 /.Solve[ kg m == s^2 n,kg][[1]]//InputForm Out[7]//InputForm= (m^2*n^2)/s^3 Example 2 Substitution in the term m^4 /s^7. (* kg is omitted here *) In[8]:= m^4 /s^7 /.Solve[ kg m == s^2 n,m][[1]]//InputForm Out[8]//InputForm= (n^4*s)/kg^4 Example 3 Substitution in the term kg m s. In[9]:= kg m s /.Solve[ kg m == s^2 n,m][[1]]//InputForm Out[9]//InputForm= n*s^3 So far so good. (2) Using direct pattern matching, working "by hand" with replacement rules First of all look at the FullFom of your expression. In[10]:= FullForm[ kg m^2 /s^2 ] Out[10]//FullForm= Times[kg,Power[m,2],Power[s,-2]] Ok. Lets find a pattern to match your expression: In[12]:= MatchQ[ kg m^2 /s^2 , kg^a_. m^b_. s^c_.] Out[12]= True Note: a_. means: Match also kg^1, the power. Important is the . after the _ Ok. Tell Mathematica to reduce the power of m by one: In[13]:= kg m^2 /s^2 /.( kg m^a_)/s^2:>m ^(a-1) n Out[13]= m n The pattern a_ matches 2, all other symbols remain the same. a - 1 reduces the power. The result is the same as of the Solve trick. :> does delayed replacing, (* This means match first, replace then*) So your problem is solved by direct pattern matching. What about the generalization presented by Example 1, Example 2, Example 3? You have to extend the replacement rule. Example 1 Substitution in the term kg^2 m^4 /s^7 In[15]:= kg^2 m^4 /s^7 //. kg^a_. m^b_. s^c_.:>n kg^(a-1) m^(b-1)s^(c+2)//InputForm Out[15]//InputForm= (m^2*n^2)/s^3 The result is the same as with the Solve trick. Note: //. tells Mathematica: Do repeated replacement until nothing changes in the result. Example 2 Substitution in the term m^4 /s^7. (* kg is omitted here *) In[17]:= m^4/s^7//. kg^a_. m^b_. s^c_.:>n kg^(a-1) m^(b-1)s^(c+2)//InputForm Out[17]//InputForm= m^4/s^7 Apparently this form of replacement is not sufficient. No replacement is done. Example 3 Substitution in the term kg m s. In[19]:= kg m s/. kg^a_. m^b_. s^c_.:>n kg^(a-1) m^(b-1)s^(c+2)//InputForm Out[19]//InputForm= n*s^3 This yields the same result as with the Solve method. In order to provide the same results as with the Solve method also in Example 2, you have to (a) rewrite the replacement rule kg^a_. m^b_. s^c_.:>n kg^(a-1) m^(b-1)s^(c+2) (b) build in safeguards in the rule. What about the terms like kg^hallo m^symbol1 / s^symbol2 etc...? (3) The third method: Functional approach. I think this might do Allan Hayes, Harmut Wolf, Bob Hanlon, Jens Peer Kuska, Tedd Ersek, Andrzej Koslowski, .... They also have tricks to improve (1) and (2). BTW, I learned a lot of their contributions to this news group... Best wishes, Reinhard >I have a rather straight forward question on pattern matching. > >Say that I have a expression given as: > >(kg m^2) /(s^2) > >When I try a pattern matching substitution such as: > >(kg m^2) /(s^2) /. (kg m)/(s^2) -> n > >Returns: >(kg m^2) /(s^2) > > >Am I missing some sort of rule? How can I do this sort of pattern matching. > >Thanks for you help. >Mike ******************************************** Dr. Reinhard Simonovits Handelsakademie | Karl Franzens University Math Department | Inst. of Th. Physics Grazbachgasse 71 | Universitaetsplatz 5 A-8010 Graz, Austria Email: Reinhard.Simonovits at uni-graz.at *********************************************