Ask Your Question
2

Partial derivatives and polar coordinates

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

ortollj gravatar image

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

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)
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

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

Emmanuel Charpentier gravatar image

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

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,

edit flag offensive delete link more

Comments

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 +0100 )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 +0100 )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 +0100 )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}} $$

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2021-09-24 12:36:40 +0100 )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])))
dtxy=theta.gradient().dot_product(vector([dx, dy]))(x,y)
dtr=dtxy.subs({x^2+y^2:r^2})
show(dtr)
ortollj gravatar imageortollj ( 2021-09-24 12:47:03 +0100 )edit
1

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

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

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

edit flag offensive delete link more

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 ( 2021-09-24 07:32:01 +0100 )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 +0100 )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 +0100 )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)
eric_g gravatar imageeric_g ( 2021-09-24 15:33:35 +0100 )edit
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 ( 2021-09-24 16:05:20 +0100 )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

Stats

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

Seen: 646 times

Last updated: Sep 24 '21