import numpy as np
import scipy as sp
import scipy.sparse as sparse
import scipy.sparse.linalg as sla
import matplotlib.pyplot as plt
%matplotlib inline
n = 100
A = sparse.diags([-1, 2, -1], [-1, 0, 1], shape=(n,n), format='csr')
b = np.zeros((n,))
I = sparse.eye(n, format='csr')
Dinv = 0.5 * I
D = 2 * I
E = -sparse.tril(A, -1)
omega = 2.0/3.0
rnorm = []
monitor = True
x = np.random.rand(n)
for i in range(100):
x[:] = x - omega * Dinv * A * x
#x[:] = x - sla.spsolve(D-E, A*x)
rnorm.append(np.linalg.norm(A * x))
if monitor:
plt.ion()
plt.figure(1)
plt.plot(x)
plt.draw()
plt.plot(x)
np.linalg.norm(x - (x - omega * Dinv * A * x))