NumPy ufunc Trigonometric – Fast Sine, Cosine, Tangent Calculations
Introduction – Why Learn Trigonometric ufuncs in NumPy?
Trigonometric functions are the cornerstone of geometry, physics, engineering, and signal processing. NumPy’s trigonometric ufuncs let you compute sine, cosine, tangent, and their inverse functions across entire arrays — fast, vectorized, and element-wise.
These ufuncs make it easy to handle:
- Angle conversions (degrees ↔ radians)
- Waveform modeling (sine/cosine)
- Phase shifts and periodic analysis
- Inverse trig for arc-angles (asin, acos, atan)
By the end of this guide, you’ll:
- Use
sin(),cos(),tan()and their inverses - Convert between degrees and radians
- Work with multi-dimensional arrays
- Apply trigonometric modeling for real-world systems
Step 1: Compute Basic Trigonometric Functions
import numpy as np
angles_rad = np.array([0, np.pi/2, np.pi])
print("sin:", np.sin(angles_rad))
print("cos:", np.cos(angles_rad))
print("tan:", np.tan(angles_rad))
Output:
sin: [0. 1. 0.]
cos: [ 1. 0. -1.]
tan: [ 0. inf 0.]
Explanation:
- All functions are element-wise ufuncs
np.tan(np.pi/2)tends toward infinity →inf
Step 2: Convert Degrees to Radians and Back
deg = np.array([0, 90, 180])
rad = np.deg2rad(deg)
print("Radians:", rad)
deg_back = np.rad2deg(rad)
print("Back to Degrees:", deg_back)
Output:
Radians: [0. 1.57079633 3.14159265]
Back to Degrees: [ 0. 90. 180.]
Use np.deg2rad() and np.rad2deg() for clean conversions
Step 3: Inverse Trigonometric Functions
x = np.array([0, 0.5, 1])
print("arcsin:", np.arcsin(x)) # inverse of sin
print("arccos:", np.arccos(x)) # inverse of cos
print("arctan:", np.arctan(x)) # inverse of tan
Output (in radians):
arcsin: [0. 0.52359878 1.57079633]
arccos: [1.57079633 1.04719755 0. ]
arctan: [0. 0.46364761 0.78539816]
All values returned in radians
Use np.rad2deg() to convert if needed
Step 4: Work with Arrays and Matrices
matrix = np.array([[0, np.pi/2], [np.pi, 3*np.pi/2]])
print(np.sin(matrix))
Output:
[[ 0.000000e+00 1.000000e+00]
[ 1.224647e-16 -1.000000e+00]]
Trigonometric ufuncs work element-wise on any shape
Step 5: Hyperbolic Trigonometric Functions
x = np.array([0, 1, 2])
print("sinh:", np.sinh(x))
print("cosh:", np.cosh(x))
print("tanh:", np.tanh(x))
Output:
sinh: [ 0. 1.17520119 3.62686041]
cosh: [1. 1.54308063 3.76219569]
tanh: [0. 0.76159416 0.96402758]
Used in signal processing, neural networks, and calculus
Summary of Trigonometric ufuncs
| Function | Description |
|---|---|
sin(x) | Sine of x (in radians) |
cos(x) | Cosine of x |
tan(x) | Tangent of x |
arcsin(x) | Inverse sine (range: [−π/2, π/2]) |
arccos(x) | Inverse cosine |
arctan(x) | Inverse tangent |
deg2rad(x) | Degrees → Radians |
rad2deg(x) | Radians → Degrees |
sinh(x) | Hyperbolic sine |
cosh(x) | Hyperbolic cosine |
tanh(x) | Hyperbolic tangent |
Common Mistakes to Avoid
| Mistake | Fix / Explanation |
|---|---|
Passing degrees to sin() | Convert to radians using np.deg2rad() first |
| Expecting degrees as output | Use np.rad2deg() after inverse functions |
Using values >1 or <−1 in arcsin() | Will return nan, as the domain is limited |
| Forgetting radians as default | All trig functions assume input is in radians |
Summary – Recap & Next Steps
NumPy’s trigonometric ufuncs make it easy to perform fast, element-wise computations for periodic, geometric, and wave-related problems across arrays of any shape.
Key Takeaways:
- Use
np.sin(),np.cos(),np.tan()on radians - Use
np.arcsin(),np.arccos(),np.arctan()for inverse trig - Convert between degrees and radians using
deg2rad()/rad2deg() - Trig ufuncs work on vectors, matrices, and nD arrays
Real-world relevance: Used in geometry, robotics, graphics, physics, DSP, and machine learning activations
FAQs – NumPy Trigonometric ufuncs
Are trigonometric functions in degrees or radians by default?
Radians. Always use np.deg2rad() if starting with degrees.
How do I get the angle from sine or cosine values?
Use np.arcsin(), np.arccos(), or np.arctan().
Can I apply trig functions to matrices?
Yes. NumPy ufuncs apply element-wise to arrays of any dimension.
What if my input to arcsin() is out of range?
Values outside [−1, 1] return nan.
What are hyperbolic functions used for?
Used in signal processing, activation functions, and engineering curves.
Share Now :
