# Complex numbers. The cmath module. Functions for converting to polar coordinates and vice versa. Power and logarithmic functions

### Contents

Search other resources:

##### 1. Functions to convert to polar coordinates and vice versa

Python has 3 functions that work with polar coordinates:

• 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.

### ⇑

##### 1.1. Function cmath.phase(). Get the phase of a complex number

Function

`cmath.phase(x)`

returns the phase of a complex number x. The function is equivalent to calling

`cmath.atan2(x.imag, x.real)`

The result of the function is in the range from –π to π.

Example.

```# Include the cmath module
import cmath

# Function cmath.phase(x)
# Create a complex number
x = complex(2, -3) # x = 2 - 3*j

# Call the function
res = cmath.phase(x)

# Display the result
print("res = ", res)```

Program result

`res = -0.982793723247329`

### ⇑

##### 1.2. Function cmath.polar(). Get a polar representation of a complex number

Function

`cmath.polar(x)`

returns a polar representation of x. The function returns a pair (r, phi), where r is the modulus of x and phi is the phase of x.
Calling a function is equivalent to calling

`(abs(x), phase(x))`

Example.

```# Include the cmath module
import cmath

# Function cmath.polar(x)

# Create a complex number
x = complex(1, -1) # x = 1 - 1*j

# Call the function
res = cmath.polar(x)

# Display the result
print("res = ", res)```

Program result

`res = (1.4142135623730951, -0.7853981633974483)`

### ⇑

##### 1.3. Function cmath.rect(). Get complex number based on polar coordinates

Function

`cmath.rect(r, phi)`

allows you to get a complex number based on the modulus r and the phase phi. Calling a function is equivalent to calling

`r * (math.cos(phi) + math.sin(phi)*1j)`

Example.

```# Include cmath module
import cmath

# Function cmath.rect(x)

# Create a complex number as polar coordinates
r = 4 # modulus of number
phi = 1.2 # phase in radians

# Invoke the function
x = cmath.rect(r, phi)

# Display the result
print("x = ", x)```

Program result

`x = (1.4494310179066945+3.728156343868905j)`

### ⇑

##### 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 a 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.

### ⇑

##### 2.1. Function cmath.exp(). Exponent of a complex number

Function

`cmath.exp(x)`

returns the number e raised to the power of x. The e value is the basis of the natural logarithm. The x value is a complex number.

Example.

```# Include the cmath module
import cmath

# Function cmath.exp(x)

# Create a complex number
re = float(input("re = "))
im = float(input("im = "))

x = complex(re, im)

# Call the function
res = cmath.exp(x)

# Display the result
print("res = ", res)```

Test example

```re = 1
im = 0
res = (2.718281828459045+0j)```

### ⇑

##### 2.2. Function cmath.log(). Logarithm of a complex number

Function

`cmath.log(x [, base])`

returns the logarithm of a complex number x with the specified base. If base is not specified, then the natural logarithm of the complex number x is returned.

Example.

```# Include the cmath module
import cmath

# Function cmath.log()

# Create a complex number
re = float(input("re = "))
im = float(input("im = "))

x = complex(re, im)

# Call function for natural logarithm
res1 = cmath.log(x)
print("res1 = ", res1)

# Call the function for the base 4 logarithm
res2 = cmath.log(x, 4)
print("res2 = ", res2)```

Test example

```re = 1
im = -3
res1 = (1.151292546497023-1.2490457723982544j)
res2 = (0.8304820237218407-0.9009960708411433j)```

### ⇑

##### 2.3. Function cmath.log10(). Decimal logarithm

Function

`cmath.log10(x)`

returns the base 10 logarithm of a complex number x.

Example.

```# Include the cmath module
import cmath

# Function cmath.log10()

# Create a complex number
re = float(input("re = "))
im = float(input("im = "))

x = complex(re, im)

# Call the function for the decimal logarithm
res = cmath.log10(x)
print("res = ", res)```

Test example

```re = 1
im = -5
res = (0.707486673985409-0.5964603745259144j)```

### ⇑

##### 2.4. Function cmath.sqrt(). Square root of the number x

Function

`cmath.sqrt(x)`

returns the square root of a complex number x.

Example.

```# Include the cmath module
import cmath

# Function cmath.sqrt()

# Create a complex number
re = float(input("re = "))
im = float(input("im = "))

x = complex(re, im)

# Call the function for the decimal logarithm
res = cmath.sqrt(x)
print("res = ", res)```

Test example

```re = 2
im = -3
res = (1.6741492280355401-0.8959774761298381j)```