The isinf() function is defined under the numpy library, which can be imported as import numpy as np, and we can create a multidimensional array and derive other mathematical statistics.
Numpy isinf
The np.isinf() is a numpy library function used to test element-wise for positive or negative infinity. The isinf() method returns a boolean array of the same shape as x, True where x == +/-inf, otherwise False.
Numpy isinf() function tests if an element is a positive or negative infinity. The isinf() function takes two parameters, out of which one is optional. We can pass the arrays also to check whether the items present in the array belong to a +ve infinity or -ve class. If it is not +ve or -ve infinity, the method returns False otherwise True. So, the isinf() function returns Boolean values.
Syntax
numpy.isinf(array or the scalar value, out(output array))
The second parameter is optional.
Parameters
Numpy isinf() function takes two parameters, out of which one parameter is optional.
The first parameter is an input array or the input we want to check whether it is +ve infinity or –ve infinity.
The second parameter is an n-dimensional array, which is optional. It is the output array which is placed with the result.
Return Value
The isinf() function returns a Boolean array, which results if we pass an array and Boolean value true or false if we pass a scalar value.
Example programs on isinf() method in Python
Write the program to show the working of the isinf() function in Python.
# app.py import numpy as np print("Infinity - : ", np.isinf(933), "\n") # Scalar Values print("Infinity - : ", np.isinf(444), "\n") print("Infinity - :", np.isinf(np.inf), "\n") # checking for infinity value print("Infinity - :", np.isinf(np.NINF), "\n")
Output
python3 app.py Infinity - : False Infinity - : False Infinity - : True Infinity - : True
In this example, we have seen that bypassing two scalar values in the function isinf(), we get False as it doesn’t represent any positive or negative infinity. Still, using infinite values, we’re getting True.
Let’s see another example.
Let’s create a numpy array using np arange() function.
Write a program to use isinf() function on math module values.
See the following code.
# app.py import math as m # For finite values print("Infinity:- ", m.isinf(999)) print("Infinity:- ", m.isinf(-999)) # For not a number and +ve infinity and -ve infinity values print("Infinity:- ", m.isinf(float("nan"))) print("Infinity:- ", m.isinf(float("inf"))) print("Infinity:- ", m.isinf(float("-inf")))
Output
python3 app.py Infinity:- False Infinity:- False Infinity:- False Infinity:- True Infinity:- True
In this example, we can see that we have used the Python math module, and the output values are getting different sets of True and False values according to the values passed in the function.
That’s it.