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 'indeces =', A.indices
print 'indptr =', 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]:
@jit(nopython=True)
def _csr_matvec(num_rows, data, indices, indptr, x, out):
for i in range(num_rows):
dot_prod = 0
for k in range(indptr[i], indptr[i+1]):
dot_prod += data[k] * x[indices[k]]
out[i] = dot_prod
In [6]:
@jit(nopython=True)
def _csc_vecmat(num_cols, x, data, indices, indptr, out):
for j in range(num_cols):
dot_prod = 0
for k in range(indptr[j], indptr[j+1]):
dot_prod += data[k] * x[indices[k]]
out[j] = dot_prod
In [7]:
x = np.arange(n)
In [8]:
y = np.empty(m)
A_csc = sparse.csc_matrix(A)
_csc_vecmat(m, x, A_csc.data, A_csc.indices, A_csc.indptr, y)
print y
In [9]:
x.dot(A.toarray())
Out[9]:
In [10]:
y = np.empty(m)
_csr_vecmat(n, x, A.data, A.indices, A.indptr, y)
print y
In [11]:
y = np.empty(m)
A_csc = sparse.csc_matrix(A)
%timeit _csc_vecmat(m, x, A_csc.data, A_csc.indices, A_csc.indptr, y)
y = np.empty(m)
%timeit _csr_vecmat(n, x, A.data, A.indices, A.indptr, y)
In [12]:
def csr_vecmat(x, A):
n, m = A.shape
if len(x) != n:
raise ValueError('dimensions do not match')
y = np.zeros(m)
A_csc = sparse.csc_matrix(A)
_csc_vecmat(m, x, A_csc.data, A_csc.indices, A_csc.indptr, y)
return y
In [13]:
csr_vecmat(x, A)
Out[13]:
In [14]:
A.T.dot(x)
Out[14]:
In [15]:
x.dot(A.toarray())
Out[15]:
In [16]:
%timeit csr_vecmat(x, A)
%timeit A.T.dot(x)
%timeit x.dot(A.toarray())
In [17]:
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 [18]:
n = 10**3
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]:
n = 10**4
m = n
A = sparse.rand(n, m, density=0.4, format='csr')
x = np.ones(n)
In [21]:
%timeit csr_vecmat(x, A)
%timeit A.T.dot(x)
%timeit x.dot(A.toarray())
In [22]:
def csc_vecmat(x, A):
n, m = A.shape
if len(x) != n:
raise ValueError('dimensions do not match')
y = np.zeros(m)
_csc_vecmat(m, x, A.data, A.indices, A.indptr, y)
return y
In [23]:
n = 10**3
m = n
A = sparse.rand(n, m, density=0.4, format='csr')
x = np.ones(n)
%timeit csc_vecmat(x, A)
%timeit A.T.dot(x)
%timeit x.dot(A.toarray())
In [24]:
n = 10**4
m = n
A = sparse.rand(n, m, density=0.4, format='csr')
x = np.ones(n)
%timeit csc_vecmat(x, A)
%timeit A.T.dot(x)
%timeit x.dot(A.toarray())
In [25]:
n = 10**3
m = n
A = sparse.rand(n, m, density=0.4, format='csr')
x = np.ones(n)
y = np.empty(m)
A_csc = sparse.csc_matrix(A)
%timeit _csc_vecmat(m, x, A_csc.data, A_csc.indices, A_csc.indptr, y)
y = np.empty(m)
%timeit _csr_vecmat(n, x, A.data, A.indices, A.indptr, y)
In [25]: