[英]n-sphere coordinate system to Cartesian coordinate system
Is there any efficient way of changing between Cartesian coordinate system and n-spherical one ? 在笛卡尔坐标系和n球坐标系之间有没有有效的改变方法? The transformation is as follows:
转型如下:
The following is my code but I want to get rid of the loop: 以下是我的代码,但我想摆脱循环:
import numpy as np
import scipy.sparse
def coord_transform_n(r,alpha):
"""alpha: the n-2 values between [0,\pi) and last one between [0,2\pi)
"""
x=[]
for i in range(alpha.shape[0]):
x.append(r*np.prod(np.sin(alpha[0:i]))*np.cos(alpha[i]))
return np.asarray(x)
print coord_transform_n(1,np.asarray(np.asarray([1,2])))
Your original code can be sped up by memoizing intermediate sin
product, ie 您的原始代码可以通过memoizing中间被加快
sin
产品,即
def ct_dynamic(r, alpha):
"""alpha: the n-2 values between [0,\pi) and last one between [0,2\pi)
"""
x = np.zeros(len(alpha) + 1)
s = 1
for e, a in enumerate(alpha):
x[e] = s*np.cos(a)
s *= np.sin(a)
x[len(alpha)] = s
return x*r
But still loses in speed to numpy based approach 但仍然在速度上失去基于numpy的方法
def ct(r, arr):
a = np.concatenate((np.array([2*np.pi]), arr))
si = np.sin(a)
si[0] = 1
si = np.cumprod(si)
co = np.cos(a)
co = np.roll(co, -1)
return si*co*r
>>> n = 10
>>> c = np.random.random_sample(n)*np.pi
>>> all(ct(1,c) == ct_dynamic(1,c))
True
>>> timeit.timeit('from __main__ import coord_transform_n as f, c; f(2.4,c)', number=10000)
2.213547945022583
>>> timeit.timeit('from __main__ import ct_dynamic as f, c; f(2.4,c)', number=10000)
0.9227950572967529
>>> timeit.timeit('from __main__ import ct as f, c; f(2.4,c)', number=10000)
0.5197498798370361
我的建议:在一个载体中组装窦,在其上使用cumprod,然后将每个与cosinus相乘(预先组装在另一个载体中)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.