[SciPy-User] Sparse matrix question

Luke Bloy lbloy at seas.upenn.edu
Tue Jul 12 17:01:37 EDT 2011


Hi,

I have what I hope is a fairly simple scipy question about sparse matrices.

I have a sparse matrix (A) that i use to build a constraint matrix for 
an optimisation problem. The constraints, that concern A, are that 
A_{i,j} d_{j} - A_{j,i} d_{i} == 0. I would then find the optimal d vector.

So I'm having problems efficiently building my constraints. The first 
part is simple as it is just the nonzero elements of A but accessing A 
transpose in the same order is difficult.

The basic logic is this....

   rows=numpy.zeros(2*A.nnz, dtype=numpy.int32)
   cols=numpy.zeros(2*A.nnz, dtype=numpy.int32)
   data=numpy.zeros(2*A.nnz, dtype=numpy.float64)

   #A_{i,j} d_{j}
   numNonZeros = A.nnz
   rows[:numNonZeros] = numpy.arange(numNonZeros)
   cols[:numNonZeros] = A.col
   data[:numNonZeros] = A.data

   #-A_{j,i} d_{i}
   rows[numNonZeros:] =  numpy.arange(numNonZeros)
   cols[numNonZeros:] =  A.row
   data[numNonZeros:] = - A[A.col,A.row] ### Atranspose[A.row, A.col]

The problem is that this takes too long (> 2minutes for a 3000x3000 
matrix with 3 million nonzeros). similary code in matlab is an order of 
magnitude faster?

I've tried both csr and csc for doing the memory access of the 
transpose, (attached is the csrTest , i can send the matrix file if you 
are interested)

Does anyone have any suggestions on speeding this up?

Thanks,
Luke
-------------- next part --------------
A non-text attachment was scrubbed...
Name: testCsr.py
Type: text/x-python
Size: 1516 bytes
Desc: not available
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20110712/88cac094/attachment.py>


More information about the SciPy-User mailing list