# Compute element-wise arc tangent of x1/x2 choosing the quadrant correctly in Python

PythonNumpyServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

The quadrant is chosen so that arctan2(x1, x2) is the signed angle in radians between the ray ending at the origin and passing through the point (1,0), and the ray ending at the origin and passing through the point (x2, x1).

The 1st parameter is the y-coordinates. The 2nd parameter is the x-coordinates. If x1.shape != x2.shape, they must be broadcastable to a common shape. The method returns array of angles in radians, in the range [-pi, pi]. This is a scalar if both x1 and x2 are scalars.

## Steps

At first, import the required library −

import numpy as np

Creating arrays using the array() method. These are four points in different quadrants −

x = np.array([-1, +1, +1, -1])
y = np.array([-1, -1, +1, +1])

Display the array1 −

print("Array1 (x coordinates)...\n", x)


Display the array2 −

print("\nArray2 (y coordinates)...\n", y)

To compute element-wise arc tangent of x1/x2 choosing the quadrant correctly, use the numpy, arctan2() method in Python −

print("\nResult...",np.arctan2(y, x) * 180 / np.pi)


## Example

import numpy as np

# The quadrant is chosen so that arctan2(x1, x2) is the signed angle in radians between the ray
# ending at the origin and passing through the point (1,0), and the ray ending at the origin and
# passing through the point (x2, x1).

# Creating arrays using the array() method
# These are four points in different quadrants
x = np.array([-1, +1, +1, -1])
y = np.array([-1, -1, +1, +1])

# Display the array1
print("Array1 (x coordinates)...\n", x)

# Display the array2
print("\nArray2 (y coordinates)...\n", y)

# To compute element-wise arc tangent of x1/x2 choosing the quadrant correctly, use the numpy, arctan2() method in Python
print("\nResult...",np.arctan2(y, x) * 180 / np.pi)

## Output

Array1 (x coordinates)...
[-1 1 1 -1]

Array2 (y coordinates)...
[-1 -1 1 1]

Result... [-135. -45. 45. 135.]