In [1]:
import numpy as np
from scipy import sparse
from numba import jit
In [2]:
n, m = 5, 5
A = sparse.rand(n, m, density=0.4, format='csr')
In [3]:
print A.toarray()
print A.data
print A.indices
print A.indptr
In [4]:
@jit(nopython=True)
def _csr_vecmat(num_rows, x, data, indices, indptr, out):
for i in range(num_rows):
for k in range(indptr[i], indptr[i+1]):
out[indices[k]] += x[i] * data[k]
In [5]:
def csr_vecmat(x, A):
n, m = A.shape
if len(x) != n:
raise ValueError('dimensions do not match')
y = np.zeros(m)
_csr_vecmat(n, x, A.data, A.indices, A.indptr, y)
return y
In [6]:
x = np.arange(n)
In [7]:
csr_vecmat(x, A)
Out[7]:
In [8]:
x.dot(A.toarray())
Out[8]:
In [9]:
A.T.dot(x)
Out[9]:
In [10]:
%timeit csr_vecmat(x, A)
%timeit A.T.dot(x)
%timeit x.dot(A.toarray())
In [11]:
n, m = 5, 3
A = sparse.rand(n, m, density=0.4, format='csr')
x = np.ones(n)
In [12]:
csr_vecmat(x, A)
Out[12]:
In [13]:
A.T.dot(x)
Out[13]:
In [14]:
n = 10**2
m = n
A = sparse.rand(n, m, density=0.4, format='csr')
x = np.ones(n)
%timeit csr_vecmat(x, A)
%timeit A.T.dot(x)
%timeit x.dot(A.toarray())
In [15]:
n = 10**2 * 3
m = n
A = sparse.rand(n, m, density=0.4, format='csr')
x = np.ones(n)
%timeit csr_vecmat(x, A)
%timeit A.T.dot(x)
%timeit x.dot(A.toarray())
In [16]:
n = 10**3
m = n
A = sparse.rand(n, m, density=0.4, format='csr')
x = np.ones(n)
In [17]:
%timeit csr_vecmat(x, A)
%timeit A.T.dot(x)
%timeit x.dot(A.toarray())
In [18]:
n = 10**4
m = n
A = sparse.rand(n, m, density=0.4, format='csr')
x = np.ones(n)
In [19]:
%timeit csr_vecmat(x, A)
%timeit A.T.dot(x)
%timeit x.dot(A.toarray())
In [20]:
import platform
print platform.platform()
In [21]:
import sys
print sys.version
In [22]:
print np.__version__
In [23]:
import scipy
print scipy.__version__
In [24]:
import numba
print numba.__version__
In [24]: