The isposinf() function of NumPy module

Overview:

  • Positive infinity is greater than any real number.

  • The numpy.isposinf() checks each element of a NumPy array-like whether it is positive infinity. If an element is positive infinity then the result is True. The result is False otherwise. The results are returned in an ndarray containing Boolean values. If the input to the function is a scalar then a single Boolean value is returned.

  • To check for negative infinity, the function isneginf() can be used.

Example:

In the example below the array element at [1][2] is an expression subtracting infinity from infinity which evaluates to "Not a Number" (nan). In mathematics, the operation of subtracting infinity from infinity is undefined as infinity is not a finite number. The expression -numpy.inf-(-numpy.inf) also results in nan.

# Example Python program that tests an ndarray of numbers 
# for positive infinity using numpy.isposinf().
import numpy
import math

infArray = numpy.ndarray(shape = (2, 3), dtype = float)

infArray[0][0] = 1/0.00000000000000000001
infArray[0][1] = numpy.inf + 100000000000000
infArray[0][2] = -math.inf
infArray[1][0] = math.inf 
infArray[1][1] = numpy.inf
infArray[1][2] = numpy.inf - numpy.inf

print("Elements of the array:")
print(infArray)

print("Results of the numpy.isposinf():")
results = numpy.isposinf(infArray)
print(results)

Output:

Elements of the array:

[[1.e+20    inf   -inf]

 [   inf    inf    nan]]

Results of the numpy.isposinf():

[[False  True False]

 [ True  True False]]

 


Copyright 2024 © pythontic.com