def csr_scp_lil_pysp(A_scp): [n_row, n_col] = A_scp.shape if n_row != n_col: raise Exception('give a square matrix please') n = n_row A_real_pysp = spmatrix.ll_mat( n, n, A_scp.nnz ) #The third option is a sizehint, the number of nonzeros. Prevents new memory allocations. A_imag_pysp = spmatrix.ll_mat(n, n, A_scp.nnz) [row_nonzero, col_nonzero] = A_scp.nonzero() #pysparse assignments need python integers and not numpy.int32. Problems otherwise. ->tolist() returns python ints. row_nonzero = row_nonzero.tolist() col_nonzero = col_nonzero.tolist() A_real_pysp.update_add_at( np.array(A_scp[row_nonzero, col_nonzero].real.tolist()[0]), row_nonzero, col_nonzero) A_imag_pysp.update_add_at( np.array(A_scp[row_nonzero, col_nonzero].imag.tolist()[0]), row_nonzero, col_nonzero) return [A_real_pysp, A_imag_pysp]
def __init__(self, rows, cols, bandwidth=0, sizeHint=None, matrix=None, storeZeros=True): """Instantiates and wraps a Pysparse `ll_mat` matrix Parameters ---------- rows : int The number of matrix rows cols : int The number of matrix columns bandwidth : int The proposed band width of the matrix. sizeHint : int Estimate of the number of non-zeros matrix : ~pysparse.spmatrix.ll_mat Pre-assembled Pysparse matrix to use for storage storeZeros : bool Instructs pysparse to store zero values if possible. """ sizeHint = sizeHint or max(rows, cols) * bandwidth if matrix is None: tmpMatrix = spmatrix.ll_mat(1, 1, 1) if hasattr(tmpMatrix, 'storeZeros'): matrix = spmatrix.ll_mat(rows, cols, sizeHint, storeZeros) else: matrix = spmatrix.ll_mat(rows, cols, sizeHint) super(_PysparseMatrixFromShape, self).__init__(matrix=matrix)
def __init__(self, rows, cols, bandwidth=0, sizeHint=None, matrix=None, storeZeros=True): """Instantiates and wraps a pysparse `ll_mat` matrix :Parameters: - `rows`: The number of matrix rows - `cols`: The number of matrix columns - `bandwidth`: The proposed band width of the matrix. - `sizeHint`: estimate of the number of non-zeros - `matrix`: pre-assembled `ll_mat` to use for storage - `storeZeros`: Instructs pysparse to store zero values if possible. """ sizeHint = sizeHint or max(rows, cols) * bandwidth if matrix is None: tmpMatrix = spmatrix.ll_mat(1, 1, 1) if hasattr(tmpMatrix, 'storeZeros'): matrix = spmatrix.ll_mat(rows, cols, sizeHint, storeZeros) else: matrix = spmatrix.ll_mat(rows, cols, sizeHint) _PysparseMatrix.__init__(self, matrix=matrix)
def initA(self, urt=None, nu=0, nr=0, nt=0): ''' Create the relation matrix A A is an adjacency matrix for the graph, where: - graph nodes are either users, resources or tags) - edges link either users and tags, users and resources or resources and tags ''' # beginning positions for users, resources and tags in the matrix self.u_beg = 0 self.r_beg = self.nu self.t_beg = self.nu + self.nr if urt == None: urt = [] # total number of entities --> gives the size of the matrix self.n = self.nu + self.nr + self.nt # create the relation matrix A self.A = spmatrix.ll_mat(self.n, self.n) # create adjacency matrix in the graph for u,r,t in urt: i = self.u_beg + u j = self.r_beg + r k = self.t_beg + t self.A[i,j] = self.A[j,i] = self.A[i,j] + 1 self.A[i,k] = self.A[k,i] = self.A[i,k] + 1 self.A[k,j] = self.A[j,k] = self.A[k,j] + 1
def getSparseWeightMatrix(self, format="lil"): """ Returns a weight matrix representation of the graph as a scipy sparse lil_matrix by default. The indices in the matrix correspond to the keys returned by getAllVertexIds. Edge labels are assigned to 1 for edges with non-numeric values. Available formats are: lil for scipy.sparse.lil_matrix, csr for scipy.sparse.csr_matrix, csc for scipy.sparse.csc_matrix, and pysparse for pysparse's ll_mat. :param format: The format of the sparse matrix. """ if format=="lil": W = scipy.sparse.lil_matrix((self.size, self.size)) W = self.__populateWeightMatrix(W) elif format=="csr": W = scipy.sparse.lil_matrix((self.size, self.size)) W = self.__populateWeightMatrix(W) W = W.tocsr() elif format=="csc": W = scipy.sparse.lil_matrix((self.size, self.size)) W = self.__populateWeightMatrix(W) W = W.tocsc() elif format=="pysparse": from pysparse import spmatrix W = spmatrix.ll_mat(self.size, self.size) W = self.__populateWeightMatrix(W) else: raise ValueError("Invalid format: " + format) return W
def setWeightMatrix(self, W): """ Set the weight matrix of this graph. Requires as input an ndarray with the same dimensions as the current weight matrix. Edges are represented by non-zero edges. :param W: The name of the file to load. :type W: :class:`ndarray` """ #Parameter.checkClass(W, numpy.ndarray) if W.shape != (self.vList.getNumVertices(), self.vList.getNumVertices()): raise ValueError("Weight matrix has wrong shape : " + str(W.shape)) if self.undirected and type(W) == numpy.ndarray and (W != W.T).any(): raise ValueError("Weight matrix of undirected graph must be symmetric") self.W = spmatrix.ll_mat(self.getNumVertices(), self.getNumVertices()) if type(W) == numpy.ndarray: rowInds, colInds = numpy.nonzero(W) self.W.put(W[(rowInds, colInds)], rowInds, colInds) elif isinstance(W, spmatrix.LLMatType): self.setWeightMatrixSparse(W) else: raise ValueError("Invalid matrix type: " + str(type(W)))
def getSparseWeightMatrix(self, format="lil"): """ Returns a weight matrix representation of the graph as a scipy sparse lil_matrix by default. The indices in the matrix correspond to the keys returned by getAllVertexIds. Available formats are: lil for scipy.sparse.lil_matrix, csr for scipy.sparse.csr_matrix, csc for scipy.sparse.csc_matrix, and pysparse for pysparse's ll_mat. :param format: The format of the sparse matrix. """ if format == "lil": W = scipy.sparse.lil_matrix((self.size, self.size)) W = self.__populateWeightMatrix(W) elif format == "csr": W = scipy.sparse.lil_matrix((self.size, self.size)) W = self.__populateWeightMatrix(W) W = W.tocsr() elif format == "csc": W = scipy.sparse.lil_matrix((self.size, self.size)) W = self.__populateWeightMatrix(W) W = W.tocsc() elif format == "pysparse": from pysparse import spmatrix W = spmatrix.ll_mat(self.size, self.size) W = self.__populateWeightMatrix(W) else: raise ValueError("Invalid format: " + format) return W
def __init__(self, **kwargs): nrow = kwargs.get('nrow', 0) ncol = kwargs.get('ncol', 0) bandwidth = kwargs.get('bandwidth', 0) matrix = kwargs.get('matrix', None) sizeHint = kwargs.get('sizeHint', 0) symmetric = 'symmetric' in kwargs and kwargs['symmetric'] size = kwargs.get('size',0) if size > 0: if nrow > 0 or ncol > 0: if size != nrow or size != ncol: msg = 'size argument was given but does not match ' msg += 'nrow and ncol' raise ValueError, msg else: nrow = ncol = size if matrix is not None: self.matrix = matrix else: if symmetric and nrow==ncol: if sizeHint is None: sizeHint = nrow if bandwidth > 0: sizeHint += 2*(bandwidth-1)*(2*nrow-bandwidth-2) self.matrix = spmatrix.ll_mat_sym(nrow, sizeHint) else: if sizeHint is None: sizeHint = min(nrow,ncol) if bandwidth > 0: sizeHint = bandwidth * (2*sizeHint-bandwidth-1)/2 self.matrix = spmatrix.ll_mat(nrow, ncol, sizeHint)
def testNonzero(self): try: from pysparse import spmatrix from apgl.util.PySparseUtils import PySparseUtils except ImportError as error: return n = 10 X = spmatrix.ll_mat(n, n) self.assertTrue( (PySparseUtils.nonzero(X)[0] == numpy.array([], numpy.int)).all()) self.assertTrue( (PySparseUtils.nonzero(X)[0] == numpy.array([], numpy.int)).all()) X[1, 1] = 5 X[2, 4] = 6.1 X[3, 1] = 2.5 self.assertTrue( (PySparseUtils.nonzero(X)[0] == numpy.array([1, 2, 3], numpy.int)).all()) self.assertTrue( (PySparseUtils.nonzero(X)[1] == numpy.array([1, 4, 1], numpy.int)).all())
def testEntry(self): def assignUP(): self.S[0,1] = 1.0 def assignLeft(): self.S[-11,0] = 1.0 def assignRight(): self.S[10,0] = 1.0 def assignTop(): self.S[0,-11] = 1.0 def assignBottom(): self.S[0,10] = 1.0 self.A[0,0] = 1.0 self.S[0,0] = 1.0 self.failUnless(self.A[0,0] == 1.0) self.failUnless(self.A.nnz == 1) self.failUnless(self.S[0,0] == 1.0) self.failUnless(self.S.nnz == 1) self.failUnlessRaises(IndexError, assignUP) self.A[0,0] += 1.0 self.failUnless(self.A[0,0] == 2.0) self.failUnless(self.A.nnz == 1) self.A[0,0] -= 2.0 self.failUnless(self.A[0,0] == 0.0) self.failUnless(self.A.nnz == 0) # indices out of bounds #for f in [assignLeft, assignRight, assignTop, assignBottom]: #for f in [assignRight, assignTop, assignBottom]: # self.failUnlessRaises(IndexError, f) self.failUnlessRaises(IndexError, assignRight) self.failUnlessRaises(IndexError, assignTop) self.failUnlessRaises(IndexError, assignBottom) # negative indices I = spmatrix.ll_mat(10, 10, 100) for i in range(10): for j in range(10): I[i,j] = 10*i + j for i in range(-10, 0): for j in range(-10, 0): self.failUnless(I[i,j] == I[10+i,10+j])
def removeAllEdges(self): """ Removes all edges from this graph. """ #Not sure why this doesn't work #self.W.scale(0) self.W = spmatrix.ll_mat(self.getNumVertices(), self.getNumVertices())
def _thckEvolve(self, rhs, ans, mask, calc_rhs, old_thck, new_thck, diffu, lsrf, acab): matrix = pysp.ll_mat(self.totpts, self.totpts) # Boundary Conditions --------------------------------------------------------------- self._applyULBCs(matrix, old_thck, new_thck, self.mask, calc_rhs, rhs, ans) self._applyLRBCs(matrix, old_thck, new_thck, self.mask, calc_rhs, rhs, ans) # Ice body sumd = np.zeros(5, dtype=np.float) for ns in range(1, self.mainGrid.ny - 1): for ew in range(1, self.mainGrid.nx - 1): if mask[ew, ns] != 0: self._findSums(diffu, sumd, ew - 1, ew, ns - 1, ns) # Matrix elements self._fillMatrix(matrix, self.mask, sumd, ew, ns) # RHS vector if calc_rhs: rhs[mask[ew, ns] - 1] = self._calcRHS( old_thck, lsrf, acab, sumd, ew, ns) ans[mask[ew, ns] - 1] = new_thck[ew, ns] # Solve system self._solveSystem(matrix, rhs, ans) # Rejig the solution onto a 2D array self._regrid(new_thck, self.mask, ans) # Remove negatives new_thck[:] = np.maximum(0.0, new_thck)
def to_ll_mat(self): n = self.n nnz = self.nnz H = spmatrix.ll_mat(n,n, nnz) for i,j,h in self: H[i,j] = h return H
def _thckEvolve(self,rhs,ans,mask,calc_rhs,old_thck,new_thck,diffu,lsrf,acab): matrix = pysp.ll_mat(self.totpts,self.totpts) # Boundary Conditions --------------------------------------------------------------- self._applyULBCs(matrix,old_thck,new_thck,self.mask,calc_rhs,rhs,ans) self._applyLRBCs(matrix,old_thck,new_thck,self.mask,calc_rhs,rhs,ans) # Ice body sumd = np.zeros(5,dtype=np.float) for ns in range(1,self.mainGrid.ny-1): for ew in range(1,self.mainGrid.nx-1): if mask[ew,ns] != 0: self._findSums(diffu,sumd,ew-1,ew,ns-1,ns) # Matrix elements self._fillMatrix(matrix,self.mask,sumd,ew,ns) # RHS vector if calc_rhs: rhs[mask[ew,ns]-1] = self._calcRHS(old_thck,lsrf,acab,sumd,ew,ns) ans[mask[ew,ns]-1] = new_thck[ew,ns] # Solve system self._solveSystem(matrix,rhs,ans) # Rejig the solution onto a 2D array self._regrid(new_thck,self.mask,ans) # Remove negatives new_thck[:] = np.maximum(0.0,new_thck)
def setWeightMatrix(self, W): """ Set the weight matrix of this graph. Requires as input an ndarray with the same dimensions as the current weight matrix. Edges are represented by non-zero edges. :param W: The name of the file to load. :type W: :class:`ndarray` """ #Parameter.checkClass(W, numpy.ndarray) if W.shape != (self.vList.getNumVertices(), self.vList.getNumVertices()): raise ValueError("Weight matrix has wrong shape : " + str(W.shape)) if self.undirected and type(W) == numpy.ndarray and (W != W.T).any(): raise ValueError( "Weight matrix of undirected graph must be symmetric") self.W = spmatrix.ll_mat(self.getNumVertices(), self.getNumVertices()) if type(W) == numpy.ndarray: rowInds, colInds = numpy.nonzero(W) self.W.put(W[(rowInds, colInds)], rowInds, colInds) elif isinstance(W, spmatrix.LLMatType): self.setWeightMatrixSparse(W) else: raise ValueError("Invalid matrix type: " + str(type(W)))
def _convert_mat(mtx): from pysparse import spmatrix A = spmatrix.ll_mat(*mtx.shape) for i in range(mtx.indptr.shape[0] - 1): ii = slice(mtx.indptr[i], mtx.indptr[i + 1]) n_in_row = ii.stop - ii.start A.update_add_at(mtx.data[ii], [i] * n_in_row, mtx.indices[ii]) return A
def to_csr(self): n = self.n nnz = self.nnz H = spmatrix.ll_mat(n,n, nnz) for i,j,h in self: H[i,j] = h mat = H.to_csr() return mat
def _convert_mat(mtx): from pysparse import spmatrix A = spmatrix.ll_mat(*mtx.shape) for i in range(mtx.indptr.shape[0] - 1): ii = slice(mtx.indptr[i], mtx.indptr[i+1]) n_in_row = ii.stop - ii.start A.update_add_at(mtx.data[ii], [i] * n_in_row, mtx.indices[ii]) return A
def poisson1d_vec(n): L = spmatrix.ll_mat(n, n, 3*n-2) e = numpy.ones(n) d = numpy.arange(n, dtype=numpy.int) L.put(2*e, d, d) L.put(-e[1:], d[1:], d[:-1]) L.put(-e[1:], d[:-1], d[1:]) return L
def __init__(self, size, bandwidth=0, sizeHint=None, matrix=None, storeZeros=True): """Creates a `_PysparseMatrix`. :Parameters: - `mesh`: The `Mesh` to assemble the matrix for. - `bandwidth`: The proposed band width of the matrix. - `storeZeros`: Instructs pysparse to store zero values if possible. """ sizeHint = sizeHint or size * bandwidth if matrix is None: tmpMatrix = spmatrix.ll_mat(1, 1, 1) if hasattr(tmpMatrix, "storeZeros"): matrix = spmatrix.ll_mat(size, size, sizeHint, storeZeros) else: matrix = spmatrix.ll_mat(size, size, sizeHint) _PysparseMatrixBase.__init__(self, matrix=matrix)
def profileSlicePys(self): A = spmatrix.ll_mat(self.N, self.N) A.put(self.val, self.rowInds, self.colInds) def runSlice(): for i in range(10): sliceInds = numpy.array(numpy.random.randint(0, self.M, self.N), dtype=numpy.int32) B = A[:, sliceInds] ProfileUtils.profile('runSlice()', globals(), locals())
def testInit(self): numVertices = 0 numFeatures = 1 vList = VertexList(numVertices, numFeatures) graph = PySparseGraph(vList) numVertices = 10 numFeatures = 1 vList = VertexList(numVertices, numFeatures) graph = PySparseGraph(vList) self.assertRaises(ValueError, PySparseGraph, []) self.assertRaises(ValueError, PySparseGraph, vList, 1) self.assertRaises(ValueError, PySparseGraph, vList, True, 1) #Now test invalid values of W W = scipy.sparse.csr_matrix((numVertices, numVertices)) self.assertRaises(ValueError, PySparseGraph, vList, True, W) W = numpy.zeros((numVertices + 1, numVertices)) self.assertRaises(ValueError, PySparseGraph, vList, True, W) W = numpy.zeros((numVertices, numVertices)) W[0, 1] = 1 self.assertRaises(ValueError, PySparseGraph, vList, True, W) W = spmatrix.ll_mat(numVertices, numVertices) graph = PySparseGraph(vList, W=W) self.assertTrue(isinstance(W, spmatrix.LLMatType)) #Test intialising with non-empty graph numVertices = 10 W = spmatrix.ll_mat(numVertices, numVertices) W[1, 0] = 1.1 W[0, 1] = 1.1 graph = PySparseGraph(numVertices, W=W) self.assertEquals(graph[1, 0], 1.1) #Test just specifying number of vertices graph = PySparseGraph(numVertices) self.assertEquals(graph.size, numVertices)
def testInit(self): numVertices = 0 numFeatures = 1 vList = VertexList(numVertices, numFeatures) graph = PySparseGraph(vList) numVertices = 10 numFeatures = 1 vList = VertexList(numVertices, numFeatures) graph = PySparseGraph(vList) self.assertRaises(ValueError, PySparseGraph, []) self.assertRaises(ValueError, PySparseGraph, vList, 1) self.assertRaises(ValueError, PySparseGraph, vList, True, 1) #Now test invalid values of W W = scipy.sparse.csr_matrix((numVertices, numVertices)) self.assertRaises(ValueError, PySparseGraph, vList, True, W) W = numpy.zeros((numVertices+1, numVertices)) self.assertRaises(ValueError, PySparseGraph, vList, True, W) W = numpy.zeros((numVertices, numVertices)) W[0, 1] = 1 self.assertRaises(ValueError, PySparseGraph, vList, True, W) W = spmatrix.ll_mat(numVertices, numVertices) graph = PySparseGraph(vList, W=W) self.assertTrue(isinstance(W, spmatrix.LLMatType)) #Test intialising with non-empty graph numVertices = 10 W = spmatrix.ll_mat(numVertices, numVertices) W[1, 0] = 1.1 W[0, 1] = 1.1 graph = PySparseGraph(numVertices, W=W) self.assertEquals(graph[1, 0], 1.1) #Test just specifying number of vertices graph = PySparseGraph(numVertices) self.assertEquals(graph.size, numVertices)
def profileSumPys(self): A = spmatrix.ll_mat(self.N, self.N) A.put(self.val, self.rowInds, self.colInds) def runSum(): for i in range(1000): i = PySparseUtils.sum(A) print(i) ProfileUtils.profile('runSum()', globals(), locals())
def nativeAdjacencyMatrix(self): """ Return the adjacency matrix in sparse format. """ A = spmatrix.ll_mat(self.vList.getNumVertices(), self.vList.getNumVertices()) nonzeros = PySparseUtils.nonzero(self.W) A.put(1, nonzeros[0], nonzeros[1]) return A
def T(self): """Transpose matrix Returns ------- ~fipy.matrices.pysparseMatrix._PysparseMatrix Examples -------- >>> import fipy as fp >>> mesh = fp.Grid1D(nx=10) >>> ids = fp.CellVariable(mesh=mesh, value=mesh._globalOverlappingCellIDs) >>> mat = _PysparseColMeshMatrix(mesh=mesh, rows=1) >>> mat.put(vector=ids.value, ... id1=[fp.parallelComm.procID] * mesh.numberOfCells, ... id2=mesh._localOverlappingCellIDs, ... overlapping=True) # doctest: +SERIAL >>> print(mat.T.numpyArray) # doctest: +SERIAL [[ 0.] [ 1.] [ 2.] [ 3.] [ 4.] [ 5.] [ 6.] [ 7.] [ 8.] [ 9.]] """ val, irow, jcol = self.matrix.find() rows, cols = self.matrix.shape if hasattr(self.matrix, 'storeZeros'): A_T = spmatrix.ll_mat(cols, rows, self.matrix.nnz, self.matrix.storeZeros) else: A_T = spmatrix.ll_mat(cols, rows, self.matrix.nnz) A_T.put(val, jcol, irow) return _PysparseMatrix(matrix=A_T)
def convert_mat(mtx): """ Converts a scipy matrix "mtx" to a pysparse matrix. """ mtx = mtx.tocsr() A = spmatrix.ll_mat(*mtx.shape) for i in xrange( mtx.indptr.shape[0] - 1 ): ii = slice( mtx.indptr[i], mtx.indptr[i+1] ) n_in_row = ii.stop - ii.start A.update_add_at( mtx.data[ii], [i] * n_in_row, mtx.indices[ii] ) return A
def profileGetNonZerosPys(self): A = spmatrix.ll_mat(self.N, self.N) A.put(self.val, self.rowInds, self.colInds) def runNonZeros(): for i in range(1000): (rows, cols) = PySparseUtils.nonzero(A) nzVals = numpy.zeros(len(rows)) A.take(nzVals, rows, cols) ProfileUtils.profile('runNonZeros()', globals(), locals())
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
def setWeightMatrixSparse(self, W): """ Set the weight matrix of this graph. Requires as input a scipy sparse matrix with the same dimensions as the current weight matrix. Edges are represented by non-zero edges. :param W: The weight matrix to use. """ if not isinstance(W, spmatrix.LLMatType) and not sparse.issparse(W): raise ValueError("Input must be a sparse matrix, not " + str(type(W))) if W.shape != (self.vList.getNumVertices(), self.vList.getNumVertices()): raise ValueError("Weight matrix has wrong shape : " + str(W.shape)) try: self.W = spmatrix.ll_mat(W.shape[0], W.shape[0], W.getnnz()) except AttributeError: self.W = spmatrix.ll_mat(W.shape[0], W.shape[0], W.nnz) if isinstance(W, spmatrix.LLMatType): #Warning: no check for symmetric matrix #if self.undirected: # raise ValueError("Weight matrix of undirected graph must be symmetric") items = W.items() for inds, val in items: self.W[inds[0], inds[1]] = val else: if self.undirected and (W - W.transpose()).nonzero()[0].shape[0]: raise ValueError( "Weight matrix of undirected graph must be symmetric") rowInds, colInds = W.nonzero() for i in range(rowInds.shape[0]): self.W[int(rowInds[i]), int(colInds[i])] = W[int(rowInds[i]), int(colInds[i])]
def inDegreeSequence(self): """ Return a vector of the (in)degree sequence for each vertex. """ A = self.nativeAdjacencyMatrix() j = spmatrix.ll_mat(self.vList.getNumVertices(), 1) j[:, 0] = 1 degrees = spmatrix.dot(A, j) degrees = PysparseMatrix(matrix=degrees) degrees = numpy.array(degrees.getNumpyArray().ravel(), numpy.int) return degrees
def convert_mat(mtx): """ Converts a scipy matrix "mtx" to a pysparse matrix. """ from pysparse import spmatrix mtx = mtx.tocsr() A = spmatrix.ll_mat(*mtx.shape) for i in xrange(mtx.indptr.shape[0] - 1): ii = slice(mtx.indptr[i], mtx.indptr[i + 1]) n_in_row = ii.stop - ii.start A.update_add_at(mtx.data[ii], [i] * n_in_row, mtx.indices[ii]) return A
def poisson2d_vec(n): n2 = n * n L = spmatrix.ll_mat(n2, n2, 5 * n2 - 4 * n) d = numpy.arange(n2, dtype=numpy.int) L.put(4.0, d) L.put(-1.0, d[:-n], d[n:]) L.put(-1.0, d[n:], d[:-n]) for i in xrange(n): di = d[i * n:(i + 1) * n] L.put(-1.0, di[1:], di[:-1]) L.put(-1.0, di[:-1], di[1:]) return L
def poisson2d_vec(n): n2 = n*n L = spmatrix.ll_mat(n2, n2, 5*n2-4*n) d = numpy.arange(n2, dtype=numpy.int) L.put(4.0, d) L.put(-1.0, d[:-n], d[n:]) L.put(-1.0, d[n:], d[:-n]) for i in xrange(n): di = d[i*n:(i+1)*n] L.put(-1.0, di[1:], di[:-1]) L.put(-1.0, di[:-1], di[1:]) return L
def testCompressStress(self): n = 20 A = spmatrix.ll_mat(n, n) for k in range(20): for i in range(n*n/2): i = random.randrange(n) j = random.randrange(n) A[i, j] = 1.0 for i in range(n*n/2): i = random.randrange(n) j = random.randrange(n) A[i, j] = 0.0
def profileSlicePys(self): A = spmatrix.ll_mat(self.N, self.N) A.put(self.val, self.rowInds, self.colInds) def runSlice(): for i in range(10): sliceInds = numpy.array(numpy.random.randint( 0, self.M, self.N), dtype=numpy.int32) B = A[:, sliceInds] ProfileUtils.profile('runSlice()', globals(), locals())
def as_llmat(A,tol= 1e-10): """Fill out any matrix as ll_mat type.""" if isinstance(A,np.ndarray): dim = A.shape m,n = dim[0],dim[1] if min(m,n)>1: spmat_ = spmatrix.ll_mat(m,n) nz = np.nonzero(A) r = nz[0].size for i in range(r): ii = np.int(nz[0][i]) ij = np.int(nz[1][i]) spmat_[ii,ij] = A[nz[0][i],nz[1][i]] spmat= spmat_ return spmat else: n,m =dim vec = np.zeros((1,max(n,m))) for i in range(max(n,m)): vec[0,i] = A[0,i] return vec elif isinstance(A, PysparseMatrix): dim = A.shape m,n =dim spmat_ = spmatrix.ll_mat(m,n) #nz = np.nonzero(A) #print nz for i in range(m): for j in range(n): spmat_[i,j] = A[i,j] spmat= spmat_ return spmat else: t = type(A) m,n= A.shape spmat_ = spmatrix.ll_mat(m,n) spmat_[:,:]=A return spmat_
def solved(self): A = spmatrix.ll_mat(5, 5) for i in range(5): A[i, i] = i + 1 A = A.to_csr() B = np.ones(5) x = np.empty(5) LU = superlu.factorize(A, diag_pivot_thresh=0.0) LU.solve(B, x) return np.array_str(x) def build(self): return Label(text=self.solved)
def convert(self, matrix): psp_mat = spmatrix.ll_mat(matrix.shape[0], matrix.shape[1]) for i in range(matrix.shape[0]): for j in range(matrix.shape[1]): if matrix[i, j]: psp_mat[i, j] = matrix[i, j] data, row, col = psp_mat.find() csrmat = ss.csr_matrix((data, (row, col)), shape=psp_mat.shape) return csrmat
def _thckEvolve(self, rhs, ans, mask, calc_rhs, old_thck, new_thck, diffu, lsrf, acab): matrix = pysp.ll_mat(self.totpts, self.totpts) # Boundary Conditions --------------------------------------------------------------- self._applyULBCs(matrix, old_thck, new_thck, self.mask, calc_rhs, rhs, ans) self._applyLRBCs(matrix, old_thck, new_thck, self.mask, calc_rhs, rhs, ans) # Ice body sumd = np.zeros(5, dtype=np.float) rawIdx = np.nonzero(self.mask[1:-1, 1:-1]) idx = (self.mask[1:-1, 1:-1])[rawIdx] - 1 idx2 = (self.mask[0:-2, 1:-1])[rawIdx] - 1 sum1 = (self.fc2_1 * (diffu[0:-1, 0:-1] + diffu[0:-1, 1:])) matrix.put(sum1.ravel(), idx, idx2) idx2 = (self.mask[2:, 1:-1])[rawIdx] - 1 sum2 = (self.fc2_1 * (diffu[1:, 0:-1] + diffu[1:, 1:])) matrix.put(sum2.ravel(), idx, idx2) idx2 = (self.mask[1:-1, 0:-2])[rawIdx] - 1 sum3 = (self.fc2_5 * (diffu[0:-1, 0:-1] + diffu[1:, 0:-1])) matrix.put(sum3.ravel(), idx, idx2) idx2 = (self.mask[1:-1, 2:])[rawIdx] - 1 sum4 = (self.fc2_5 * (diffu[0:-1, 1:] + diffu[1:, 1:])) matrix.put(sum4.ravel(), idx, idx2) sum5 = -(sum1 + sum2 + sum3 + sum4) matrix.put(1. + sum5.ravel(), idx, idx) # RHS vector if calc_rhs: rhs[idx] = self._calcRHS(old_thck, lsrf, acab, sum1, sum2, sum3, sum4, sum5, rawIdx) ans[idx] = (new_thck[1:-1, 1:-1])[rawIdx] # Solve system self._solveSystem(matrix, rhs, ans) # Rejig the solution onto a 2D array self._regrid(new_thck, self.mask, ans) # Remove negatives new_thck[:] = np.maximum(0.0, new_thck)
def convert(self, matrix): psp_mat = spmatrix.ll_mat(matrix.shape[0], matrix.shape[1]) for i in range(matrix.shape[0]): for j in range(matrix.shape[1]): if matrix[i,j]: psp_mat[i,j] = matrix[i,j] data, row, col = psp_mat.find() csrmat = ss.csr_matrix((data, (row, col)), shape=psp_mat.shape) return csrmat
def setWeightMatrixSparse(self, W): """ Set the weight matrix of this graph. Requires as input a scipy sparse matrix with the same dimensions as the current weight matrix. Edges are represented by non-zero edges. :param W: The weight matrix to use. """ if not isinstance(W, spmatrix.LLMatType) and not sparse.issparse(W): raise ValueError("Input must be a sparse matrix, not " + str(type(W))) if W.shape != (self.vList.getNumVertices(), self.vList.getNumVertices()): raise ValueError("Weight matrix has wrong shape : " + str(W.shape)) try: self.W = spmatrix.ll_mat(W.shape[0], W.shape[0], W.getnnz()) except AttributeError: self.W = spmatrix.ll_mat(W.shape[0], W.shape[0], W.nnz) if isinstance(W, spmatrix.LLMatType): #Warning: no check for symmetric matrix #if self.undirected: # raise ValueError("Weight matrix of undirected graph must be symmetric") items = W.items() for inds, val in items: self.W[inds[0], inds[1]] = val else: if self.undirected and (W - W.transpose()).nonzero()[0].shape[0]: raise ValueError("Weight matrix of undirected graph must be symmetric") rowInds, colInds = W.nonzero() for i in range(rowInds.shape[0]): self.W[int(rowInds[i]), int(colInds[i])] = W[int(rowInds[i]), int(colInds[i])]
def poisson2d(n): L = spmatrix.ll_mat(n*n, n*n) for i in range(n): for j in range(n): k = i + n*j L[k,k] = 4 if i > 0: L[k,k-1] = -1 if i < n-1: L[k,k+1] = -1 if j > 0: L[k,k-n] = -1 if j < n-1: L[k,k+n] = -1 return L
def __init__(self, size = None, bandwidth = 0, matrix = None, sizeHint = None): """ Creates a `_PysparseMatrix`. :Parameters: - `size`: The size N for an N by N matrix. - `bandwidth`: The proposed band width of the matrix. - `matrix`: The starting `spmatrix` id there is one. """ if matrix != None: self.matrix = matrix else: sizeHint = sizeHint or size * bandwidth self.matrix = spmatrix.ll_mat(size, size, sizeHint)
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 poisson2d_sym_blk_vec(n): n2 = n*n L = spmatrix.ll_mat_sym(n2, 3*n2-2*n) D = spmatrix.ll_mat_sym(n, 2*n-1) e = numpy.ones(n) d = numpy.arange(n, dtype=numpy.int) D.put(4*e, d, d) D.put(-e[1:], d[1:], d[:-1]) P = spmatrix.ll_mat(n, n, n-1) P.put(-e,d,d) for i in xrange(n-1): L[i*n:(i+1)*n, i*n:(i+1)*n] = D L[(i+1)*n:(i+2)*n, i*n:(i+1)*n] = P # Last diagonal block L[n2-n:n2, n2-n:n2] = D return L
def _thckEvolve(self,rhs,ans,mask,calc_rhs,old_thck,new_thck,diffu,lsrf,acab): matrix = pysp.ll_mat(self.totpts,self.totpts) # Boundary Conditions --------------------------------------------------------------- self._applyULBCs(matrix,old_thck,new_thck,self.mask,calc_rhs,rhs,ans) self._applyLRBCs(matrix,old_thck,new_thck,self.mask,calc_rhs,rhs,ans) # Ice body sumd = np.zeros(5,dtype=np.float) rawIdx = np.nonzero(self.mask[1:-1,1:-1]) idx = (self.mask[1:-1,1:-1])[rawIdx] -1 idx2 = (self.mask[0:-2,1:-1])[rawIdx] -1 sum1 = (self.fc2_1 * (diffu[0:-1,0:-1] + diffu[0:-1,1:])) matrix.put(sum1.ravel(), idx, idx2) idx2 = (self.mask[2:,1:-1])[rawIdx] -1 sum2 = (self.fc2_1 * (diffu[1:,0:-1] + diffu[1:,1:])) matrix.put(sum2.ravel(), idx, idx2) idx2 = (self.mask[1:-1,0:-2])[rawIdx] -1 sum3 = (self.fc2_5 * (diffu[0:-1,0:-1] + diffu[1:,0:-1])) matrix.put(sum3.ravel(), idx, idx2) idx2 = (self.mask[1:-1,2:])[rawIdx] -1 sum4 = (self.fc2_5 * (diffu[0:-1,1:] + diffu[1:,1:])) matrix.put(sum4.ravel(), idx, idx2) sum5 = -(sum1 + sum2 + sum3 + sum4) matrix.put(1.+sum5.ravel(),idx,idx) # RHS vector if calc_rhs: rhs[idx] = self._calcRHS(old_thck,lsrf,acab,sum1,sum2,sum3,sum4,sum5,rawIdx) ans[idx] = (new_thck[1:-1,1:-1])[rawIdx] # Solve system self._solveSystem(matrix,rhs,ans) # Rejig the solution onto a 2D array self._regrid(new_thck,self.mask,ans) # Remove negatives new_thck[:] = np.maximum(0.0,new_thck)
def testSum(self): try: from pysparse import spmatrix from apgl.util.PySparseUtils import PySparseUtils except ImportError as error: return n = 10 X = spmatrix.ll_mat(n, n) self.assertEquals(PySparseUtils.sum(X), 0.0) X[1, 1] = 5 X[2, 4] = 6.1 X[3, 1] = 2.5 self.assertEquals(PySparseUtils.sum(X), 13.6)
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 __init__(self, vertices, undirected=True, W=None, sizeHint=1000): """ Create a PySparseGraph with a given AbstractVertexList or number of vertices, and specify whether it is directed. One can optionally pass in a sparse matrix W which is used as the weight matrix of the graph. Different kinds of sparse matrix can impact the speed of various operations. The currently supported sparse matrix types are: ll_mat. :param vertices: the initial set of vertices as a AbstractVertexList object, or an int to specify the number of vertices in which case vertices are stored in a GeneralVertexList. :param undirected: a boolean variable to indicate if the graph is undirected. :type undirected: :class:`boolean` :param W: a square sparse matrix of the same size as the number of vertices, or None to create the default one. :param sizeHint: the expected number of edges in the graph for efficient memory usage. :type sizeHint: :class:`int` """ Parameter.checkBoolean(undirected) if isinstance(vertices, AbstractVertexList): self.vList = vertices elif isinstance(vertices, int): self.vList = GeneralVertexList(vertices) else: raise ValueError("Invalid vList parameter: " + str(vertices)) if W != None and not (isinstance(W, spmatrix.LLMatType) and W.shape == (len(self.vList), len(self.vList))): raise ValueError( "Input argument W must be None or spmatrix.ll_mat of size " + str(len(self.vList))) self.undirected = undirected if W == None: #Should use ll_mat_sym for undirected graphs but it has several unimplemented methods self.W = spmatrix.ll_mat(len(self.vList), len(self.vList), sizeHint) else: self.W = W #The next line is for error checking mainly self.setWeightMatrix(W)
def psp_pseudoinverse(Mat, precision): list_nz = (Mat.sum(axis=1) == 1) list_mat = [] for i in range(list_nz): if list_nz[i]: list_mat.append(i) temp_Mat = Mat[list_mat, :] matrix = spmatrix.ll_mat(temp_Mat.shape[0], temp_Mat.shape[1]) matrix.update_add_at(temp_Mat.tocoo().data, temp_Mat.tocoo().row, temp_Mat.tocoo().col) if matrix.shape[0] <= matrix.shape[1]: k = int((precision * matrix.shape[0]) / 100) ut, s, vt = sparsesvd(matrix.tocsc(), k) UT = ss.csr_matrix(ut) SI = ss.csr_matrix(np.diag(1 / s)) VT = ss.csr_matrix(vt) temp_matrix = spmatrixmul(VT.transpose(), SI) pinv_matrix = spmatrixmul(temp_matrix, UT) del ut, s, vt, UT, SI, VT, temp_matrix else: k = int((precision * matrix.transpose().shape[0]) / 100) ut, s, vt = sparsesvd(matrix.transpose().tocsc(), k) UT = ss.csr_matrix(ut) SI = ss.csr_matrix(np.diag(1 / s)) VT = ss.csr_matrix(vt) temp_matrix = spmatrixmul(UT.transpose(), SI) pinv_matrix = spmatrixmul(temp_matrix, VT) del ut, s, vt, UT, SI, VT, temp_matrix return pinv_matrix.tocsr()
def __init__(self, size=100, conn=1, inputs=1, outputs=1): self.N = size self.ins = inputs self.outs = outputs self.Wout = np.random.rand(self.outs, self.N + self.ins) * 2 - 1 self.W = np.random.rand(self.N, self.N) * 2 - 1 self.Win = np.random.rand(self.N, self.ins) * 2 - 1 self.x = np.zeros((self.N)) # for aureservoir self.aunet = au.DoubleESN() self.aunet.setSize(size) self.aunet.setInputs(inputs) self.aunet.setOutputs(outputs) self.aunet.setInitParam(au.ALPHA, 1.) self.aunet.setInitParam(au.CONNECTIVITY, conn) self.aunet.setInitParam(au.IN_CONNECTIVITY, 1.) self.aunet.init() self.aunet.setWout(self.Wout) self.aunet.post() # for sparse simulation self.aunet.getW(self.W) self.Wsp = sparse.csr_matrix( self.W) # not efficient, use lil to construct matrix # create matrix for pysparse and pyublas.sparse tmp = spmatrix.ll_mat(self.N, self.N, int(self.N * self.N * conn)) # tmp2 = pyublas.zeros((self.N, self.N), flavor=pyublas.SparseBuildMatrix ) for i in range(self.N): for j in range(self.N): if self.W[i, j] != 0: tmp[i, j] = self.W[i, j] # tmp2[i,j] = self.W[i,j] self.Wsp2 = tmp.to_csr()