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
| Parameter | Type | Description |
|---|---|---|
x | array_like | Input array containing angles in radians. |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array where the result is stored. If None, a new array is created. |
where | array_like, optional | Boolean mask specifying which elements to compute. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior when computing the tangent function. |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Defines the data type of the output array. |
subok | bool, optional | Determines 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
