Python Numpy-复数-是否有将极坐标转换为矩形的函数? [英] Python Numpy - Complex Numbers - Is there a function for Polar to Rectangular conversion?
问题描述
是否有内置的Numpy函数将极性形式的复数,大小和角度(度)转换为实部和虚部的一个?
Is there a built-in Numpy function to convert a complex number in polar form, a magnitude and an angle (degrees) to one in real and imaginary components?
很明显,我可以编写自己的代码,但是似乎某些类型的事物的某个模块中包含了优化的版本?
Clearly I could write my own but it seems like the type of thing for which there is an optimised version included in some module?
更具体地说,我有一个幅度数组和一个角度数组:
More specifically, I have an array of magnitudes and an array of angles:
>>> a
array([1, 1, 1, 1, 1])
>>> b
array([120, 121, 120, 120, 121])
我想要的是:
>>> c
[(-0.5+0.8660254038j),(-0.515038074+0.8571673007j),(-0.5+0.8660254038j),(-0.5+0.8660254038j),(-0.515038074+0.8571673007j)]
推荐答案
There isn't a function to do exactly what you want, but there is angle, which does the hardest part. So, for example, one could define two functions:
def P2R(radii, angles):
return radii * exp(1j*angles)
def R2P(x):
return abs(x), angle(x)
这些函数将弧度用于输入和输出,对于度数,在两个函数中都需要将弧度转换为弧度.
These functions are using radians for input and output, and for degrees, one would need to do the conversion to radians in both functions.
在numpy中参考有一个关于处理复数的部分,这里列出了您要查找的函数(因此,因为它们不存在,所以我认为它们不存在于numpy中).
In the numpy reference there's a section on handling complex numbers, and this is where the function you're looking for would be listed (so since they're not there, I don't think they exist within numpy).
这篇关于Python Numpy-复数-是否有将极坐标转换为矩形的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!