NumPy tan()

The numpy.tan() function computes the trigonometric tangent of each element in an input array. It is equivalent to np.sin(x) / np.cos(x) computed element-wise.

Syntax

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

Parameters

ParameterTypeDescription
xarray_likeInput array containing angles in radians.
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 tangent 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 tangent values of the input array elements. If the input is a scalar, a scalar is returned.


Examples

1. Computing Tangent of a Single Value

Here, we compute the tangent of a single angle in radians.

</>
Copy
import numpy as np

# Define an angle in radians
angle = np.pi / 4  # 45 degrees in radians

# Compute the tangent of the angle
result = np.tan(angle)

# Print the result
print("Tangent of 45 degrees (π/4 radians):", result)

Output:

Tangent of 45 degrees (π/4 radians): 0.9999999999999999
Computing Tangent of a Single Value

2. Computing Tangent for an Array of Angles

We compute the tangent values for multiple angles provided in an array.

</>
Copy
import numpy as np

# Define an array of angles in radians
angles = np.array([0, np.pi/6, np.pi/4, np.pi/3, np.pi/2])  # [0°, 30°, 45°, 60°, 90°]

# Compute the tangent of each angle
tan_values = np.tan(angles)

# Print the results
print("Angles (in radians):", angles)
print("Tangent values:", tan_values)

Output:

Angles (in radians): [0.         0.52359878 0.78539816 1.04719755 1.57079633]
Tangent values: [0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00
 1.63312394e+16]
Computing Tangent for an Array of Angles

Note that the tangent function approaches infinity at π/2 (90°), which is why the last value is very large.

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 angles
angles = np.array([0, np.pi/4, np.pi/2, np.pi])

# Create an output array with the same shape
output_array = np.empty_like(angles)

# Compute tangent and store the result in output_array
np.tan(angles, out=output_array)

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

Output:

Computed tangent values: [ 0.00000000e+00  1.00000000e+00  1.63312394e+16 -1.22464680e-16]
numpy.tan() Using the out Parameter

4. Using the where Parameter

Using a condition to compute tangent only for selected elements.

</>
Copy
import numpy as np

# Define an array of angles
angles = np.array([0, np.pi/4, np.pi/2, np.pi])

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

# Compute tangent values where mask is True
result = np.tan(angles, where=mask)

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

Output:

Computed tangent values with mask: [0.00000000e+00 0.00000000e+00 1.63312394e+16 0.00000000e+00]
numpy.tan() Using the where Parameter

The tangent values are computed only for elements where mask=True. The other values remain unchanged.