相当于j in NumPy

2024-04-27 18:24:16 发布

您现在位置:Python中文网/ 问答频道 /正文

在NumPy中,八度音阶的j等价于什么?如何在Python中使用j

八度音阶:

octave:1> j
ans =  0 + 1i
octave:1> j*pi/4
ans =  0.00000 + 0.78540i

但在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

Tags: importnumpyasnppifunctionarrayat
2条回答

在Python中,1j0+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])

从文字创建数组:

In [18]: np.array([1j])
Out[18]: array([ 0.+1.j])

注意,Michael9发布的内容创建了一个复杂的数组,而不是一个复杂的数组:

In [21]: np.complex(0,1)
Out[21]: 1j
In [22]: type(_)
Out[22]: complex

如果需要,您可以创建一个或使用1j哪个复杂类的实例

 >>> 1j #complex object
 1j
 >>> type(1j)
 <class 'complex'>
 >>> j = np.complex(0,1) #create complex number
 >>> j
 1j

相关问题 更多 >