2016-10-10 3 views
0

Ich bin neu in Python also bitte mit mir ertragen. Das Problem ist:komplexe Zahl zu polarer Form in Python

"Schreiben Sie eine Funktion polar (z), um eine komplexe Zahl in ihre polare Form (r, Theta) zu konvertieren. Sie können die Mathatan2 und Math.Hypot Funktionen, aber nicht die cmath-Bibliothek verwenden. "

Ich weiß nicht einmal, wo mit diesem starten, aber bisher habe ich:

import math 
def polar(z): 
    z = a + bj 
    r = math.hypot(a,b) 
    theta = math.atan2(b,a) 
    print "(",r,",",theta,")" 

Jede Hilfe tun!

Antwort

1

Sie können object.real und object.imag verwenden, um die Werte von realen und imaginären Werten zu erhalten. Check this answer

import math 
def polar(z): 
    a= z.real 
    b= z.imag 
    r = math.hypot(a,b) 
    theta = math.atan2(b,a) 
    return r,theta # use return instead of print. 

u=3+5j 
print polar(u) 

Output:

(5.830951894845301, 1.0303768265243125)

Read Differenz b/w-Druck und in Funktionen geben.