Kompleksni brojevi u Pythonu 1.deo
Laboratorijske vežbe iz predmeta osnovi elektrotehnike 2
Pored rada se realnim brojevima, Python može da radi i sa kompleksnim brojevim zahvaljujući
velikom broju pridruženih funkcija koristeći datoteku „cmath“. Kompleksni brojevi se koriste u
elektrotehnici (fizici) kao i u matematici, a Python pruža brojne korisne alate za rad sa njima.
Uključivanje modula
Sledeći kod nam omogućava da odredimo sve dostupne funkcije iz određenog modula.
import math #poziv modula math
everything=dir(math)
print (everything)
Izvršavanjem programa dobija se:
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh',
'atan', 'atan2', 'atanh', 'cbrt', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf',
'erfc', 'exp', 'exp2', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd',
'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p',
'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh',
'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']
Ovde se pokazuju sve dostupne funkcije iz math modula.
Izvršavanjem sledećeg koda dobićemo listu svih funkcija u modulu cmath
import cmath #poziv modula cmath
everything=dir(cmath)
print (everything)
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh',
'atan', 'atanh', 'cos', 'cosh', 'e', 'exp', 'inf', 'infj', 'isclose', 'isfinite', 'isinf', 'isnan', 'log', 'log10',
'nan', 'nanj', 'phase', 'pi', 'polar', 'rect', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau']
Zapis kompleksnog broja
Kompleksni broj je predstavljen sa „x + iy“. Python pretvara realne brojeve x i y u kompleksne
koristeći funkciju complex(x,y). Realnom delu se može pristupiti pomoću funkcije real(), a
imaginarnom delu se pristupa pomoću funkcije imag().
# Python code to demonstrate the working of
# complex(), real() and imag()
# importing "cmath" for complex number operations
import cmath
# Initializing real numbers
x = 5
y = 3
# converting x and y into complex number
z = complex(x,y);
# printing real and imaginary part of complex number
print ("The real part of complex number is : ",end="")
print (z.real)
print ("The imaginary part of complex number is : ",end="")
print (z.imag)
Izvršavanjem programa dobija se:
The real part of complex number is : 5.0
The imaginary part of complex number is : 3.0
Moduo i argument (faza) kompleksnog broja
Geometrijski, argument (faza) kompleksnog broja je ugao između pozitivne realne ose i vektora
(fazora) koji predstavlja kompleksni broj. Pojam faza (koristi se u elektrotehnici i fizici) u
matematici je poznat kao argument kompleksnog broja. Faza se dobija pomoću funkcije
phase(), koja uzima kompleksni broj kao argument. Opseg faza je od -pi do +pi. odnosno od -
3,14 do +3,14 (u matematici je ovaj pojam poznat kao glavni argument).
# Python code to demonstrate the working of
# phase()
# importing "cmath" for complex number operations
import cmath
# Initializing real numbers
x = -1.0
y = 0.0
# converting x and y into complex number
z = complex(x,y);
# printing phase of a complex number using phase()
print ("The phase of complex number is : ",end="")
print (cmath.phase(z))
Izvršavanjem programa dobija se:
The phase of complex number is : 3.141592653589793
Pitanja i zadaci
1. Dopuniti kod tako da se uz argument (fazu) dobija i moduo kompleksnog broja.
2. Dopuniti kod tako da se argument prikazuje u stepenima
Pomoć
1. import math, print (math.sqrt(x*x+y*y))
2. print (math.degrees(cmath.phase(z)))
Rešenje:
# Python code to demonstrate the working of
# phase()
import math
# importing "cmath" for complex number operations
import cmath
# Initializing real numbers
x = -3.0
y = 4.0
# converting x and y into complex number
z = complex(x,y);
# printing phase of a complex number using phase()
print ("The phase of complex number is : ",end="")
print (cmath.phase(z))
# printing modulus of a complex number using Pythagoras theorem
print ("The modulus of complex number is : ",end="")
print (math.sqrt(x*x+y*y))
# printing phase of complex number in degrees
print ("The phase of complex number in degrees is : ",end="")
print (math.degrees(cmath.phase(z)))
# printing modulus of a complex number using abs()
print (abs(z))
Izvršavanjem se dobija:
The phase of complex number is : 2.214297435588181
The modulus of complex number is : 5.0
The phase of complex number in degrees is : 126.86989764584402
5.0
Rezultat se može proveriti na linku: https://www.intmath.com/complex-numbers/convert-
polar-rectangular-interactive.php
Šta je rezultat izvršavanja sledećeg koda?
#Ask user to enter a complex number of form a+bj
x=complex(input("Enter complex number in form a+bj: "))
import cmath
y=cmath.sqrt((x.real)**2+(x.imag)**2)
print("The modulus of ",x," is", y.real)
Rezultat:
Enter complex number in form a+bj: 3+4j
The modulus of (3+4j) is 5.0
Enter complex number in form a+bj: -6+8j
The modulus of (-6+8j) is 10.0
Enter complex number in form a+bj: -8-6j
The modulus of (-8-6j) is 10.0
Nagradno pitanje:
Da li ovaj način izračunavanja modula ima nedostataka?
Ako ima ilustruj primerom!