def __init__(self, dim, dicto=None, shifts=None): """ attributes: shifts, dims, data, is1D, length methods : dump """ self.shifts = shifts if type(dim) == type(()) or type(dim) == type([]): self.data = spmatrix.ll_mat(reduce(operator.mul, dim), 1) self.dims = dim if dicto: for k, v in dicto.iteritems(): shk = map(operator.__sub__, k, shifts) self.data[self.comp(shk), 0] = v elif type(dim) == IntType: self.data = spmatrix.ll_mat(dim, 1) self.dims = dim if dicto: for k, v in dicto.iteritems(): shk = k - shifts self.data[shk, 0] = v self.is1D = type(self.dims) == IntType
def __init__(self,dim,dicto=None,shifts=None): """ attributes: shifts, dims, data, is1D, length methods : dump """ self.shifts=shifts if type(dim)==type(()) or type(dim)==type([]): self.data = spmatrix.ll_mat(reduce(operator.mul, dim), 1) self.dims = dim if dicto: for k, v in dicto.iteritems(): shk = map(operator.__sub__, k, shifts) self.data[self.comp(shk), 0]=v elif type(dim)==IntType: self.data = spmatrix.ll_mat(dim,1) self.dims = dim if dicto: for k, v in dicto.iteritems(): shk = k - shifts self.data[shk,0] = v self.is1D = type(self.dims)==IntType
def LinearSystem(self): r""" Assembly linear system Depends on Velocity field and Gamma """ # assembly matrix of linear system # using pysparse optimized matrix non zero elements 5*M self.mUt = spmatrix.ll_mat(self.Nz*self.Nx, self.Nz*self.Nx, 5*self.Nz*self.Nx-2*self.Nz-2*self.Nx) for Ln in range(0, self.Nz*self.Nx, 1): # 1.0*u(x-1,z) + Gamma(x,z)*u(x,z) + 1.0*u(x+1,z) + 1.0*u(x,z-1) + 1.0*u(x,z+1) # turn the indices to the one of original matrix i = Ln%self.Nx k = Ln/self.Nx self.mUt[Ln,Ln] = self.Gamma(k, i) #is this right? if(i-1 >= 0): # u(x-1,z) inside grid in I self.mUt[Ln,Ln-1] = 1.0 if(i+1 < self.Nx): # u(x+1,z) inside grid in I self.mUt[Ln,Ln+1] = 1.0 if(k-1 >= 0): #u(x,z-1) self.mUt[Ln,Ln-self.Nx]= 1.0 if(k+1 < self.Nz): #u(x,z+1) self.mUt[Ln,Ln+self.Nx]= 1.0 return self.mUt
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 test_on_diagonal_matrix(self): a = spmatrix.ll_mat(3, 3) a.put([1, 2, 3]) r = jdsym.jdsym(a, None, None, 3, 1, 1e-9, 100, krylov.qmrs) self.assertEqual(r[0], 3) self.assertTrue(numpy.allclose(r[1], [1, 2, 3])) self.assertTrue(numpy.allclose(numpy.abs(r[2]), numpy.identity(3)))
def LinearSystem(self): r""" Assembly linear system Depends on Velocity field and Gamma """ # assembly matrix of linear system # using pysparse optimized matrix non zero elements 5*M self.mUt = spmatrix.ll_mat( self.Nz * self.Nx, self.Nz * self.Nx, 5 * self.Nz * self.Nx - 2 * self.Nz - 2 * self.Nx) for Ln in range(0, self.Nz * self.Nx, 1): # 1.0*u(x-1,z) + Gamma(x,z)*u(x,z) + 1.0*u(x+1,z) + 1.0*u(x,z-1) + 1.0*u(x,z+1) # turn the indices to the one of original matrix i = Ln % self.Nx k = Ln / self.Nx self.mUt[Ln, Ln] = self.Gamma(k, i) #is this right? if (i - 1 >= 0): # u(x-1,z) inside grid in I self.mUt[Ln, Ln - 1] = 1.0 if (i + 1 < self.Nx): # u(x+1,z) inside grid in I self.mUt[Ln, Ln + 1] = 1.0 if (k - 1 >= 0): #u(x,z-1) self.mUt[Ln, Ln - self.Nx] = 1.0 if (k + 1 < self.Nz): #u(x,z+1) self.mUt[Ln, Ln + self.Nx] = 1.0 return self.mUt
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 construct_pysparse_matrix(n, nbr_elements): A = spmatrix.ll_mat(n, n, nbr_elements) for i in xrange(nbr_elements): A[i % n, (2 * i + 1) % n] = i / 3 return A
def __init__(self, portfolio): from pysparse.sparse import spmatrix # set up sparse matrices # these first two lists define all indices for asset and issuer arrays self.issuers = [i for i in portfolio.issuers()] self.assets = [a for a in portfolio.assets] self.asset_issuer_map = makeAssetIssuerIndexMap(self.issuers, self.assets) def ppfGen(assets): for ass in assets: yield norm.ppf(ass.dp) self.thresholds = np.fromiter(ppfGen(self.assets), np.double) self.n_issuers = len(self.issuers) self.n_assets = len(self.assets) factor_indices = portfolio.factor_indices() # on class for testing help self.n_factors = len(factor_indices.keys()) #do the factor weights, also running sum for ideosyncratic weights wm = spmatrix.ll_mat(self.n_issuers, self.n_factors+self.n_issuers) for i, iss in enumerate(self.issuers): wsum = 0.0 for f in iss.factors: j = factor_indices[f.name] w = np.sqrt(max(f.weight, 0.0)) wm[i, j] = w wsum += w*w wm[i, self.n_factors+i] = np.sqrt(max(1.0 - wsum, 0.0)) self.weights = wm.to_csr()
def ComputeMassMatrix(self, capacityVector): C = spmatrix.ll_mat(len(self.node),len(self.node)) capIndex = 0 for coords in self.elems: n1 = coords[0] n2 = coords[1] n3 = coords[2] n = [n1, n2, n3] x1 = self.node[n1][0] y1 = self.node[n1][1] x2 = self.node[n2][0] y2 = self.node[n2][1] x3 = self.node[n3][0] y3 = self.node[n3][1] Area = 0.5*(x2*y3-x3*y2+x3*y1-x1*y3+x1*y2-x2*y1) for i in range(0,3): i_global = n[i] for j in range(0,3): j_global = n[j] C[i_global, j_global] += float(capacityVector[capIndex]*(1.0/6.0)*Area) capIndex += 1 return C
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 setUp(self): self.nbr_elements = 100000 self.size = 1000000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, dtype=FLOAT64_T) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) self.A_s = lil_matrix((self.size, self.size), dtype=np.float64) # how do we reserve space in advance?
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 poisson1d(n): L = spmatrix.ll_mat(n, n, 3*n-2) for i in range(n): L[i,i] = 2 if i > 0: L[i,i-1] = -1 if i < n-1: L[i,i+1] = -1 return L
def setUp(self): self.nbr_elements = 10000 self.size = 100000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements)
def ComputeStiffnessMatrix(self, readings): X = spmatrix.ll_mat(len(self.node), len(self.node)) #Keeping track of index for conductivity conductIndex = 0 for coords in self.elems: n1 = coords[0] n2 = coords[1] n3 = coords[2] n = [n1, n2, n3] x1 = self.node[n1][0] y1 = self.node[n1][1] x2 = self.node[n2][0] y2 = self.node[n2][1] x3 = self.node[n3][0] y3 = self.node[n3][1] a1 = y2 - y3 a2 = y3 - y1 a3 = y1 - y2 b1 = x3 - x2 b2 = x1 - x3 b3 = x2 - x1 Area = .5 * ((x2 * y3) - (x3 * y2) + (x3 * y1) - (x1 * y3) + (x1 * y2) - (x2 * y1)) gx1 = a1 / (2 * Area) gx2 = a2 / (2 * Area) gx3 = a3 / (2 * Area) gy1 = b1 / (2 * Area) gy2 = b2 / (2 * Area) gy3 = b3 / (2 * Area) gx = [gx1, gx2, gx3] gy = [gy1, gy2, gy3] for i in range(0, 3): i_global = n[i] for j in range(0, 3): j_global = n[j] X[i_global, j_global] += float((gx[i] * gx[j] + gy[i] * gy[j]) * Area * readings[conductIndex]) conductIndex += 1 print X[0:5, 0:5] return X
def lsq(lsq_ff): """ :param lsq_ff: Convert the LSQP in the First Form(FF) :: minimize c'x + 1/2|Qx-d|^2 subject to L <= Bx <= U, (LSQP-FF) l <= x <= u, to the Second Form (SF):: minimize c'x +1/2|r|^2 subject to. [d] <= [Q I][r] <= [d], [L] <= [B 0][x] <= [U], (LSQP-SF) [l] <= [x] <= [u], -[inf] <= [r] <= [inf]. """ p,n = lsq_ff.Q.shape m,n = lsq_ff.B.shape new_B = spmatrix.ll_mat(m+p, n+p, m+n+2*p+lsq_ff.B.nnz+lsq_ff.Q.nnz) new_B[:p,:n] = lsq_ff.Q new_B[p:,:n] = lsq_ff.B new_B.put(1, range(p), range(n,n+p)) new_Lcon = np.zeros(p+m) new_Lcon[:p] = lsq_ff.d new_Lcon[p:] = lsq_ff.Lcon new_Ucon = np.zeros(p+m) new_Ucon[:p] = lsq_ff.d new_Ucon[p:] = lsq_ff.Ucon new_Lvar = -np.inf * np.ones(n+p) new_Lvar[:n] = lsq_ff.Lvar new_Uvar = np.inf * np.ones(n+p) new_Uvar[:n] = lsq_ff.Uvar new_Q = PysparseMatrix(nrow=n+p, ncol=n+p,\ sizeHint=p) new_Q.put(1, range(n,n+p), range(n,n+p)) new_d = np.zeros(n+p) new_c = np.zeros(n+p) new_c[:n] = lsq_ff.c return LSQModel(Q=new_Q, B=new_B, d=new_d, c= new_c, Lcon=new_Lcon, \ Ucon=new_Ucon, Lvar=new_Lvar, Uvar=new_Uvar, name= lsq_ff.name, dimQB=(p,n,m))
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 setUp(self): self.nbr_elements = 100000 self.size = 1000000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, dtype=FLOAT64_T) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) self.A_s = lil_matrix( (self.size, self.size), dtype=np.float64) # how do we reserve space in advance?
def poisson2d_vec(n): n2 = n*n L = spmatrix.ll_mat(n2, n2, 5*n2-4*n) d = np.arange(n2, dtype=np.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 ll_mat_rand(n, m, density): """return a ll_mat object representing a general n-by-m sparse matrix filled with random non-zero values The number of non-zero entries is less or equal than n*m*density. The values of the non-zero entries are in the range [0.0, 1.0).""" nnz = int(density * n * m) A = spmatrix.ll_mat(n, m, nnz) for k in xrange(nnz): i = random.randrange(n) j = random.randrange(m) A[i, j] = random.random() return A
def setUp(self): self.nbr_elements = 1000 self.size = 10000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements) self.stride = 10 self.v = np.arange(0, self.size * self.stride, dtype=np.float64)
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 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 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 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 setUp(self): self.nbr_elements = 5000 self.size = 1000000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, itype=INT32_T, dtype=FLOAT64_T) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) self.A_s = lil_matrix((self.size, self.size), dtype=np.float64) self.list_of_matrices = [] self.list_of_matrices.append(self.A_c) self.list_of_matrices.append(self.A_p) self.list_of_matrices.append(self.A_s) construct_random_matrices(self.list_of_matrices, self.size, self.nbr_elements) self.v = np.arange(0, self.size, dtype=np.float64)
def setUp(self): self.nbr_elements = 1000 self.size = 10000 self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, itype=INT32_T, dtype=FLOAT64_T) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) self.A_s = lil_matrix((self.size, self.size), dtype=np.float64) self.list_of_matrices = [] self.list_of_matrices.append(self.A_c) self.list_of_matrices.append(self.A_p) self.list_of_matrices.append(self.A_s) construct_random_matrices(self.list_of_matrices, self.size, self.nbr_elements) self.v = np.arange(0, self.size, dtype=np.float64)
def setUp(self): self.nbr_elements = 1000 self.size = 10000 self.put_size = 10000 assert self.put_size <= self.size self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, itype=INT32_T, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements) self.id1 = np.arange(0, self.put_size, dtype=np.int32) self.id2 = np.full(self.put_size, 37, dtype=np.int32) self.b = np.arange(0, self.put_size,dtype=np.float64)
def setUp(self): self.nbr_elements = 1000 self.size = 10000 self.nbr_elements_to_add = 1000 assert self.nbr_elements_to_add < self.size self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, itype=INT32_T, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements) self.id1 = np.ones(self.nbr_elements_to_add, dtype=np.int32) self.id2 = self.v = np.arange(0, self.nbr_elements_to_add, dtype=np.int32) self.val = np.empty(self.nbr_elements_to_add, dtype=np.float64)
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 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 poisson2d_vec2(n): # Second version, allocate long arrays n2 = n*n L = spmatrix.ll_mat(n2, n2, 5*n2-4*n) e = numpy.ones(n2) d = numpy.arange(n2, dtype=numpy.int) L.put(4*e, d, d) din = d[:n] for i in xrange(n): # Diagonal blocks L.put(-e[:n-1], din[1:], din[:-1]) L.put(-e[:n-1], din[:-1], din[1:]) # Outer blocks L.put(-e[:n], n+din, din) L.put(-e[:n], din, n+din) din = d[i*n:(i+1)*n] # Last diagonal block L.put(-e[:n-1], din[1:], din[:-1]) L.put(-e[:n-1], din[:-1], din[1:]) return L
def poisson2d_vec2(n): # Second version, allocate long arrays n2 = n * n L = spmatrix.ll_mat(n2, n2, 5 * n2 - 4 * n) e = numpy.ones(n2) d = numpy.arange(n2, dtype=numpy.int) L.put(4 * e, d, d) din = d[:n] for i in xrange(n): # Diagonal blocks L.put(-e[:n - 1], din[1:], din[:-1]) L.put(-e[:n - 1], din[:-1], din[1:]) # Outer blocks L.put(-e[:n], n + din, din) L.put(-e[:n], din, n + din) din = d[i * n:(i + 1) * n] # Last diagonal block L.put(-e[:n - 1], din[1:], din[:-1]) L.put(-e[:n - 1], din[:-1], din[1:]) return L
def poisson2d_vec(n): # First version, proceed block by block n2 = n * n L = spmatrix.ll_mat(n2, n2, 5 * n2 - 4 * n) e = numpy.ones(n) d = numpy.arange(n, dtype=numpy.int) din = d for i in xrange(n): # Diagonal blocks L.put(4 * e, din, din) L.put(-e[1:], din[1:], din[:-1]) L.put(-e[1:], din[:-1], din[1:]) # Outer blocks L.put(-e, n + din, din) L.put(-e, din, n + din) din = d + i * n # Last diagonal block L.put(4 * e, din, din) L.put(-e[1:], din[1:], din[:-1]) L.put(-e[1:], din[:-1], din[1:]) return L
def setUp(self): self.nbr_elements = 1000 self.size = 10000 self.put_size = 10000 assert self.put_size <= self.size self.A_c = LLSparseMatrix(size=self.size, size_hint=self.nbr_elements, itype=INT32_T, dtype=FLOAT64_T) construct_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat(self.size, self.size, self.nbr_elements) construct_sparse_matrix(self.A_p, self.size, self.nbr_elements) self.id1 = np.arange(0, self.put_size, dtype=np.int32) self.id2 = np.full(self.put_size, 37, dtype=np.int32) self.b = np.arange(0, self.put_size, dtype=np.float64)
def poisson2d_vec(n): # First version, proceed block by block n2 = n*n L = spmatrix.ll_mat(n2, n2, 5*n2-4*n) e = numpy.ones(n) d = numpy.arange(n, dtype=numpy.int) din = d for i in xrange(n): # Diagonal blocks L.put(4*e, din, din) L.put(-e[1:], din[1:], din[:-1]) L.put(-e[1:], din[:-1], din[1:]) # Outer blocks L.put(-e, n+din, din) L.put(-e, din, n+din) din = d + i*n # Last diagonal block L.put(4*e, din, din) L.put(-e[1:], din[1:], din[:-1]) L.put(-e[1:], din[:-1], din[1:]) return L
import time from pysparse.sparse 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 __init__(self, A, **kwargs): """ Create a PyMa27Context object representing a context to solve the square symmetric linear system of equations A x = b. A should be given in ll_mat format and should be symmetric. The system will first be analyzed and factorized, for later solution. Residuals will be computed dynamically if requested. The factorization is a multi-frontal variant of the Bunch-Parlett factorization, i.e. A = L B Lt where L is unit lower triangular, and B is symmetric and block diagonal with either 1x1 or 2x2 blocks. A special option is available is the matrix A is known to be symmetric (positive or negative) definite, or symmetric quasi-definite (sqd). SQD matrices have the general form [ E Gt ] [ G -F ] where both E and F are positive definite. As a special case, positive definite matrices and negative definite matrices are sqd. SQD matrices can be factorized with potentially much sparser factors. Moreover, the matrix B reduces to a diagonal matrix. Currently accepted keyword arguments are: sqd Flag indicating symmetric quasi-definite matrix (default: False) Example: from nlpy.linalg import pyma27 from nlpy.tools import norms P = pyma27.PyMa27Context( A ) P.solve( rhs, get_resid = True ) print norms.norm2( P.residual ) Pyma27 relies on the sparse direct multifrontal code MA27 from the Harwell Subroutine Library archive. """ if isinstance(A, PysparseMatrix): thisA = A.matrix else: thisA = A Sils.__init__(self, thisA, **kwargs) # Statistics on A self.rwords = 0 # Number of real words used during factorization self.iwords = 0 # int self.ncomp = 0 # data compresses performed in analysis self.nrcomp = 0 # real self.nicomp = 0 # int self.n2x2pivots = 0 # 2x2 pivots used self.neig = 0 # negative eigenvalues detected # Factors self.L = spmatrix.ll_mat(self.n, self.n, 0) self.B = spmatrix.ll_mat_sym(self.n, 0) # Analyze and factorize matrix self.context = _pyma27.factor(thisA, self.sqd) (self.rwords, self.iwords, self.ncomp, self.nrcomp, self.nicomp, self.n2x2pivots, self.neig, self.rank) = self.context.stats() self.isFullRank = (self.rank == self.n)
import math, os, sys, time import numpy as np from pysparse.sparse import spmatrix from pysparse.itsolvers.krylov import pcg, minres, qmrs, cgs from pysparse.precon import precon ll = spmatrix.ll_mat(5,5) print ll print ll[1,1] print ll ll[2,1] = 1.0 ll[1,3] = 2.0 print ll print ll.to_csr() print ll[1,3] print ll[1,-1] print ll.nnz ll.export_mtx('test.mtx') L = spmatrix.ll_mat(10, 10) for i in range(0, 10): L[i,i] = float(i+1) A = L.to_csr() x = np.ones([10], 'd') y = np.zeros([10], 'd') print A, x, y A.matvec(x, y)
def setUp(self): self.n = 10 self.A = spmatrix.ll_mat(self.n, self.n) self.S = spmatrix.ll_mat_sym(self.n)
def ComputeStiffnessMatrix(self, thermalReadings): Y = spmatrix.ll_mat(len(self.node), len(self.node)) computeTetra = False computeTri = False #Keeping track of index for conductivity conductIndex = 0 if len(self.node) == 3: computeTri = True else: computeTetra = True if computeTri == True: for coords in self.elems: n1 = coords[0] n2 = coords[1] n3 = coords[2] n = [n1, n2, n3] x1 = self.node[n1][0] y1 = self.node[n1][1] x2 = self.node[n2][0] y2 = self.node[n2][1] x3 = self.node[n3][0] y3 = self.node[n3][1] a1 = y2 - y3 a2 = y3 - y1 a3 = y1 - y2 b1 = x3 - x2 b2 = x1 - x3 b3 = x2 - x1 Area = .5 * ((x2 * y3) - (x3 * y2) + (x3 * y1) - (x1 * y3) + (x1 * y2) - (x2 * y1)) gx1 = a1 / (2 * Area) gx2 = a2 / (2 * Area) gx3 = a3 / (2 * Area) gy1 = b1 / (2 * Area) gy2 = b2 / (2 * Area) gy3 = b3 / (2 * Area) gx = [gx1, gx2, gx3] gy = [gy1, gy2, gy3] for i in range(0, 3): i_global = n[i] for j in range(0, 3): j_global = n[j] Y[i_global, j_global] += float( (gx[i] * gx[j] + gy[i] * gy[j]) * Area * thermalReadings[conductIndex]) conductIndex += 1 if computeTetra == True: for coords in self.elems: n1 = coords[0] n2 = coords[1] n3 = coords[2] n4 = coords[3] n = [n1, n2, n3, n4] x1 = self.node[n1][0] y1 = self.node[n1][1] z1 = self.node[n1][2] x2 = self.node[n2][0] y2 = self.node[n2][1] z2 = self.node[n2][2] x3 = self.node[n3][0] y3 = self.node[n3][1] z3 = self.node[n3][2] x4 = self.node[n4][0] y4 = self.node[n4][1] z4 = self.node[n4][2] a1 = -(y3 * z4 - z3 * y4 - y2 * z4 + z2 * y4 + y2 * z3 - z2 * y3) a2 = (y4 * z1 - z4 * y1 - y3 * z1 + z3 * y1 + y3 * z4 - z3 * y4) a3 = -(y1 * z2 - z1 * y2 - y4 * z2 + z4 * y2 + y4 * z1 - z4 * y1) a4 = (y2 * z3 - z2 * y3 - y1 * z3 + z1 * y3 + y1 * z2 - z1 * y2) b1 = -(x2 * z4 - x2 * z3 - x3 * z4 + x3 * z2 + x4 * z3 - x4 * z2) b2 = (x3 * z1 - x3 * z4 - x4 * z1 + x4 * z3 + x1 * z4 - x1 * z3) b3 = -(x4 * z2 - x4 * z1 - x1 * z2 + x1 * z4 + x2 * z1 - x2 * z4) b4 = (x1 * z3 - x1 * z2 - x2 * z3 + x2 * z1 + x3 * z2 - x3 * z1) c1 = -(x2 * y3 - x2 * y4 - x3 * y2 + x3 * y4 + x4 * y2 - x4 * y3) c2 = (x3 * y4 - x3 * y1 - x4 * y3 + x4 * y1 + x1 * y3 - x1 * y4) c3 = -(x4 * y1 - x4 * y2 - x1 * y4 + x1 * y2 + x2 * y4 - x2 * y1) c4 = (x1 * y2 - x1 * y3 - x2 * y1 + x2 * y3 + x3 * y1 - x3 * y2) Volume = (1.0 / 6.0) * ( x2 * y3 * z4 - x2 * z3 * y4 - x3 * y2 * z4 + x3 * z2 * y4 + x4 * y2 * z3 - x4 * z2 * y3 - x1 * y3 * z4 + x1 * z3 * y4 + x3 * y1 * z4 - x3 * z1 * y4 - x4 * y1 * z3 + x4 * z1 * y3 + x1 * y2 * z4 - x1 * z2 * y4 - x2 * y1 * z4 + x2 * z1 * y4 + x4 * y1 * z2 - x4 * z1 * y2 - x1 * y2 * z3 + x1 * z2 * y3 + x2 * y1 * z3 - x2 * z1 * y3 - x3 * y1 * z2 + x3 * z1 * y2) gx1 = a1 / (6 * Volume) gx2 = a2 / (6 * Volume) gx3 = a3 / (6 * Volume) gx4 = a4 / (6 * Volume) gy1 = b1 / (6 * Volume) gy2 = b2 / (6 * Volume) gy3 = b3 / (6 * Volume) gy4 = b4 / (6 * Volume) gz1 = c1 / (6 * Volume) gz2 = c2 / (6 * Volume) gz3 = c3 / (6 * Volume) gz4 = c4 / (6 * Volume) gx = [gx1, gx2, gx3, gx4] gy = [gy1, gy2, gy3, gy4] gz = [gz1, gz2, gz3, gz4] for i in range(0, 4): i_global = n[i] for j in range(0, 4): j_global = n[j] Y[i_global, j_global] += float( (gx[i] * gx[j] + gy[i] * gy[j] + gz[i] * gz[j]) * Volume * thermalReadings[conductIndex]) conductIndex += 1 return Y
from pysparse.sparse import spmatrix from pysparse.direct import umfpack import numpy as np from poisson2dvec import poisson2d_vec_sym_blk l = spmatrix.ll_mat(5, 5) l[0, 0] = 2.0 l[0, 1] = 3.0 l[1, 4] = 6.0 l[1, 0] = 3.0 l[1, 2] = 4.0 l[2, 1] = -1.0 l[2, 2] = -3.0 l[2, 3] = 2.0 l[3, 2] = 1.0 l[4, 1] = 4.0 l[4, 2] = 2.0 l[4, 4] = 1.0 b = np.array([8.0, 45.0, -3.0, 3.0, 19.0], "d") x = np.zeros(5, "d") umf = umfpack.factorize(l) umf.solve(b, x, 'UMFPACK_A') print umf.getlists() print x print "------------------------------" n = 50 L = poisson2d_vec_sym_blk(n) b = np.ones(n * n, 'd')
import math, os, sys, time import numpy as np from pysparse.sparse import spmatrix from pysparse.itsolvers.krylov import pcg, minres, qmrs, cgs from pysparse.precon import precon ll = spmatrix.ll_mat(5, 5) print ll print ll[1, 1] print ll ll[2, 1] = 1.0 ll[1, 3] = 2.0 print ll print ll.to_csr() print ll[1, 3] print ll[1, -1] print ll.nnz ll.export_mtx('test.mtx') L = spmatrix.ll_mat(10, 10) for i in range(0, 10): L[i, i] = float(i + 1) A = L.to_csr() x = np.ones([10], 'd') y = np.zeros([10], 'd') print A, x, y A.matvec(x, y)
import numpy as np import traceback from pysparse.sparse import spmatrix from pysparse.tools import spmatrix_util def printMatrix(M): n, m = M.shape Z = np.zeros((n,m), 'd') for i in range(n): for j in range(m): Z[i,j] = M[i,j] print str(Z) + '\n' n = 10 A = spmatrix.ll_mat(n,n) As = spmatrix.ll_mat_sym(n) Is = spmatrix.ll_mat_sym(n) I = spmatrix.ll_mat(n,n) Os = spmatrix.ll_mat_sym(n) O = spmatrix.ll_mat(n,n) for i in range(n): for j in range(n): if i >= j: A[i,j] = 10*i + j else: A[i,j] = 10*j + i O[i,j] = 1 for i in range(n): for j in range(n):
from pysparse.sparse.spmatrix import ll_mat import numpy as np import time n = 1000 nnz = 50000 A = ll_mat(n, n, nnz) R = np.random.random_integers(0, n-1, (nnz,2)) t1 = time.clock() for k in xrange(nnz): A[int(R[k,0]), int(R[k,1])] = k print 'Time for populating matrix: %8.2f s' % (time.clock() - t1) print 'nnz(A) = ', A.nnz B = A[:,:] A.shift(-1.0, B) print A
from pysparse.sparse import spmatrix from pysparse.direct import umfpack import numpy as np from poisson2dvec import poisson2d_vec_sym_blk l = spmatrix.ll_mat(5, 5) l[0,0] = 2.0 l[0,1] = 3.0 l[1,4] = 6.0 l[1,0] = 3.0 l[1,2] = 4.0 l[2,1] = -1.0 l[2,2] = -3.0 l[2,3] = 2.0 l[3,2] = 1.0 l[4,1] = 4.0 l[4,2] = 2.0 l[4,4] = 1.0 b = np.array([8.0, 45.0, -3.0, 3.0, 19.0], "d") x = np.zeros(5, "d") umf = umfpack.factorize(l) umf.solve(b, x, 'UMFPACK_A') print umf.getlists() print x print "------------------------------" n = 50 L = poisson2d_vec_sym_blk(n) b = np.ones(n * n, 'd')
def __init__( self, A, **kwargs ): """ Create a PyMa27Context object representing a context to solve the square symmetric linear system of equations A x = b. A should be given in ll_mat format and should be symmetric. The system will first be analyzed and factorized, for later solution. Residuals will be computed dynamically if requested. The factorization is a multi-frontal variant of the Bunch-Parlett factorization, i.e. A = L B Lt where L is unit lower triangular, and B is symmetric and block diagonal with either 1x1 or 2x2 blocks. A special option is available is the matrix A is known to be symmetric (positive or negative) definite, or symmetric quasi-definite (sqd). SQD matrices have the general form [ E Gt ] [ G -F ] where both E and F are positive definite. As a special case, positive definite matrices and negative definite matrices are sqd. SQD matrices can be factorized with potentially much sparser factors. Moreover, the matrix B reduces to a diagonal matrix. Currently accepted keyword arguments are: sqd Flag indicating symmetric quasi-definite matrix (default: False) Example: from nlpy.linalg import pyma27 from nlpy.tools import norms P = pyma27.PyMa27Context( A ) P.solve( rhs, get_resid = True ) print norms.norm2( P.residual ) Pyma27 relies on the sparse direct multifrontal code MA27 from the Harwell Subroutine Library archive. """ if isinstance(A, PysparseMatrix): thisA = A.matrix else: thisA = A Sils.__init__( self, thisA, **kwargs ) # Statistics on A self.rwords = 0 # Number of real words used during factorization self.iwords = 0 # int self.ncomp = 0 # data compresses performed in analysis self.nrcomp = 0 # real self.nicomp = 0 # int self.n2x2pivots = 0 # 2x2 pivots used self.neig = 0 # negative eigenvalues detected # Factors self.L = spmatrix.ll_mat( self.n, self.n, 0 ) self.B = spmatrix.ll_mat_sym( self.n, 0 ) # Analyze and factorize matrix self.context = _pyma27.factor( thisA, self.sqd ) (self.rwords, self.iwords, self.ncomp, self.nrcomp, self.nicomp, self.n2x2pivots, self.neig, self.rank) = self.context.stats() self.isFullRank = (self.rank == self.n)