Mathematica to sympy diff() problem

17 views
Skip to first unread message

Scott

unread,
Oct 18, 2008, 11:34:10 AM10/18/08
to sympy
What is the sympy translation of the following Mathematica syntax?
When I try to code this in sage or sympy xdot and zdot remain as a
diff object and I cannot us them in symbolic manifulations. My sympy
version is 5.15 (ubuntu 8.10) or whatever is embedded in sage 3.1.2.

V/R

Scott

x=q[t];
z=1/2 *x^2;
xdot=D[x,t];
zdot=D[z,t];
D[z*zdot,xdot]

out: 1/2 q[t]^3

Ondrej Certik

unread,
Oct 19, 2008, 2:05:07 PM10/19/08
to sy...@googlegroups.com
Hi Scott!

The above four lines would be translated as:

In [1]: from sympy import *

In [2]: var("t")
Out[2]: t

In [3]: x = Function("x")(t)

In [4]: z = x**2/2

In [5]: xdot = diff(x, t)

In [6]: zdot = diff(z, t)

In [7]: xdot
Out[7]: D(x(t), t)

In [8]: zdot
Out[8]: x(t)*D(x(t), t)

> D[z*zdot,xdot]

In [11]: diff((z*zdot).subs(xdot, s), s)
Out[11]: 1/2*x(t)**3

Currently our diff can only differentiate with respect to a symbol, so
you need to use the substitution above. We should enhance that.

>
> out: 1/2 q[t]^3

This agrees.

Let us know if you have further questions.

Ondrej

Ondrej Certik

unread,
Oct 19, 2008, 2:05:51 PM10/19/08
to sy...@googlegroups.com

You need to put here this line to introduce the "s" variable:

In [9]: var("s")
Out[9]: s

>
> In [11]: diff((z*zdot).subs(xdot, s), s)
> Out[11]: 1/2*x(t)**3


Ondrej

Scott

unread,
Oct 20, 2008, 1:05:06 PM10/20/08
to sympy
Ondrej


Thank you for the tip to use the subs function, I was expecting it to
exist but I had trouble finding it.

I will be working with 2-d arrays. Which 2d formats play well with
both the subs and the diff funtions?

It seems that Matrix lacks the diff attribute , list and numpy.array
lack the subs attribute.
Is there a better way to handle this than a lot of
Matrix(diff(Matrix(rdotarray).subs(xdot,s)[:],s)).subs(s,xdot) ?

Where in the Docs am I most likely to find the answers to my question?

I am using sympy 6.2 now.



Thanks again

Scott



diff((z*zdot).subs(xdot, s), s)

Ondrej Certik

unread,
Nov 3, 2008, 7:27:12 PM11/3/08
to sy...@googlegroups.com
Hi Scott!

On Mon, Oct 20, 2008 at 6:05 PM, Scott <scott...@yahoo.com> wrote:
>
> Ondrej
>
>
> Thank you for the tip to use the subs function, I was expecting it to
> exist but I had trouble finding it.
>
> I will be working with 2-d arrays. Which 2d formats play well with
> both the subs and the diff funtions?

I think our Matrix should work with them in some way.

>
> It seems that Matrix lacks the diff attribute , list and numpy.array
> lack the subs attribute.
> Is there a better way to handle this than a lot of
> Matrix(diff(Matrix(rdotarray).subs(xdot,s)[:],s)).subs(s,xdot) ?

Yes, you can use Matrix.applyfunc(). See it's docstring for usage.

>
> Where in the Docs am I most likely to find the answers to my question?

http://docs.sympy.org/modules/matrices.html

>
> I am using sympy 6.2 now.

If you have further questions, please ask. Sorry for my late reply.

Ondrej

Reply all
Reply to author
Forward
0 new messages