Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Matrix sum is 220 times slower than scipy

Ronny
2012-05-22
2013-05-20
  • Ronny
    Ronny
    2012-05-22

    The following code was used to calculate the sum of sparse matrix A:

    [i]pysparse B init:[/i]
    B = pysparse.spmatrix.ll_mat(1,N)
    val = np.ones(N)
    row = np.zeros(N)
    col = np.arange(0,N)
    B.put(val,row,col)
    [b]272 seconds[/b]
        C = pysparse.spmatrix.matrixmultiply(B,A)
        a = np.zeros(N,dtype=int)
        C.take(a,np.zeros(N,dtype=int),np.arange(0,N,dtype=int))
    [b]214 seconds[/b]
        f = pysparse.spmatrix.matrixmultiply(B,A).find()
        a = np.zeros(N, dtype=np.int32)
        for i, c in enumerate(f[2]): a[c] += f[0][i]
    88 seconds
        a = np.zeros(N, dtype=np.int32)
        for i in tree.dA.find()[2]: a[i] += 1
    [i]Scipy.sparse:[/i]
    [b]0.4 seconds[/b]
        a = np.array(scipy.sparse.csr_matrix.sum(A,0))[0]
    

    Am I doing something wrong here or is there no simple/fast way of getting the sum of A into a numpy array?

     
  • Ronny
    Ronny
    2012-05-22

    Also, there seems to be a memory leak when using ll_matrix.nnz
    (I'm not sure how to create a bug report so I write it here)