
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
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. |