2016-10-10 1 views
0

Je suis nouveau à Python alors s'il vous plaît ours avec moi. Le problème est:nombre complexe à la forme polaire en python

"Ecrivez une fonction polaire (z) pour convertir un nombre complexe en sa forme polaire (r, thêta) Vous pouvez utiliser les fonctions math.atan2 et math.hypot mais pas la bibliothèque cmath. "

Je ne sais même pas où commencer avec celui-ci, mais jusqu'à présent je:

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

Toute aide fera!

Répondre

1

Vous pouvez utiliser object.real et object.imag pour obtenir les valeurs de valeurs réelles et imaginaires. 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) 

Sortie:

(5.830951894845301, 1.0303768265243125)

différence Lire b/w impression et revenir dans les fonctions.