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)
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
>
Right. We should investigate the other CASes and follow the same interface.
Ondrej
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
> 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>:
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
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
> 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>: