MathGroup Archive 2012

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

Search the Archive

Re: Getting the Derivative of an HoldForm Expression

On 28/08/2012 09:49, nitgun at wrote:
> Thank you for your reply.
> Unfortunately I made my example a bit confusing. Because actually I need the replacement the other way round. I want to replace common subexpressions.  Some occur after some fancy calculations and others I can avoid by substituting right at the beginning (as my given example). That's why I want to include the terms for the replacement in the notebook as well so that others can understand the calculations. Otherwise nobody would know what a[t] stands for, unless he finds the replacement in the end. But maybe it works when I put it into such a replacement list.
Replacing common subexpressions will work too. For example:

In[4]:= a[2 t] + b[t] + c[2 t] + d[ t] /. c[x_] + a[x_] -> g[x]

Out[4]= b[t] + d[t] + g[2 t]

Some cases are trickier

In[5]:= (a + b)/Sqrt[c + d] /. Sqrt[c + d] -> g[c, d]

Out[5]= (a + b)/Sqrt[c + d]

You sometimes need to know a bit about how expressions are represented 
internally in Mathematica:

In[6]:= (a + b)/Sqrt[c + d] /. (c + d)^p_. -> g[c, d]^(2 p)

Out[6]= (a + b)/g[c, d]

Note that Mathematica is automatically aware that additions and 
subtractions can be shuffled into any order (likewise for 
multiplications), and that divisions and roots are expressed internally 
in power notation.

Assignments are far more useful inside pieces of actual code, but they 
can be quite dangerous in hand explorations of equations, because a left 
over assignment (say m=1) messes up expressions like (m c^2), and in a 
large expression that might not be instantly obvious!

Replacement rules are extremely powerful, and it is well worth exploring 
them in depth.

David Bailey

  • Prev by Date: What is this shorthand notation?
  • Next by Date: Re: Luke's list of mathematica questions
  • Previous by thread: Re: Getting the Derivative of an HoldForm Expression
  • Next by thread: Re: Getting the Derivative of an HoldForm Expression