Package maths_fns :: Module coord_transform
[hide private]
[frames] | no frames]

Source Code for Module maths_fns.coord_transform

 1  ############################################################################### 
 2  #                                                                             # 
 3  # Copyright (C) 2010 Edward d'Auvergne                                        # 
 4  #                                                                             # 
 5  # This file is part of the program relax.                                     # 
 6  #                                                                             # 
 7  # relax is free software; you can redistribute it and/or modify               # 
 8  # it under the terms of the GNU General Public License as published by        # 
 9  # the Free Software Foundation; either version 2 of the License, or           # 
10  # (at your option) any later version.                                         # 
11  #                                                                             # 
12  # relax is distributed in the hope that it will be useful;                    # 
13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
15  # GNU General Public License for more details.                                # 
16  #                                                                             # 
17  # You should have received a copy of the GNU General Public License           # 
18  # along with relax; if not, write to the Free Software                        # 
19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
20  #                                                                             # 
21  ############################################################################### 
22   
23  # Module docstring. 
24  """Module for transforming between different coordinate systems.""" 
25   
26  # Python module imports. 
27  from math import acos, atan2, cos, sin 
28  from numpy import array, float64, zeros 
29  from numpy.linalg import norm 
30   
31   
32 -def cartesian_to_spherical(vector):
33 """Convert the Cartesian vector [x, y, z] to spherical coordinates [r, theta, phi]. 34 35 The parameter r is the radial distance, theta is the polar angle, and phi is the azimuth. 36 37 38 @param vector: The Cartesian vector [x, y, z]. 39 @type vector: numpy rank-1, 3D array 40 @return: The spherical coordinate vector [r, theta, phi]. 41 @rtype: numpy rank-1, 3D array 42 """ 43 44 # The radial distance. 45 r = norm(vector) 46 47 # Unit vector. 48 unit = vector / r 49 50 # The polar angle. 51 theta = acos(unit[2]) 52 53 # The azimuth. 54 phi = atan2(unit[1], unit[0]) 55 56 # Return the spherical coordinate vector. 57 return array([r, theta, phi], float64)
58 59
60 -def spherical_to_cartesian(spherical_vect, cart_vect):
61 """Convert the spherical coordinate vector [r, theta, phi] to the Cartesian vector [x, y, z]. 62 63 The parameter r is the radial distance, theta is the polar angle, and phi is the azimuth. 64 65 66 @param spherical_vect: The spherical coordinate vector [r, theta, phi]. 67 @type spherical_vect: 3D array or list 68 @param cart_vect: The Cartesian vector [x, y, z]. 69 @type cart_vect: 3D array or list 70 """ 71 72 # Trig alias. 73 sin_theta = sin(spherical_vect[1]) 74 75 # The vector. 76 cart_vect[0] = spherical_vect[0] * cos(spherical_vect[2]) * sin_theta 77 cart_vect[1] = spherical_vect[0] * sin(spherical_vect[2]) * sin_theta 78 cart_vect[2] = spherical_vect[0] * cos(spherical_vect[1])
79