In [1]:
import scipy.sparse as sp
import numpy as np
####
# This is a quick walkthrough to help you understand the operations in scipy.sparse
####
In [2]:
# construct a sparse array, here we simply construct it from dense array
A = np.arange(12).reshape(3,4)
print(A)
X = sp.csc_matrix(A) # CSC is compressed something.
In [3]:
# after compression:
print(X)
In [4]:
w = np.ones(4)
# matrix vector multiplication still works after compression
y = X.dot(w)
print(y)
In [5]:
#
# dot product between i-th column of S and g
#
i = 0
g = np.ones(3)
In [6]:
print(X[:,i])
In [7]:
# r1 = dot(X[:,i], g), because X takes matrix syntax, we need to do it in this way
r1 = X[:,i].T.dot(g)
print(r1)
#
# This is how you can get dot(X[:,i], X[:,i]) in csc_matix
#
r2 = X[:,i].T.dot(X[:,i])[0,0]
print(r2)
In [8]:
#-------------------------------------------
# This is an alternative way to hack into data structure of csc_matrix,
# the materials before this should be sufficient for your implemenation
# see http://en.wikipedia.org/wiki/Sparse_matrix#Compressed_sparse_column_.28CSC_or_CCS.29