Trandformed Field

15 views
Skip to first unread message

Sebastián Pérez

unread,
Sep 1, 2019, 1:32:56 PM9/1/19
to sympy
Hi everybody, I hope you are doing well.

I am trying to use sympy to make and transform unitary vectors from spherical coordinates to cartesian coordinates but I have really not known how to do that.

As you can see in the code below; I would like to be able to transform e_r,e_theta, e_psi into its cartesian form. 

For instance from e_r  to (x/sqrt(x**2+y**2+z**2), y/sqrt(x**2+y**2+z**2), z/sqrt(x**2+y**2+z**2)) (its cartesian form).

Thanks for your time.

================================================================================================================================================================
import sympy as sy

#Variables of the spherical coordinates
r=sy.Symbol('r', real=True, positive=True)
theta=sy.Symbol('theta')
psi=sy.Symbol('psi') 

#Relationship Cartesian and spherical variables
x = r*sy.sin(theta)*sy.cos(psi)
y = r*sy.sin(theta)*sy.sin(psi)
z = r*sy.cos(theta)

#Derivatives
dx_dr=sy.diff(x,r)
dy_dr=sy.diff(y,r)
dz_dr=sy.diff(z,r)

dx_dtheta=sy.diff(x,theta)
dy_dtheta=sy.diff(y,theta)
dz_dtheta=sy.diff(z,theta)

dx_dpsi=sy.diff(x,psi)
dy_dpsi=sy.diff(y,psi)
dz_dpsi=sy.diff(z,psi)

#Norm
n_r=sy.sqrt(dx_dr**2+dy_dr**2+dz_dr**2)
n_theta=sy.sqrt(dx_dtheta**2+dy_dtheta**2+dz_dtheta**2)
n_psi=sy.sqrt(dx_dpsi**2+dy_dpsi**2+dz_dpsi**2)

#Scale factors
h1=sy.simplify(n_r) #h1=1
h2=sy.simplify(n_theta) #h2=r
h3=sy.refine(sy.simplify(n_psi), sy.Q.positive(sy.sin(theta))) #h3=r*sin(theta)

#Unit vectors
def simplify(fe,d1,d2,d3):
    '''
    fe=Scale factors
    d#=Respective Derivatives
    '''
    e1=sy.simplify((1/fe)*d1)
    e2=sy.simplify((1/fe)*d2) 
    e3=sy.simplify((1/fe)*d3)
    
    return (e1,e2,e3)

e_r=simplify(h1,dx_dr,dy_dr,dz_dr) 
#(sin(theta)*cos(psi), sin(psi)*sin(theta), cos(theta))

e_theta=simplify(h2,dx_dtheta,dy_dtheta,dz_dtheta) 
#(cos(psi)*cos(theta), sin(psi)*cos(theta), -sin(theta))

e_psi=simplify(h3,dx_dpsi,dy_dpsi,dz_dpsi) 
#(-sin(psi), cos(psi), 0)

Kalevi Suominen

unread,
Sep 1, 2019, 2:04:11 PM9/1/19
to sympy
There is currently some work in progress aiming to implement such transformations:

Kalevi Suominen
Reply all
Reply to author
Forward
0 new messages