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)))
print "norm(x) = %g" % np.linalg.norm(x) r = np.empty(n * n, "d") A.matvec(x, r) r = b - r print "norm(b - A*x) = %g" % np.linalg.norm(r) # ----------------------------------------------------------------------------- K_ssor = precon.ssor(S, 1.9) t1 = time.clock() x = np.empty(n * n, "d") info, iter, relres = pcg(S, b, x, tol, 2000, K_ssor) print "info=%d, iter=%d, relres=%e" % (info, iter, relres) print "Solve time using SSS matrix and SSOR preconditioner: %8.2f sec" % (time.clock() - t1) print "norm(x) = %g" % np.linalg.norm(x) r = np.empty(n * n, "d") S.matvec(x, r) r = b - r print "norm(b - A*x) = %g" % np.linalg.norm(r) # ----------------------------------------------------------------------------- from pysparse.eigen import jdsym jdsym.jdsym(S, None, None, 5, 0.0, 1e-8, 100, qmrs, clvl=1)
from pysparse.precon import precon from pysparse.eigen import jdsym import time 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 n = 200 t1 = time.clock() L = poisson2d_sym_blk(n) print 'Time for constructing the matrix: %8.2f sec' % (time.clock() - t1) print L.nnz # ----------------------------------------------------------------------------- t1 = time.clock() jdsym.jdsym(L.to_sss(), None, None, 5, 0.0, 1e-8, 100, qmrs, clvl=1) print 'Time spend in jdsym: %8.2f sec' % (time.clock() - t1)
Ms = M.to_sss() normM = M[n-1,n-1] K = diagPrecShifted(A, M, 0.006) #------------------------------------------------------------------------------- # Test 1: M = K = None print 'Test 1' lmbd_exact = zeros(ncv, 'd') for k in xrange(ncv): lmbd_exact[k] = A[k,k] kconv, lmbd, Q, it, it_inner = jdsym.jdsym(As, None, None, ncv, 0.0, tol, 150, qmrs, jmin=5, jmax=10, eps_tr=1e-4, clvl=1) assert ncv == kconv assert allclose(computeResiduals(As, None, lmbd, Q), zeros(kconv), 0.0, tol) assert allclose(lmbd, lmbd_exact, tol*tol, 0.0) print 'OK' #------------------------------------------------------------------------------- # Test 2: K = None print 'Test 2', lmbd_exact = zeros(ncv, 'd') for k in xrange(ncv):
#------------------------------------------------------------------------------- # Test 1: M = K = None print 'Test 1' lmbd_exact = zeros(ncv, 'd') for k in xrange(ncv): lmbd_exact[k] = A[k, k] kconv, lmbd, Q, it, it_inner = jdsym.jdsym(As, None, None, ncv, 0.0, tol, 150, qmrs, jmin=5, jmax=10, eps_tr=1e-4, clvl=1) assert ncv == kconv assert allclose(computeResiduals(As, None, lmbd, Q), zeros(kconv), 0.0, tol) assert allclose(lmbd, lmbd_exact, tol * tol, 0.0) print 'OK' #------------------------------------------------------------------------------- # Test 2: K = None
print 'norm(x) = %g' % np.linalg.norm(x) r = np.empty(n * n, 'd') A.matvec(x, r) r = b - r print 'norm(b - A*x) = %g' % np.linalg.norm(r) # ----------------------------------------------------------------------------- K_ssor = precon.ssor(S, 1.9) t1 = time.clock() x = np.empty(n * n, 'd') info, iter, relres = pcg(S, b, x, tol, 2000, K_ssor) print 'info=%d, iter=%d, relres=%e' % (info, iter, relres) print 'Solve time using SSS matrix and SSOR preconditioner: %8.2f sec' % ( time.clock() - t1) print 'norm(x) = %g' % np.linalg.norm(x) r = np.empty(n * n, 'd') S.matvec(x, r) r = b - r print 'norm(b - A*x) = %g' % np.linalg.norm(r) # ----------------------------------------------------------------------------- from pysparse.eigen import jdsym jdsym.jdsym(S, None, None, 5, 0.0, 1e-8, 100, qmrs, clvl=1)
import time 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 n = 200 t1 = time.clock() L = poisson2d_sym_blk(n) print 'Time for constructing the matrix: %8.2f sec' % (time.clock() - t1) print L.nnz # ----------------------------------------------------------------------------- t1 = time.clock() jdsym.jdsym(L.to_sss(), None, None, 5, 0.0, 1e-8, 100, qmrs, clvl=1) print 'Time spend in jdsym: %8.2f sec' % (time.clock() - t1)