2015-03-05 17 views
7

Qual è l'equivalente di Octave j in NumPy? Come posso usare j in Python?Equivalente di j in NumPy

In Octave:

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

Ma in 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 

risposta

15

In Python, 1j o 0+1j è un tipo letterale di tipo complesso. È possibile trasmettere che in una matrice utilizzando le espressioni, per esempio

In [17]: 1j * np.arange(5) 
Out[17]: array([ 0.+0.j, 0.+1.j, 0.+2.j, 0.+3.j, 0.+4.j]) 

Creare un array da letterali:

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

Nota che ciò che Michael9 postato crea un complesso, non è una matrice complessa:

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

È possibile creare uno se necessario o utilizzare 1j quale istanza della classe complesso

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