本文介绍了n维球面坐标系直角坐标系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有直角的改变坐标系和正球有一个任何有效的方法一>?转化如下:
以下是我的code,但我想摆脱循环的:
导入numpy的是NP
进口scipy.sparse 高清coord_transform_n(R,阿尔法):
阿尔法:[0,\\ PI)和间最后一个之间的n-2个值[0,2 \\ PI)的
X = []
因为我在范围内(alpha.shape [0]):
x.append(R * np.prod(np.sin(阿尔法[0:I]))* np.cos(阿尔法[I]))
返回np.asarray(X)
打印coord_transform_n(1,np.asarray(np.asarray([1,2])))
解决方案
您原来的code能与记忆中间罪
产品,即可以加快
高清ct_dynamic(R,阿尔法):
阿尔法:[0,\\ PI)和间最后一个之间的n-2个值[0,2 \\ PI)的
X = np.zeros(LEN(阿尔法)+ 1)
S = 1
对于E,A在枚举(阿尔法):
X [E] = S * np.cos(一)
S * = np.sin(一)
X [LEN(阿尔法)= S
返回X * R
但在速度还是输给numpy的基础的方法
高清CT(R,编曲):
A = np.concatenate((np.array([2 * np.pi]),ARR))
SI = np.sin(一)
SI [0] = 1
SI = np.cumprod(SI)
共= np.cos(一)
合= np.roll(CO,-1)
返回SI *合* R>>> N = 10
>>> C = np.random.random_sample(N)* np.pi
>>>所有(克拉(1,C)== ct_dynamic(1,C))
真正>>> timeit.timeit('从__main__进口coord_transform_n为F,C,F(2.4,C),数= 10000)
2.213547945022583>>> timeit.timeit('从__main__进口ct_dynamic为F,C,F(2.4,C),数= 10000)
0.9227950572967529>>> timeit.timeit('从__main__进口CT为F,C,F(2.4,C),数= 10000)
0.5197498798370361
Is there any efficient way of changing between Cartesian coordinate system and n-spherical one? 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 speeded up with memorizing intermediate sin
product, i.e.
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
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
这篇关于n维球面坐标系直角坐标系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!