Python Numpy - 复数 - 有极坐标到矩形转换的函数吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16444719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:37:30  来源:igfitidea点击:

Python Numpy - Complex Numbers - Is there a function for Polar to Rectangular conversion?

pythonnumpycomplex-numbers

提问by atomh33ls

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?

是否有内置的 Numpy 函数可以将极坐标形式的复数、幅度和角度(度)转换为实部和虚部中的一个?

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])

And what I would like is:

我想要的是:

>>> c
[(-0.5+0.8660254038j),(-0.515038074+0.8571673007j),(-0.5+0.8660254038j),(-0.5+0.8660254038j),(-0.515038074+0.8571673007j)]

采纳答案by tom10

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:

没有一个函数可以完全满足您的要求,但是有angle,它是最难的部分。因此,例如,可以定义两个函数:

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.

这些函数使用弧度作为输入和输出,而对于度数,需要在两个函数中都转换为弧度。

In the numpy referencethere'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).

在 numpy参考中有一个关于处理复数的部分,这就是您要查找的函数将被列出的地方(因为它们不存在,我认为它们不存在于 numpy 中)。

回答by atomh33ls

I used cmathwith itertools:

我将cmathitertools 一起使用

from cmath import rect,pi
from itertools import imap
b = b*pi/180                   # convert from deg to rad
c = [x for x in imap(rect,a,b)]

回答by Kees

import numpy as np
import cmath.rect

nprect = np.vectorize(rect)

c = nprect(a,b*np.pi/180)

回答by dyqik

There's an error in the previous answer that uses numpy.vectorize- cmath.rect is not a module that can be imported. Numpy also provides the deg2rad function that provides a cleaner piece of code for the angle conversion. Another version of that code could be:

上一个答案中使用了一个错误numpy.vectorize- cmath.rect 不是可以导入的模块。Numpy 还提供了 deg2rad 函数,该函数为角度转换提供了一段更清晰的代码。该代码的另一个版本可能是:

import numpy as np
from cmath import rect

nprect = np.vectorize(rect)

c = nprect(a, np.deg2rad(b))

The code uses numpy's vectorize function to return a numpy style version of the standard library's cmath.rectfunction that can be applied element wise across numpy arrays.

该代码使用 numpy 的 vectorize 函数返回标准库cmath.rect函数的 numpy 样式版本,该版本可以跨 numpy 数组按元素应用。

回答by Thomio

tom10 answer works fine... you can also expand the Euler's formula to:

tom10 答案工作正常...您还可以将欧拉公式扩展为:

def P2R(A, phi):
    return A * ( np.cos(phi) + np.sin(phi)*1j )