Комплексни бројеви у Питхон-у | Сет 1 (Увод)

Не само реални бројеви, Питхон такође може да обрађује комплексне бројеве и његове повезане функције користећи датотеку 'цматх'. Комплексни бројеви имају своју употребу у многим апликацијама које се односе на математику, а Питхон пружа корисне алате за руковање и манипулисање њима. Претварање реалних бројева у комплексни број Комплексни број је представљен са ' к + ии '. Питхон претвара реалне бројеве к и и у комплекс користећи функцију комплекс(ки) . Правом делу се може приступити помоћу функције реал() а имагинарни део може бити представљен помоћу слика()

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  

Алтернативни начин за иницијализацију комплексног броја  

Испод је имплементација како можемо направити комплекс бр. без употребе сложена() функција .

Python
   # An alternative way to initialize complex numbers'   # importing 'cmath' for complex number operations   import   cmath   # Initializing complex number   z   =   5  +  3  j   # 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  

Објашњење: Фаза комплексног броја Геометријски, фаза комплексног броја је угао између позитивне реалне осе и вектора који представља комплексан број . Ово је такође познато као аргумент комплексног броја. Фаза се враћа помоћу фаза() који као аргумент узима комплексан број. Опсег фазе лежи од -пи значи +пи. тј од -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  

Претварање из поларног у правоугаони облик и обрнуто Конверзија у поларну се врши помоћу полар() који враћа а пар (рпх) означавајући модул р и фаза угао пх . модул се може приказати помоћу абс() и фазно коришћење фаза() . Комплексни број се претвара у правоугаоне координате коришћењем рец (р пх) где р је модул и пх је фазни угао . Враћа вредност нумерички једнаку р * (матх.цос(пх) + матх.син(пх)*1ј)  

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 (Важне функције и константе)