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.
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
|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
|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
Diagram showing what’s cmath.phase() in Python?
Logarithmic Functions in cMath Python Module
|cmath.exp(x)||Return e raised to power x – ex .|
For example – cmath.exp(complex(10, 2)) will return
|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
|cmath.log10(x)||Returns logarithm of x with base being 10.|
For example – cmath.log10(complex(3, 5)) will return
|cmath.sqrt(x)||Returns square root of x.|
For example – cmath.sqrt(complex(29, 29)) will return
Trigonometric Functions in cMath Python Module
|cmath.sin(x)||Returns sine of complex number x.|
For example – cmath.sin(complex(28, 3)) will return
|cmath.cos(x)||Returns cosine of complex number x.|
For example – cmath.cos(complex(28, 3)) will return
|cmath.tan(x)||Returns tangent of complex number x.|
For example – cmath.tan(complex(28, 3)) will return
Constants in cMath Python Module
|cmath.pi||Returns mathematical constant π = 3.141592|
|cmath.e||Returns mathematical exponent value e = 2.718281|
|cmath.tau||Returns mathematical constant value τ = 6.283185|
|cmath.nan||Returns non a number NaN|
|cmath.inf||Floating point positive infinity|
|cmath.infj||Complex number a + bj where a = 0 and b = ∞|
|cmath.nanj||Complex number a + bj where a = 0 and b = NaN|