NumPy Math Functions

Numpy provides a wide range of mathematical functions that can be performed on arrays.

Let's explore three different types of math functions in NumPy:

  1. Trigonometric Functions
  2. Arithmetic Functions
  3. Rounding Functions

1. Trigonometric Functions

NumPy provides a set of standard trigonometric functions to calculate the trigonometric ratios (sine, cosine, tangent, etc.)

Here's a list of commonly used trigonometric functions in NumPy.

Trigonometric Function Computes (in radians)
sin() the sine of an angle
cos() cosine of an angle
tan() tangent of an angle
arcsin() the inverse sine
arccos() the inverse cosine
arctan() the inverse tangent
degrees() converts an angle in radians to degrees
radians() converts an angle in degrees to radians

Let's see the examples.

import numpy as np

# array of angles in radians
angles = np.array([0, 1, 2])
print("Angles:", angles)

# compute the sine of the angles
sine_values = np.sin(angles)
print("Sine values:", sine_values)

# compute the inverse sine of the angles
inverse_sine = np.arcsin(angles)
print("Inverse Sine values:", inverse_sine)

Output

Angles: [0 1 2]
Sine values: [0.         0.84147098 0.90929743]
Inverse Sine values: [0.         1.57079633        nan]

In this example, the sin() and arcsin() functions calculate the sine and inverse sine values, respectively, for each element in the angles array.

The resulting values are in radians.

Now let's see the examples for degrees() and radians().

import numpy as np

# define an angle in radians
angle =  1.57079633
print("Initial angle in radian:", angle)

# convert the angle to degrees
angle_degree = np.degrees(angle)
print("Angle in degrees:", angle_degree)

# convert the angle back to radians
angle_radian = np.radians(angle_degree)
print("Angle in radians (after conversion):", angle_radian)

Output

Initial angle in radian: 1.57079633
Angle in degrees: 90.0000001836389
Angle in radians (after conversion): 1.57079633

Here, we first initialized an angle in radians. Then we converted it to degrees using the degrees() function.

Similarly, we used radians() to convert the degrees back to radians.


2. Arithmetic Functions

NumPy provides a wide range of arithmetic functions to perform on arrays.

Here's a list of various arithmetic functions along with their associated operators:

Operation Arithmetic Function Operator
Addition add() +
Subtraction subtract() -
Multiplication multiply() *
Division divide() /
Exponentiation power() **
Modulus mod() %

Let's see the examples.

import numpy as np

first_array = np.array([1, 3, 5, 7])
second_array = np.array([2, 4, 6, 8])

# using the add() function
result2 = np.add(first_array, second_array)
print("Using the add() function:",result2) 

Output

Using the add() function: [ 3  7 11 15]

In the above example, first we created two arrays named: first_array and second_array. Then, we used the add() function to perform element-wise addition respectively.

To learn more about the Arithmetic Functions, visit NumPy Arithmetic Array Operations.


3. Rounding Functions

We use rounding functions to round the values in an array to a specified number of decimal places.

Here's a list of commonly used NumPy rounding functions:

Rounding Functions Functions
round() returns the value rounded to the desired precision
floor() returns the values of array down to the nearest integer that is less than each element
ceil() returns the values of array up to the nearest integer that is greater than each element.

Let's see an example.

import numpy as np

numbers = np.array([1.23456, 2.34567, 3.45678, 4.56789])

# round the array to two decimal places
rounded_array = np.round(numbers, 2)

print(rounded_array)

# Output: [1.23 2.35 3.46 4.57]

Here, we used the round() function to round the values of array numbers. Notice the line,

np.round(numbers, 2)

We've given two arguments to the round() function.

  • numbers - the array whose values are to be rounded
  • 2 - denotes the number of decimal places to which the array is rounded

Now, let's see the example of other NumPy rounding functions.

import numpy as np

array1 = np.array([1.23456, 2.34567, 3.45678, 4.56789])

print("Array after floor():", np.floor(array1))

print("Array after ceil():", np.ceil(array1))

Output

Array after floor(): [1. 2. 3. 4.]
Array after ceil(): [2. 3. 4. 5.]

In the above example, the floor() function rounds the values of array1 down to the nearest integer that is less than or equal to each element.

Whereas, the ceil() function rounds the values of array1 up to the nearest integer that is greater than or equal to each element.