Re: Replacing an expression by an expression
- To: mathgroup at smc.vnet.net
- Subject: [mg55653] Re: [mg55625] Replacing an expression by an expression
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Fri, 1 Apr 2005 05:36:29 -0500 (EST)
- References: <200503310624.BAA15217@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On 31 Mar 2005, at 08:24, carlos at colorado.edu wrote: > I have a 8 x 8 symbolic matrix B of complicated entries. > Several partial expressions, however, can be simplified. Sample: > > rep= {(J12*x14-J11*y14)*(J12*(x32+x43)-J11*(y32+y43)) -> A0*(A1-A0)}; > > The left expression appears verbatim in entry B[[1,1]], which is > > -(L21*(J12*x14-J11*y14)*(J12*(-x32-x43)+J11*(y32+y43)))/ > (16*A412*J*(J11^2+J12^2)) > > But when I say B=B/.rep, nothing happens. For a few entries I could do > cut and paste by hand: > > -(L21* A0*(A1-A0) )/ > (16*A412*J*(J11^2+J12^2)) > > But it get tedious and error prone for 64. Any suggestions on how to > get Mathematica to do the replacement? > This is a common problem; the expressions that you are trying to match are semantically the same but not syntactically so (look carefully at the minus coefficients of x32 and x43). In this case there is a very simple solution In[1]:= v = -(L21*(J12*x14 - J11*y14)*(J12*(-x32 - x43) + J11*(y32 + y43)))/(16*A412*J*(J11^2 + J12^2)); In[2]:= rep = {(J12*x14 - J11*y14)*(J12*(x32 + x43) - J11*(y32 + y43)) -> A0*(A1 - A0)}; In[3]:= Simplify[v] /. Simplify[rep] Out[3]= (A0*(-A0 + A1)*L21)/(16*A412*J*(J11^2 + J12^2)) but this is not guaranteed to work always in such cases (Simplify may in fact rearrange the matching parts so that they no longer match). There is a more subtle way to do this, which should work in all polynomial and rational function cases, but it can be somewhat tricky. Instead of rule we define an ideal: b = {(J12*x14 - J11*y14)*(J12*(x32 + x43) - J11*(y32 + y43)) - A0*(A1 - A0)}; compute a Groebner basis: gg = GroebnerBasis[b]; and then use: Simplify[Last[PolynomialReduce[v, gg, Join[Variables[v], Variables[gg]]]]] (A0*(-A0 + A1)*L21)/(16*A412*J*(J11^2 + J12^2)) We were lucky here that the order of variables proved to be just right. In general we may have to try different variable orders. Actually, all of the above will be performed automatically by Simplify with assumptions Simplify[v, (J12*x14 - J11*y14)*(J12*(x32 + x43) - J11*(y32 + y43)) == A0*(A1 - A0)] (A0*(-A0 + A1)*L21)/(16*A412*J*(J11^2 + J12^2)) but again we were lucky that the default order of variables happened to be just right. The advantage of the first approach (using PolynomialReduce) is the tone can easier control the order of the variables. Andrzej Kozlowski Chiba, Japan http://www.akikoz.net/andrzej/index.html http://www.mimuw.edu.pl/~akoz/