NumPy - Mathematical Functions
NumPy - Mathematical Functions
Section titled “NumPy - Mathematical Functions”NumPy provides a rich library of mathematical functions that operate element-wise on arrays. These Universal Functions (ufuncs) include standard trigonometric operations, rounding functions, arithmetic operations, functions for complex numbers, and much more.
Trigonometric Functions
Section titled “Trigonometric Functions”NumPy includes standard trigonometric functions like np.sin(), np.cos(), and np.tan(). These functions expect the input angle(s) to be in radians. You can convert degrees to radians by multiplying with np.pi / 180.
Example: Calculating Sine, Cosine, and Tangent
Applying trigonometric functions to an array of angles (in degrees).
import numpy as np
angles_deg = np.array([0, 30, 45, 60, 90])angles_rad = angles_deg * np.pi / 180 # Convert degrees to radians
print('Angles in Degrees:', angles_deg)print('Angles in Radians:', angles_rad)print('\n')
print('Sine values:')print(np.sin(angles_rad))print('\n')
print('Cosine values:')print(np.cos(angles_rad))print('\n')
print('Tangent values:')print(np.tan(angles_rad))Output (values may have minor floating-point variations):
Angles in Degrees: [ 0 30 45 60 90]Angles in Radians: [0. 0.52359878 0.78539816 1.04719755 1.57079633]
Sine values:[0. 0.5 0.70710678 0.8660254 1. ]
Cosine values:[1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01 6.12323400e-17]
Tangent values:[0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00 1.63312394e+16]Inverse trigonometric functions (np.arcsin(), np.arccos(), np.arctan()) are also available. They return the angle in radians corresponding to the given trigonometric ratio. You can use np.degrees() to convert the result back to degrees for verification.
Example: Inverse Trigonometric Functions
Calculating inverse sine and converting back to degrees.
import numpy as np
angles_deg = np.array([0, 30, 45, 60, 90])angles_rad = np.deg2rad(angles_deg) # Use np.deg2rad for conveniencesine_values = np.sin(angles_rad)
print('Original angles (degrees):', angles_deg)print('Sine values:', sine_values)print('\n')
print('Inverse sine (result in radians):')inv_sine_rad = np.arcsin(sine_values)print(inv_sine_rad)print('\n')
print('Inverse sine converted back to degrees:')inv_sine_deg = np.degrees(inv_sine_rad)print(inv_sine_deg)print('\n')
# Similar process applies to arccos and arctancosine_values = np.cos(angles_rad)inv_cosine_deg = np.degrees(np.arccos(cosine_values))print('Inverse cosine (degrees):', inv_cosine_deg)Output (values may have minor floating-point variations):
Original angles (degrees): [ 0 30 45 60 90]Sine values: [0. 0.5 0.70710678 0.8660254 1. ]
Inverse sine (result in radians):[0. 0.52359878 0.78539816 1.04719755 1.57079633]
Inverse sine converted back to degrees:[ 0. 30. 45. 60. 90.]
Inverse cosine (degrees): [ 0. 30. 45. 60. 90.]Functions for Rounding
Section titled “Functions for Rounding”numpy.around()
Section titled “numpy.around()”Rounds array elements to a specified number of decimal places. It uses a “round half to even” strategy for values exactly halfway between two representable numbers (e.g., 2.5 rounds to 2.0, 3.5 rounds to 4.0).
numpy.around(a, decimals=0)Parameters:
| Parameter | Description |
|---|---|
| a | Input array. |
| decimals | Number of decimal places to round to (default is 0). If negative, rounds to the left of the decimal point. |
Example: Using np.around
Demonstrating rounding to different decimal places.
import numpy as np
a = np.array([1.0, 5.55, 123, 0.567, 25.532, -1.25])
print('Original array:')print(a)print('\n')
print('Rounded to 0 decimals (default):')print(np.around(a))
print('\nRounded to 1 decimal place:')print(np.around(a, decimals=1))
print('\nRounded to -1 decimal place (nearest 10):')print(np.around(a, decimals=-1))Output:
Original array:[ 1. 5.55 123. 0.567 25.532 -1.25 ]
Rounded to 0 decimals (default):[ 1. 6. 123. 1. 26. -1.]
Rounded to 1 decimal place:[ 1. 5.6 123. 0.6 25.5 -1.2]
Rounded to -1 decimal place (nearest 10):[ 0. 10. 120. 0. 30. -0.]numpy.floor()
Section titled “numpy.floor()”Returns the floor of each element, which is the largest integer less than or equal to the element.
Example: Using np.floor
Finding the largest integer not greater than each element.
import numpy as np
a = np.array([-1.7, 1.5, -0.2, 0.6, 10.0])
print('Original array:')print(a)print('\n')
print('After applying np.floor():')print(np.floor(a))Output:
Original array:[-1.7 1.5 -0.2 0.6 10. ]
After applying np.floor():[-2. 1. -1. 0. 10.]numpy.ceil()
Section titled “numpy.ceil()”Returns the ceiling of each element, which is the smallest integer greater than or equal to the element.
Example: Using np.ceil
Finding the smallest integer not less than each element.
import numpy as np
a = np.array([-1.7, 1.5, -0.2, 0.6, 10.0])
print('Original array:')print(a)print('\n')
print('After applying np.ceil():')print(np.ceil(a))Output:
Original array:[-1.7 1.5 -0.2 0.6 10. ]
After applying np.ceil():[-1. 2. -0. 1. 10.]