# Numpy arctan2: Find element-wise arctan of x1/x2

Hello! In this tutorial, we will learn about the arctan function and the numpy arctan2 function which gives element-wise arc tangent of x1/x2 choosing the quadrants correctly.

Also read: Python if…else Conditional Statement (With Examples)

## What is the arctan or tan-1(x) function?

The arctan(x) or tan-1(x) where x is the real number gives the corresponding angle. It returns the value of an angle between -90 and 90 degrees that, when plugged into the tangent function, returns the original value. In other words, if

tan(theta) = x
, then
theta = arctan(x)
.

Numpy provides a function numpy.arctan for determining tan-1(x).

## What is the Numpy arctan2 function?

Numpy arctan NumPy.arctan2 is a function in the NumPy library that calculates the elementwise four-quadrant inverse tangent (arctangent) of y/x in radians. The four-quadrant inverse tangent of a value is the angle whose tangent is equal to the value.

By specifying y and x values, the function returns the angle in the range of -π to π, corresponding to the direction of the vector (x, y) in a plane. Suppose given an array x(or x2) and y(or x1) the numpy.arctan2 function will return the element-wise direction of the points.

In simpler words, if we take a point (x,y), the arctan2 function will return the angle made by the point (x,y) with the positive x-axis in the range [-pi, pi].

### Syntax

```numpy.arctan2(x1, x2, /, out=None)
```

Return value

An array of angles in the [-pi, pi] range, measured in radians. If x1 and x2 are both scalars or vectors, then the output will be scalar or vector respectively.

## Implementation and Examples of Numpy Arctan2

We will start by importing numpy library

```import numpy as np
```

### Example 1: Finding the direction of a vector

In this example, we will use the numpy.arctan2 function to find the angle between a given point (x,y) from the x-axis

```import numpy as np
# coordinates
x = np.array([1, 1])

# Calculate the direction (in radians)
direction = np.arctan2(x, x)

# Convert to degrees
direction_deg = np.degrees(direction)
print(direction_deg)
```

Explanation:

• Line 3: We defined a NumPy array called “x” which contains two elements with values 1 and 1 respectively. This represents the coordinates (x,y) of a point in a 2D plane.
• Line 6: Using the NumPy function “arctan2” we calculate the direction of the point from the origin in radians. The function takes two arguments: the y-coordinate (x) and the x-coordinate (x). It returns the angle between the positive x-axis and the point (x,y) measured in radians.
• Line 9: The NumPy function “degrees” converts the direction from radians to degrees. The formula for the conversion is degrees = radians * (180/π).
• Line 10: Print the value of “direction_deg”, which represents the direction of the point in degrees.

Output

The output will contain the angle between the x-axis(1,0) and point(1,1) which is 45 degrees. Here’s how you can find more information on how to find quadrants.

```45.0
```

### Example 2: Elementwise-arctan of 2 vectors

```import numpy as np
# vectors
x= [np.inf, np.inf]
y= [2, np.inf]

#output
output = np.arctan2(y,x)
print(np.degrees(output))
```

Explanation:

• Lines 3 and 4: We have defined our vectors x and y as a list. We have used numpy.inf which is a representation of positive infinity in the NumPy library.
• Line 7: element-wise arctan of the vectors x and y is stored in the output variable.

Output

```[ 0. 45.]
```

Explanation:

• The first output is 0 since y/x in the first element of the vectors will be 2/np.inf = 0.
• In the second case, we have y/x = np.inf/np.inf which is an indeterminate form in mathematics but here numpy has assumed np.inf/np.inf = 1 which gives us output as 45 degrees.

### Example 3: Elementwise-arctan of 2 vectors

We can replace scalars with vectors and get element-wise output as an array.

```import numpy as np
#x and y vectors
x = np.array([1, 2, -3, 0])
y = np.array([-1, 2, -3, -4])

angles = np.arctan2(y, x)
directions = np.degrees(angles)

#print the direction of the vector
print(directions)
```

Explanation:

Element-wise angle with the positive x-axis is printed, The first element is 1 in array x and -1 in array y, which we are passing which makes the coordinates (1,-1) which lies in the 4th quadrant so the angle is 45 degrees below the positive x-axis.

Output

```[ -45.   45. -135.  -90.]
```

Explanation:

• (2,2) is in 1st quadrant , Positive 45 degrees(above the +x-axis)
• (-3,-3) is in the 3rd quadrant, 135 degrees below the +x-axis
• (0,-4) lies on the y-axis and 90 degrees below the +x-axis.

Also check: Python fpdf module – Beginners Guide

## Summary

In this article, we have learned what is the arctan function, its domain and range, and the usage of numpy arctan2 function, using examples we saw how we can use it for determining the direction of a scalar point or a vector.

https://numpy.org/doc/stable/reference/generated/numpy.arctan2.html