Loading [MathJax]/jax/output/HTML-CSS/jax.js

First time here? Check out the FAQ!

Ask Your Question
2

Partial derivatives and polar coordinates

asked 3 years ago

ortollj gravatar image

updated 3 years ago

slelievre gravatar image

Hi

Easy by hand but difficult (for me !) with SageMath how to get this equation dth==X*dty/r^2-Y*dtx/r^2 from the code below with SageMath ?

var('theta,X,Y,r')
var('dth',latex_name=r"\partial {\theta}")
var('dtx',latex_name=r"\partial {x}")
var('dty',latex_name=r"\partial {y}")

eqL=[]
eq0=r^2==X^2+Y^2
theta=atan(Y/X)
dthX=derivative(theta,X)*dtx
dthY=derivative(theta,Y)*dty
eq1=dth==dthX+dthY
show("dthY : \t ", dthY," \t dthX : \t ",dthX," \t dth : \t ",eq1)
Preview: (hide)

2 Answers

Sort by » oldest newest most voted
2

answered 3 years ago

Emmanuel Charpentier gravatar image

updated 3 years ago

Try :

sage: var("x, y, r")
(x, y, r)
sage: theta(x,y)=atan2(y,x)
sage: dx=var("dx", latex_name=r"\mathrm{d}\,x")
sage: dy=var("dy", latex_name=r"\mathrm{d}\,y")
sage: foo = vector(map(lambda u,v:u*v, derivative(theta)(x,y).subs(x^2+y^2==r^2), vector([dx, dy])))

foo should be :

(dxyr2,dyxr2)

and foo.norm() should be

|dyxr2|2+|dxyr2|2

unless you assume rhat all the variable are real, in which case you should get :

sage: with assuming(x,y,dx,dy,r,"real"): foo.norm()
sqrt(dy^2*x^2/r^4 + dx^2*y^2/r^4)

whose Sage's latexing is ... questionable :

dy2x2r4+dx2y2r4

HTH,

Preview: (hide)
link

Comments

Thank you @Emmanuel Charpentier

whose Sage's latexing is ... questionable :

I would replace foo.norm() by sum(foo.list())

ortollj gravatar imageortollj ( 3 years ago )

I would replace foo.norm() by sum(foo.list())

Huh ?

Not the same thing :

sage: sum(foo.list())
dy*x/r^2 - dx*y/r^2

dyxr2dxyr2

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 3 years ago )

what do you mean ?

yes this is what I wanted SageMath to get, then du=dt*r, I did not want the vector norm.

ortollj gravatar imageortollj ( 3 years ago )
1

Oh, I see. This can be obtained quickier :

sage: theta.gradient().dot_product(vector([dx, dy]))(x,y).subs(x^2+y^2==r^2)
dy*x/r^2 - dx*y/r^2

dyxr2dxyr2

Or, even better :

sage: theta.gradient().dot_product(vector([dx, dy]))(x,y).subs(x^2+y^2==r^2).factor()
(dy*x - dx*y)/r^2

dyxdxyr2

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 3 years ago )

Yes simple an elegant !!. Thank you @Emmanuel Charpentier

var("x, y, r")
theta(x,y)=atan2(y,x)
dx=var("dx", latex_name=r"\mathrm{d}\,x")
dy=var("dy", latex_name=r"\mathrm{d}\,y")
#foo = vector(map(lambda u,v:u*v, derivative(theta)(x,y).subs(x^2+y^2==r^2), vector([dx, dy])))
dtxy=theta.gradient().dot_product(vector([dx, dy]))(x,y)
dtr=dtxy.subs({x^2+y^2:r^2})
show(dtr)
ortollj gravatar imageortollj ( 3 years ago )
1

answered 3 years ago

cav_rt gravatar image

You can also use the Euclidean space.

E.<x,y> = EuclideanSpace()
cartesian = E.default_chart()
polar.<r,ph> = E.polar_coordinates()

Coframes

[(E2,(dx,dy)),(E2,(dr,dϕ)),(E2,(er,eϕ))].

Thus

dph = E.coframes()[1][2]
dph.display(polar)

dϕ=dϕ,

and

dph.display(cartesian)

dϕ=(yx2+y2)dx+(xx2+y2)dy.

Or

dph.apply_map(lambda h:h.subs({x^2+y^2:r^2}))
dph.display(cartesian)

dϕ=yr2dx+xr2dy.

Preview: (hide)
link

Comments

Thank you @cav_rt Your answer confirms the fact that I use few of the SageMath features, I have a lot of work to do!

Euclidean Spaces

ortollj gravatar imageortollj ( 3 years ago )

But dph.display(cartesian) does not belong to Symbolic Ring ? it belongs to 𝚜𝚊𝚐𝚎.𝚝𝚎𝚗𝚜𝚘𝚛.𝚖𝚘𝚍𝚞𝚕𝚎𝚜.𝚏𝚘𝚛𝚖𝚊𝚝⎯𝚞𝚝𝚒𝚕𝚒𝚝𝚒𝚎𝚜.𝙵𝚘𝚛𝚖𝚊𝚝𝚝𝚎𝚍𝙴𝚡𝚙𝚊𝚗𝚜𝚒𝚘𝚗 How to convert it to an equation object to get .rhs() or .lhs() ? (Now I prefer Emmanuel solution !) should I use from sage.symbolic.expression_conversions import ExpressionTreeWalker ? maybe I do not have to come back to Symbolic Ring and all calculation should be made with this kind of object ? maybe I should open a new question?

ortollj gravatar imageortollj ( 3 years ago )

Here I'm almost certain there must be something important that i don't understand, but what's the use of generating objects that can't be used in the Symbolic Ring ? . Can someone explain it to me?

ortollj gravatar imageortollj ( 3 years ago )

You can get the Cartesian components of dph as elements of the Symbolic Ring as follows:

sage: dph[1].expr()                                                                            
-y/(x^2 + y^2)
sage: dph[2].expr()                                                                            
x/(x^2 + y^2)
sage: dph[1].expr() in SR                                                                      
True

Indeed dph[1] returns a chart function and dph[1].expr() returns the underlying symbolic expression as an element of SR. Note that you can get it alternatively as a SymPy object via

sage: dph[1].expr('sympy')                                                                     
-y/(x**2 + y**2)
eric_g gravatar imageeric_g ( 3 years ago )
1

Instead of dph = E.coframes()[1][2], you can get dph directly from the polar chart, via dph = polar.coframe()[2].

eric_g gravatar imageeric_g ( 3 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 3 years ago

Seen: 685 times

Last updated: Sep 24 '21