Python. Complex numbers. The cmath module. Creation of a complex number

Complex numbers. The cmath module. Creation of a complex number. The complex class. Functions and constants of the cmath module


Contents


Search other resources:

1. The cmath module. The purpose. General information

The Python programming language uses the cmath module to work with complex numbers. The module contains a set of functions for processing complex numbers. To use the cmath module, you first need to include it with the import directive

import cmath

The module is always accessible from the program.

The cmath module functions operate on arguments

  • integers;
  • floating point numbers;
  • complex numbers. A complex number is created by the complex() function.

 

2. Creation of a complex number. The complex class. Ways to create a complex number

The complex class is used to create a complex number. You can use one of two ways to create an instance of the complex class.

Method 1. Setting directly the real and imaginary parts of a complex number

complexNumber = complex([real[, imag]])

here

  • complexNumber – the complex number to create. The number is represented as real+imag*1j;
  • real – real part of a complex number;
  • imag – the imaginary part of a complex number.

In this case, the complex() function can receive a different number of arguments:

  • zero arguments. This means that the number 0j will be generated;
  • one argument. In this case, a complex number without the imaginary (j) part will be used;
  • two arguments. The first argument is the real part, the second argument is the imaginary part.

Each of the arguments can be of a numeric type, or even a complex number.

Method 2. Specifying a complex number as a string

complexNumber = complex(complex_number_as_string)

here

  • complexNumber – the complex number to be created;
  • complex_number_as_string – a string representing a complex number (for example, “2 + 3j”, “1-2j”, etc.). When representing a complex number, the string must not contain any space characters ”, otherwise a ValueError exception will be thrown. With this method, the complex() function receives only one argument – a string. It is forbidden to pass two arguments.

 

3. How to get the real and imaginary parts of a complex number? Example

After creating a complex number using the complex() function (see the previous paragraph), you can access its components using the following call:

  • z.real – real part of a complex number z;
  • z.imag – imaginary part of a complex number z.

Example.

# Include the cmath module
import cmath

# Create complex number z = 7-8j
z = complex(7, -8)

# Get the components of the number z
re = z.real # real part
im = z.imag # imaginary part

# Display the received components on the screen
print('re = ', re)
print('im = ', im)

Program result

re = 7.0
im = -8.0

 

4. An example showing how to create a complex number in different ways

The example creates complex numbers in different ways using the complex class.

# The complex() method - creates a complex number object
# Method 1.
# 1.1. Calling the complex() method without parameters
num1 = complex() # num1 =   0j
print("num1 = ", num1)

# 1.2. Calling the complex() method with 1 float parameter
num2 = complex(5.22) # num2 = (5.22+0j)
print("num2 = ", num2)

# 1.3. Calling the complex() method with 2 int parameters
num3 = complex(7, -4) # num3 = (7-4j)
print('num3 = ', num3)

# 2. Calling the complex() method with passing a string
num4 = complex('0+0j') # num4 = 0j
print('num4 = ', num4)

num5 = complex("2+8j") # num5 =   (2+8j)
print('num5 = ', num5)

# error - string contains spaces
# num6 = complex('1 - 4j')

# 3. Create a complex number from other complex numbers
num6 = complex(num3, num5) # num6 = (-1-2j)
print('num6 = ', num6)

Program result

num1 = 0j
num2 = (5.22+0j)
num3 = (7-4j)
num4 = 0j
num5 = (2+8j)
num6 = (-1-2j)

 

5. The composition of the cmath module. Groups of functions for working with complex numbers

In the cmath module, functions for working with complex numbers are grouped as follows:

  • functions of conversion to polar coordinates and vice versa;
  • power and logarithmic functions;
  • trigonometric functions;
  • hyperbolic functions;
  • functions of classifications.

 

5.1. Functions to convert to polar coordinates and vice versa

This category of functions includes the following:

  • cmath.phase(x) – returns the phase from the x argument as a float;
  • cmath.polar() – returns the representation of x in polar coordinates;
  • cmath.rect() – returns a complex number from polar coordinates.

 

5.2. Power and logarithmic functions

The cmath module implements the following power and logarithmic functions that operate on complex numbers:

  • cmath.exp(x) – returns the exponent of e raised to the power of x, where x can be a complex number. The exponent e is the basis of the natural logarithm;
  • cmath.log(x) – returns the natural logarithm of the x argument with the given base;
  • cmath.log10(x) – returns the base 10 logarithm of the x argument;
  • cmath.sqrt(x) – returns the square root of the x argument.

 

5.3. Trigonometric functions

The cmath module contains 6 trigonometric functions that handle complex numbers:

  • cmath.acos(x) – returns the arccosine of the x argument;
  • cmath.asin(x) – returns the arcsine of the x argument;
  • cmath.atan(x) – defines the arctangent of the argument x;
  • cmath.cos(x) – returns the cosine of the x argument;
  • cmath.sin(x) – returns the sine of the x argument;
  • cmath.tan(x) – returns the tangent of the x argument.

 

5.4. Hyperbolic functions

The cmath module contains the implementation of the following hyperbolic functions that operate on complex numbers:

  • cmath.acosh(x) – returns the inverse hyperbolic cosine of the x argument;
  • cmath.asinh(x) – returns the hyperbolic inverse sine of the argument x;
  • cmath.atanh(x) – returns the hyperbolic arctangent of the argument x;
  • cmath.cosh(x) – returns the hyperbolic cosine of the argument x;
  • cmath.sinh(x) – returns the hyperbolic sine of the argument x;
  • cmath.tanh(x) – returns the hyperbolic tangent of the x argument.

 

5.5. Classification functions

The classifications functions of the cmath module include the following:

  • cmath.isinfinite(x) – determines whether the real and imaginary parts of the complex number x are finite;
  • cmath.isinf(x) – determines whether the real or imaginary parts of the complex number x are infinite;
  • cmath.isnan(x) – determines whether the real or imaginary parts of a complex number are of Nan value;
  • cmath.isclose(x) – determines the proximity of two values to each other.

 

5.6. The constants of cmath module. Example

The cmath module contains a number of commonly used constants:

  • cmath.pi – mathematical constant π, represented as type float;
  • cmath.e – mathematical exponent e, represented as type float;
  • cmath.tau – mathematical constant τ (tau), represented as type float;
  • cmath.inf – infinity. This value is equivalent to float(‘inf’);
  • cmath.infj – a complex number in which the real part is equal to 0, the imaginary part is equal to infinity. This number is equivalent to complex(0.0, float(‘inf’));
  • cmath.nanj – a complex number in which the real part is equal to 0, the imaginary part is equal to the NaN value. This number can be obtained by calling complex(0.0, float(‘nan’)).

Example. The example displays the values of the cmath module constants.

# Include the cmath module
import cmath

# Print the values of the constants of the cmath module
print(cmath.pi)   # 3.141592653589793
print(cmath.e)     # 2.718281828459045
print(cmath.tau)   # 6.283185307179586
print(cmath.inf)  # inf
print(cmath.infj) # infj
print(cmath.nan)   # nan
print(cmath.nanj) # nanj

Program results

3.141592653589793
2.718281828459045
6.283185307179586
inf
infj
nan
nanj

 


Related topics