def solve(A, b, iters, verbose): print("Solving system...") x = np.zeros(A.shape[1]) d = np.diag(A) R = A - np.diag(d) for i in range(iters): x = (b - np.dot(R, x)) / d return x
def generate_2D(N, corners): if corners: print("Generating %dx%d 2-D adjacency system with corners..." % (N**2, N**2)) A = np.zeros((N**2, N**2)) + 8 * np.eye(N**2) else: print("Generating %dx%d 2-D adjacency system without corners..." % (N**2, N**2)) A = np.zeros((N**2, N**2)) + 4 * np.eye(N**2) # These are the same for both cases off_one = np.full(N**2 - 1, -1, dtype=np.float64) A += np.diag(off_one, k=1) A += np.diag(off_one, k=-1) off_N = np.full(N * (N - 1), -1, dtype=np.float64) A += np.diag(off_N, k=N) A += np.diag(off_N, k=-N) # If we have corners then we have four more cases if corners: off_N_plus = np.full(N * (N - 1) - 1, -1, dtype=np.float64) A += np.diag(off_N_plus, k=N + 1) A += np.diag(off_N_plus, k=-(N + 1)) off_N_minus = np.full(N * (N - 1) + 1, -1, dtype=np.float64) A += np.diag(off_N_minus, k=N - 1) A += np.diag(off_N_minus, k=-(N - 1)) # Then we can generate a random b matrix b = np.random.rand(N**2) return A, b
def precondition(A, N, corners): if corners: d = 8 * (N**2) else: d = 4 * (N**2) M = np.diag(np.full(N**2, 1.0 / d)) return M
def test(): a = lg.array([ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20], ]) an = np.array([ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20], ]) b = lg.diag(a) bn = np.diag(an) assert np.array_equal(b, bn) c = lg.diag(b) cn = np.diag(bn) assert np.array_equal(c, cn) d = lg.array([ [1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], ]) dn = np.array([ [1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], ]) e = lg.diag(d) en = np.diag(dn) assert np.array_equal(e, en) f = lg.diag(e) fn = np.diag(en) assert np.array_equal(f, fn) return