Parametric Derivative

44 views
Skip to first unread message

Alan Bromborsky

unread,
Mar 30, 2008, 12:20:28 PM3/30/08
to sympy
If F(x,y,z,w) and x=x(t) and y=y(t) here is what I did to calculate
df/dt. Do you have a built in method to do that?

from GAsympy import *
from sympy import *

set_main(sys.modules[__name__])

def PDerive(f,vlst,t):
dfdt = 0
for v in vlst:
dvdt = sympy.Symbol('d'+v.__str__()+'d'+t.__str__())
dfdt += sympy.diff(f,v)*dvdt
return(dfdt)

make_symbols('a b c x y z w t')

f = a*x+exp(-y)*b*z*c*w**2*x

print 'f =',f

dfdt = PDerive(f,[x,y],t)

print 'dfdt =',dfdt

## Program output
## f = a*x + b*c*x*z*w**2*exp(-y)
## dfdt = dxdt*(a + b*c*z*w**2*exp(-y)) - b*c*dydt*x*z*w**2*exp(-y)


Ondrej Certik

unread,
Apr 1, 2008, 9:17:04 AM4/1/08
to sy...@googlegroups.com
Hi Alan!

On Sun, Mar 30, 2008 at 6:20 PM, Alan Bromborsky <abr...@verizon.net> wrote:
>
> If F(x,y,z,w) and x=x(t) and y=y(t) here is what I did to calculate
> df/dt. Do you have a built in method to do that?

You mean dF/dt, right?

>
> from GAsympy import *
> from sympy import *
>
> set_main(sys.modules[__name__])
>
> def PDerive(f,vlst,t):
> dfdt = 0
> for v in vlst:
> dvdt = sympy.Symbol('d'+v.__str__()+'d'+t.__str__())
> dfdt += sympy.diff(f,v)*dvdt
> return(dfdt)
>
> make_symbols('a b c x y z w t')
>
> f = a*x+exp(-y)*b*z*c*w**2*x
>
> print 'f =',f
>
> dfdt = PDerive(f,[x,y],t)
>
> print 'dfdt =',dfdt
>
> ## Program output
> ## f = a*x + b*c*x*z*w**2*exp(-y)
> ## dfdt = dxdt*(a + b*c*z*w**2*exp(-y)) - b*c*dydt*x*z*w**2*exp(-y)

I think we don't have it yet. How do other CAS systems like
Maple/Mathematica/Sage doing this?

Ondrej

Alan Bromborsky

unread,
Apr 1, 2008, 10:18:46 AM4/1/08
to sy...@googlegroups.com
> My example shows that the function is easy to program, my problem is what is a good naming convention for the derivatives of the variables as python program variables so they could be accessed and substituted for at a later time in the program once dx/dt and dy/dt are are explicitly determined. dydt and dxdt work fine for first derivatives, but what would be a consistent naming convention for higher derivatives that does not lead to excessively long names. The way the naming convention should work is that the parametric diff function should recognize an implict derivative with respect to the parameter from the name of the lower order implicite derivative and construct the name of higher order derivative from that name. That is use the name of dx/dt to construct the name of d^2x/dt^2.

>

Ondrej Certik

unread,
Apr 1, 2008, 10:30:18 AM4/1/08
to sy...@googlegroups.com


Right. We should investigate the other CASes and follow the same interface.

Ondrej

Alan Bromborsky

unread,
Apr 1, 2008, 12:37:11 PM4/1/08
to sy...@googlegroups.com
I have googled the online docs for sage, mathematica, and maple and have
found no naming conventions for derivatives (I do not have any actual
books on mathematica and maple). Unless you can find something in the
literature I guess that it is up to the royal We to come up with a
convention for naming derivatives (mainly for when higher order
derivatives are automatically generated!

Friedrich Hagedorn

unread,
Apr 1, 2008, 7:14:14 PM4/1/08
to sy...@googlegroups.com

Here is my proposal:

In [1]: var('a b c x y z w t')
Out[1]: (a, b, c, x, y, z, w, t)

In [2]: f = a*x + b*c*x*z*w**2*exp(-y)

In [3]: fx = Function('fx')

In [4]: fy = Function('fy')

In [5]: g=f.subs(x,fx(t)).subs(y,fy(t))

In [6]: g
Out[6]:
2 -fy(t)
a*fx(t) + b*c*z*w *ℯ *fx(t)

In [7]: g.diff(t)
Out[7]:
d 2 -fy(t) d 2 -fy(t) d
a*──(fx(t)) + b*c*z*w *ℯ *──(fx(t)) - b*c*z*w *ℯ *fx(t)*──(fy(t))
dt dt dt

By the way, have you an advice to simply factor out the df/dt?

As I adviced in my pdf-file comment its good to distinguish the
different mathematical objects. So you have solved the naming problem.
Here is my opinion about

y = f(x, y)

So y is a function of the _symbols_ x and y. These symbols represent
maybe a real number. But when you say

x(t)

you correctly mean

(*) x = fx(t)

So x is now a function of the symbol t. When you substitute x now in f
you get an new function namly

g(t, y) = f( fx(t), y )

My question is whether anybody has a better proposal for the name fx in
(*)?


By, Friedrich

Mateusz Paprocki

unread,
Apr 1, 2008, 7:45:40 PM4/1/08
to sy...@googlegroups.com
Hi,

> My question is whether anybody has a better proposal for the name fx in (*)?

The same you can achieve with:

In [25]: f = a*x + b*c*x*z*w**2*exp(-y)

In [26]: h = f.subs({x: x(t), y: y(t)})

In [27]: h.diff(t)
Out[27]:
d 2 -y(t) d 2 -y(t) d
a*──(x(t)) + b*c*z*w *ℯ *──(x(t)) - b*c*z*w *ℯ *x(t)*──(y(t))
dt dt dt

Mateusz

2008/4/2, Friedrich Hagedorn <fried...@gmx.de>:

Friedrich Hagedorn

unread,
Apr 1, 2008, 8:05:48 PM4/1/08
to sy...@googlegroups.com
On Wed, Apr 02, 2008 at 01:45:40AM +0200, Mateusz Paprocki wrote:
> Hi,
>
> > My question is whether anybody has a better proposal for the name fx in (*)?
>
> The same you can achieve with:
>
> In [25]: f = a*x + b*c*x*z*w**2*exp(-y)
>
> In [26]: h = f.subs({x: x(t), y: y(t)})

Oh, this is really shorter and looks nice. But this notation is contrary
against my advice to distinguish the different objects :-) Once x is a symbol
and then x is a function.

By, Friedrich

Alan Bromborsky

unread,
Apr 1, 2008, 8:40:28 PM4/1/08
to sy...@googlegroups.com

How would you do this is x and y are passed as a list and you did not
know their names, ie x and y
but rather say they would be vlst[0] and vlst[1] ?

Friedrich Hagedorn

unread,
Apr 1, 2008, 8:58:12 PM4/1/08
to sy...@googlegroups.com
On Tue, Apr 01, 2008 at 08:40:28PM -0400, Alan Bromborsky wrote:
> How would you do this is x and y are passed as a list and you did not
> know their names, ie x and y
> but rather say they would be vlst[0] and vlst[1] ?

Do you mean something like that?

In [1]: var('a b c x y z w t')
Out[1]: (a, b, c, x, y, z, w, t)

In [2]: vlst=[x,y]

In [3]: subdict={}

In [4]: for sym in vlst:
...: subdict[sym] = sym(t)
...:
...:

In [7]: f = a*x + b*c*x*z*w**2*exp(-y)

In [8]: g=f.subs_dict(subdict)

In [17]: g.diff(t)
Out[17]:

d 2 -y(t) d 2 -y(t) d
a*──(x(t)) + b*c*z*w *ℯ *──(x(t)) - b*c*z*w *ℯ *x(t)*──(y(t))
dt dt dt


Friedrich

Alan Bromborsky

unread,
Apr 2, 2008, 9:42:23 AM4/2/08
to sy...@googlegroups.com
Works great, but how would you substitute a value or expression later in
the calculation for the derivatives of x and y.

Mateusz Paprocki

unread,
Apr 2, 2008, 12:47:19 PM4/2/08
to sy...@googlegroups.com
Hi,

> Works great, but how would you substitute a value or expression
> later in the calculation for the derivatives of x and y.

Probably you have meant this:

In [1]: var('a b c x y z w t')
Out[1]: (a, b, c, x, y, z, w, t)

In [2]: vlist = [x, y]

In [3]: subsdict = dict( (a, a(t)) for a in vlist )

In [4]: subsdict
Out[4]: {y: y(t), x: x(t)}

In [5]: f = a*x + b*c*x*z*w**2*exp(-y)

In [6]: g = f.subs(subsdict)

In [7]: h = g.diff(t)

In [8]: h
Out[8]:


d 2 -y(t) d 2 -y(t) d
a*──(x(t)) + b*c*z*w *ℯ *──(x(t)) - b*c*z*w *ℯ *x(t)*──(y(t))
dt dt dt

In [9]: flist = [sin(2*t), exp(3*t)]

In [10]: subsdict = dict( (a(t), f) for a, f in zip(vlist, flist) )

In [11]: subsdict
Out[11]:
⎧ 3*t ⎫
⎨y(t): ℯ , x(t): sin(2*t)⎬
⎩ ⎭

In [12]: h.subs(subsdict)
Out[12]:
3*t 3*t
2 3*t - ℯ 2 -ℯ
2*a*cos(2*t) - 3*b*c*z*w *ℯ *sin(2*t) + 2*b*c*z*w *cos(2*t)*ℯ

However subs() shouldn't auto-evaluate those derivatives, as we have:

In [14]: Derivative(x(t),t)
Out[14]:
d
──(x(t))
dt

In [15]: Derivative(sin(x),t)
Out[15]:
d
──(sin(t))
dt

but

In [16]: _14.subs(x(t), sin(t))
Out[16]: cos(t)

line [16] should read:

In [16]: _14.subs(x(t), sin(t)).doit()
Out[16]: cos(t)

In [17]: _15.doit()
Out[17]: cos(t)

to make the interface coherent.

Mateusz

2008/4/2, Alan Bromborsky <abr...@verizon.net>:

Reply all
Reply to author
Forward
0 new messages