Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Automatically replacing derivatives in Mathematica

1,664 views
Skip to first unread message

HEREMAN WILLY

unread,
Jun 13, 1995, 3:00:00 AM6/13/95
to


Dear Colleagues:

If in Mathematica you have built up an equation, such as

eq=D[f[x,t],t]+6*f[x,t]*D[f[x,t],x]+D[f[x,t],{x,3}] (1)

and you THEN want to replace f[x,t] and ITS DERIVATIVES by an
explicit expression, such as,

f[x,t] = u[0][x,t]*g[x,t]^(alpha) (2)

how do you tell Mathematica to replace the function and its derivatives,
without having to write replacement rules for the derivatives of f[x,t]
that occur in the equation (1)?
Note that we do NOT want to give the form of f[x,t] first and then build up
the equation. That, of course, would be trivial and also besides the point.

Thanks in advance for your help and suggestions,

Willy

------------

Willy A. Hereman
Department of Mathematical and Computer Sciences
Colorado School of Mines
Golden, CO 80401-1887

Phone: (303) 273-3881 (O) or 440-6089 (H)
Fax: (303) 273-3875 (mention for Dr. Hereman)
Email: WHER...@FLINT.MINES.COLORADO.EDU or
WHER...@LIE.MINES.COLORADO.EDU

-------------


Jorma Virtamo

unread,
Jun 14, 1995, 3:00:00 AM6/14/95
to

wher...@slate.Mines.Colorado.EDU (HEREMAN WILLY) wrote:
>
> If in Mathematica you have built up an equation, such as
>
> eq=D[f[x,t],t]+6*f[x,t]*D[f[x,t],x]+D[f[x,t],{x,3}] (1)
>
> and you THEN want to replace f[x,t] and ITS DERIVATIVES by an
> explicit expression, such as,
>
> f[x,t] = u[0][x,t]*g[x,t]^(alpha) (2)
>
> how do you tell Mathematica to replace the function and its derivatives,
> without having to write replacement rules for the derivatives of f[x,t]
> that occur in the equation (1)?
> Note that we do NOT want to give the form of f[x,t] first and then build up
> the equation. That, of course, would be trivial and also besides the point.
>

The problem you have is revealed by evaluating

In[]:= eq//FullForm

Out[]= Plus[Derivative[0, 1][f][x, t],
Times[6, Power[g[x, t], alpha], u[0][x, t],
Derivative[1, 0][f][x, t]], Derivative[3, 0][f][x, t]]

The derivatives operate on the function f itself, not on its value f[x,t].
To get the desired behavior write

f = Function[{x,t},u[0][x,t]*g[x,t]^(alpha)]

or equivalently

f = u[0][#1,#2]*g[#1,#2]^(alpha)&

Then eq evaluates to what you probably want.


-- Jorma Virtamo


============================================================
Jorma Virtamo
VTT Information Technology / Telecommunications
P.O. Box 1202, FIN-02044 VTT, Finland
phone: +358 0 456 5612 fax: +358 0 455 0115
email: jorma....@vtt.fi web: http://www.vtt.fi/tte/
============================================================


Robert Villegas

unread,
Jun 14, 1995, 3:00:00 AM6/14/95
to
wher...@slate.Mines.Colorado.EDU (HEREMAN WILLY) asks:


> If in Mathematica you have built up an equation, such as
>
> eq=D[f[x,t],t]+6*f[x,t]*D[f[x,t],x]+D[f[x,t],{x,3}] (1)
>
> and you THEN want to replace f[x,t] and ITS DERIVATIVES by an
> explicit expression, such as,
>
> f[x,t] = u[0][x,t]*g[x,t]^(alpha) (2)
>
> how do you tell Mathematica to replace the function and its derivatives,
> without having to write replacement rules for the derivatives of f[x,t]
> that occur in the equation (1)?


Dave Wagner shows that if you replace 'f' with a formula wrapped in an
& pure function, then the derivatives will automatically evaluate:


> You need to define f as a pure function:
>
> In[1]:=


> eq=D[f[x,t],t]+6*f[x,t]*D[f[x,t],x]+D[f[x,t],{x,3}]

> Out[1]=
> (0,1) (1,0) (3,0)
> f [x, t] + 6 f[x, t] f [x, t] + f [x, t]
> In[2]:=


> f = u[0][#1,#2]*g[#1,#2]^(alpha)&

> Out[2]=
> alpha
> u[0][#1, #2] g[#1, #2] &
> In[3]:=
> eq
> Out[3]=
> -1 + alpha (0,1)
> alpha g[x, t] u[0][x, t] g [x, t] +
>
>
> [...much output deleted...]
>
>
> alpha (3,0)
> g[x, t] (u[0]) [x, t]

The reason this works is that all those superscripted expressions like

(0,1)
f [x, t]

are partial derivative operators acting on f, and just waiting for
f to become something they can act upon. If you look at the FullForm of
a D result, you'll see a Derivative operator wrapped around f:

In[9]:= D[f[x, t], x] //FullForm

Out[9]//FullForm= Derivative[1, 0][f][x, t]


Derivative[1, 0] is acting on a meaningless symbol f here, but if
you give it a meaningful function, say using #1, #2, & notation
as Dave suggests, it will differentiate it. Let's take a simpler example
where we want f[x_, t_] := Sin[x] Cos[t] to be the formula.

In[10]:= Derivative[1, 0][ Sin[#1] Cos[#2] & ]

Out[10]= 1 Cos[#1] Cos[#2] + Sin[#1] -(0 Sin[#2]) &

(the 1 and 0 multipliers are there only because the structural
transformations that Derivative did are left unevaluated until the
function is used)


Substituting a #,& pure function for f isn't the only way to
get the derivative expression to evaluate, though. 'Derivative'
will take effect on a symbol that is defined as a function in the
usual "f[x_, t_] := formula" manner, too, so if you were going to do
that anyway, you can make use of it. All you have to do is re-evaluate
expr after defining f:


In[1]:= expr = D[f[x, t], x] + D[f[x, t], t]

(0,1) (1,0)
Out[1]= f [x, t] + f [x, t]

In[4]:= f[x_, t_] := Sin[x] Cos[t]


(* Derivative looks up f's definition and differentiates the formula: *)

In[5]:= expr

Out[5]= Cos[t] Cos[x] - Sin[t] Sin[x]


Even if you don't want to have an assignment to f in effect, you can take
advantage of Derivative's lookup of function assignments. Just make
an assignment to f within a Block and evaluate 'expr' while this is in
effect, and it will all work:

In[6]:= Clear[f]

In[7]:= formula = Block[{f}, f[x_, t_] := Sin[x] Cos[t] ; expr ]

Out[7]= Cos[t] Cos[x] - Sin[t] Sin[x]

(* f was Block'd, so it hasn't acquired the assignment permanently: *)

In[8]:= ? f
Global`f

Still another method, really the same as Dave's, but exhibiting the
ability of nameless, or pure, functions to use named variables:

In[15]:= expr /. f -> Function[{x, t}, Sin[x] Cos[t]]

Out[15]= Cos[t] Cos[x] - Sin[t] Sin[x]


If you had a formula in terms of x and y lying about already, this might
be easier to use; you don't have to substitute #1 for x and #2 for t
manually or programmatically.


Robby Villegas


Daniel Lichtblau

unread,
Jun 14, 1995, 3:00:00 AM6/14/95
to
In article <3rj3pa$e...@news0.cybernetics.net>
wher...@slate.Mines.Colorado.EDU (HEREMAN WILLY) writes:
>
>
>
> Dear Colleagues:

>
> If in Mathematica you have built up an equation, such as
>
> eq=D[f[x,t],t]+6*f[x,t]*D[f[x,t],x]+D[f[x,t],{x,3}] (1)
>
> and you THEN want to replace f[x,t] and ITS DERIVATIVES by an
> explicit expression, such as,
>
> f[x,t] = u[0][x,t]*g[x,t]^(alpha) (2)
>
> how do you tell Mathematica to replace the function and its derivatives,
> without having to write replacement rules for the derivatives of f[x,t]
> that occur in the equation (1)?
> Note that we do NOT want to give the form of f[x,t] first and then build
up
> the equation. That, of course, would be trivial and also besides the
point.
>
> Thanks in advance for your help and suggestions,
>
> Willy
>
> ------------
>
> Willy A. Hereman
> Department of Mathematical and Computer Sciences
> Colorado School of Mines
> Golden, CO 80401-1887
>
> Phone: (303) 273-3881 (O) or 440-6089 (H)
> Fax: (303) 273-3875 (mention for Dr. Hereman)
> Email: WHER...@FLINT.MINES.COLORADO.EDU or
> WHER...@LIE.MINES.COLORADO.EDU
>
> -------------
>

One method is as below.

In[2]:= eq = Hold[D[f[x,t],t]+6*f[x,t]*D[f[x,t],x]+D[f[x,t],{x,3}]]

Out[2]= Hold[D[f[x, t], t] + 6 f[x, t] D[f[x, t], x] + D[f[x, t], {x, 3}]]

In[3]:= neweq = Release[eq /. f[x,t] -> u[0][x,t]*g[x,t]^(alpha)] //
InputForm

Out[3]//InputForm=
alpha*g[x, t]^(-1 + alpha)*u[0][x, t]*Derivative[0, 1][g][x, t] +
g[x, t]^alpha*Derivative[0, 1][u[0]][x, t] +
(-2 + alpha)*(-1 + alpha)*alpha*g[x, t]^(-3 + alpha)*u[0][x, t]*
Derivative[1, 0][g][x, t]^3 +
3*(-1 + alpha)*alpha*g[x, t]^(-2 + alpha)*Derivative[1, 0][g][x, t]^2*
Derivative[1, 0][u[0]][x, t] +
6*g[x, t]^alpha*u[0][x, t]*(alpha*g[x, t]^(-1 + alpha)*u[0][x, t]*
Derivative[1, 0][g][x, t] + g[x, t]^alpha*Derivative[1, 0][u[0]][x,
t]
) + 3*(-1 + alpha)*alpha*g[x, t]^(-2 + alpha)*u[0][x, t]*
Derivative[1, 0][g][x, t]*Derivative[2, 0][g][x, t] +
3*alpha*g[x, t]^(-1 + alpha)*Derivative[1, 0][u[0]][x, t]*
Derivative[2, 0][g][x, t] +
3*alpha*g[x, t]^(-1 + alpha)*Derivative[1, 0][g][x, t]*
Derivative[2, 0][u[0]][x, t] +
alpha*g[x, t]^(-1 + alpha)*u[0][x, t]*Derivative[3, 0][g][x, t] +
g[x, t]^alpha*Derivative[3, 0][u[0]][x, t]


Daniel Lichtblau, WRI


0 new messages