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

NumPy - Trigonometric Functions



NumPy Trigonometric Functions

In mathematics, trigonometric functions are used to relate the angles of a triangle to the lengths of its sides. The most common trigonometric functions are sine (sin), cosine (cos), and tangent (tan), which are based on a right triangle.

These functions help in various fields such as geometry, physics, and engineering, especially for calculating angles and distances.

NumPy provides several functions like sin(), cos(), and tan() to compute these values for arrays of angles.

Sine, Cosine, and Tangent Functions

The basic trigonometric functions in NumPy include sine, cosine, and tangent. These functions operate element-wise on arrays, meaning they are applied to each individual element of the array. Trigonometric functions are typically applied to angles, and the angle is usually provided in radians.

For example, the sine of an angle is the ratio of the opposite side to the hypotenuse in a right triangle. The cosine is the ratio of the adjacent side to the hypotenuse, and the tangent is the ratio of the opposite side to the adjacent side.

Example: Sine, Cosine, and Tangent

In the following example, we calculate the sine, cosine, and tangent of an array of angles in radians using NumPy functions −

import numpy as np

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

# Calculate sine, cosine, and tangent
sine_values = np.sin(angles)
cosine_values = np.cos(angles)
tangent_values = np.tan(angles)

print("Sine values:", sine_values)
print("Cosine values:", cosine_values)
print("Tangent values:", tangent_values)

Following is the output obtained −

Sine values: [0.00000000e+00 7.07106781e-01 1.00000000e+00 1.22464680e-16]
Cosine values: [ 1.00000000e+00  7.07106781e-01  6.12323400e-17 -1.00000000e+00]
Tangent values: [ 0.00000000e+00  1.00000000e+00  1.63312394e+16 -1.22464680e-16]
Note that the tangent of pi/2 is infinity because the cosine of pi/2 is zero, and division by zero is undefined.

Inverse Trigonometric Functions

In addition to basic trigonometric functions, NumPy also provides inverse trigonometric functions that can be used to calculate the angles given the values of the trigonometric ratios. These include the inverse sine (arcsine), inverse cosine (arccosine), and inverse tangent (arctangent) functions.

Inverse trigonometric functions are useful when you have the value of a trigonometric ratio and need to find the corresponding angle.

Example: Inverse Sine, Inverse Cosine, and Inverse Tangent

In the following example, we calculate the inverse sine, inverse cosine, and inverse tangent of a set of values using NumPy functions −

import numpy as np

# Define an array of values for which we want to calculate inverse trigonometric functions
values = np.array([0, 0.5, 1])

# Calculate inverse sine, inverse cosine, and inverse tangent
arcsine_values = np.arcsin(values)
arccosine_values = np.arccos(values)
arctangent_values = np.arctan(values)

print("Inverse Sine values:", arcsine_values)
print("Inverse Cosine values:", arccosine_values)
print("Inverse Tangent values:", arctangent_values)

We get the output as shown below −

Inverse Sine values: [0.         0.52359878 1.57079633]
Inverse Cosine values: [1.57079633 1.04719755 0.        ]
Inverse Tangent values: [0.         0.46364761 0.78539816]

Hyperbolic Trigonometric Functions

NumPy also includes functions to calculate hyperbolic trigonometric functions, which are analogs of the standard trigonometric functions but for a hyperbola instead of a circle. These functions include hyperbolic sine, cosine, and tangent.

The hyperbolic sine and cosine functions are similar to the regular sine and cosine functions but use the exponential function for their calculation. The hyperbolic tangent is the ratio of the hyperbolic sine to the hyperbolic cosine.

Example: Hyperbolic Sine, Cosine, and Tangent

In the following example, we calculate the hyperbolic sine, hyperbolic cosine, and hyperbolic tangent of an array of values using NumPy functions −

import numpy as np

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

# Calculate hyperbolic sine, cosine, and tangent
sinh_values = np.sinh(values)
cosh_values = np.cosh(values)
tanh_values = np.tanh(values)

print("Hyperbolic Sine values:", sinh_values)
print("Hyperbolic Cosine values:", cosh_values)
print("Hyperbolic Tangent values:", tanh_values)

After executing the above code, we get the following output −

Hyperbolic Sine values: [0.         1.17520119 3.62686041]
Hyperbolic Cosine values: [1.         1.54308063 3.76219569]
Hyperbolic Tangent values: [0.         0.76159416 0.96402758]

Trigonometric Functions with Degrees

By default, the input to trigonometric functions in NumPy is in radians. However, you may sometimes want to work with degrees instead of radians. To convert between degrees and radians, you can use the numpy.deg2rad() function and numpy.rad2deg() function.

The numpy.deg2rad() function converts an angle from degrees to radians, while the numpy.rad2deg() function converts an angle from radians to degrees. You can then use these functions to perform trigonometric calculations with angles in degrees.

Example: Using Trigonometric Functions with Degrees

In this example, we first convert degrees to radians and then calculate the sine and cosine of the resulting angles −

import numpy as np

# Define an array of angles in degrees
angles_deg = np.array([0, 45, 90, 180])

# Convert degrees to radians
angles_rad = np.deg2rad(angles_deg)

# Calculate sine and cosine
sine_values = np.sin(angles_rad)
cosine_values = np.cos(angles_rad)

print("Sine values:", sine_values)
print("Cosine values:", cosine_values)

We get the following output as shown below −

Sine values: [0.00000000e+00 7.07106781e-01 1.00000000e+00 1.22464680e-16]
Cosine values: [ 1.00000000e+00  7.07106781e-01  6.12323400e-17 -1.00000000e+00]

Applications of Trigonometric Functions

Trigonometric functions are widely used in various fields, especially in science, engineering, and computer graphics. They help model periodic behavior, oscillations, waveforms, and rotations. Some examples of applications are −

  • Signal Processing: Trigonometric functions are used to model and analyze sound waves, electromagnetic waves, and other periodic signals.
  • Physics: In physics, trigonometric functions are used to describe the behavior of waves, circular motion, and harmonic oscillators.
  • Computer Graphics: Trigonometric functions are used in 2D and 3D transformations, rotations, and rendering.

Functions List

NumPy has a standard trigonometric functions that return trigonometric ratios for angles given in radians. Following is the list of them −

Sr.No. Operation & Description
1 numpy.sin()

Trigonometric sine, element-wise.

2 numpy.cos()

Cosine element wise.

3 numpy.tan()

Compute tangent element-wise.

4 numpy.arcsin()

Inverse sine, element-wise.

5 numpy.arccos()

Trigonometric inverse cosine, element-wise.

6 numpy.arctan()

Trigonometric inverse tangent, element-wise.

7 numpy.arctan2()

Element-wise arc tangent of x1/x2 choosing the quadrant correctly.

8 numpy.hypot()

Equivalent to element-wise sqrt(x12 + x22), broadcasting scalars.

9 numpy.sinh()

Hyperbolic sine, element-wise.

10 numpy.cosh()

Hyperbolic cosine, element-wise.

11 numpy.tanh()

Compute hyperbolic tangent element-wise.

12 numpy.arcsinh()

Inverse hyperbolic sine element-wise.

13 numpy.arccosh()

Inverse hyperbolic cosine, element-wise.

14 numpy.arctanh()

Inverse hyperbolic tangent element-wise.

15 numpy.deg2rad()

Convert angles from degrees to radians.

16 numpy.rad2deg()

Convert angles from radians to degrees.

Advertisements