The isneginf() function of NumPy module

Overview:

  • Negative infinity is less than any real number. Subtracting any known smallest real number will still result in negative infinity.

  • The function numpy.isneginf() checks the elements of a numpy array-like for negative infinity. If an element evaluates to negative infinity than the result is True. The result is False otherwise. When the input is a scalar the result is a single Boolean value representing True or False.

  • The numpy function isinf() returns True for both positive infinity and negative infinity (excluding Not a Number) elements of a NumPy array-like. To check whether the elements or a scalar is a real number the NumPy function isreal() can be used.

Example 1:

# Example Python program that checks numbers
# for negative infinity using numpy.isneginf()
# function
import numpy

# Check positive infinity for negative infinity
result = numpy.isneginf(numpy.inf)
print(result)

# Create negative infinity
negInf = 1 - numpy.inf 
print(negInf)

# Check negative infinity for negative infinity
result = numpy.isneginf(negInf)
print(result)

Output:

False

-inf

True

Example 2:

# Example Python program that checks each element for
# negative infinity on a NumPy ndarray
import numpy
import math

# Create an NumPy ndarray 
infArray = numpy.ndarray(shape = (2, 3), dtype = float)

# Populate the ndarray with literals initialized to 
# negative infinity, expressions evaluating to negative infinity
# and non-infinity 
infArray[0][0] = math.inf
infArray[0][1] = -math.inf
infArray[0][2] = -1/0.00000000000000000001
infArray[1][0] = -numpy.inf
infArray[1][1] = -numpy.inf + 100000000000000
infArray[1][2] = -numpy.inf - numpy.inf

print("Array contents:")
print(infArray)
print(type(infArray[0][1]))
print(type(infArray[1][0]))

print("Results of numpy.isneginf():")
negInfResults = numpy.isneginf(infArray)
print(negInfResults)

# Check complex infinity
complexInfinity = 1-(1j*numpy.inf)
print(complexInfinity)
print(numpy.isinf(complexInfinity))
print(numpy.isneginf(complexInfinity)

Output:

Array contents:

[[    inf    -inf -1.e+20]

 [   -inf    -inf    -inf]]

<class 'numpy.float64'>

<class 'numpy.float64'>

Results of numpy.isneginf():

[[False  True False]

 [ True  True  True]]

(nan-infj)

True

 

Traceback (most recent call last):

  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/numpy/lib/_ufunclike_impl.py", line 200, in isneginf

    signbit = nx.signbit(x)

              ^^^^^^^^^^^^^

.

.

 

  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/numpy/lib/_ufunclike_impl.py", line 203, in isneginf

    raise TypeError(f'This operation is not supported for {dtype} values '

TypeError: This operation is not supported for complex128 values because it would be ambiguous.

 


Copyright 2024 © pythontic.com