RE: Can someone explain this?

*To*: mathgroup at smc.vnet.net*Subject*: [mg19165] RE: [mg19106] Can someone explain this?*From*: "Ersek, Ted R" <ErsekTR at navair.navy.mil>*Date*: Thu, 5 Aug 1999 23:59:08 -0400*Sender*: owner-wri-mathgroup at wolfram.com

Benjamin Lotto wrote, ---------------------- No matter what variation I try, I can't get Mathematica to replace Tan[x] with Sin[x]/Cos[x]. In[1]:= Tan[x]/.Tan[z_]->Sin[z]/Cos[z] Out[1]= Tan[x] ----------------------- It actually does the replacement, but it after the replacement is made the kernel simplifies Sin[x]/Cos[x] to guess what? Tan[x] Before I give a solution I have to bring to your attention a problem with your use of (lhs->rhs). Consider the following: In[2]:= Tan[x]/.Tan[z_]->f2[z] Out[2]= f2[z] OK that works fine, so you continue doing more stuff. ----------------------- In[3]:= z=2.234 + 4.565 I; .... .... .... (* You do a lot more stuff *) (* and the value of z hasn't changed. *) In[134]:= Tan[x]/.Tan[z_]->f2[z] Out[134]= f2[2.234 + 4.565 I] Opps! That isn't right. Whenever you use (lhs->rhs) and (lhs) uses a symbol to name a pattern the global value of the symbol will be used in (rhs). In that case this you should use (lhs:>rhs) as below. I have to inform people of this problem about twice a month. I plan to put it on a web page before I develop carpal tunnel syndrome from typing it over and over. ------------------------- In[135]:= Tan[x]/.Tan[z_]:>f2[z] Out[135]= f2[x] In[136]:= z Out[136]:= 2.234 + 4.565 I The replacement worked above even though z has a global value. --------------------------- Now back to your problem. You can use HoldForm to ensure the kernel doesn't evaluate Sin[x]/Cos[x]. In[137]:= expr=Tan[x]/.Tan[z_]:>HoldForm[Sin[z]/Cos[z]] Out[137]= Sin[x]/Cos[x] But now look what happens when you try to use the result. --------------------------- In[138]:= D[expr,x] Out[138]= (1+Tan[x]^2) HoldForm'[Tan[x]] ------------------ You can use ReleaseHold to remove use of HoldForm, Hold, or HoldComplete. For example the line below succeeds in doing the derivative. In[139]:= D[ReleaseHold[expr],x] Out[139]= Sec[x]^2 One of the features I am dying to see in a future version is a way to prevent further evaluation without requiring the use of ReleaseHold when the result is used later. ------------------- Regards, Ted Ersek