
- 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 ufunc
Trigonometric Universal Functions (ufunc)
Trigonometric universal functions (ufuncs) in NumPy are functions that perform trigonometric operations on each element of an array. These functions can calculate various trigonometric values such as sine, cosine, tangent, and their inverses for each element in the input array.
These functions operate element-wise on arrays and are optimized for performance, making them much faster than using Python loops.
NumPy Sine Function
The numpy.sin() function is used to calculate the sine of each element in an array. The input values are assumed to be in radians.
The sine of an angle in a right-angled triangle is the ratio of the length of the opposite side to the hypotenuse. It is denoted as sin().
Example
In the following example, we use the numpy.sin() function to calculate the sine of each element in an array −
import numpy as np # Define an array of angles in radians angles = np.array([0, np.pi/2, np.pi, 3*np.pi/2]) # Calculate the sine of each angle sine_values = np.sin(angles) print("Sine values:", sine_values)
Following is the output obtained −
Sine values: [ 0.0000000e+00 1.0000000e+00 1.2246468e-16 -1.0000000e+00]
NumPy Cosine Function
The numpy.cos() function is used to calculate the cosine of each element in an array.
The cosine of an angle in a right-angled triangle is the ratio of the length of the adjacent side to the hypotenuse. It is denoted as cos().
Example
In the following example, we use the numpy.cos() function to calculate the cosine of each element in an array −
import numpy as np # Define an array of angles in radians angles = np.array([0, np.pi/2, np.pi, 3*np.pi/2]) # Calculate the cosine of each angle cosine_values = np.cos(angles) print("Cosine values:", cosine_values)
This will produce the following result −
Cosine values: [ 1.0000000e+00 6.1232340e-17 -1.0000000e+00 -1.8369702e-16]
NumPy Tangent Function
The numpy.tan() function is used to calculate the tangent of each element in an array.
The tangent of an angle in a right-angled triangle is the ratio of the length of the opposite side to the adjacent side. It is denoted as tan().
Example
In the following example, we use the numpy.tan() function to calculate the tangent of each element in an array −
import numpy as np # Define an array of angles in radians angles = np.array([0, np.pi/4, np.pi/2, np.pi]) # Calculate the tangent of each angle tangent_values = np.tan(angles) print("Tangent values:", tangent_values)
The result produced is as follows −
Tangent values: [ 0.00000000e+00 1.00000000e+00 1.63312394e+16 -1.22464680e-16]
NumPy also provides functions for calculating the inverse trigonometric functions (arcsine, arccosine, and arctangent) of array elements. These functions return the angle in radians for a given trigonometric value.
NumPy Arcsine Function
The numpy.arcsin() function is used to calculate the inverse sine of each element in an array, returning the angle in radians.
The arcsine is the inverse function of sine, which returns the angle whose sine is a given number. It is denoted as arcsin(x) or sin(x).
Example
In this example, we use the numpy.arcsin() function to calculate the inverse sine of each element in an array −
import numpy as np # Define an array of angles in radians angles = np.array([0, np.pi/4, np.pi/2, np.pi]) # Calculate the Inverse Sine of each angle inverse_sine_values = np.arcsin(angles) print("Inverse Sine values:", inverse_sine_values)
We get the output as shown below −
Inverse Sine values: [0. 0.90333911 nan nan]
NumPy Arccosine Function
The numpy.arccos() function is used to calculate the inverse cosine of each element in an array, returning the angle in radians.
The arccosine is the inverse function of cosine, which returns the angle whose cosine is a given number. It is denoted as arccos(x) or cos(x).
Example
In this example, we use the numpy.arccos() function to calculate the inverse cosine of each element in an array −
import numpy as np # Define an array of angles in radians angles = np.array([0, np.pi/4, np.pi/2, np.pi]) # Calculate the Inverse Cosine of each angle inverse_cosine_values = np.arccos(angles) print("Inverse Cosine values:", inverse_cosine_values)
After executing the above code, we get the following output −
Inverse Cosine values: [1.57079633 0.66745722 nan nan]
NumPy Arctangent Function
The numpy.arctan() function is used to calculate the inverse tangent of each element in an array, returning the angle in radians.
The arctangent is the inverse function of tangent, which returns the angle whose tangent is a given number. It is denoted as arctan(x) or tan(x).
Example
In the example below, we use the numpy.arctan() function to calculate the inverse tangent of each element in an array −
import numpy as np # Define an array of angles in radians angles = np.array([0, np.pi/4, np.pi/2, np.pi]) # Calculate the Inverse Tangent of each angle inverse_tangent_values = np.arctan(angles) print("Inverse Tangent values:", inverse_tangent_values)
After executing the above code, we get the following output −
Inverse Tangent values: [0. 0.66577375 1.00388482 1.26262726]
NumPy Hyperbolic Functions
Hyperbolic functions are mathematical functions similar to trigonometric functions but based on hyperbolas instead of circles.
NumPy also provides functions for calculating the hyperbolic sine (sinh), hyperbolic cosine (cosh), and hyperbolic tangent (tanh), along with their inverses for array elements. These functions are analogous to the trigonometric functions but are applied to hyperbolic angles.
Example
In the following example, we use the numpy.sinh(), numpy.cosh(), and numpy.tanh() functions to calculate the hyperbolic values of elements in an array −
import numpy as np # Define an array of values values = np.array([0, 1, 2]) # Calculate the hyperbolic values 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)
This will produce the following result −
Hyperbolic sine values: [0. 1.17520119 3.62686041] Hyperbolic cosine values: [1. 1.54308063 3.76219569] Hyperbolic tangent values: [0. 0.76159416 0.96402758]