[SciPy-User] 0 * inf = 0 for some sparse matrix operations

Lutz Maibaum lutz.maibaum@gmail....
Fri Aug 26 20:39:54 CDT 2011


most of the time, trying to multiply 0 by infinity results in NaNs. However, in some sparse matrix operations such as matrix multiplication, such terms seem to be silently ignored (or interpreted as zeros). Is this the intended behavior? Is there a way to perform matrix multiplications such that 0*inf = NaN?

Any help would be much appreciated. Best wishes,


In [2]: import numpy as np

In [3]: from scipy.sparse import csr_matrix

In [4]: 0 * np.inf
Out[4]: nan

In [5]: np.array([0]) * np.array([np.inf])
/opt/local/bin/ipython-2.6:1: RuntimeWarning: invalid value encountered in multiply
Out[5]: array([ nan])

In [6]: np.array([0]).dot(np.array([np.inf]))
Out[6]: nan

In [7]: (csr_matrix([0]) * csr_matrix([np.inf])).toarray()
Out[7]: array([[ 0.]])

In [8]: (csr_matrix([0]).dot(csr_matrix([np.inf]))).toarray()
Out[8]: array([[ 0.]])

In [9]: (csr_matrix([0]).multiply(csr_matrix([np.inf]))).toarray()
Out[9]: array([[ nan]])

More information about the SciPy-User mailing list