#!/usr/bin/env python
# coding: utf-8

# # Stationary Iterative Methods for Linear Systems
# 
# Copyright (C) 2020 Andreas Kloeckner
# 
# <details>
# <summary>MIT License</summary>
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# </details>

# In[2]:


import numpy as np
import scipy.linalg as la

import matplotlib.pyplot as pt


# Let's solve $u''=-30x^2$ with $u(0)=1$ and $u(1)=-1$.

# In[3]:


n = 50

mesh = np.linspace(0, 1, n)
h = mesh[1] - mesh[0]


# Set up the system matrix `A` to carry out centered finite differences
# 
# $$
# u''(x)\approx \frac{u(x+h) - 2u(x) + u(x-h)}{h^2}.
# $$
# 
# Use `np.eye(n, k=...)`. What needs to be in the first and last row?

# In[5]:


A = (np.eye(n, k=1) + -2*np.eye(n) + np.eye(n, k=-1))/h**2
A[0] = 0
A[-1] = 0
A[0,0] = 1
A[-1,-1] = 1


# Next, fix the right hand side:

# In[9]:


b = -30*mesh**2
b[0] = 1
b[-1] = -1


# Compute a reference solution `x_true` to the linear system:

# In[10]:


x_true = la.solve(A, b)
pt.plot(mesh, x_true)


# Next, we'll try all the stationary iterative methods we have seen.

# ## Jacobi

# In[44]:


x = np.zeros(n)


# Next, apply a Jacobi step:

# In[42]:


x_new = np.empty(n)

for i in range(n):
    x_new[i] = b[i]
    for j in range(n):
        if i != j:
            x_new[i] -= A[i,j]*x[j]

    x_new[i] = x_new[i] / A[i,i]

x = x_new


# In[43]:


pt.plot(mesh, x)
pt.plot(mesh, x_true, label="true")
pt.legend()


# * Ideas to accelerate this?
# * Multigrid

# ## Gauss-Seidel

# In[45]:


x = np.zeros(n)


# In[64]:


x_new = np.empty(n)

for i in range(n):
    x_new[i] = b[i]
    for j in range(i):
        x_new[i] -= A[i,j]*x_new[j]
    for j in range(i+1, n):
        x_new[i] -= A[i,j]*x[j]

    x_new[i] = x_new[i] / A[i,i]

x = x_new
pt.plot(mesh, x)
pt.plot(mesh, x_true, label="true")
pt.legend()


# ### And now Successive Over-Relaxation ("SOR")

# In[92]:


x = np.zeros(n)


# In[106]:


x_new = np.empty(n)

for i in range(n):
    x_new[i] = b[i]
    for j in range(i):
        x_new[i] -= A[i,j]*x_new[j]
    for j in range(i+1, n):
        x_new[i] -= A[i,j]*x[j]

    x_new[i] = x_new[i] / A[i,i]

direction = x_new - x
omega = 1.5
x = x + omega*direction

pt.plot(mesh, x)
pt.plot(mesh, x_true, label="true")
pt.legend()
pt.ylim([-1.3, 1.3])


# In[ ]:




