How to Use Numpy i0 in Python?

Numpy Modified Bessel Function First Kind

This article will talk about the Numpy i0 function. The year was 1817. It was a fine evening when a German astronomer by the name of Friedrich Wilhelm Bessel was taking a closer look at the movement of planets. Well, what else could he have referred to rather than the most advanced source available around that time – Kepler’s equations of planetary motion! While he went deep into investigating those equations, it was only a matter of time before he, himself discovered something.

An equation that explains the motion of fluids around a cylinder. Thus was born Bessel’s equation. In the later years, it will be used to provide mathematical solutions to the problems involving,

  • The flow of heat in a solid cylinder
  • The flow of electric current in a solid cylinder
  • The motion of electromagnetic waves around a wire
  • Deformation in an elastic body
The Bessels Equation
The Bessel’s Equation

When the above differential equation is solved, it results in Bessel’s function. Saving us from the tedious task of deriving the results through each step, Python helps us to jump straight to the results through an exclusive function – the numpy.i0 (i-zero; not ‘o’). This is a modified form of Bessel’s function that is of the first kind and orders zero.

We would be exploring this very function in this article, but before that, there is something that needs to be done. Importing the numpy library. The following code can be used to serve the purpose.

import numpy as np

Thereafter, we shall explore further the numpy.i0( ) function through each of the following sections.

  • Syntax of the Numpy i0 function
  • Using Numpy i0 on N-Dimensional Arrays
  • Creating plots for Numpy i0 results

Syntax of the Numpy i0 function

The syntax of this function is pretty straightforward and is given below. Mathematically Bessel’s function of the first kind, order zero is denoted by I0.

numpy.i0(x)

where,

  • x – Scalar or N-dimensional array which is the argument for the Bessel’s function

Using Numpy i0 on N-Dimensional Arrays

Let us construct an array of dimensions 4×1 and subject it to the Numpy i0( ) function to see what happens.

ar1 = np.array([-1, 0, -7, 9])
np.i0(ar1)

Following are the results for the above code.

Results For Ar1 1 Numpy i0
Results for ‘ar1’

One can observe that the results are returned in an array of the same dimensions as that of the input array ‘ar1’. Each element in the output array is the result of the Bessel’s function when its corresponding value from the input is being processed.

The i0( ) function does not hold well when complex numbers are given as its inputs. The same is demonstrated below using an input array with complex numbers.

ar2= np.array([1.3, -2.5j])
np.i0(ar2)

The below error shall appear stating that the i0( ) function is not compatible with the complex numbers.

TypeError: i0 not supported for complex values

Creating plots for numpy.i0( ) results

A mathematical function does not come a full circle till efforts are not taken to convert them into a plot. We shall do just that in this section. For this, we need to import the pyplot from the matplotlib library using the following code.

import matplotlib.pyplot as plt

Once done, let us use the results of the ar1 as the coordinates for the plot.

ar1 = np.array([-1, 0, 2, 5])
r1 = np.i0(ar1)

Then the results shall be plotted against the x-axis in red using the below code.

x = np.arange(0, 28, 7)
plt.plot(x, r1, 'r')
Plot For Bessels Function Results  Numpy i0
Plot for Bessel’s function Results

Conclusion

Now that we have reached the end of this article, hope it has elaborated on how to use the i0( ) function from the numpy library. Here’s another article that details the usage of the positive( ) function from the numpy library in Python. There are numerous other enjoyable and equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. Audere est faucere!