Re: another bug...

*To*: mathgroup at smc.vnet.net*Subject*: [mg47667] Re: another bug...*From*: ab_def at prontomail.com (Maxim)*Date*: Wed, 21 Apr 2004 05:23:05 -0400 (EDT)*References*: <c62jsh$rgh$1@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

Enrique Zeleny <ezeleny at fismat1.fcfm.buap.mx> wrote in message news:<c62jsh$rgh$1 at smc.vnet.net>... > Hi, > > > In[1]:= expr = FunctionExpand[Cos[Pi/22]]; > > The next input doesn't work in 5.0, the expression that I want to replace > by a appears many times in expr > > In[2]:= expr /. (1/2)*(-1 + Sqrt[5]) + I*Sqrt[2 - (1/2)*(-1 - Sqrt[5])] -> a > > only works doing this > > In[3]:= expr //. (1/2)*(-1 + Sqrt[5]) + I*Sqrt[2 - (1/2)*(m_)] -> a > > If you look at FullForm > > In[4]:= FullForm[(1/2)*(-1 + Sqrt[5]) + I*Sqrt[2 - (1/2)*(-1 - Sqrt[5])]] > > Out[4]:= Plus[Times[Rational[1, 2], Plus[-1, Power[5, Rational[1, 2]]]], > Times[Complex[0, 1], > Power[Plus[2, Times[Rational[1, 2], Plus[1, Power[5, Rational[1, 2]]]]], > Rational[1, 2]]]] > > but if you copy and paste the result of expr and you use //. > (1/2)*(Sqrt[5] - 1) + I*Sqrt[(1/2)*(Sqrt[5] + 1) + 2] -> a to replace works well!!! > > In Version 4.1 only this works well (but not in 5.0), but the expression > that appears many times is written with +'s > > In[2]:= expr //. (1/2)*(-1 + Sqrt[5]) + I*Sqrt[2 + (1/2)*(1 + Sqrt[5])] -> > a > > If you copy and paste the result of expr and you use only /. instead of > //. to replace (1/2)*(-1 + Sqrt[5]) + I*Sqrt[2 + (1/2)*(1 + Sqrt[5])] ->a > works fine!!! > > > Clearly the problem is recognizing > > - (1/2)*(-1 - Sqrt[5]) > > > > Enrique Zeleny > Universidad Autonoma de Puebla > Mexico I'd say there are three questions here. First is that we have to know the internal representation of the expression: In[1]:= MatchQ[1/2, HoldPattern[1/2]] Out[1]= False because 1/2 is Rational[1,2] but with HoldPattern preventing evaluation 1/2 is stored as Times[1, Power[2,-1]]. Second is understanding that Mathematica can perform some transformations automatically: In[2]:= MatchQ[-1/2(-a-b), k_(-a-b)] Out[2]= False because -1/2(-a-b) is rewritten as 1/2(a+b). This is more annoying; for example, In[3]:= MatchQ[-2(-a-b), k_(-a-b)] Out[3]= True works differently -- no transformation is done here. And the third thing is that in the case of FunctionExpand[Cos[Pi/22]] there is another complication: -1/2(-1-Sqrt[5]) in the output is not rewritten as 1/2(1+Sqrt[5]). I think this violates the principle of infinite evaluation (I like the term fixed point evaluation), because when you copy and paste the output and reevaluate it, you get a different expression, now with -1/2(-1-Sqrt[5]) converted to 1/2(1+Sqrt[5]). ReplaceAll outcome will also be different. Maxim Rytin m.r at prontomail.com