  Complex Numbers in Python | Set 2 (Essential Functions and Constants)

File handling | Python Methods and Functions

1. exp () : — This function returns the exponent of the complex number mentioned in its argument.

2. log (x, b) : — This function returns the logarithmic base b value of x , both mentioned in their arguments. If no base is specified, the natural logarithm of x is returned.

 # Python code to demonstrate how it works # exp (), log ()   # import & quot; cmath & quot ; for operations with complex numbers import cmath import math   # Initializing real numbers x = 1.0 y = 1.0    # convert x and y to complex number z = complex (x, y);    # print complex number metric print ( "The exponent of complex number is:" , end = "") print (cmath.exp (z))    # printing the complex issue form journal print ( "The log (base 10) of complex number is:" , end = " ") print (cmath.log (z, 10 ))

Exit d:

The exponent of complex number is: (1.4686939399158851 + 2.2873552871788423j) The log (base 10) of complex number is: (0.15051499783199057 + 0.3410940884604603j)

3. log10 () : — This function returns a base 10 logs of a complex number.

4. sqrt () : — This calculates the square root of a complex number.

 # Python code to demonstrate how it works # log10 (), sqrt () # import & quot; cmath & quot; for operations with complex numbers import cmath import math   # Initializing real numbers x = 1.0 y = 1.0    # convert x and y to complex number z = complex (x, y);    # printing log10 complex number print ( "The log10 of complex number is:" , end = "") print (cmath.log10 (z))    # printing the square root of the complex number print ( " The square root of complex number is: " , end = "") print ( cmath.sqrt (z))

Output:

The log10 of complex number is: (0.15051499 783199057 + 0.3410940884604603j) The square root of complex number is: (1.09868411346781 + 0.45508986056222733j)

5. isfinite () : — Returns true if the real and imaginary parts of the complex number are finite , otherwise it returns false.

6. isinf () : — returns true if the real or imaginary part of the complex number is infinite , otherwise returns false.

7. isnan () : — returns true if the real or imaginary part of the complex number is NaN , otherwise returns false.

 # Python code to demonstrate how it works # isnan (), isinf (), isfinite ()    # import & quot; cmath & quot; for operations with complex numbers import cmath import math   # Initializing real numbers x = 1.0 y = 1.0 a = math. inf b = math.nan   # convert x and y to complex number z = complex (x, y);    # convert x and a to a complex number w = complex (x, a);    # convert x and b to complex number v = complex (x, b);    # check if both numbers are finite if cmath.isfinite (z): print ( "Complex number is finite" ) else : print ( " Complex number is infinite " )    # check if any number is infinite if cmath.isinf (w): print ( "Complex number is infinite" < code class = "plain">) else : print ( " Complex number is finite " )    # check if any number is infinite if cmath.isnan (v):   print ( "Complex number is NaN" ) else : print ( " Complex number is not NaN " )

Output:

Complex number is finite Complex number is infinite Complex numbe r is NaN

Constants

The cmath module defines two constants “pi” that return a numeric value pi. The second —  "e", which returns the numeric value of the exponent.

 # Python code to demonstrate how it works # pi and e   # import & quot; cmath & quot; for operations with complex numbers import cmath import math   # printing pi value print ( "The value of pi is:" , end = " ") print (cmath.pi)   # print e value print ( " The value of exponent is: " , end = "") print (cmath.e)

Output:

The value of pi is: 3.141592653589793 The value of exponent is: 2.718281828459045

Manjit Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.