import numpy as np from numpy random import randn as random_matrix fro

 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
import numpy as np
from numpy.random import randn as random_matrix
from random import randint
import scipy
def ssor_precondition(w=1.0, n=5):
# tridiagonal
random = random_matrix(n, n)
random_triang_upper = np.triu(random,-1)
random_tridiag = np.tril(random_triang_upper, 1)
L = random_tridiag
L_t = np.transpose(L)
# diagonal matrix
D = np.zeros([n, n])
D[np.diag_indices_from(D)] = [randint(1, 10) for i in xrange(n)]
return D + L + L_t, (D - w * L) * np.linalg.inv(D) * np.transpose(D - w * L)
def jacobi_precondition(n=10):
A = random_matrix(n, n)
D = np.tril(np.triu(A, 0))
return A, D