logo

מספרים מורכבים בפייתון | סט 1 (מבוא)

לא רק מספרים אמיתיים Python יכול גם לטפל במספרים מרוכבים ובפונקציות הקשורות אליו באמצעות הקובץ 'cmath'. מספרים מורכבים יש את השימושים שלהם ביישומים רבים הקשורים למתמטיקה ופיתון מספק כלים שימושיים לטפל בהם ולתפעל אותם. המרת מספרים ממשיים למספר מרוכב מספר מרוכב מיוצג על ידי ' x + yi '. פייתון ממיר את המספרים הממשיים x ו-y למורכבים באמצעות הפונקציה מורכב(xy) . ניתן לגשת לחלק האמיתי באמצעות הפונקציה רִיאָל() וחלק דמיוני יכול להיות מיוצג על ידי image()

Python
# 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:' z.real) print('The imaginary part of complex number is:' z.imag) 

תְפוּקָה
The real part of complex number is: 5.0 The imaginary part of complex number is: 3.0 

דרך חלופית לאתחל מספר מרוכב  



להלן היישום של איך אנחנו יכולים לעשות מורכב לא. ללא שימוש פונקציית complex() .

Python
# An alternative way to initialize complex numbers' # importing 'cmath' for complex number operations import cmath # Initializing complex number z = 5+3j # Print the parts of Complex No. print('The real part of complex number is : ' end='') print(z.real) print('The imaginary part of complex number is : ' end='') print(z.imag) 

תְפוּקָה
The real part of complex number is : 5.0 The imaginary part of complex number is : 3.0 

הֶסבֵּר: שלב של מספר מרוכב מבחינה גיאומטרית השלב של מספר מרוכב הוא ה זווית בין הציר הממשי החיובי לווקטור המייצג מספר מרוכב . זה ידוע גם בשם הטיעון של מספר מרוכב. השלב מוחזר באמצעות שָׁלָב() שלוקח מספר מרוכב כארגומנט. טווח השלב נע מ -pi פירושו +pi. כלומר מ -3.14 עד +3.14 .

Python
# 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:' cmath.phase(z)) 

תְפוּקָה
The phase of complex number is: 3.141592653589793 

המרה מצורה קוטבית למלבן ולהיפך המרה לקוטב מתבצעת באמצעות קוֹטבִי() מה שמחזיר א זוג (rph) מציין את מודול r ושלב זווית ph . ניתן להציג מודולוס באמצעות abs() ושימוש בשלב שָׁלָב() . מספר מרוכב הופך לקואורדינטות מלבניות על ידי שימוש rect(r ph) אֵיפֹה r הוא מודולוס ו ph היא זווית פאזה . הוא מחזיר ערך מספרית השווה ל r * (math.cos(ph) + math.sin(ph)*1j)  



Python
# importing 'cmath' for complex number operations import cmath import math # Initializing real numbers x = 1.0 y = 1.0 # converting x and y into complex number z = complex(x y) # converting complex number into polar using polar() w = cmath.polar(z) # printing modulus and argument of polar complex number print('The modulus and argument of polar complex number is:' w) # converting complex number into rectangular using rect() w = cmath.rect(1.4142135623730951 0.7853981633974483) # printing rectangular form of complex number print('The rectangular form of complex number is:' w) 

תְפוּקָה
The modulus and argument of polar complex number is: (1.4142135623730951 0.7853981633974483) The rectangular form of complex number is: (1.0000000000000002+1j) 


מספרים מורכבים בפייתון | סט 2 (פונקציות וקבועים חשובים)