NumPy tan()

The tan() function computes the tangent of elements in an array. The tangent is the trigonometric function that calculates the ratio of the length of the side opposite an angle to the length of the side adjacent to the angle in a right-angled triangle.

Example

import numpy as np

# array of angles in radians
angles = np.array([0, 1, 4])

# compute the tangent of the angles result = np.tan(angles)
print(result) # Output : [0. 1.55740772 1.15782128]

tan() Syntax

The syntax of tan() is:

numpy.tan(array, out = None, dtype = None)

tan() Arguments

The tan() function takes following arguments:

  • array - the input arrays
  • out (optional) - the output array where the result will be stored
  • dtype (optional) - data type of the output array

tan() Return Value

The tan() function returns an array containing the element-wise tangent values of the input array.


Example 1: Compute tangent of the Angles

import numpy as np

# array of angles in radians
angles = np.array([0, np.pi/4, np.pi/2, np.pi])
print("Angles:", angles)

# compute the tangent of the angles tangent_values = np.tan(angles)
print("tangent values:", tangent_values)

Output

Angles: [0.         0.78539816 1.57079633 3.14159265]
tangent values: [ 0.00000000e+00  1.00000000e+00  1.63312394e+16 -1.22464680e-16]

In this example, we have the array angles containing four angles in radians: 0, π/4, π/2, and π.

The np.tan() function is used to calculate the tangent values for each element in the angles array.


Example 2: Use out to Store the Result in Desired Location

import numpy as np

# create an array of angles in radians
angles = np.array([0, np.pi/6, np.pi/4, np.pi/3, np.pi/2])

# create an empty array to store the result
result = np.empty_like(angles)

# compute the tangent of angles and store the result in the 'result' array np.tan(angles, out=result)
print("Result:", result)

Output

Result: [1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01
 6.12323400e-17]

Here, we have used tan() with the out parameter to compute the tangent of the angles array and store the result directly in the result array.

The result array contains the computed tangent values.


Example 3: Use of dtype Argument in tan()

import numpy as np

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

# calculate the tangent of each angle with a specific dtype tangents_float = np.tan(angles, dtype=float) tangents_complex = np.tan(angles, dtype=complex)
print("Tangents with 'float' dtype:") print(tangents_float) print("\nTangents with 'complex' dtype:") print(tangents_complex)

Output

Tangents with 'float' dtype:
[ 0.00000000e+00  1.00000000e+00  1.63312394e+16 -1.22464680e-16]

Tangents with 'complex' dtype:
[ 0.00000000e+00+0.j  1.00000000e+00+0.j  1.63312394e+16+0.j
 -1.22464680e-16+0.j]

Here, by specifying the desired dtype, we can specify the data type of the output array according to our requirements.

Note: To learn more about the dtype argument, please visit NumPy Data Types.