Python NumPy 中 j 的等价物
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28872862/
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
Equivalent of j in NumPy
提问by Programmer
What is the equivalent of Octave's j
in NumPy? How can I use j
in Python?
j
NumPy中 Octave 的等价物是什么?我如何j
在 Python 中使用?
In Octave:
在八度:
octave:1> j
ans = 0 + 1i
octave:1> j*pi/4
ans = 0.00000 + 0.78540i
But in Python:
但在 Python 中:
>>> import numpy as np
>>> np.imag
<function imag at 0x2368140>
>>> np.imag(3)
array(0)
>>> np.imag(3,2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: imag() takes exactly 1 argument (2 given)
>>> np.imag(32)
array(0)
>>>
>>> 0+np.imag(1)
1
采纳答案by ArekBulski
In Python, 1j
or 0+1j
is a literal of complex type. You can broadcast that into an array using expressions, for example
在 Python 中,1j
or0+1j
是复杂类型的文字。例如,您可以使用表达式将其广播到数组中
In [17]: 1j * np.arange(5)
Out[17]: array([ 0.+0.j, 0.+1.j, 0.+2.j, 0.+3.j, 0.+4.j])
Create an array from literals:
从文字创建一个数组:
In [18]: np.array([1j])
Out[18]: array([ 0.+1.j])
Note that what Michael9 posted creates a complex, not a complex array:
请注意,Michael9 发布的内容创建了一个复杂的,而不是一个复杂的数组:
In [21]: np.complex(0,1)
Out[21]: 1j
In [22]: type(_)
Out[22]: complex
回答by styvane
You can create one if needed or use 1j
which instance of complex class
您可以根据需要创建一个或使用1j
复杂类的哪个实例
>>> 1j #complex object
1j
>>> type(1j)
<class 'complex'>
>>> j = np.complex(0,1) #create complex number
>>> j
1j