NumPy – Understanding tan(), arctan(), and arctan2() functions (6 examples)

Updated: February 28, 2024 By: Guest Contributor Post a comment

Introduction

NumPy is a fundamental package for scientific computing in Python, offering a comprehensive mathematical functions library, multi-dimensional array objects, and tools for integrating C/C++ and Fortran code. Among its broad array of functions, the trigonometric functions are essential for various scientific and engineering applications. In this tutorial, we will delve into the intricacies of three such functions: tan(), arctan(), and arctan2(), illustrating their uses with six examples ranging from basic to advanced complexity.

Understanding Trigonometric Functions

Before we delve into the examples, let’s briefly recap what these functions represent:

  • tan(): Represents the tangent of an angle, a fundamental trigonometric function expressing the ratio of the opposite side to the adjacent side of a right-angled triangle.
  • arctan(): The arctan or inverse tangent function is the inverse of the tangent function. It returns the angle whose tangent is the given number.
  • arctan2(): This version of the inverse tangent accepts two arguments representing the coordinates of a point (y, x) and returns the angle whose tangent is y/x. Unlike arctan(), arctan2() can determine the correct quadrant of the angle, making it more precise in certain applications.

Example 1: Basic Use of tan()

Let’s start with the simplest application of the tan() function.

import numpy as np

# Angle in radians
angle_radians = np.pi / 4

# Computing the tangent of the angle
tangent = np.tan(angle_radians)

print('The tangent of π/4 is:', tangent)

Output:

The tangent of π/4 is: 1.0

Example 2: Vectorized tan()

NumPy shines in its ability to perform vectorized operations. Let’s compute the tangents of multiple angles at once.

import numpy as np

# Angles in radians
angles = np.array([0, np.pi/4, np.pi/2])

# Computing tangents
tangents = np.tan(angles)

print('Tangents:', tangents)

Output:

Tangents: [0.         1.         1.63312394e+16]

Example 3: Exploring arctan()

Now, let’s flip the script and find the angle given the tangent using arctan().

import numpy as np

# Tangent values
tangents = np.array([0, 1, -1])

# Computing the angles in radians
angles = np.arctan(tangents)

print('Angles (radians):', angles)

Output:

Angles (radians): [ 0.          0.78539816 -0.78539816]

Example 4: Understanding arctan2()

Let’s illustrate the advantage of arctan2() over arctan() by computing the direction angle of a vector.

import numpy as np

# Vector coordinates
coordinates = np.array([[1, 1], [-1, 1], [-1, -1], [1, -1]])

# Computing angles with arctan2()
angles = np.arctan2(coordinates[:,1], coordinates[:,0])

print('Angles (radians):', angles)

Output:

Angles (radians): [ 0.78539816  2.35619449 -2.35619449 -0.78539816]

Example 5: Complex Angle Computations

Building on the previous example, let’s add an additional layer of complexity by transforming these angles back to vector components and verify the results.

import numpy as np

# Given angles
angles = np.array([0.78539816, 2.35619449, -2.35619449, -0.78539816])

# Transforming angles to vector components
x_components = np.cos(angles)
y_components = np.sin(angles)

vectors = np.column_stack((x_components, y_components))

print('Vectors:', vectors)

Output:

Vectors: [[ 0.70710678  0.70710678]
          [-0.70710678  0.70710678]
          [-0.70710678 -0.70710678]
          [ 0.70710678 -0.70710678]]

Example 6: Solving Trigonometric Equations

Finally, let’s explore a more advanced scenario: solving trigonometric equations utilizing our understanding of these functions.

import numpy as np
from scipy.optimize import fsolve

# Defining the equation to solve: tan(x) = 2
# where x is the angle in radians

func = lambda x: np.tan(x) - 2

# Initial guess
initial_guess = 0.1

# Solving for x
solution = fsolve(func, initial_guess)

print('Solution (radian):', solution)

Output:

Solution (radian): [1.10714872]

Conclusion

This tutorial has walked you through the use of NumPy’s tan(), arctan(), and arctan2() functions with several examples demonstrating their applications from basic to advanced levels. With a solid understanding of these trigonometric functions, their nuances, and how to apply them in vectorized operations, you’re well-equipped to tackle a wide range of scientific and engineering problems. Remember, practice is key to mastering these concepts, so don’t hesitate to experiment with these examples and beyond.