Python, complex types to work with complex numbers (absolute values, declination, polar transformations, etc.)

Money and Business

Python has a standard type for handling complex numbers, the COMPLEX type. If you just want to do simple calculations, you do not need to import any modules, but if you import the standard library cmath, you can also use mathematical functions (exponential, logarithmic, trigonometric, etc.) corresponding to complex numbers.

The following contents are explained here with sample code.

  • Generate complex variables
  • Get real and imaginary parts:real,imagattribute
  • Get conjugate complex numbers:conjugate()method
  • Get absolute value (magnitude):abs()function (e.g. math, programming, programing)
  • Obtain declination (phase):math,cmathmodule
  • Polar coordinate transformation (polar form representation):math,cmathmodule
  • Computation of complex numbers (quadrature, powers, square roots)

Generate complex variables

Denote the imaginary unit by j and write the following, note that it is not i.

c = 3 + 4j

# (3+4j)
# <class 'complex'>

If the imaginary part is 1, omitting it results in a NameError. If a variable named j is defined first, it is considered to be that variable.

It should be explicitly stated in this way.

# c = 3 + j
# NameError: name 'j' is not defined

c = 3 + 1j

# (3+1j)

If the real part is 0, it can be omitted.

c = 3j

# 3j

If you want to define a value with an imaginary part of 0 as a complex complex type, write 0 explicitly. As described below, operations can be performed between the complex type and integer type or floating-point type.

c = 3 + 0j

# (3+0j)

Real and imaginary parts can be specified as floating-point float type. Exponential notation is also acceptable.

c = 1.2e3 + 3j

# (1200+3j)

It can also be generated by a constructor of type “complex”, as in “complex(real part, imaginary part)”.

c = complex(3, 4)

# (3+4j)
# <class 'complex'>

Get real and imaginary parts of complex numbers: real, imagattribute

The real and imaginary parts of a complex complex type can be obtained with the real and imag attributes, respectively. Both are floating-point float types.

c = 3 + 4j

# 3.0
# <class 'float'>

# 4.0
# <class 'float'>

It is read only and cannot be changed.

# c.real = 5.5
# AttributeError: readonly attribute

Get conjugate complex numbers: conjugate()

To obtain conjugate complex numbers, use the conjugate() method.

c = 3 + 4j

# (3-4j)

Obtain the absolute value (magnitude) of a complex number: abs()

To obtain the absolute value (magnitude) of a complex number, use the built-in function abs().

c = 3 + 4j

# 5.0

c = 1 + 1j

# 1.4142135623730951

Obtain the declination (phase) of a complex number: math, cmathmodule

To obtain the declination (phase) of a complex number, use the math or cmath module.

The cmath module is a mathematical function module for complex numbers.

It can be calculated with the inverse tangent function math.atan2() as defined, or use cmath.phase(), which returns the declination (phase).

import cmath
import math

c = 1 + 1j

print(math.atan2(c.imag, c.real))
# 0.7853981633974483

# 0.7853981633974483

print(cmath.phase(c) == math.atan2(c.imag, c.real))
# True

In both cases, the unit of angle that can be obtained is radians. To convert to degrees, use math.degrees().

# 45.0

Polar coordinate transformation of complex numbers (polar formal representation): math, cmathmodule

As mentioned above, the absolute value (magnitude) and declination (phase) of a complex number can be obtained, but using cmath.polar(), they can be obtained together as a (absolute value, declination) tuple.

c = 1 + 1j

# (1.4142135623730951, 0.7853981633974483)
# <class 'tuple'>

print(cmath.polar(c)[0] == abs(c))
# True

print(cmath.polar(c)[1] == cmath.phase(c))
# True

Conversion from polar coordinates to Cartesian coordinates is done using cmath.rect(). cmath.rect(absolute value, deviation) and similar arguments can be used to obtain values of the equivalent complex complex complex type.

print(cmath.rect(1, 1))
# (0.5403023058681398+0.8414709848078965j)

print(cmath.rect(1, 0))
# (1+0j)

print(cmath.rect(cmath.polar(c)[0], cmath.polar(c)[1]))
# (1.0000000000000002+1j)

The real and imaginary parts are equivalent to the results calculated by cosine math.cos() and sine math.sin() from absolute values and declination angles.

r = 2
ph = math.pi

print(cmath.rect(r, ph).real == r * math.cos(ph))
# True

print(cmath.rect(r, ph).imag == r * math.sin(ph))
# True

Computation of complex numbers (quadrature, powers, square roots)

Four arithmetic operations and power calculations can be performed using the usual arithmetic operators.

c1 = 3 + 4j
c2 = 2 - 1j

print(c1 + c2)
# (5+3j)

print(c1 - c2)
# (1+5j)

print(c1 * c2)
# (10+5j)

print(c1 / c2)
# (0.4+2.2j)

print(c1 ** 3)
# (-117+44j)

Square root can be calculated with **0.5, but it introduces error. cmath.sqrt() can be used to calculate the exact value.

print((-3 + 4j) ** 0.5)
# (1.0000000000000002+2j)

print((-1) ** 0.5)
# (6.123233995736766e-17+1j)

print(cmath.sqrt(-3 + 4j))
# (1+2j)

# 1j

It can also perform arithmetic operations with complex types, int types, and float types.

print(c1 + 3)
# (6+4j)

print(c1 * 0.5)
# (1.5+2j)
Copied title and URL