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.

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().

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.

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.

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.

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.