simple (?) symbolic array differentiation

14 views
Skip to first unread message

astozzia

unread,
May 20, 2008, 3:18:23 PM5/20/08
to SympyCore
Hey y'all
I am solving a couple of ODEs using the lagragian (mechanics). To make
the story short, and after typing:

In[1]: from sympy import *
import scipy as s

assume I have the following parameters:

In[2]: m,theta,phi = symbols('m','theta','phi')
In[3]: f = m*s.array([1,2,0]) + theta**2*s.array([1,0,0]) -
3*phi*theta*s.array([1,0,1])

Now I want to differentiate the expression "f" w.r.t "theta".
In[4]: diff(f,theta)

and i get the following error (see below). Note that I also tried by
defining f = Function("f") and f=... same...
Any help/hint would be highly appeciated.
Thanks
Astozzia
-----

---------------------------------------------------------------------------
SympifyError Traceback (most recent call
last)

/home/acmae/Documents/Studies/research/project/p2.wing-body/<ipython
console> in <module>()

/usr/lib/python2.5/site-packages/sympy/core/function.py in diff(f, x,
times, evaluate)
666 """
667
--> 668 return Derivative(f,x,times, **{'evaluate':evaluate})
669
670 def expand(e, **hints):

/usr/lib/python2.5/site-packages/sympy/core/function.py in
__new__(cls, expr, *symbols, **assumptions)
451
452 def __new__(cls, expr, *symbols, **assumptions):
--> 453 expr = sympify(expr)
454 if not symbols: return expr
455 symbols = Derivative._symbolgen(*symbols)

/usr/lib/python2.5/site-packages/sympy/core/sympify.py in sympify(a,
sympify_lists, locals)
125 return
ast_parser.SymPyParser(local_dict=locals).parse_expr(a)
126 except Exception, exc:
--> 127 raise SympifyError(a, exc)
128 raise SympifyError("%r is NOT a valid SymPy expression" %
a)
129

SympifyError: Sympify of expression '[Mw + theta 3*Mw theta + 2*Mw]'
failed, because of exception being raised:
SyntaxError: invalid syntax (line 1)

Ondrej Certik

unread,
May 20, 2008, 4:41:27 PM5/20/08
to sy...@googlegroups.com, symp...@googlegroups.com
On Tue, May 20, 2008 at 9:18 PM, astozzia <acm...@gmail.com> wrote:
>
> Hey y'all
> I am solving a couple of ODEs using the lagragian (mechanics). To make
> the story short, and after typing:
>
> In[1]: from sympy import *
> import scipy as s
>
> assume I have the following parameters:
>
> In[2]: m,theta,phi = symbols('m','theta','phi')
> In[3]: f = m*s.array([1,2,0]) + theta**2*s.array([1,0,0]) -
> 3*phi*theta*s.array([1,0,1])
>
> Now I want to differentiate the expression "f" w.r.t "theta".
> In[4]: diff(f,theta)
>
> and i get the following error (see below). Note that I also tried by
> defining f = Function("f") and f=... same...
> Any help/hint would be highly appeciated.

It works for me:

In [1]: import scipy as s

In [2]: m,theta,phi = symbols('m','theta','phi')

In [3]: f = m*s.array([1,2,0]) + theta**2*s.array([1,0,0]) -
3*phi*theta*s.array([1,0,1])

In [4]: f
Out[4]: [m - 3*phi*theta + theta**2 2*m -3*phi*theta]

In [5]: diff(f, theta)
Out[5]: [-3*φ + 2*θ, 0, -3*φ]

Use the latest sympy. BTW, you are using a wrong mailinglist -- this
mailinglist is for the sympycore project, we use this list for sympy:

http://groups.google.com/group/sympy

If you do some examples from Lagrangian mechanics, please share them
with us, so that we can add them to the sympy/examples directory.

Thanks,
Ondrej

astozzia

unread,
May 21, 2008, 2:33:18 PM5/21/08
to SympyCore
thanks indeed and sorry for spamming you. I will post on the group you
mentioned.
I did download and install the latest sympy. i guess i need to redo
the install again...
astozzia

Reply all
Reply to author
Forward
0 new messages