Home > Backend Development > Python Tutorial > How to represent complex numbers in python

How to represent complex numbers in python

Release: 2019-07-08 09:11:44
Original
34157 people have browsed it

How to represent complex numbers in python

We call numbers in the form z=a bj (a, b are real numbers) called complex numbers, where a is called the real part, b is called the imaginary part, and j is the Imaginary unit.

A complex number is a pair of ordered floating-point numbers (x,y), where x is the real part and y is the imaginary part.

The concept of complex numbers in the Python language:

1. Imaginary numbers cannot exist alone. They always form a complex number together with a real number part with a value of 0.0

2. A complex number consists of a real part and an imaginary part

3. The syntax for expressing complex numbers: real imagej

4. The real part and the imaginary part are both floating point numbers

5. The imaginary part There must be a suffix j or J

aa=123-12j
print aa.real  # output 实数部分 123.0  
print aa.imag  # output虚数部分 -12.0
Copy after login

The output result is:

123.0
-12.0
Copy after login

Built-in properties of complex numbers:

The complex number object has data properties, which are the real and imaginary parts of the complex number. department.

Complex numbers also have the conjugate method, which can be called to return the conjugate complex object of the complex number.

Complex number attributes: real (real part of complex number), imag (imaginary part of complex number), conjugate() (returns the conjugate complex number of complex number)

class Complex(object):
    '''创建一个静态属性用来记录类版本号'''
    version=1.0
    '''创建个复数类,用于操作和初始化复数'''
    def __init__(self,rel=15,img=15j):
        self.realPart=rel
        self.imagPart=img
       
    #创建复数
    def creatComplex(self):
        return self.realPart+self.imagPart
    #获取输入数字部分的虚部
    def getImg(self):
        #把虚部转换成字符串
        img=str(self.imagPart)
        #对字符串进行切片操作获取数字部分
        img=img[:-1] 
        return float(img)  
                       
def test():
    print "run test..........."
    com=Complex()
    Cplex= com.creatComplex()
    if Cplex.imag==com.getImg():
        print com.getImg()
    else:
        pass
    if Cplex.real==com.realPart:
        print com.realPart
    else:
        pass
    #原复数
    print "the religion complex is :",Cplex
    #求取共轭复数
    print "the conjugate complex is :",Cplex.conjugate()
    
if __name__=="__main__":
    test()
Copy after login

More Python related technical articles, Please visit the Python Tutorial column to learn!

The above is the detailed content of How to represent complex numbers in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template