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.


[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]

In [3]:
# after compression: 
print(X)


  (1, 0)	4
  (2, 0)	8
  (0, 1)	1
  (1, 1)	5
  (2, 1)	9
  (0, 2)	2
  (1, 2)	6
  (2, 2)	10
  (0, 3)	3
  (1, 3)	7
  (2, 3)	11

In [4]:
w = np.ones(4)

#  matrix vector multiplication still works after compression
y = X.dot(w)
print(y)


[  6.  22.  38.]

In [5]:
#
# dot product between i-th column of S and g
#
i = 0
g = np.ones(3)

In [6]:
print(X[:,i])


  (1, 0)	4
  (2, 0)	8

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)


[ 12.]
80

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