Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

NumPy tan() Function



The NumPy tan() function is used to calculate the tangent of each element in an input array. It computes the tangent of the input value (in radians), where the tangent of an angle is defined as the ratio of the length of the opposite side to the adjacent side in a right-angled triangle.

  • Domain: The function accepts input values in radians, and it can handle any real number. The domain is all real numbers, except for odd multiples of /2, where the tangent function is undefined.
  • Range: The output values can be any real number, as the tangent of an angle can go to infinity for odd multiples of /2.

Syntax

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

numpy.tan(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, in radians. The function computes the tangent 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 tangent of the corresponding element in the input array x.

Example: Basic Usage of tan() Function

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

import numpy as np

# Creating a 1-dimensional array
arr = np.array([0, np.pi/4, np.pi/2, 3*np.pi/4])

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

The output obtained will be −

[ 0.00000000e+00  1.00000000e+00  1.63312394e+16 -1.00000000e+00]

Example: Tangent of Angles in Degrees

In this example, we convert angles from degrees to radians before calculating their tangent using numpy.tan() function. The tangent function expects input in radians, so we use numpy.radians() function to perform the conversion −

import numpy as np

# Angles in degrees
angles_degrees = np.array([0, 30, 45, 60, 90])

# Convert degrees to radians
angles_radians = np.radians(angles_degrees)

# Calculate tangent of each angle in radians
result = np.tan(angles_radians)
print(result)

This will produce the following result −

[0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00
 1.63312394e+16]

Example: Tangent of a Single Scalar Value

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

import numpy as np

# Scalar value
scalar = np.pi/4

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

The output obtained is −

0.9999999999999999

Example: tan() Function with Angles at Odd Multiples of /2

In this example, we calculate the tangent of angles at odd multiples of /2, where the tangent function becomes undefined and approaches infinity −

import numpy as np

# Angles at odd multiples of /2
angles_odd_multiples = np.array([np.pi/2, 3*np.pi/2, 5*np.pi/2])

# Applying tan to the angles
result = np.tan(angles_odd_multiples)
print(result)  

This will produce the following result −

[1.63312394e+16 5.44374645e+15 3.26624787e+15]
numpy_trigonometric_functions.htm
Advertisements