cmath Python Module

Python have five built-in data types one of which is Numbers, now as numbers can be integers, floats or complex. Doing mathematics on these require different functions because you cannot mathematically treat intergers/float in the same way as Complex Numbers because these have an imaginary part while integers/floats don’t.

Owing to this reason, Python’s core developers have put together two modules containing Mathematical Functions.
cmath module for Complex Numbers
math module for all other types of numbers

How to import cmath in Python? You can import python’s cmath module into code by using import cmath statement and to access functions inside it do cmath.function_name(parameter).

If your unaware of Python’s Math Module then you can see – Math Module in Python Programming Language article which I put up on this website some days back.
Below are some tables explaining what are functions there inside cMath Module? with some examples for each.

Stop 🖐🏻
What are Complex Numbers? 🤷🏻 Those numbers which are made up of two parts – Real and Complex. And have form like a + bi where both a, b are real numbers while i is iota = √ -1 (Square root of -1).
Note – Complex numbers are not real but real number can be complex. Like if in a + bi, b = 0 then we will have a + 0i which is both real, complex.
That’s why functions in cMath Module can be applied to Integers/Floats as well but functions in Math Module can’t be applied to Complex Numbers.

How to create Complex Number in Python? For having simple numbers in Python, you just need to type it like 3, 2, 29. But owing to specific syntax of complex numbers a + bj. You need to you complex(a, b) constructor for creating these in Python, as a + bj will be considered just a String by Python Interpreter.

Just a Fact
In mathematics imaginary number is denoted by i (iota) but in Python conventionally its denoted by j

Examples of using cmath Module in Python Code

# Firstly import cmath module
import cmath

# Printing Mathematical constants Pie, Square root of -1 iota
print(cmath.pi)
print(cmath.sqrt(-1))

# Above code outputs
3.141592653589793
1j

Conversion Functions in cMath Module

FunctionDescription
cmath.phase(x)Returns phase of x as a float.
For example – cmath.phase(complex(10, 2)) will return 0.19739555984988075
cmath.polar(x)Return representation of x as polar coordinates.
For example – cmath.phase(complex(10, 2)) will return
0.19739555984988075
cmath.rect(r, phi)Return the complex number x with polar coordinates r and phi. Equivalent to r * (math.cos(phi) + math.sin(phi)* i) here i is iota
For example – cmath.rect(4,16) will return
(-3.8306379212935386-1.1516132666602612j)

Diagram showing what’s cmath.phase() in Python?

What is cmath.phase in Python Programming Langauge?

Logarithmic Functions in cMath Python Module

FunctionDescription
cmath.exp(x)Return e raised to power x – ex .
For example – cmath.exp(complex(10, 2)) will return
(-9166.244060822655+20028.608669281643j)
cmath.log(x, base)If base is not defined then returns natural logarithm of x, otherwise returns log of x to a specified base.
For example – cmath.log10(complex(19, 2)) will return
(1.2811464322282373+0.04554747576944601j)
cmath.log10(x)Returns logarithm of x with base being 10.
For example – cmath.log10(complex(3, 5)) will return
(0.7657394585211276+0.44748697004049304j)
cmath.sqrt(x)Returns square root of x.
For example – cmath.sqrt(complex(29, 29)) will return
(5.916595022004622+2.4507339011834555j)

Trigonometric Functions in cMath Python Module

FunctionDescription
cmath.sin(x)Returns sine of complex number x.
For example – cmath.sin(complex(28, 3)) will return
(2.7273879093833497-9.643265173120367j)
cmath.cos(x)Returns cosine of complex number x.
For example – cmath.cos(complex(28, 3)) will return
(-9.691190497197827-2.713900304379616j)
cmath.tan(x)Returns tangent of complex number x.
For example – cmath.tan(complex(28, 3)) will return
(-0.002574685032008448+0.9957757629883296j)

Constants in cMath Python Module

FunctionDescription
cmath.piReturns mathematical constant π = 3.141592
cmath.eReturns mathematical exponent value e = 2.718281
cmath.tauReturns mathematical constant value τ = 6.283185
cmath.nanReturns non a number NaN
cmath.infFloating point positive infinity
cmath.infjComplex number a + bj where a = 0 and b = ∞
cmath.nanjComplex number a + bj where a = 0 and b = NaN

Josh

Hi, I'm Josh a Computer Science graduate from California State University, Sacramento since coming out with my Master's from university. I've worked with multiple startups across US and in UK as well primarily as a Python Developer. Here on this website, I'm sharing my knowledge of Python. If you want to ask me anything about Python feel free to reach out, I would be happy to help you out.

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts