def __mul__(self, other): """ Multiply a sparse matrix by another sparse matrix >>> L1 = _PysparseMatrixFromShape(rows=3, cols=3) >>> L1.put([3.,10.,numerix.pi,2.5], [0,0,1,2], [2,1,1,0]) >>> L2 = _PysparseIdentityMatrix(size=3) >>> L2.put([4.38,12357.2,1.1], [2,1,0], [1,0,2]) >>> tmp = numerix.array(((1.23572000e+05, 2.31400000e+01, 3.00000000e+00), ... (3.88212887e+04, 3.14159265e+00, 0.00000000e+00), ... (2.50000000e+00, 0.00000000e+00, 2.75000000e+00))) >>> numerix.allclose((L1 * L2).numpyArray, tmp) 1 or a sparse matrix by a vector >>> tmp = numerix.array((29., 6.28318531, 2.5)) >>> numerix.allclose(L1 * numerix.array((1,2,3),'d'), tmp) 1 or a vector by a sparse matrix >>> tmp = numerix.array((7.5, 16.28318531, 3.)) >>> numerix.allclose(numerix.array((1,2,3),'d') * L1, tmp) ## The multiplication is broken. Numpy is calling __rmul__ for every element instead of with the whole array. 1 """ N = self.matrix.shape[1] if isinstance(other, _PysparseMatrix): return _PysparseMatrix( matrix=spmatrix.matrixmultiply(self.matrix, other.matrix)) else: shape = numerix.shape(other) if shape == (): L = spmatrix.ll_mat(N, N, N) L.put(other * numerix.ones(N, 'l')) return _PysparseMatrix( matrix=spmatrix.matrixmultiply(self.matrix, L)) elif shape == (N, ): y = numerix.empty((self.matrix.shape[0], )) self.matrix.matvec(other, y) return y else: raise TypeError
def jac(self, x, *args, **kwargs): """ Evaluate sparse Jacobian of constraints at x. Returns a sparse matrix in format self.mformat (0 = compressed sparse row, 1 = linked list). The constraints appear in the following order: 1. equalities 2. lower bound only 3. upper bound only 4. range constraints. """ store_zeros = kwargs.get('store_zeros', False) store_zeros = 1 if store_zeros else 0 if len(args) > 0: if type(args[0]).__name__ == 'll_mat': J = self.model.eval_J(x, self.mformat, store_zeros, args[0]) else: return None else: J = self.model.eval_J(x, self.mformat, store_zeros) self.Jeval += 1 # Warning!! This next line doesn't work for the coordinate format option. if self.scale_con is not None: J = spmatrix.matrixmultiply(self.scale_con_diag, J) return J #[self.permC,:]
def __mul__(self, other): if isinstance(other, _PysparseMeshMatrix): return _PysparseMeshMatrix(mesh=self.mesh, matrix=spmatrix.matrixmultiply( self.matrix, other.matrix)) else: return _PysparseMatrixFromShape.__mul__(self, other)
def __mul__(self, other): """ Multiply a sparse matrix by another sparse matrix >>> L1 = _PysparseMatrix(size = 3) >>> L1.put([3.,10.,numerix.pi,2.5], [0,0,1,2], [2,1,1,0]) >>> L2 = _PysparseIdentityMatrix(size = 3) >>> L2.put([4.38,12357.2,1.1], [2,1,0], [1,0,2]) >>> tmp = numerix.array(((1.23572000e+05, 2.31400000e+01, 3.00000000e+00), ... (3.88212887e+04, 3.14159265e+00, 0.00000000e+00), ... (2.50000000e+00, 0.00000000e+00, 2.75000000e+00))) >>> numerix.allclose((L1 * L2).getNumpyArray(), tmp) 1 or a sparse matrix by a vector >>> tmp = numerix.array((29., 6.28318531, 2.5)) >>> numerix.allclose(L1 * numerix.array((1,2,3),'d'), tmp) 1 or a vector by a sparse matrix >>> tmp = numerix.array((7.5, 16.28318531, 3.)) >>> numerix.allclose(numerix.array((1,2,3),'d') * L1, tmp) ## The multiplication is broken. Numpy is calling __rmul__ for every element instead of with the whole array. 1 """ N = self.matrix.shape[0] if isinstance(other, _PysparseMatrix): return _PysparseMatrix(matrix = spmatrix.matrixmultiply(self.matrix, other._getMatrix())) else: shape = numerix.shape(other) if shape == (): L = spmatrix.ll_mat(N, N, N) L.put(other * numerix.ones(N)) return _PysparseMatrix(matrix = spmatrix.matrixmultiply(self.matrix, L)) elif shape == (N,): y = other.copy() self.matrix.matvec(other, y) return y else: raise TypeError
def outDegreeSequence(self): """ Return a vector of the (out)degree for each vertex. """ A = self.nativeAdjacencyMatrix() j = spmatrix.ll_mat(self.vList.getNumVertices(), 1) j[:, 0] = 1 degrees = spmatrix.matrixmultiply(A, j) degrees = PysparseMatrix(matrix=degrees) degrees = numpy.array(degrees.getNumpyArray().ravel(), numpy.int) return degrees
def __mul__(self, other): """ Multiply a sparse matrix by another sparse matrix >>> L1 = PysparseMatrix(size = 3) >>> L1.put([3.,10.,numpy.pi,2.5], [0,0,1,2], [2,1,1,0]) >>> L2 = PysparseMatrix(size = 3) >>> L2.put(numpy.ones(3), numpy.arange(3), numpy.arange(3)) >>> L2.put([4.38,12357.2,1.1], [2,1,0], [1,0,2]) >>> tmp = numpy.array(((1.23572000e+05, 2.31400000e+01, 3.00000000e+00), ... (3.88212887e+04, 3.14159265e+00, 0.00000000e+00), ... (2.50000000e+00, 0.00000000e+00, 2.75000000e+00))) >>> numpy.allclose((L1 * L2).getNumpyArray(), tmp) 1 or a sparse matrix by a vector >>> tmp = numpy.array((29., 6.28318531, 2.5)) >>> numpy.allclose(L1 * numpy.array((1,2,3),'d'), tmp) 1 or a vector by a sparse matrix >>> tmp = numpy.array((7.5, 16.28318531, 3.)) >>> numpy.allclose(numpy.array((1,2,3),'d') * L1, tmp) ## The multiplication is broken. Numpy is calling __rmul__ for every element instead of with the whole array. 1 """ M, N = self.getShape() if isinstance(other, PysparseMatrix): if N != other.getShape()[0]: raise TypeError, 'Matrices dimensions do not match for product' p = spmatrix.matrixmultiply(self.matrix, other.getMatrix()) return PysparseMatrix(matrix=p) else: shape = numpy.shape(other) if shape == (): # other is a scalar p = self.matrix.copy() p.scale(other) return PysparseMatrix(matrix=p) elif shape == (N,): y = numpy.empty(M) self.matrix.matvec(other, y) return y else: raise TypeError, 'Cannot multiply objects'
def testRandomMat(self): eps = 2.2204460492503131E-16 n = 30; m = 60; k = 30 for i in range(100): A = spmatrix_util.ll_mat_rand(n, k, 0.9) B = spmatrix_util.ll_mat_rand(k, m, 0.4) C = spmatrix.matrixmultiply(A, B) t = numpy.zeros(k, 'd') y1 = numpy.zeros(n, 'd') y2 = numpy.zeros(n, 'd') for s in range(10): x = RandomArray.random((m, )) C.matvec(x, y1) B.matvec(x, t) A.matvec(t, y2) self.failUnless(math.sqrt(numpy.dot(y1 - y2, y1 - y2)) < eps * n*m*k)
def normalizeA(self): ''' Normalize matrix A on rows ''' # compute sum of rows in sum vector v = np.ones(self.n) sum = np.zeros(self.n) self.A.matvec(v, sum) # sum = A*v D = spmatrix.ll_mat(self.n, self.n) for i in range(self.n): if sum[i] != 0: D[i,i] = 1.0/sum[i] else: D[i,i] = 1 self.A = spmatrix.matrixmultiply(self.A, D)
def A(self, *args, **kwargs): """ Evaluate sparse Jacobian of the linear part of the constraints. Useful to obtain constraint matrix when problem is a linear programming problem. """ store_zeros = kwargs.get('store_zeros', False) store_zeros = 1 if store_zeros else 0 if len(args) == 1: if type(args[0]).__name__ == 'll_mat': Amat = self.model.eval_A(store_zeros, args[0]) else: return None else: Amat = self.model.eval_A(store_zeros) if self.scale_con is not None: Amat = spmatrix.matrixmultiply(Amat, self.scale_con_diag) return Amat
def A(self, *args, **kwargs): """ Evaluate sparse Jacobian of the linear part of the constraints. Useful to obtain constraint matrix when problem is a linear programming problem. """ store_zeros = kwargs.get('store_zeros', False) store_zeros = 1 if store_zeros else 0 if len(args) == 1: if type(args[0]).__name__ == 'll_mat': Amat = self.model.eval_A(store_zeros,args[0]) else: return None else: Amat = self.model.eval_A(store_zeros) if self.scale_con is not None: Amat = spmatrix.matrixmultiply(Amat, self.scale_con_diag) return Amat
def spmatrixmul(matrix_a, matrix_b): """ Sparse Matrix Multiplication using pysparse matrix Objective: ---------- To multiply two sparse matrices - relatively dense Reason: ------- Scipy.sparse unfortunately has matrix indices with datatype int32. While pysparse is more robust and more efficient. Process: -------- It saves the scipy matrices to disk in the standard matrix market format to the disk. Then reads it to a pysparse format and uses Pysparse's inbuilt matrixmultipy operation. The result is converted back to a scipy csr matrix. This function takes two scipy matrices as input. """ sp_matrix_a = spmatrix.ll_mat(matrix_a.shape[0], matrix_a.shape[1]) sp_matrix_b = spmatrix.ll_mat(matrix_b.shape[0], matrix_b.shape[1]) # read it to form a pysparse spmatrix. sp_matrix_a.update_add_at(matrix_a.tocoo().data, matrix_a.tocoo().row, matrix_a.tocoo().col) sp_matrix_b.update_add_at(matrix_b.tocoo().data, matrix_b.tocoo().row, matrix_b.tocoo().col) # multiply the matrices. sp_result = spmatrix.matrixmultiply(sp_matrix_a, sp_matrix_b) #conversion to scipy sparse matrix data, row, col = sp_result.find() result = ss.csr_matrix((data, (row, col)), shape=sp_result.shape) #deleting files and refreshing memory del sp_result, sp_matrix_a, sp_matrix_b, matrix_a, matrix_b return result
T[4:8,1:3] = As[4:8,1:3] printMatrix(T) print 'this should raise execptions...\n' try: T[6:9,6:9] = A[6:9,6:9] except: traceback.print_exc() try: T[5:9, 4:10] = A[5:9, 4:10] except: traceback.print_exc() print 'Matrix multiplications' printMatrix(spmatrix.matrixmultiply(I, A)) printMatrix(spmatrix.matrixmultiply(Is, A)) printMatrix(spmatrix.matrixmultiply(O, O)) printMatrix(spmatrix.matrixmultiply(Os, O)) print 'Dot product' printMatrix(spmatrix.dot(I, A)) print 'Matrix export' A[:4,:4].export_mtx('A.mtx', 3) As[:4,:4].export_mtx('As.mtx', 3) print open('A.mtx').read() print open('As.mtx').read()
import time from pysparse import spmatrix n = 1000000 # create 2 nxn tridiag matrices A = spmatrix.ll_mat(n, n) B = spmatrix.ll_mat(n, n) for i in xrange(n): A[i, i] = i B[i, i] = i if i > 0: A[i, i - 1] = 1 B[i, i - 1] = 1 if i < n - 1: A[i, i + 1] = 1 B[i, i - 1] = 1 t1 = time.clock() C = spmatrix.matrixmultiply(A, B) t_mult = time.clock() - t1 print 'time for multiplying %dx%d matrices: %.2f sec' % (n, n, t_mult) print C[:10, :10]
def __mul__(self, other): if isinstance(other, _PysparseMeshMatrix): return _PysparseMeshMatrix(mesh=self.mesh, matrix=spmatrix.matrixmultiply(self.matrix, other.matrix)) else: return _PysparseMatrixFromShape.__mul__(self, other)
T[4:8, 1:3] = As[4:8, 1:3] printMatrix(T) print 'this should raise execptions...\n' try: T[6:9, 6:9] = A[6:9, 6:9] except: traceback.print_exc() try: T[5:9, 4:10] = A[5:9, 4:10] except: traceback.print_exc() print 'Matrix multiplications' printMatrix(spmatrix.matrixmultiply(I, A)) printMatrix(spmatrix.matrixmultiply(Is, A)) printMatrix(spmatrix.matrixmultiply(O, O)) printMatrix(spmatrix.matrixmultiply(Os, O)) print 'Dot product' printMatrix(spmatrix.dot(I, A)) print 'Matrix export' A[:4, :4].export_mtx('A.mtx', 3) As[:4, :4].export_mtx('As.mtx', 3) print open('A.mtx').read() print open('As.mtx').read()
import time from pysparse import spmatrix n = 1000000 # create 2 nxn tridiag matrices A = spmatrix.ll_mat(n, n) B = spmatrix.ll_mat(n, n) for i in xrange(n): A[i,i] = i B[i,i] = i if i > 0: A[i,i-1] = 1 B[i,i-1] = 1 if i < n-1: A[i,i+1] = 1 B[i,i-1] = 1 t1 = time.clock() C = spmatrix.matrixmultiply(A, B) t_mult = time.clock() - t1 print 'time for multiplying %dx%d matrices: %.2f sec' % (n, n, t_mult) print C[:10,:10]