本文整理汇总了Python中pysparse.spmatrix.ll_mat函数的典型用法代码示例。如果您正苦于以下问题:Python ll_mat函数的具体用法?Python ll_mat怎么用?Python ll_mat使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ll_mat函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setWeightMatrix
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)))
示例2: __init__
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)
示例3: testEntry
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])
示例4: getSparseWeightMatrix
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
示例5: removeAllEdges
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())
示例6: _thckEvolve
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)
示例7: initA
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
示例8: _convert_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
示例9: poisson1d_vec
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
示例10: __init__
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)
示例11: profileSlicePys
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())
示例12: testInit
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)
示例13: nativeAdjacencyMatrix
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
示例14: profileSumPys
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())
示例15: profileGetNonZerosPys
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())