# Partial derivatives and polar coordinates

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)

edit retag close merge delete

Sort by ยป oldest newest most voted

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}}}$$

HTH,

more

Thank you @Emmanuel Charpentier

whose Sage's latexing is ... questionable :

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

( 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}}$$

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

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


$$\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}}$$

( 2021-09-24 12:36:40 +0200 )edit

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])))
dtr=dtxy.subs({x^2+y^2:r^2})
show(dtr)

( 2021-09-24 12:47:03 +0200 )edit

You can also use the Euclidean space.

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


Coframes

$$\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].$$

Thus

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


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

and

dph.display(cartesian)


$$\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.$$

Or

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


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

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

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

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

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

( 2021-09-24 15:33:35 +0200 )edit
1

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

( 2021-09-24 16:05:20 +0200 )edit