Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #11478 > unrolled thread

Getting the Derivative of an HoldForm Expression

Started bynitgun@gmail.com
First post2012-08-24 00:56 +0000
Last post2012-09-02 08:45 +0000
Articles 6 — 2 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Getting the Derivative of an HoldForm Expression nitgun@gmail.com - 2012-08-24 00:56 +0000
    Re: Getting the Derivative of an HoldForm Expression David Bailey <dave@removedbailey.co.uk> - 2012-08-25 08:27 +0000
      Re: Getting the Derivative of an HoldForm Expression nitgun@gmail.com - 2012-08-28 08:49 +0000
        Re: Getting the Derivative of an HoldForm Expression David Bailey <dave@removedbailey.co.uk> - 2012-08-30 08:09 +0000
      Re: Getting the Derivative of an HoldForm Expression nitgun@gmail.com - 2012-08-31 08:01 +0000
        Re: Getting the Derivative of an HoldForm Expression David Bailey <dave@removedbailey.co.uk> - 2012-09-02 08:45 +0000

#11478 — Getting the Derivative of an HoldForm Expression

Fromnitgun@gmail.com
Date2012-08-24 00:56 +0000
SubjectGetting the Derivative of an HoldForm Expression
Message-ID<k16jfv$bep$1@smc.vnet.net>
Hi,
am currently fiddling with some equations. To make it more easy I want to introduce subexpressions which should not be evaluated. For this purpose I found HoldForm. But this creates some weird results when I want to get the derivative of such a term. But let's have a look at an example:
In1:=T=HoldForm[2*a[t]]
Out1=2 a[t]

In2:= a[t]=b[t]+c[t]
Out2=b[t]+c[t]

In3:=T
Out3=2 a[t]   <-- this is what I need HoldForm for, as I do not want 2(b[t]+c[t]) as result

In4:=D[T,t]
Out4=2 a'[t] HoldForm'[2(b[t]+c[t])]

I would expect 2 a'[t] as result so the term HoldForm'[...] seems to be a bit weird to me. Can anyone explain to me how I can avoid this term?
Thanks in advance.

[toc] | [next] | [standalone]


#11493

FromDavid Bailey <dave@removedbailey.co.uk>
Date2012-08-25 08:27 +0000
Message-ID<k1a28l$icg$1@smc.vnet.net>
In reply to#11478
On 24/08/2012 01:56, nitgun@gmail.com wrote:
> Hi,
> am currently fiddling with some equations. To make it more easy I want to introduce subexpressions which should not be evaluated. For this purpose I found HoldForm. But this creates some weird results when I want to get the derivative of such a term. But let's have a look at an example:
> In1:=T=HoldForm[2*a[t]]
> Out1=2 a[t]
>
> In2:= a[t]=b[t]+c[t]
> Out2=b[t]+c[t]
>
> In3:=T
> Out3=2 a[t]<-- this is what I need HoldForm for, as I do not want 2(b[t]+c[t]) as result
>
> In4:=D[T,t]
> Out4=2 a'[t] HoldForm'[2(b[t]+c[t])]
>
> I would expect 2 a'[t] as result so the term HoldForm'[...] seems to be a bit weird to me. Can anyone explain to me how I can avoid this term?
> Thanks in advance.
>
The simplest answer, is not to use HoldForm for this purpose, and not to 
make assignments such as

a[t]=b[t]+c[t]

unless you really want them to be used everywhere! Now you can take your 
derivative of T quite easily. Then, if you want to replace a[t] in the 
resultant expression, use

expr /. a[t_]->b[t]+c[t]

Note I have used a pattern here, so that an expression like a[2 k] would 
also get replaced.

I often find it is convenient to collect replacement rules in a list, 
and then just apply the whole set at various points in a calculation. 
For example:

constants={c->299792.0,em->9.1*10^-31, etc etc}

expr=expr/.constants

David Bailey
http://www.dbaileyconsultancy.co.uk

[toc] | [prev] | [next] | [standalone]


#11518

Fromnitgun@gmail.com
Date2012-08-28 08:49 +0000
Message-ID<k1i0nc$4n5$1@smc.vnet.net>
In reply to#11493
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.

[toc] | [prev] | [next] | [standalone]


#11553

FromDavid Bailey <dave@removedbailey.co.uk>
Date2012-08-30 08:09 +0000
Message-ID<k1n736$e6i$1@smc.vnet.net>
In reply to#11518
On 28/08/2012 09:49, nitgun@gmail.com 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
http://www.dbaileyconsultancy.co.uk

[toc] | [prev] | [next] | [standalone]


#11563

Fromnitgun@gmail.com
Date2012-08-31 08:01 +0000
Message-ID<k1pr0f$k0a$1@smc.vnet.net>
In reply to#11493
Thank you very much. I'll keep this in mind and start to forget about HoldForm.

[toc] | [prev] | [next] | [standalone]


#11591

FromDavid Bailey <dave@removedbailey.co.uk>
Date2012-09-02 08:45 +0000
Message-ID<k1v6bp$5bd$1@smc.vnet.net>
In reply to#11563
On 31/08/2012 09:01, nitgun@gmail.com wrote:
> Thank you very much. I'll keep this in mind and start to forget about HoldForm.
>

Remember that HoldForm is very useful in the right circumstances. It is 
useful to show expressions that are deliberately left unvevaluated - 
e.g. HoldForm[2+2].

In other contexts where an expression needs to be held, Hold is 
preferable because it is visible.

David Bailey
http://www.dbaileyconsultancy.co.uk

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web