/usr/share/pyshared/SparsExamples/poisson_test.py is in python-sparse-examples 1.1-1.3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | import numpy
import math
from pysparse import spmatrix
from pysparse import itsolvers
from pysparse import precon
import time
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(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_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
tol = 1e-8
n = 100
t1 = time.clock()
L = poisson2d_sym_blk(n)
print 'Time for constructing the matrix using poisson2d_sym_blk: %8.2f sec' % (time.clock() - t1, )
t1 = time.clock()
L = poisson2d_sym(n)
print 'Time for constructing the matrix using poisson2d_sym : %8.2f sec' % (time.clock() - t1, )
t1 = time.clock()
L = poisson2d(n)
print 'Time for constructing the matrix using poisson2d : %8.2f sec' % (time.clock() - t1, )
A = L.to_csr()
S = L.to_sss()
print L.nnz
print S.nnz
print A.nnz
b = numpy.ones(n*n, 'd')
# ---------------------------------------------------------------------------------------
t1 = time.clock()
x = numpy.zeros(n*n, 'd')
info, iter, relres = itsolvers.pcg(S, b, x, tol, 2000)
print 'info=%d, iter=%d, relres=%e' % (info, iter, relres)
print 'Time for solving the system using SSS matrix: %8.2f sec' % (time.clock() - t1, )
print 'norm(x) = %g' % math.sqrt(numpy.dot(x, x))
r = numpy.zeros(n*n, 'd')
S.matvec(x, r)
r = b - r
print 'norm(b - A*x) = %g' % math.sqrt(numpy.dot(r, r))
print x[0:10]
# ---------------------------------------------------------------------------------------
t1 = time.clock()
x = numpy.zeros(n*n, 'd')
info, iter, relres = itsolvers.pcg(A, b, x, tol, 2000)
print 'info=%d, iter=%d, relres=%e' % (info, iter, relres)
print 'Time for solving the system using CSR matrix: %8.2f sec' % (time.clock() - t1, )
print 'norm(x) = %g' % math.sqrt(numpy.dot(x, x))
r = numpy.zeros(n*n, 'd')
A.matvec(x, r)
r = b - r
print 'norm(b - A*x) = %g' % math.sqrt(numpy.dot(r, r))
# ---------------------------------------------------------------------------------------
t1 = time.clock()
x = numpy.zeros(n*n, 'd')
info, iter, relres = itsolvers.pcg(L, b, x, tol, 2000)
print 'info=%d, iter=%d, relres=%e' % (info, iter, relres)
print 'Time for solving the system using LL matrix: %8.2f sec' % (time.clock() - t1, )
print 'norm(x) = %g' % math.sqrt(numpy.dot(x, x))
r = numpy.zeros(n*n, 'd')
A.matvec(x, r)
r = b - r
print 'norm(b - A*x) = %g' % math.sqrt(numpy.dot(r, r))
# ---------------------------------------------------------------------------------------
K_ssor = precon.ssor(S, 1.9)
t1 = time.clock()
x = numpy.zeros(n*n, 'd')
info, iter, relres = itsolvers.pcg(S, b, x, tol, 2000, K_ssor)
print 'info=%d, iter=%d, relres=%e' % (info, iter, relres)
print 'Time for solving the system using SSS matrix and SSOR preconditioner: %8.2f sec' % (time.clock() - t1, )
print 'norm(x) = %g' % math.sqrt(numpy.dot(x, x))
r = numpy.zeros(n*n, 'd')
S.matvec(x, r)
r = b - r
print 'norm(b - A*x) = %g' % math.sqrt(numpy.dot(r, r))
# ---------------------------------------------------------------------------------------
from pysparse import jdsym
jdsym.jdsym(S, None, None, 5, 0.0, 1e-8, 100, itsolvers.qmrs, clvl=1)
|