Cylindrical polar unit vectors

23 views
Skip to first unread message

Oscar Benjamin

unread,
Aug 13, 2021, 8:20:35 AM8/13/21
to sympy
Hi all,

Does anyone know how to use cylindrical polars with SymPy's vector module?

I just saw this SO question here:
https://stackoverflow.com/questions/68767945/how-do-i-work-with-vectors-in-cylindrical-and-spherical-systems-with-sympy

We can define a coordinate system like this:

In [25]: from sympy.vector import CoordSys3D
...:
...: N = CoordSys3D('N', transformation='cylindrical')

In [26]: N.base_scalars()
Out[26]: (r_N, theta_N, z_N)

In [27]: N.base_vectors()
Out[27]: (i_N, j_N, k_N)

Although the scalars are r, theta and z the unit vectors are i, j and
k. How would I get vectors like e_r and e_theta to work with those?

Oscar

Davide Sandona'

unread,
Aug 13, 2021, 9:06:51 AM8/13/21
to sy...@googlegroups.com
Although the scalars are r, theta and z the unit vectors are i, j and
k. How would I get vectors like e_r and e_theta to work with those?

As of now, that's not possible. It is hard-coded and it only represents cartesian unit vectors [1]

As per the SO question, I don't think SymPy is currently able to do that.

So far, I found the vector module difficult and not trivial to use. There was another question about this module a few months ago, about the difficulty to interact with it. I think we should open a discussion on Github, collecting examples that don't work and examples that could be improved (in terms of user experience) in order to improve our comprehension of what needs to be fixed, eventually preparing the way for a future GSoC.

Davide.


--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAHVvXxQeGu9q_i5YsCKCrevjSyOMQU49cdzHA2kG2PArXX0%3Dfg%40mail.gmail.com.

Davide Sandona'

unread,
Aug 13, 2021, 9:08:18 AM8/13/21
to sy...@googlegroups.com
By the way, what are the differences between sympy.vector and sympy.physics.vector ?  Why two different modules?

Davide.

Oscar Benjamin

unread,
Aug 13, 2021, 10:05:59 AM8/13/21
to sympy
On Fri, 13 Aug 2021 at 14:06, Davide Sandona' <sandona...@gmail.com> wrote:
>>
>> Although the scalars are r, theta and z the unit vectors are i, j and
>> k. How would I get vectors like e_r and e_theta to work with those?
>
> As of now, that's not possible. It is hard-coded and it only represents cartesian unit vectors [1]
>
> As per the SO question, I don't think SymPy is currently able to do that.

I guess it can be done like this:

In [1]: from sympy.vector import CoordSys3D

   ...: 

   ...: N = CoordSys3D('N', transformation='cylindrical')


In [2]: e_r = cos(N.theta)*N.i + sin(N.theta)*N.j


In [3]: e_theta = -sin(N.theta)*N.i + cos(N.theta)*N.j


In [4]: e_z = N.k


In [9]: v1 = 4*e_r + 3*e_z


In [10]: v1.magnitude()

Out[10]: 

   _________________________________________

        2                  2              

╲╱  16⋅sin (theta_N) + 16⋅cos (theta_N) + 9 


In [11]: v1.magnitude().simplify()

Out[11]: 5


Although that works it's not really satisfactory that the polar unit vectors can not be manipulated directly. The usual reason for using the polar unit vectors for a calculation like this is so that you don't need trig functions everywhere. Probably implementing e_r properly is tricky because it would need to be a function of theta and so would need differentiation support etc.

> So far, I found the vector module difficult and not trivial to use. There was another question about this module a few months ago, about the difficulty to interact with it. I think we should open a discussion on Github, collecting examples that don't work and examples that could be improved (in terms of user experience) in order to improve our comprehension of what needs to be fixed, eventually preparing the way for a future GSoC.

I think that the main problem is that both the documentation and API are focussed on more complicated usage involving e.g. multiple reference frames. I'm pretty sure most potential users of the module would be happy to work in a single reference frame and would really just like something like:

from sympy.vector import x, y, z, i, j, k, dot

a = 2*i + j

b = k + j

print(dot(a, b))


There was an attempt to implement that somewhere but it got bogged down with printing issues:

> By the way, what are the differences between sympy.vector and sympy.physics.vector ?  Why two different modules?

The sympy.physics.vector module came first and is used in sympy.physics e.g. for mechanics. The problem with it is that it uses mutable vectors so the classes are not Basic subclasses. The sympy.vector module is a reimplementation using Basic subclasses.

I think ideally the mechanics module would be converted to use the newer sympy.vector module and the older module would not be used any more. I don't know whether there is any significant work that needs doing to make that possible. The internal implementation of the classes in sympy.vector looks messy to me and can probably be better designed.

--
Oscar

Reply all
Reply to author
Forward
0 new messages