When writing programs in our day-to-day life, we usually come across situations where we need to use a little maths to get the task done. Like other programming languages, python provides various operators to perform basic calculations like * for multiplication, % for modulus, and // for floor division.
If you are writing a program to perform specific tasks like studying periodic motion or simulating electric circuits, you will need to work with trigonometric functions as well as complex numbers. While you can't use these functions directly, you can access them by including two mathematical modules first. These modules are math and cmath .
The first one gives you access to hyperbolic, trigonometric, and logarithmic functions for real numbers, while the latter allows you to work with complex numbers. In this tutorial, I will go over all the important functions offered by these modules. Unless explicitly mentioned, all the values returned are floats.
Arithmetic FunctionsThese functions perform various arithmetic operations like calculating the floor, ceiling, or absolute value of a number using the floor(x) , ceil(x) , and fabs(x) functions respectively. The function ceil(x) will return the smallest integer that is greater than or equal to x . Similarly, floor(x) returns the largest integer less than or equal to x . The fabs(x) function returns the absolute value of x .
You can also perform non-trivial operations like calculating the factorial of a number using factorial(x) . A factorial isthe product of an integer and all the positive integers smaller than it. Itis used extensively when dealing with combinations and permutations. It can also be used to calculate the value of sine and cosine functions.
import mathdef getsin(x):
multiplier = 1
result = 0
for i in range(1,20,2):
result += multiplier*pow(x,i)/math.factorial(i)
multiplier *= -1
return result
getsin(math.pi/2) # returns 1.0
getsin(math.pi/4) # returns 0.7071067811865475
Another useful function in the math module is gcd(x,y) , which gives you the greatest common divisor (GCD) of two numbers x and y . When x and y are both not zero, this function returns the largest positive integer that divides both x and y . You can use it indirectly to calculate the lowest common multiple of two numbers using the following formula:
gcd(a, b) x lcm(a, b) = a x bHere are a few of the arithmetic functions that Python offers:
import mathmath.ceil(1.001) # returns 2
math.floor(1.001) # returns 1
math.factorial(10) # returns 3628800
math.gcd(10,125) # returns 5
math.trunc(1.001) # returns 1
math.trunc(1.999) # returns 1 Trigonometric Functions
These functions relate the angles of a triangle to its sides. They have a lot of applications, including the study of triangles and the modeling of periodic phenomena like sound and light waves.Keep in mind that the angle you provide is in radians.
You can calculate sin(x) , cos(x) , and tan(x) directly using this module. However, there is no direct formula to calculate cosec(x) , sec(x) , and cot(x) , buttheir value is equal to the reciprocal of the value returned by sin(x) , cos(x) , and tan(x) respectively.
Instead of calculating the value of trigonometric functions at a certain angle, you can also do the inverse and calculate the angle at which they have a given value by using asin(x) , acos(x) , and atan(x) .
Are you familiar withthe Pythagorean theorem ? It states thatthat the square of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the other two sides. The hypotenuse is also the largest side of a right-angled triangle. The math module provides the hypot(a, b) function to calculate the length of the hypotenuse.
import mathmath.sin(math.pi/4) # returns 0.7071067811865476
math.cos(math.pi) # returns -1.0
math.tan(math.pi/6) # returns 0.5773502691896257
math.hypot(12,5) # returns 13.0
math.atan(0.5773502691896257) # returns 0.5235987755982988
math.asin(0.7071067811865476) # returns 0.7853981633974484 Hyperbolic Functions
Hyperbolic functions are analogs of trigonometric functions that are based on a hyperbola instead of a circle. In trigonometry, the points (cos b , sin b ) represent the points of a unit circle. In the case of hyperbolic functions, the points(cosh b ,sinh b )represent the points that form the right half of an equilateral hyperbola.
Just like the trigonometric functions, you can calculate the value of sinh(x) , cosh(x) , and tanh(x) directly. The rest of the values can be calculated using various relations among these three values. There are also other functions like asinh(x) , acosh(x) , and atanh(x) , which can be used to calculate the inverse of the corresponding hyperbolic values.
import mathmath.sinh(math.pi) # returns 11.548739357257746
math.cosh(math.pi) # returns 11.591953275521519
math.cosh(math.pi) # returns 0.99627207622075
math.asinh(11.548739357257746) # returns 3.141592653589793
math.acosh(11.591953275521519) # returns 3.141592653589793
math.atanh(0.99627207622075) # returns 3.141592653589798
Since math.pi is equal to about 3.141592653589793, when we used asinh() on the value returned by sinh(math.pi) , we got ourπ back.
Power and Logarithmic FunctionsYou will probably be dealing with powers and logarithms more often than hyperbolic or trigonometric functions. Fortunately, the math module provides a lot of functions to help us calculate logarithms.
You can use log(x,[base]) to calculate the log of a given number x to the given base.If you leave out the optional base argument, the log of x is calculated to the base e. Here, e is a mathematical constant whose value is2.71828182.... and it can be accessed using math.e . By the way, Python also allows you to access another constantπ using math.pi .If you want to calculate the base-2 or base-10 logarithm values, using log2(x) and log10(x) will return more accurate results than log(x, 2) and log(x, 10) . Keep in mind that there is no log3(x) function, so you will have to keep using log(x, 3) for calculating base-3 logarithm values. The same goes for all other bases.
If the value whose logarithm you are calculating is very close to 1, you can use log1p(x) . The 1p in log1p signifies 1 plus. Therefore, log1p(x) calculates log(1+x) where x is close to zero. However, the results are more accurate with log1p(x) .