Trigonometric and Angular functions in Python

In this tutorial, we will learn about the different trigonometric and angular functions in Python. The Python programming language supports a wide variety of built-in functions.

The math module contains trigonometric and angular functions. This module provides access to mathematical functions but cannot be used with complex numbers. Before accessing these functions, we should import the math module. This helps us to access all the functions contained in the math module.

But, if we want to use only specific functions, then write from math import sin, cos which will include only the particular functions for use. Also, if you do not want to write the big module names every time with functions use aliasing import math as m. So, now math.sin(x) is the same as m.sin(x). So, let’s discuss Trigonometric and Angular functions in detail in the Python programming language.

Trigonometric functions in Python

  1. math.sin(x) : Returns the sine of the value of x radians.
  2. math.cos(x) : Returns the cosine of the value of x radians.
  3. math.tan(x) : Returns the tangent of the value of x radians.
  4. math.asin(x) : Returns the arc sine of the value of x in radians.
  5. math.acos(x) : Returns the arc cosine of the value of x in radians.
  6. math.atan(x) : Returns the arc tangent of the value of x in radians.
  7. math.atan2(x) : Returns atan(y/x) in radians. The result lies between -pi to pi and the vector from the origin to the point (x,y) in the plane makes this angle with the positive x-axis. The signs of both inputs are known to atan2() so that it can calculate the correct quadrant for the angle. Example- The result of both atan(1) and atan2(1,1) is pi/4 but the result of atan2(-1,-1) is -3*pi/4.
  8. math.hypot(*coordinates): It returns the Euclidean norm sqrt(sum(x**2 for x in coordinates)). This is the length of the vector from the origin to the given coordinates.
    For a 2-D point (x,y), it is equivalent to computing the hypothesis sqrt(x*x + y*y), which is the length of the vector from the origin to the point (x,y).
    In version 3.8, added support is provided for n-dimensional points but initially, only 2-D cases were supported.
  9. math.dist(p,q): Returns the Euclidean distance between the points p and q. These points are given as a sequence of coordinates but must have the same direction. This feature is new in version 3.8 and is roughly equivalent to:
sqrt(sum((px - qx) ** 2.0 for px, qx in zip(p, q)))

Angular conversion functions in Python

  1. math.degrees(x): It converts the angle x, from radians to degrees and the argument must be in radians.
  2. math.radians(x): It converts the angle x, from degrees to radians and the argument must be in degrees.

The implementation of some of these functions is illustrated below with sample code:

import math
print("sin(3)", math.sin(3))
print("cos(3)", math.cos(3))
print("tan(3)", math.tan(3))
print("degrees(3)", math.degrees(3))
print("radians(60)", math.radians(3))
print("hypot(3,4)", math.hypot(3,4))
print("asin(1)", math.asin(1))
print("acos(1)", math.acos(1))
print("atan(1)", math.atan(1))
print("atan2(1,0)", math.atan2(1,0))

Output:

sin(3) 0.1411200080598672
cos(3) -0.9899924966004454
tan(3) -0.1425465430742778
degrees(3) 171.88733853924697
radians(60) 0.05235987755982989
hypot(3,4) 5.0
asin(1) 1.5707963267948966
acos(1) 0.0
atan(1) 0.7853981633974483
atan2(1,0) 1.5707963267948966

Related Posts:

Difference between Eval() and Exec() functions in Python
How to Auto-generate a list with random elements in Python?

Leave a Reply

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