NumPy – Trigonometric Universal Function (ufunc)
Trigonometric functions are widely used in mathematics, physics, engineering, computer graphics, signal processing, and machine learning.
NumPy provides a powerful set of Trigonometric Universal Functions (ufuncs) that allow you to compute angles, waves, and periodic behavior efficiently across entire arrays.
These functions are vectorized, meaning they work on arrays element-by-element without loops, making them fast and scalable in NumPy.
What are Trigonometric ufuncs?
Trigonometric ufuncs are mathematical functions that relate angles and sides of triangles using sine, cosine, and tangent relationships.
They are based on the unit circle:
Where:
- x = cos(θ)
- y = sin(θ)
Why Use Trigonometric Functions?
✔ Model waves and oscillations
✔ Perform geometric calculations
✔ Process audio signals
✔ Compute rotations in graphics
✔ Solve physics problems
Import NumPy
import numpy as np1. Using np.sin()
Calculates sine of angles (in radians).
Example
import numpy as np
angles = np.array([0, np.pi/2, np.pi])
result = np.sin(angles)
print(result)Output
[0. 1. 0.]2. Using np.cos()
Calculates cosine values.
import numpy as np
angles = np.array([0, np.pi/2, np.pi])
print(np.cos(angles))Output
[ 1. 0. -1.]3. Using np.tan()
Calculates tangent values.
import numpy as np
angles = np.array([0, np.pi/4, np.pi/2])
print(np.tan(angles))Output
[0. 1. 1.63312394e+16]⚠️ Note: tan(π/2) approaches infinity.
4. Inverse Trigonometric Functions
np.arcsin()
import numpy as np
values = np.array([0, 0.5, 1])
print(np.arcsin(values))Output
[0. 0.52359878 1.57079633]np.arccos()
import numpy as np
values = np.array([1, 0.5, 0])
print(np.arccos(values))Output
[0. 1.04719755 1.57079633]np.arctan()
import numpy as np
values = np.array([0, 1, -1])
print(np.arctan(values))Output
[ 0. 0.78539816 -0.78539816]5. Degrees and Radians Conversion
np.deg2rad()
import numpy as np
degrees = np.array([0, 90, 180])
print(np.deg2rad(degrees))Output
[0. 1.57079633 3.14159265]np.rad2deg()
import numpy as np
radians = np.array([0, np.pi/2, np.pi])
print(np.rad2deg(radians))Output
[ 0. 90. 180.]6. Trigonometric Identity Example
import numpy as np
x = np.array([0, np.pi/4, np.pi/2])
result = np.sin(x)**2 + np.cos(x)**2
print(result)Output
[1. 1. 1.]7. Working with Arrays
import numpy as np
angles = np.linspace(0, 2*np.pi, 5)
print(np.sin(angles))8. Real-World Applications
📊 Signal Processing
- Audio wave analysis
- Radio frequency modulation
🎮 Game Development
- Character rotation
- Camera movement
📈 Data Science
- Periodic trend modeling
- Time-series cyclic patterns
🧠 Machine Learning
- Feature engineering
- Neural network transformations
Performance Advantage
Python Loop (Slow)
import math
result = [math.sin(x) for x in angles]NumPy (Fast)
np.sin(angles)✔ Vectorized
✔ Optimized in C
✔ Fast for large datasets
Common Trigonometric ufuncs
| Function | Description |
|---|---|
| np.sin() | Sine |
| np.cos() | Cosine |
| np.tan() | Tangent |
| np.arcsin() | Inverse sine |
| np.arccos() | Inverse cosine |
| np.arctan() | Inverse tangent |
| np.deg2rad() | Degrees → Radians |
| np.rad2deg() | Radians → Degrees |
Best Practices
- Always use radians for trig functions
- Use vectorized operations for performance
- Convert degrees when needed
- Avoid manual loops
- Combine trig with NumPy arrays for modeling
Summary
NumPy trigonometric ufuncs provide fast and efficient tools for computing angles, waves, and periodic behavior across arrays.
They are essential for:
- Mathematics
- Physics
- Engineering
- Data science
- Computer graphics
These functions are highly optimized in NumPy and form the backbone of many scientific and computational applications.
Conclusion
Trigonometric universal functions in Python make it easy to perform advanced mathematical operations on arrays with minimal code.
By mastering functions like sin(), cos(), and tan(), you can efficiently solve real-world problems involving waves, motion, and periodic systems.

%20%E2%80%93%20Complete%20Guide%20with%20Examples%20in%20Python.jpg)
0 Comments