Python Digital Series-mathematical operations of complex numbers

Source: Internet
Author: User
Tags sin cmath

Mathematical operation problems of complex numbers

The latest network authentication scheme code you've written has encountered a problem, and your only solution is to use complex spaces. Or you just need to use a complex number to perform some calculation operations.

Solution Solutions

The complex number can be complex(real, imag) specified using a function or a floating-point number with the suffix J. Like what:


The corresponding real, imaginary, and conjugate complex numbers can be easily obtained. Just like this:

A.  Real2.0a.  IMAG4.0a.  Conjugate() (2-4j)>>>            

In addition, all common mathematical operations can work:

B (5-1j)B (26+2j)b( -0.4117647058823529+0.6470588235294118j)abs(a)  4.47213595499958>>>          

If you want to perform other complex functions such as sine, cosine, or square root, use the cmath module:

Cmathcmath.  Sin(a)(24.83130584894638-11.356612711218174j)cmath.  Cos(a)( -11.36423470640106-24.814651485634187j)cmath.  Exp(a)( -4.829809383269385-5.5920560936409816j)>>>    

Most of the math-related modules in Python can handle complex numbers. For example, if you use it numpy , you can easily construct a complex array and perform various operations on the array:

>>>ImportNumPyAsNp>>>A=Np.Array([2+3j,4+5j6-< Span class= "Mi" >7j8+9j) Span class= "GP" >>>> aarray ([2.+3.J, 4.+5.J, 6.-7.J, 8.+9.J]) >>> a + 2array ([4.+3. J, 6.+5.J, 8.-7.J, 10.+9.J]) >>> np. Sin (a) array ([9.15449915-4.16890696j,- 56.16227422-48.50245524j, -153.20827755-526.47684926j, 4008.42651446-589.49948373j]) >>>               

The standard mathematical function of Python does not produce complex values, so it is not possible to have complex return values in your code. Like what:

MathMath.  SQRT(-1)"<stdin>"1<module>math domain error> >>             

If you want to generate a complex number to return the result, you must display the use of the cmath module, or declare the use of the plural type in a library that supports complex numbers. Like what:

Cmathcmath.  SQRT(-1)1j>>>        

Python Digital Series-mathematical operations of complex numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.