NumPy arctan() Function



The NumPy arctan() function is used to compute the inverse tangent (arctangent) of each element in an input array. It calculates the angle (in radians) whose tangent is the input value.

  • Domain: The function accepts input values for all real numbers. It can handle any scalar or array of real numbers.
  • Range: The output values lie in the range [-/2, /2], as the arctangent function returns angles in this range.

Syntax

Following is the syntax of the NumPy arctan() function −

numpy.arctan(x, /, out=None, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])

Parameters

This function accepts the following parameters −

  • x: The input array or scalar. The function computes the arctangent of each element of the array or scalar.
  • out (optional): A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
  • where (optional): This condition is broadcast over the input. At locations where the condition is True, the result will be computed. Otherwise, the result will retain its original value.
  • casting (optional): Controls what kind of data casting may occur. Defaults to 'same_kind'.
  • order (optional): Controls the memory layout order of the result. 'C' means C-order, 'F' means Fortran-order, 'A' means 'F' if inputs are all F, 'C' otherwise, 'K' means match the layout of the inputs as closely as possible.
  • dtype (optional): The type of the returned array and of the accumulator in which the elements are processed. The dtype of x is used by default unless dtype is specified.
  • subok (optional): If True, then sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array.

Return Value

This function returns an array where each element is the arctangent (inverse tangent) of the corresponding element in the input array x, in radians.

Example: Basic Usage of arctan() Function

In the following example, we use the arctan() function to compute the arctangent of each element in a 1-dimensional array −

import numpy as np

# Creating a 1-dimensional array
arr = np.array([-1, 0, 1, 2])

# Applying arctan to each element
result = np.arctan(arr)
print(result)

The output obtained will be −

[-0.78539816  0.          0.78539816  1.10714872]

Example: Arctangent of Angles in Degrees

In this example, we first calculate the arctangent in radians and then convert the result to degrees using numpy.degrees() function −

import numpy as np

# Creating an array of values
values = np.array([-1, 0, 1, 2])

# Calculate arctangent in radians
radians_result = np.arctan(values)

# Convert radians to degrees
degrees_result = np.degrees(radians_result)
print(degrees_result)

This will produce the following result −

[-45.           0.          45.          63.43494882]

Example: Arctangent of a Single Scalar Value

In this example, we are using the arctan() function to calculate the arctangent of a single scalar value −

import numpy as np

# Scalar value
scalar = 1

# Applying arctan to the scalar
result = np.arctan(scalar)
print(result)

The output obtained is −

0.7853981633974483

Example: Arctangent of Negative Values

In this example, we calculate the arctangent of negative values. The arctangent function returns negative angles for negative inputs −

import numpy as np

# Negative values
negative_values = np.array([-2, -1, -0.5])

# Applying arctan to the array
result = np.arctan(negative_values)
print(result)

This will produce the following result −

[-1.10714872 -0.78539816 -0.46364761]
numpy_trigonometric_functions.htm
Advertisements