Arithmetic & Trigonometric Functions in NumPy
NumPy provides a rich collection of built-in arithmetic and trigonometric functions that operate efficiently on arrays. These functions are element-wise and are designed to perform mathematical operations with high performance and ease.
Whether you're performing simple operations like addition and subtraction or advanced trigonometric transformations, NumPy functions are optimized for speed and vectorization.
- Arithmetic Functions: Perform operations like add(), subtract(), multiply(), divide(), and more.
- Trigonometric Functions: Work with angles using functions such as sin(), cos(), tan(), and their inverses.
These functions are essential for tasks in data analysis, signal processing, simulations, and scientific computing.
Arithmetic Functions in NumPy
NumPy provides a set of vectorized arithmetic functions that perform element-wise operations on arrays. These include addition, subtraction, multiplication, division, and more.
These functions are optimized for speed and can be used on arrays of any shape — as long as they are compatible for broadcasting.
import numpy as np
a = np.array([10, 20, 30])
b = np.array([2, 4, 5])
# Basic arithmetic
print("Add:", np.add(a, b)) # [12 24 35]
print("Subtract:", np.subtract(a, b)) # [ 8 16 25]
print("Multiply:", np.multiply(a, b)) # [ 20 80 150]
print("Divide:", np.divide(a, b)) # [5. 5. 6.]
print("Floor Divide:", np.floor_divide(a, b)) # [5 5 6]
print("Modulo:", np.mod(a, b)) # [0 0 0]
print("Remainder:", np.remainder(a, b)) # [0 0 0]
print("Power:", np.power(a, b)) # [100 160000 24300000]
import numpy as np
a = np.array([10, 20, 30])
b = np.array([2, 4, 5])
# Basic arithmetic
print("Add:", np.add(a, b)) # [12 24 35]
print("Subtract:", np.subtract(a, b)) # [ 8 16 25]
print("Multiply:", np.multiply(a, b)) # [ 20 80 150]
print("Divide:", np.divide(a, b)) # [5. 5. 6.]
print("Floor Divide:", np.floor_divide(a, b)) # [5 5 6]
print("Modulo:", np.mod(a, b)) # [0 0 0]
print("Remainder:", np.remainder(a, b)) # [0 0 0]
print("Power:", np.power(a, b)) # [100 160000 24300000]
How It Works:
- np.add(a, b): Adds elements of a and b element-wise.
- np.subtract(a, b): Subtracts b from a.
- np.multiply(a, b): Multiplies corresponding elements.
- np.divide(a, b): Performs element-wise division (returns float).
- np.floor_divide(a, b): Performs integer division, discards remainder.
- np.mod(a, b): Returns the remainder from division.
- np.remainder(a, b): Similar to mod; differs in negative cases.
- np.power(a, b): Raises a to the power of b element-wise.
Output
Add: [12 24 35]
Subtract: [ 8 16 25]
Multiply: [ 20 80 150]
Divide: [5. 5. 6.]
Floor Divide: [5 5 6]
Modulo: [0 0 0]
Remainder: [0 0 0]
Power: [ 100 160000 24300000]
Add: [12 24 35]
Subtract: [ 8 16 25]
Multiply: [ 20 80 150]
Divide: [5. 5. 6.]
Floor Divide: [5 5 6]
Modulo: [0 0 0]
Remainder: [0 0 0]
Power: [ 100 160000 24300000]
You can also use Python's arithmetic operators directly on NumPy arrays, thanks to operator overloading:
print(a + b) # [12 24 35]
print(a - b) # [ 8 16 25]
print(a * b) # [ 20 80 150]
print(a / b) # [5. 5. 6.]
print(a // b) # [5 5 6]
print(a % b) # [0 0 0]
print(a ** b) # [ 100 160000 24300000]
print(a + b) # [12 24 35]
print(a - b) # [ 8 16 25]
print(a * b) # [ 20 80 150]
print(a / b) # [5. 5. 6.]
print(a // b) # [5 5 6]
print(a % b) # [0 0 0]
print(a ** b) # [ 100 160000 24300000]
💡 Tip: NumPy arithmetic functions are great for readability and consistency. They also offer better support for data types and error handling than raw Python operators.
Trigonometric Functions in NumPy
NumPy includes a wide range of trigonometric functions for computing angles and performing element-wise operations on arrays. These functions work with angles in radians by default.
You can calculate standard trigonometric values such as sine, cosine, and tangent, as well as inverse functions and conversions between degrees and radians.
import numpy as np
# Angles in radians
angles = np.array([0, np.pi/2, np.pi])
# Trigonometric functions
print("Sine:", np.sin(angles)) # [0. 1. 0.]
print("Cosine:", np.cos(angles)) # [1. 0. -1.]
print("Tangent:", np.tan(angles)) # [0. ~inf 0.]
# Inverse trigonometric functions
values = np.array([0, 0.5, 1.0])
print("Arcsin:", np.arcsin(values)) # [0. 0.523... 1.57...]
print("Arccos:", np.arccos(values)) # [1.57... 1.047... 0.]
print("Arctan:", np.arctan(values)) # [0. 0.463... 0.785...]
# Conversion between degrees and radians
deg = np.array([0, 90, 180])
print("Radians:", np.radians(deg)) # [0. 1.57 3.14]
print("Degrees:", np.degrees(angles))# [0. 90. 180.]
import numpy as np
# Angles in radians
angles = np.array([0, np.pi/2, np.pi])
# Trigonometric functions
print("Sine:", np.sin(angles)) # [0. 1. 0.]
print("Cosine:", np.cos(angles)) # [1. 0. -1.]
print("Tangent:", np.tan(angles)) # [0. ~inf 0.]
# Inverse trigonometric functions
values = np.array([0, 0.5, 1.0])
print("Arcsin:", np.arcsin(values)) # [0. 0.523... 1.57...]
print("Arccos:", np.arccos(values)) # [1.57... 1.047... 0.]
print("Arctan:", np.arctan(values)) # [0. 0.463... 0.785...]
# Conversion between degrees and radians
deg = np.array([0, 90, 180])
print("Radians:", np.radians(deg)) # [0. 1.57 3.14]
print("Degrees:", np.degrees(angles))# [0. 90. 180.]
How It Works:
- np.sin(), np.cos(), np.tan(): Compute trigonometric functions using radian inputs.
- np.arcsin(), np.arccos(), np.arctan(): Return inverse trigonometric values in radians.
- np.radians(): Converts degrees to radians.
- np.degrees(): Converts radians to degrees.
Output
Sine: [0.000000e+00 1.000000e+00 1.224647e-16]
Cosine: [ 1.000000e+00 6.123234e-17 -1.000000e+00]
Tangent: [ 0.00000000e+00 1.63312394e+16 -1.22464680e-16]
Arcsin: [0. 0.52359878 1.57079633]
Arccos: [1.57079633 1.04719755 0. ]
Arctan: [0. 0.46364761 0.78539816]
Radians: [0. 1.57079633 3.14159265]
Degrees: [ 0. 90. 180.]
Sine: [0.000000e+00 1.000000e+00 1.224647e-16]
Cosine: [ 1.000000e+00 6.123234e-17 -1.000000e+00]
Tangent: [ 0.00000000e+00 1.63312394e+16 -1.22464680e-16]
Arcsin: [0. 0.52359878 1.57079633]
Arccos: [1.57079633 1.04719755 0. ]
Arctan: [0. 0.46364761 0.78539816]
Radians: [0. 1.57079633 3.14159265]
Degrees: [ 0. 90. 180.]
💡 Tip: Always ensure your angle units match the function you're using. Most NumPy trigonometric functions expect radians — use np.radians() and np.degrees() to convert as needed.
Frequently Asked Questions
What are arithmetic functions in NumPy?
What are arithmetic functions in NumPy?
Arithmetic functions in NumPy like add(), subtract(), multiply(), and divide() allow you to perform element-wise operations on arrays quickly and efficiently.
Can I use regular operators like + or * with NumPy arrays?
Can I use regular operators like + or * with NumPy arrays?
Yes, NumPy supports operator overloading, so you can use +, -, *, /, and ** directly on arrays for element-wise operations.
What is the difference between divide and floor_divide in NumPy?
What is the difference between divide and floor_divide in NumPy?
np.divide returns a float result of division, while np.floor_divide performs integer division by discarding the remainder.
Do NumPy trigonometric functions use degrees or radians?
Do NumPy trigonometric functions use degrees or radians?
By default, NumPy trigonometric functions such as sin(), cos(), and tan() use angles in radians. Use np.radians() or np.degrees() to convert between units.
Can I calculate inverse trigonometric functions using NumPy?
Can I calculate inverse trigonometric functions using NumPy?
Yes, NumPy provides arcsin(), arccos(), and arctan() to compute inverse trigonometric values from input numbers.
What's Next?
Next, we’ll explore Hyperbolic and Logarithmic Functions in NumPy — powerful mathematical tools used in fields such as calculus, machine learning, and signal processing.