Ask Your Question

Partial derivatives and polar coordinates

asked 2021-09-23 09:18:40 +0200

ortollj gravatar image

updated 2021-09-24 06:31:00 +0200

slelievre gravatar image


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('dth',latex_name=r"\partial {\theta}")
var('dtx',latex_name=r"\partial {x}")
var('dty',latex_name=r"\partial {y}")

show("dthY : \t ", dthY," \t dthX : \t ",dthX," \t dth : \t ",eq1)
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2021-09-23 10:22:54 +0200

Emmanuel Charpentier gravatar image

updated 2021-09-23 10:29:05 +0200

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 :

$$ \left(-\frac{{\mathrm{d}\,x} y}{r^{2}},\,\frac{{\mathrm{d}\,y} x}{r^{2}}\right) $$

and foo.norm() should be

$$ \sqrt{{\left| \frac{{\mathrm{d}\,y} x}{r^{2}} \right|}^{2} + {\left| \frac{{\mathrm{d}\,x} y}{r^{2}} \right|}^{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 :

$$ \sqrt{\frac{{\mathrm{d}\,y}^{2} x^{2}}{r^{4}} + \frac{{\mathrm{d}\,x}^{2} y^{2}}{r^{4}}} $$


edit flag offensive delete link more


Thank you @Emmanuel Charpentier

whose Sage's latexing is ... questionable :

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

ortollj gravatar imageortollj ( 2021-09-23 10:32:50 +0200 )edit

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

$$ \frac{{\mathrm{d}\,y} x}{r^{2}} - \frac{{\mathrm{d}\,x} y}{r^{2}} $$

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2021-09-23 18:10:43 +0200 )edit

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 ( 2021-09-23 19:53:11 +0200 )edit

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

$$ \frac{{\mathrm{d}\,y} x}{r^{2}} - \frac{{\mathrm{d}\,x} y}{r^{2}} $$

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

$$ \frac{{\mathrm{d}\,y} x - {\mathrm{d}\,x} y}{r^{2}} $$

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2021-09-24 12:36:40 +0200 )edit

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

var("x, y, r")
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)
ortollj gravatar imageortollj ( 2021-09-24 12:47:03 +0200 )edit

answered 2021-09-24 00:58:28 +0200

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()


$$\left[\left(\mathbb{E}^{2}, \left(\mathrm{d} x,\mathrm{d} y\right)\right), \left(\mathbb{E}^{2}, \left(\mathrm{d} r,\mathrm{d} {\phi}\right)\right), \left(\mathbb{E}^{2}, \left(e^{ r },e^{ {\phi} }\right)\right)\right].$$


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

$$\mathrm{d} {\phi} = \mathrm{d} {\phi},$$



$$\mathrm{d} {\phi} = \left( -\frac{y}{x^{2} + y^{2}} \right) \mathrm{d} x + \left( \frac{x}{x^{2} + y^{2}} \right) \mathrm{d} y.$$


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

$$\mathrm{d} {\phi} = -\frac{y}{r^{2}} \mathrm{d} x + \frac{x}{r^{2}} \mathrm{d} y.$$

edit flag offensive delete link more


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 ( 2021-09-24 07:32:01 +0200 )edit

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 ( 2021-09-24 09:19:04 +0200 )edit

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 ( 2021-09-24 13:10:43 +0200 )edit

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                                                                      

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 ( 2021-09-24 15:33:35 +0200 )edit

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 ( 2021-09-24 16:05:20 +0200 )edit

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


Asked: 2021-09-23 09:18:40 +0200

Seen: 489 times

Last updated: Sep 24 '21