Mathematical Functions In Numpy

In this tutorial, we will discuss the various mathematical functions of NumPy in Python. By using these mathematical operations be will play with the arrays in Python. In mathematical functions, we have to discuss trigonometric and Exponents and Logarithms functions. These functions play a very important role in Python programming.

Mathematical Functions

We can divide the math functions of NumPy library into two major parts.

  • Trigonometric functions
  • Exponents and Logarithms functions

Trigonometric Functions of NumPy in Python

  1. np.sin(m) : This function gives the value of sine of the element containing in array m.
  2. np.cos(m) : It returns the value of cosine of the element containing in array m.
  3. np.tan(m) : it returns the value of the tangent of the value of element containing in array m.
  4. np.arcsin(m) : It returns the value of the inverse sine of the element containing in array m.
  5. np.arccos(m) : It returns the value of the inverse cosine of the element containing in array m.
  6. np.arctan(m) : It returns the value of the inverse tangent of the element containing in array m.

The code containing above function is given below:

import numpy as np
a=np.array([1,2,3])
np.sin(a)
np.cos(a)
np.tan(a)
np.arcsin(a)
np.arccos(a)
np.arctan(a)

Their respective outputs are:

array([0.84147098, 0.90929743, 0.14112001])
array([ 0.54030231, -0.41614684, -0.9899925 ])
array([ 1.55740772, -2.18503986, -0.14254654])
array([1.57079633,        nan,        nan])
array([ 0., nan, nan])
array([0.78539816, 1.10714872, 1.24904577])

Exponents and Logarithms Functions of NumPy in Python

  1. np.exp(m): It returns the exponent of the values of m.
  2.  np.exp2(m): It returns the 2 raise to the power m means 2^m.
  3. np.power(2,m): It gives the same result as np.exp2(m).
  4. np.log(m): It returns the logarithmic value of m with base e.
  5. np.log2(m): it returns the logarithmic value of m with base 2.

The code containing all the above function is given below:

import numpy as np
a=np.array([1,2,3])
np.exp(a)
np.exp2(a)
np.power(2,a)
np.log(a)
np.log2(a)

Its output is given respectively as:

array([ 2.71828183, 7.3890561 , 20.08553692])
array([2., 4., 8.])
array([2, 4, 8], dtype=int32)
array([0.        , 0.69314718, 1.09861229])
array([0. , 1. , 1.5849625])

Thanks for reading.

You can also see:

Merge Python Key Value To List

Introduction to Tkinter module in Python

Leave a Reply

Your email address will not be published. Required fields are marked *