Numpy: does matrix-vector multiplication not skip computation when some vector elements are equal to zero? Numpy: does matrix-vector multiplication not skip computation when some vector elements are equal to zero? numpy numpy

Numpy: does matrix-vector multiplication not skip computation when some vector elements are equal to zero?


How about experimenting with a simple function like?

def dot2(A,v):    ind = np.where(v)[0]    return np.dot(A[:,ind],v[ind])In [352]: A=np.ones((100,100))In [360]: timeit v=np.zeros((100,));v[::60]=1;dot2(A,v)10000 loops, best of 3: 35.4 us per loopIn [362]: timeit v=np.zeros((100,));v[::40]=1;dot2(A,v)10000 loops, best of 3: 40.1 us per loopIn [364]: timeit v=np.zeros((100,));v[::20]=1;dot2(A,v)10000 loops, best of 3: 46.5 us per loopIn [365]: timeit v=np.zeros((100,));v[::60]=1;np.dot(A,v)10000 loops, best of 3: 29.2 us per loopIn [366]: timeit v=np.zeros((100,));v[::20]=1;np.dot(A,v)10000 loops, best of 3: 28.7 us per loop

A fully iterative Python implentation would be:

def dotit(A,v, test=False):    n,m = A.shape      res = np.zeros(n)    if test:        for i in range(n):            for j in range(m):                if v[j]:                    res[i] += A[i,j]*v[j]    else:        for i in range(n):            for j in range(m):                res[i] += A[i,j]*v[j]    return res

Obviously this won't be as fast as the compiled dot, but I expect the relative advantages of testing still apply. For further testing you could implement it in cython.

Notice that the v[j] test occurs deep in the iteration.

For a sparse v (3 out of 100 elements) testing saves time:

In [374]: timeit dotit(A,v,True)100 loops, best of 3: 3.81 ms per loopIn [375]: timeit dotit(A,v,False)10 loops, best of 3: 21.1 ms per loop

but it costs time if v is dense:

In [376]: timeit dotit(A,np.arange(100),False)10 loops, best of 3: 22.7 ms per loopIn [377]: timeit dotit(A,np.arange(100),True)10 loops, best of 3: 25.6 ms per loop


For simple arrays, Numpy doesn't perform such optimizations, but if You need, You may use sparse matrices which may improve dot product timings in that case.For more on the topic, see: http://docs.scipy.org/doc/scipy/reference/sparse.html