在解决物理和工程问题时,"选择比努力更重要" 。
选择一个合适的坐标系,往往能把一个复杂的偏微分方程变成简单的代数方程。
- 方方正正的物体 (如芯片、房间):用直角坐标。
- 轴对称的物体 (如光纤、波导、同轴电缆):用柱坐标。
- 中心对称的物体 (如原子、行星、点波源):用球坐标。
本文将总结这三者之间的转换关系,并附带 Python 代码实现。
01. 直角坐标系 (Cartesian Coordinates)
这是我们最熟悉的"出厂设置"。
- 定义 :通过三条互相垂直的轴 ( x , y , z ) (x, y, z) (x,y,z) 来定位空间中的一点。
- 基矢量 : x ^ , y ^ , z ^ \hat{x}, \hat{y}, \hat{z} x^,y^,z^ (或 i ⃗ , j ⃗ , k ⃗ \vec{i}, \vec{j}, \vec{k} i ,j ,k )。
- 微元体积 : d V = d x d y d z dV = dx dy dz dV=dxdydz。
02. 柱坐标系 (Cylindrical Coordinates)
想象你把一张二维的极坐标图,沿着 z 轴拉长成一根管子。
2.1 定义
由三个参数 ( ρ , ϕ , z ) (\rho, \phi, z) (ρ,ϕ,z) 构成:
- ρ \rho ρ (径向距离) :点到 z 轴 的垂直距离 ( ρ ≥ 0 \rho \ge 0 ρ≥0)。
- ϕ \phi ϕ (方位角) :点在 xy 平面上的投影与 x 轴的夹角 ( 0 ≤ ϕ < 2 π 0 \le \phi < 2\pi 0≤ϕ<2π)。
- z z z (高度):与直角坐标的 z 完全一样。
2.2 转换公式
从 柱坐标 ( ρ , ϕ , z ) (\rho, \phi, z) (ρ,ϕ,z) → \to → 直角坐标 ( x , y , z ) (x, y, z) (x,y,z):
{ x = ρ cos ϕ y = ρ sin ϕ z = z \begin{cases} x = \rho \cos\phi \\ y = \rho \sin\phi \\ z = z \end{cases} ⎩ ⎨ ⎧x=ρcosϕy=ρsinϕz=z
从 直角坐标 ( x , y , z ) (x, y, z) (x,y,z) → \to → 柱坐标 ( ρ , ϕ , z ) (\rho, \phi, z) (ρ,ϕ,z):
{ ρ = x 2 + y 2 ϕ = arctan ( y x ) z = z \begin{cases} \rho = \sqrt{x^2 + y^2} \\ \phi = \arctan(\frac{y}{x}) \\ z = z \end{cases} ⎩ ⎨ ⎧ρ=x2+y2 ϕ=arctan(xy)z=z
注意:计算 ϕ \phi ϕ 时通常使用 atan2(y, x) 函数以区分象限。
- 微元体积 : d V = ρ d ρ d ϕ d z dV = \rho d\rho d\phi dz dV=ρdρdϕdz (注意多了一个雅可比因子 ρ \rho ρ)。
Image of Cylindrical coordinate system diagram
03. 球坐标系 (Spherical Coordinates)
这是处理球对称问题(如氢原子波函数)的神器。
注意:数学界和物理/工程界对符号的定义常有不同,本文采用ISO 标准/物理学通用定义。
3.1 定义
由三个参数 ( r , θ , ϕ ) (r, \theta, \phi) (r,θ,ϕ) 构成:
- r r r (径向距离) :点到 原点 的距离 ( r ≥ 0 r \ge 0 r≥0)。
- θ \theta θ (极角/天顶角) :点与 z 轴正方向 的夹角 ( 0 ≤ θ ≤ π 0 \le \theta \le \pi 0≤θ≤π)。
- ϕ \phi ϕ (方位角) :点在 xy 平面上的投影与 x 轴的夹角 ( 0 ≤ ϕ < 2 π 0 \le \phi < 2\pi 0≤ϕ<2π)。这与柱坐标的 ϕ \phi ϕ 是一样的。
3.2 转换公式
从 球坐标 ( r , θ , ϕ ) (r, \theta, \phi) (r,θ,ϕ) → \to → 直角坐标 ( x , y , z ) (x, y, z) (x,y,z):
{ x = r sin θ cos ϕ y = r sin θ sin ϕ z = r cos θ \begin{cases} x = r \sin\theta \cos\phi \\ y = r \sin\theta \sin\phi \\ z = r \cos\theta \end{cases} ⎩ ⎨ ⎧x=rsinθcosϕy=rsinθsinϕz=rcosθ
从 直角坐标 ( x , y , z ) (x, y, z) (x,y,z) → \to → 球坐标 ( r , θ , ϕ ) (r, \theta, \phi) (r,θ,ϕ):
{ r = x 2 + y 2 + z 2 θ = arccos ( z r ) ϕ = arctan ( y x ) \begin{cases} r = \sqrt{x^2 + y^2 + z^2} \\ \theta = \arccos(\frac{z}{r}) \\ \phi = \arctan(\frac{y}{x}) \end{cases} ⎩ ⎨ ⎧r=x2+y2+z2 θ=arccos(rz)ϕ=arctan(xy)
- 微元体积 : d V = r 2 sin θ d r d θ d ϕ dV = r^2 \sin\theta dr d\theta d\phi dV=r2sinθdrdθdϕ (注意雅可比因子是 r 2 sin θ r^2 \sin\theta r2sinθ)。
Image of Spherical coordinate system diagram
04. Python 代码实战
在实际计算中(特别是涉及大量数据点时),我们强烈建议使用 numpy,因为它能自动处理数组运算,并且 arctan2 函数能完美解决分母为 0 和象限判断的问题。
python
import numpy as np
class CoordTransform:
"""
空间坐标系转换工具类
支持单点或 Numpy 数组的批量转换
"""
@staticmethod
def cartesian_to_cylindrical(x, y, z):
"""直角 (x,y,z) -> 柱 (rho, phi, z)"""
rho = np.sqrt(x**2 + y**2)
phi = np.arctan2(y, x) % (2 * np.pi) # 归一化到 [0, 2pi]
return rho, phi, z
@staticmethod
def cylindrical_to_cartesian(rho, phi, z):
"""柱 (rho, phi, z) -> 直角 (x,y,z)"""
x = rho * np.cos(phi)
y = rho * np.sin(phi)
return x, y, z
@staticmethod
def cartesian_to_spherical(x, y, z):
"""直角 (x,y,z) -> 球 (r, theta, phi)"""
r = np.sqrt(x**2 + y**2 + z**2)
# 防止 r=0 导致除零错误
theta = np.zeros_like(r)
mask = r > 0
if isinstance(r, np.ndarray):
theta[mask] = np.arccos(z[mask] / r[mask])
elif r > 0:
theta = np.arccos(z / r)
phi = np.arctan2(y, x) % (2 * np.pi)
return r, theta, phi
@staticmethod
def spherical_to_cartesian(r, theta, phi):
"""球 (r, theta, phi) -> 直角 (x,y,z)"""
x = r * np.sin(theta) * np.cos(phi)
y = r * np.sin(theta) * np.sin(phi)
z = r * np.cos(theta)
return x, y, z
# --- 测试案例 ---
if __name__ == "__main__":
# 定义一个直角坐标点: (1, 1, 1)
x, y, z = 1.0, 1.0, 1.0
print(f"原始直角坐标: ({x}, {y}, {z})")
# 1. 转柱坐标
rho, phi, z_cyl = CoordTransform.cartesian_to_cylindrical(x, y, z)
print(f"柱坐标: rho={rho:.2f}, phi={np.degrees(phi):.2f}°, z={z_cyl}")
# 2. 转球坐标
r, theta, phi_sph = CoordTransform.cartesian_to_spherical(x, y, z)
print(f"球坐标: r={r:.2f}, theta={np.degrees(theta):.2f}°, phi={np.degrees(phi_sph):.2f}°")
# 3. 验证还原
x_new, y_new, z_new = CoordTransform.spherical_to_cartesian(r, theta, phi_sph)
print(f"还原直角坐标: ({x_new:.2f}, {y_new:.2f}, {z_new:.2f})")