【发布时间】:2019-09-16 04:59:46
【问题描述】:
我正在尝试实现一个在球坐标中旋转的函数。 最后我需要用 Javascript 实现,但首先我试图让它在 python 中工作,因为我更熟悉这种语言。
我想要一个函数rotate,它以参数为参数:
- 旋转角度(alpha)
- 要旋转的点的球坐标 theta 和 phi (theta, phi)
- 参考轴的球坐标 theta 和 phi,通过球体原点进行旋转 (THETA, PHI)
使用http://stla.github.io/stlapblog/posts/RotationSphericalCoordinates.html 的方法,这是我目前得到的:
执行:
rotate(
alpha = 90, # roation angle
theta = 90, # point theta
phi = 0, # point phi
THETA = 0, # axis theta
PHI = 0, # axis phi
degrees = True # angles in degrees
)
这应该提供一个新的坐标 theta = 90 和 phi = 90。我得到的是 theta = 180 和 phi = 90。
我真的不确定的部分是旋转函数中 theta_ 和 psi_ 的计算。在文章中它说 psi_ 应该是一个 2x1 矩阵,但我得到的是一个 2x2 矩阵。
这是我的实现尝试:
import numpy as np
from math import cos, sin, atan, pi
from cmath import exp, phase
#####################################################
def rotate(alpha, theta, phi, THETA, PHI, degrees=True):
## DEGREES TO RAD
if degrees:
alpha = pi/180 * alpha
theta = pi/180 * theta
phi = pi/180 * phi
THETA = pi/180 * THETA
PHI = pi/180 * PHI
psi_ = Psi_(alpha, theta, phi, THETA, PHI)
theta_ = 2 * atan(abs(psi_[1][1])/abs(psi_[0][0]))
phi_ = phase(psi_[1][1]) - phase(psi_[0][0])
## RAD TO DEGREES
if degrees:
return theta_ * 180/pi, phi_ * 180/pi
return theta_, phi_
#####################################################
def Psi_(alpha, theta, phi, THETA, PHI):
return Rn(THETA, PHI, alpha) * \
Psi(alpha, theta, phi)
#####################################################
def Psi(alpha, theta, phi):
return np.array([
[cos(theta)/2],
[exp(1j*phi) * sin(theta/2)]
])
#####################################################
def Rn(THETA, PHI, alpha):
return Rz(PHI) * \
Ry(THETA) * \
Rz(alpha) * \
Ry(THETA).conj().T * \
Rz(PHI).conj().T
#####################################################
def Rx(alpha):
return np.array([
[cos(alpha/2), -1j * sin(alpha/2)],
[-1j * sin(alpha/2), cos(alpha/2)]
])
#####################################################
def Ry(alpha):
return np.array([
[cos(alpha/2), -sin(alpha/2)],
[sin(alpha/2), cos(alpha/2)]
])
#####################################################
def Rz(alpha):
return np.array([
[exp(-1j * alpha/2), 0],
[0, exp(1j * alpha/2)]
])
#####################################################
if __name__ == "__main__":
print(rotate(
alpha = 90, # roation angle
theta = 90, # point theta
phi = 0, # point phi
THETA = 0, # axis theta
PHI = 0, # axis phi
degrees = True # angles in degrees
))
感谢您的帮助!
【问题讨论】:
-
避免问多个问题。最好问两个单独的问题 - 一个标记为
python关于您当前的实施,另一个标记为javascript。 -
你说得对,谢谢!
-
你能给我们举几个更常见的例子吗?很难从一个实例中得到一种模式,尤其是位于已知问题边界上的模式。例如,旋转 45 到 135、60 到 30 等。
-
另外,你声明你得到一个 2x2 结果,但你只引用了一个 2x1: [180 90]。是哪个?
-
您发布的代码缺少一个主程序:它所做的只是定义一些函数并退出。由于您还提出了使用问题,因此您的测试用例和调用顺序至关重要。
标签: python math rotation coordinates spherical-coordinate