NumPy arctan()

The numpy.arctan() function computes the trigonometric inverse tangent (arctangent) of each element in an input array. The inverse tangent function returns values in the range [-π/2, π/2].

Syntax

</>
Copy
numpy.arctan(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)

Parameters

ParameterTypeDescription
xarray_likeInput values for which the inverse tangent is computed.
outndarray, None, or tuple of ndarray and None, optionalOptional output array where the result is stored. If None, a new array is created.
wherearray_like, optionalBoolean mask specifying which elements to compute. Elements where where=False retain their original value.
castingstr, optionalDefines the casting behavior when computing the function.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalDefines the data type of the output array.
subokbool, optionalDetermines if subclasses of ndarray are preserved in the output.

Return Value

Returns an array with the inverse tangent values of the input elements. If the input is a scalar, a scalar is returned. The output values are in the range [-π/2, π/2].


Examples

1. Computing Arctangent of a Single Value

Here, we compute the inverse tangent of a single value.

</>
Copy
import numpy as np

# Define a value
value = 1.0  

# Compute the inverse tangent (arctan) of the value
result = np.arctan(value)

# Print the result
print("Arctan of 1:", result)

Output:

Arctan of 1: 0.7853981633974483

The result is approximately 0.785, which is equivalent to π/4.

2. Computing Arctangent for an Array of Values

We compute the arctangent values for multiple input values in an array.

</>
Copy
import numpy as np

# Define an array of values
values = np.array([-1, 0, 1, np.tan(np.pi/3)])

# Compute the inverse tangent of each value
arctan_values = np.arctan(values)

# Print the results
print("Input values:", values)
print("Arctan values:", arctan_values)

Output:

Input values: [-1.          0.          1.          1.73205081]
Arctan values: [-0.78539816  0.          0.78539816  1.04719755]

The results show that arctan(-1) is -π/4, arctan(0) is 0, arctan(1) is π/4, and arctan(√3) is π/3.

3. Using the out Parameter

Using an output array to store results instead of creating a new array.

</>
Copy
import numpy as np

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

# Create an output array with the same shape
output_array = np.ndarray(shape=[1, 3])

# Compute arctan and store the result in output_array
np.arctan(values, out=output_array)

# Print the results
print("Computed arctan values:", output_array)

Output:

Computed arctan values: [-0.78539816  0.          0.78539816]

4. Using the where Parameter

Using a condition to compute arctangent only for selected elements.

</>
Copy
import numpy as np

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

# Define a mask (compute arctan only where mask is True)
mask = np.array([True, False, True, False])

# Compute arctan values where mask is True
result = np.arctan(values, where=mask)

# Print the results
print("Computed arctan values with mask:", result)

Output:

Computed arctan values with mask: [-7.85398163e-001  0.00000000e+000  7.85398163e-001  4.95242130e+223]

The arctangent is computed only for elements where mask=True. The other values remain unchanged.