def poisson2d_sym_blk(n): L = spmatrix.ll_mat_sym(n*n) I = spmatrix.ll_mat_sym(n) P = spmatrix.ll_mat_sym(n) for i in range(n): I[i,i] = -1 for i in range(n): P[i,i] = 4 if i > 0: P[i,i-1] = -1 for i in range(0, n*n, n): L[i:i+n,i:i+n] = P if i > 0: L[i:i+n,i-n:i] = I return L
def poisson2d_vec_sym_blk(n): n2 = n*n L = spmatrix.ll_mat_sym(n2, 3*n2-2*n) D = spmatrix.ll_mat_sym(n, 2*n-1) d = np.arange(n, dtype=np.int) D.put(4.0, d) D.put(-1.0, d[1:], d[:-1]) P = spmatrix.ll_mat_sym(n, n-1) P.put(-1,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[-n:,-n:] = D return L
def __init__(self, A, **kwargs): GenericPreconditioner.__init__(self, A, **kwargs) n = self.shape[0] self.bandwidth = kwargs.get('bandwidth', 5) self.threshold = kwargs.get('threshold', 1.0e-3) # See how many nonzeros are in the requested band of A nnz = 0 sumrowelm = np.zeros(n, 'd') for j in range(n): for i in range(min(self.bandwidth, n-j-1)): if A[j+i+1,j] != 0.0: nnz += 1 sumrowelm[j] += abs(A[j+i+1,j]) M = spmatrix.ll_mat_sym(n, nnz+n) # Assemble banded matrix --- ensure it is positive definite for j in range(n): M[j,j] = max(A[j,j], 2*sumrowelm[j]+ self.threshold) for i in range(min(self.bandwidth, n-j-1)): if A[j+i+1,j] != 0.0: M[j+i+1,j] = A[j+i+1,j] # Factorize preconditioner self.lbl = LBLContext(M) # Only need factors of M --- can discard M itself del M
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 poisson1d_sym(n): L = spmatrix.ll_mat_sym(n, 2*n-1) for i in range(n): L[i,i] = 2 if i > 0: L[i,i-1] = -1 return L
def poisson1d_sym_vec(n): L = spmatrix.ll_mat_sym(n, 2 * n - 1) 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]) return L
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 __init__(self, A, **kwargs): GenericPreconditioner.__init__(self, A, **kwargs) n = self.shape[0] self.bandwidth = kwargs.get('bandwidth', 5) self.threshold = kwargs.get('threshold', 1.0e-3) # See how many nonzeros are in the requested band of A nnz = 0 sumrowelm = np.zeros(n, 'd') for j in range(n): for i in range(min(self.bandwidth, n - j - 1)): if A[j + i + 1, j] != 0.0: nnz += 1 sumrowelm[j] += abs(A[j + i + 1, j]) M = spmatrix.ll_mat_sym(n, nnz + n) # Assemble banded matrix --- ensure it is positive definite for j in range(n): M[j, j] = max(A[j, j], 2 * sumrowelm[j] + self.threshold) for i in range(min(self.bandwidth, n - j - 1)): if A[j + i + 1, j] != 0.0: M[j + i + 1, j] = A[j + i + 1, j] # Factorize preconditioner self.lbl = LBLContext(M) # Only need factors of M --- can discard M itself del M
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 poisson1d_sym_vec(n): L = spmatrix.ll_mat_sym(n, 2*n-1) 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]) 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_vec_sym(n): n2 = n*n L = spmatrix.ll_mat_sym(n2, 3*n2-2*n) d = np.arange(n2, dtype=np.int) L.put(4.0, d) 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:]) return L
def testNormSymmetric(self): A = spmatrix.ll_mat_sym(4) A[0, 0] = 1 A[1, 1] = 2 A[2, 2] = 3 A[3, 3] = 4 A[1, 0] = 3 A[2, 0] = 2 A[3, 0] = 2 self.failUnless(A.norm("fro") == 8)
def testNormSymmetric(self): A = spmatrix.ll_mat_sym(4) A[0, 0] = 1 A[1, 1] = 2 A[2, 2] = 3 A[3, 3] = 4 A[1, 0] = 3 A[2, 0] = 2 A[3, 0] = 2 self.failUnless(A.norm('fro') == 8)
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, is_symmetric=True) construct_sym_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat_sym(self.size, self.size, self.nbr_elements) construct_sym_sparse_matrix(self.A_p, self.size, self.nbr_elements)
def poisson2d_sym(n): L = spmatrix.ll_mat_sym(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 j > 0: L[k,k-n] = -1 return L
def poisson2d_sym(n): L = spmatrix.ll_mat_sym(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 j > 0: L[k, k - n] = -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, store_symmetric=True) construct_sym_sparse_matrix(self.A_c, self.size, self.nbr_elements) self.A_p = spmatrix.ll_mat_sym(self.size, self.size, self.nbr_elements) construct_sym_sparse_matrix(self.A_p, self.size, self.nbr_elements)
def testSubmatrix(self): n = self.n Psym = poisson.poisson1d_sym(n) P = poisson.poisson1d(n) for i in range(n): P[i, i] = 4.0 Psym[i, i] = 4.0 # read and test diagonal blocks for i in range(n): self.failUnless( llmat_isEqual(self.A[n * i:n * (i + 1), n * i:n * (i + 1)], P)) #self.failUnless(llmat_isEqual(self.S[n*i:n*(i+1),n*i:n*(i+1)], P)) self.failUnless( llmat_isEqual(self.A[n * i:n * (i + 1), n * i:n * (i + 1)], Psym)) #self.failUnless(llmat_isEqual(self.S[n*i:n*(i+1),n*i:n*(i+1)], Psym)) # store and get diagonal blocks R = spmatrix_util.ll_mat_rand(n * n, n * n, 0.01) # random matrix for i in range(n): R[n * i:n * (i + 1), n * i:n * (i + 1)] = P self.failUnless( llmat_isEqual(R[n * i:n * (i + 1), n * i:n * (i + 1)], P)) R[n * i:n * (i + 1), n * i:n * (i + 1)] = Psym self.failUnless( llmat_isEqual(R[n * i:n * (i + 1), n * i:n * (i + 1)], Psym)) # store and get off-diagonal blocks for i in range(n - 1): R[n * i:n * (i + 1), n * (i + 1):n * (i + 2)] = P self.failUnless( llmat_isEqual(R[n * i:n * (i + 1), n * (i + 1):n * (i + 2)], P)) R[n * i:n * (i + 1), n * (i + 1):n * (i + 2)] = Psym self.failUnless( llmat_isEqual(R[n * i:n * (i + 1), n * (i + 1):n * (i + 2)], Psym)) # store and get diagonal blocks in symmetric matrix R = spmatrix.ll_mat_sym(n * n) for i in range(n): R[n * i:n * (i + 1), n * i:n * (i + 1)] = Psym self.failUnless( llmat_isEqual(R[n * i:n * (i + 1), n * i:n * (i + 1)], Psym)) # store and get off-diagonal blocks in symmetric matrix for i in range(n - 1): R[n * (i + 1):n * (i + 2), n * i:n * (i + 1)] = P self.failUnless( llmat_isEqual(R[n * (i + 1):n * (i + 2), n * i:n * (i + 1)], P)) R[n * (i + 1):n * (i + 2), n * i:n * (i + 1)] = Psym self.failUnless( llmat_isEqual(R[n * (i + 1):n * (i + 2), n * i:n * (i + 1)], Psym))
def setUp(self): import numpy self.n = 30 self.P = poisson.poisson1d(self.n) for i in range(self.n): self.P[i, i] = 4.0 self.A = poisson.poisson2d(self.n) self.S = poisson.poisson2d_sym(self.n) self.I = spmatrix.ll_mat_sym(self.n) for i in range(self.n): self.I[i, i] = -1.0 self.mask = numpy.zeros(self.n ** 2, "l") self.mask[self.n / 2 * self.n : (self.n / 2 + 1) * self.n] = 1 self.mask1 = numpy.zeros(self.n ** 2, "l") self.mask1[(self.n / 2 + 1) * self.n : (self.n / 2 + 2) * self.n] = 1
def setUp(self): import numpy self.n = 30 self.P = poisson.poisson1d(self.n) for i in range(self.n): self.P[i, i] = 4.0 self.A = poisson.poisson2d(self.n) self.S = poisson.poisson2d_sym(self.n) self.I = spmatrix.ll_mat_sym(self.n) for i in range(self.n): self.I[i, i] = -1.0 self.mask = numpy.zeros(self.n**2, 'l') self.mask[self.n / 2 * self.n:(self.n / 2 + 1) * self.n] = 1 self.mask1 = numpy.zeros(self.n**2, 'l') self.mask1[(self.n / 2 + 1) * self.n:(self.n / 2 + 2) * self.n] = 1
def poisson2d_sym_vec(n): n2 = n*n L = spmatrix.ll_mat_sym(n2, 3*n2-2*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]) # Outer blocks L.put(-e, n+din, din) din = d + i*n # Last diagonal block L.put(4*e, din, din) L.put(-e[1:], din[1:], din[:-1]) return L
def poisson2d_sym_vec(n): n2 = n * n L = spmatrix.ll_mat_sym(n2, 3 * n2 - 2 * 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]) # Outer blocks L.put(-e, n + din, din) din = d + i * n # Last diagonal block L.put(4 * e, din, din) L.put(-e[1:], din[1:], din[:-1]) return L
def Factorize(self): """ Assemble projection matrix and factorize it P = [ G A^T ] [ A 0 ], where G is the preconditioner, or the identity matrix if no preconditioner was given. """ if self.A is None: raise ValueError, 'No linear equality constraints were specified' # Form projection matrix P = spmatrix.ll_mat_sym(self.n + self.m, self.nnzA + self.n) if self.precon is not None: P[:self.n,:self.n] = self.precon else: r = range(self.n) P.put(1, r, r) #for i in range(self.n): # P[i,i] = 1 P[self.n:,:self.n] = self.A # Add regularization if requested. if self.dreg > 0.0: r = range(self.n, self.n + self.m) P.put(-self.dreg, r, r) if self.debug: msg = 'Factorizing projection matrix ' msg += '(size %-d, nnz = %-d)...\n' % (P.shape[0],P.nnz) self._write(msg) self.t_fact = cputime() self.Proj = LBLContext(P) self.t_fact = cputime() - self.t_fact if self.debug: msg = ' done (%-5.2fs)\n' % self.t_fact self._write(msg) self.factorized = True return
def Factorize(self): """ Assemble projection matrix and factorize it P = [ G A^T ] [ A 0 ], where G is the preconditioner, or the identity matrix if no preconditioner was given. """ if self.A is None: raise ValueError, 'No linear equality constraints were specified' # Form projection matrix P = spmatrix.ll_mat_sym(self.n + self.m, self.nnzA + self.n) if self.precon is not None: P[:self.n, :self.n] = self.precon else: r = range(self.n) P.put(1, r, r) #for i in range(self.n): # P[i,i] = 1 P[self.n:, :self.n] = self.A # Add regularization if requested. if self.dreg > 0.0: r = range(self.n, self.n + self.m) P.put(-self.dreg, r, r) if self.debug: msg = 'Factorizing projection matrix ' msg += '(size %-d, nnz = %-d)...\n' % (P.shape[0], P.nnz) self._write(msg) self.t_fact = cputime() self.Proj = LBLContext(P) self.t_fact = cputime() - self.t_fact if self.debug: msg = ' done (%-5.2fs)\n' % self.t_fact self._write(msg) self.factorized = True return
def testSubmatrix(self): n = self.n Psym = poisson.poisson1d_sym(n) P = poisson.poisson1d(n) for i in range(n): P[i, i] = 4.0 Psym[i, i] = 4.0 # read and test diagonal blocks for i in range(n): self.failUnless(llmat_isEqual(self.A[n * i : n * (i + 1), n * i : n * (i + 1)], P)) # self.failUnless(llmat_isEqual(self.S[n*i:n*(i+1),n*i:n*(i+1)], P)) self.failUnless(llmat_isEqual(self.A[n * i : n * (i + 1), n * i : n * (i + 1)], Psym)) # self.failUnless(llmat_isEqual(self.S[n*i:n*(i+1),n*i:n*(i+1)], Psym)) # store and get diagonal blocks R = spmatrix_util.ll_mat_rand(n * n, n * n, 0.01) # random matrix for i in range(n): R[n * i : n * (i + 1), n * i : n * (i + 1)] = P self.failUnless(llmat_isEqual(R[n * i : n * (i + 1), n * i : n * (i + 1)], P)) R[n * i : n * (i + 1), n * i : n * (i + 1)] = Psym self.failUnless(llmat_isEqual(R[n * i : n * (i + 1), n * i : n * (i + 1)], Psym)) # store and get off-diagonal blocks for i in range(n - 1): R[n * i : n * (i + 1), n * (i + 1) : n * (i + 2)] = P self.failUnless(llmat_isEqual(R[n * i : n * (i + 1), n * (i + 1) : n * (i + 2)], P)) R[n * i : n * (i + 1), n * (i + 1) : n * (i + 2)] = Psym self.failUnless(llmat_isEqual(R[n * i : n * (i + 1), n * (i + 1) : n * (i + 2)], Psym)) # store and get diagonal blocks in symmetric matrix R = spmatrix.ll_mat_sym(n * n) for i in range(n): R[n * i : n * (i + 1), n * i : n * (i + 1)] = Psym self.failUnless(llmat_isEqual(R[n * i : n * (i + 1), n * i : n * (i + 1)], Psym)) # store and get off-diagonal blocks in symmetric matrix for i in range(n - 1): R[n * (i + 1) : n * (i + 2), n * i : n * (i + 1)] = P self.failUnless(llmat_isEqual(R[n * (i + 1) : n * (i + 2), n * i : n * (i + 1)], P)) R[n * (i + 1) : n * (i + 2), n * i : n * (i + 1)] = Psym self.failUnless(llmat_isEqual(R[n * (i + 1) : n * (i + 2), n * i : n * (i + 1)], Psym))
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)
def setUp(self): self.n = 10 self.A = spmatrix.ll_mat(self.n, self.n) self.S = spmatrix.ll_mat_sym(self.n)
u = Q[:, k].copy() A.matvec(u, r) if M <> None: M.matvec(u, t) else: t = u r = r - lmbd[k] * t residuals[k] = sqrt(dot(r, r)) return residuals n = 1000 ncv = 5 tol = 1e-6 A = spmatrix.ll_mat_sym(n) for i in xrange(n): A[i, i] = i + 1.0 As = A.to_sss() M = spmatrix.ll_mat_sym(n) for i in xrange(n): M[i, i] = float(n / 2) + i Ms = M.to_sss() normM = M[n - 1, n - 1] K = diagPrecShifted(A, M, 0.006) #------------------------------------------------------------------------------- # Test 1: M = K = None
for p in primes[:nof]: if i%p == 0 or p*p > i: break if i%p <> 0: primes[nof] = i nof += 1 if nof >= nofPrimes: break i = i+2 return primes n = 20000 print 'Generating first %d primes...' % n primes = get_primes(n) print 'Assembling coefficient matrix...' A = spmatrix.ll_mat_sym(n, n*8) d = 1 while d < n: for i in range(d, n): A[i,i-d] = 1.0 d *= 2 for i in range(n): A[i,i] = 1.0 * primes[i] A = A.to_sss() K = precon.ssor(A) print 'Solving linear system...' b = np.zeros(n); b[0] = 1.0 x = np.empty(n) info, iter, relres = minres(A, b, x, 1e-16, n, K)
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)
if i % p == 0 or p * p > i: break if i % p <> 0: primes[nof] = i nof += 1 if nof >= nofPrimes: break i = i + 2 return primes n = 20000 print 'Generating first %d primes...' % n primes = get_primes(n) print 'Assembling coefficient matrix...' A = spmatrix.ll_mat_sym(n, n * 8) d = 1 while d < n: for i in range(d, n): A[i, i - d] = 1.0 d *= 2 for i in range(n): A[i, i] = 1.0 * primes[i] A = A.to_sss() K = precon.ssor(A) print 'Solving linear system...' b = np.zeros(n) b[0] = 1.0 x = np.empty(n)
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):
u = zeros((n, ), 'd') t = zeros((n, ), 'd') for k in xrange(kconv): u = Q[:,k].copy() A.matvec(u, r) if M <> None: M.matvec(u, t) else: t = u r = r - lmbd[k]*t residuals[k] = sqrt(dot(r,r)) return residuals n = 1000; ncv = 5; tol = 1e-6 A = spmatrix.ll_mat_sym(n) for i in xrange(n): A[i,i] = i+1.0 As = A.to_sss() M = spmatrix.ll_mat_sym(n) for i in xrange(n): M[i,i] = float(n/2) + i Ms = M.to_sss() normM = M[n-1,n-1] K = diagPrecShifted(A, M, 0.006) #------------------------------------------------------------------------------- # Test 1: M = K = None