[WIP] Multiple Coordinate System #12020

Open
wants to merge 4 commits into
from

Projects

None yet

3 participants

@parsoyaarihant
Contributor

This PR is related to implementing multiple coordinate system in SymPy. So far I have implemented classes CoordinateSystem (Super class for remaining coordinate systems), CartesianCoordinateSystem and SphericalCoordinateSystem using Lamè parameters. Using Lamè parameters , other orthogonal coordinate systems(such as CylindricalCoordinateSystem, ParabolicCoordinateSystem) can be easily implemented.

Sample Program

>>> from sympy import *
>>> from sympy.vector.coordsys import SphericalCoordinateSystem
>>> Sph = SphericalCoordinateSystem('Sph')
>>> e_r, e_theta, e_phi = Sph.base_vectors()
>>> r, theta, phi = Sph.base_scalars()
>>> s_delop = Sph.delop
>>> w = r*theta*phi * (e_r + e_theta + e_phi)
>>> pprint(((s_delop ^ w).doit()))
⎛2⋅Sph_phi⋅Sph_r⋅Sph_theta - Sph_phi⋅Sph_r⎞ Sph_e_phi + ⎛                     
⎜─────────────────────────────────────────⎟             ⎜sin(Sph_theta)⋅Sph_ph
⎝                  Sph_r                  ⎠             ⎜─────────────────────
                                                        ⎜                     
                                                        ⎝                     

       2                               2                  2          ⎞ Sph_e_r
i⋅Sph_r  + cos(Sph_theta)⋅Sph_phi⋅Sph_r ⋅Sph_theta - Sph_r ⋅Sph_theta⎟        
─────────────────────────────────────────────────────────────────────⎟        
                                 2                                   ⎟        
             sin(Sph_theta)⋅Sph_r                                    ⎠        

 +-2⋅sin(Sph_theta)⋅Sph_phi⋅Sph_r⋅Sph_theta + Sph_r⋅Sph_theta⎞ Sph_e_theta
   ⎜───────────────────────────────────────────────────────────⎟            
   ⎝                    sin(Sph_theta)⋅Sph_r                   ⎠            
                                                                            
                                                                            
>>> w = r*e_r + theta*e_theta + phi*e_phi
>>> pprint((s_delop & w).doit())
sin(Sph_theta)⋅Sph_r + cos(Sph_theta)⋅Sph_r⋅Sph_theta                1        
───────────────────────────────────────────────────── + 3 + ──────────────────
                                    2                       sin(Sph_theta)⋅Sph
                sin(Sph_theta)⋅Sph_r                                          

  
──
_r
  
>>> pprint(s_delop(r*theta*phi).doit())
⎛  Sph_theta   ⎞ Sph_e_phi + (Sph_phi⋅Sph_theta) Sph_e_r + (Sph_phi) Sph_e_the
⎜──────────────⎟                                                              
⎝sin(Sph_theta)⎠                                                              

ta
  

The results in the above examples are correct and results can be verified here.

  • The API remains same for CartesianCoordinateSystem.

Notes:

Todo

  • Update Documentation.
  • Add more Tests.
  • Complete implementation of class SphericalCoordinateSystem.

I wanted to get as much feedback as possible from the commumity to improve my work and get SphericalCoordinateSystem implementation right before implementing remaining coordinate systems.

sympy/vector/coordsys.py
+ Process can be slow for less common coordinate systems, use
+ built-in systems when you can.
+
+ built-in : 'cartesian', 'spherical', 'cylindrical',
@jksuom
jksuom Jan 5, 2017 Member

It seems that this and the following lines are not rendered as expected. Apparently Sphinx does not know what should be done.

@parsoyaarihant
Contributor

@Upabjojr , @jbbskinny , ping.

@parsoyaarihant
Contributor
doc/src/modules/vector/api/classes.rst
@@ -5,10 +5,10 @@ Essential Classes in sympy.vector (doctrings)
CoordSysCartesian
@jksuom
jksuom Jan 10, 2017 Member

Should this title be changed, too?

@parsoyaarihant
parsoyaarihant Jan 10, 2017 Contributor

Yes, because CoordSysCartesian class is replaced by CartesianCoordinateSystem. I will change it in the next commit.

sympy/vector/coordsys.py
@@ -44,6 +54,7 @@ def __new__(cls, name, location=None, rotation_matrix=None,
Used for simple str printing.
"""
+ from sympy.abc import a, x, y, z
@jksuom
jksuom Jan 10, 2017 Member

These should probably be dummies instead of public symbols.

@jksuom
jksuom Jan 10, 2017 Member

Moreover, since these are used to denote angles etc. in some coordinate systems, more 'neutral' names could be better. For example, u, v, w instead of x, y, z.

@parsoyaarihant
parsoyaarihant Jan 10, 2017 edited Contributor

These should probably be dummies instead of public symbols.

I am not aware of the difference it would make. Can you specify any undesired consequences?

@jksuom
jksuom Jan 10, 2017 Member

It these names are used in the code calling this code, confusion may result. Dummies are unique symbols that are different from any other symbol, even those with the same name.

@parsoyaarihant
parsoyaarihant Jan 11, 2017 Contributor

Thanks, I have made the required changes.

sympy/vector/coordsys.py
+ # Handling for different coordinate systems
+ coord_type_map = {
+ 'cartesian': [
+ Lambda((x, y, z), (1, 1, 1)),
@jksuom
jksuom Jan 10, 2017 Member

Should this really be the constant map (1, 1, 1)?

@parsoyaarihant
parsoyaarihant Jan 10, 2017 edited Contributor

I am storing coordinate relations of other coordinates with respect to CartesianCoordinateSystem so coordinate relation of Cartesian System should be an identity map.

sympy/vector/coordsys.py
else:
_check_strings('variable_names', vector_names)
variable_names = list(variable_names)
latex_scalars = [(r"\mathbf{{%s}_{%s}}" % (x, name)) for
x in variable_names]
pretty_scalars = [(name + '_' + x) for x in variable_names]
- obj._x = BaseScalar(variable_names[0], 0, obj,
+ obj._e1 = BaseScalar(variable_names[0], 0, obj,
@jksuom
jksuom Jan 10, 2017 Member

e1, e2, ... etc. are generally used as base vector names. It would look better to use something else for coordinate scalars, e.g., x1, x2, ... (or xi1, xi2, ...).

sympy/vector/coordsys.py
+ lame_lambda = _get_lame_lambda(system)
+ coord_sys_type = name
+ else:
+ raise TypeError("see the docs for proper useage")
@jksuom
jksuom Jan 10, 2017 Member

useage -> usage

@parsoyaarihant
parsoyaarihant Jan 10, 2017 Contributor

Thanks !

@parsoyaarihant parsoyaarihant Made changes based on suggestions
replaced x, y, z with u, v, w

* Changed names of base vectors from e1, e2, e3 to x1, x2, x3.

* Fixed typo error
d1a066b
sympy/vector/coordsys.py
@@ -54,8 +55,10 @@ def __new__(cls, name, location=None, rotation_matrix=None,
Used for simple str printing.
"""
- from sympy.abc import a, x, y, z, u, v, w
-
+ a = Dummy('a')
@jksuom
jksuom Jan 11, 2017 Member

It is also possible to define several dummies on the same line: c, d = symbols('c,d', cls=Dummy).

+ # Handling for different coordinate systems
+ coord_type_map = {
+ 'cartesian': [
+ Lambda((u, v, w), (1, 1, 1)),
@jksuom
jksuom Jan 12, 2017 Member

Should this be the identity mapping: Lambda((u, v, w), (u, v, w))?

@parsoyaarihant
parsoyaarihant Jan 12, 2017 Contributor

I am storing coordinate relations of other coordinates with respect to CartesianCoordinateSystem so coordinate relation of Cartesian System should be an identity map.

@jbbskinny
Contributor
@parsoyaarihant parsoyaarihant Changed public symbols to Dummy symbols
Previously,  x, y, z variables were public defined. Which could lead to
the confusion when the code is called. Hence the variables are changed
to dummy variables.
0d26265
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment