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

# # LU Factorization with Partial Pivoting
# 
# Copyright (C) 2021 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[1]:


import numpy as np
import numpy.linalg as la

np.set_printoptions(linewidth=150, suppress=True, precision=3)


# In[99]:


n = 5

np.random.seed(235)
A = np.random.randn(n, n)
A[0,0] = 0
A


# ## Permutation Matrices
# This function returns a matrix that swaps rows `i` and `j`:

# In[100]:


def row_swap_mat(i, j):
    P = np.eye(n)
    P[i] = 0
    P[j] = 0
    P[i, j] = 1
    P[j, i] = 1
    return P


# ## Pivoted LU: Initialization
# 
# We're trying to obtain $PA=LU$. Initialize:

# In[101]:


i = 0
I = np.eye(n)
P = I.copy()
Lsub = np.zeros_like(A)
U = np.zeros_like(A)
remaining = A.copy()

remaining


# ## First column
# 
# First, find the pivot as `ipiv`:

# In[102]:


ipiv = i + np.argmax(np.abs(remaining[i:, i]))
ipiv


# Swap the rows in `remaining`, record in `P`:

# In[103]:


swap_mat = row_swap_mat(i, ipiv)
P = swap_mat @ P
remaining = swap_mat @ remaining
remaining


# Now carry out a step of LU, as above:

# In[104]:


U[i, i:] = remaining[i, i:]
Lsub[i+1:,i] = remaining[i+1:,i]/U[i,i]
remaining[i+1:, i+1:] -= np.outer(Lsub[i+1:,i], U[i, i+1:])

i = i + 1

print(P@A-(Lsub+I)@U)


# ## Subsequent columns
# 
# Find the pivot and perform the swaps so that you still have a valid $PA=LU$ factorization:

# In[114]:


ipiv = i + np.argmax(np.abs(remaining[i:, i]))
swap_mat = row_swap_mat(i, ipiv)

P = swap_mat @ P
Lsub = swap_mat @ Lsub
remaining = swap_mat @ remaining


# Here are some checks to make sure you're on the right track:

# In[115]:


print("Should maintain the same 'zero fringe' as the previous step:")
print(P @ A - (Lsub+I) @ U)

print("Should be zero:")
print(remaining[i:, i:] - (P @ A - (Lsub+I) @ U)[i:, i:])


# Carry out a step of LU, as always:

# In[116]:


U[i, i:] = remaining[i, i:]
Lsub[i+1:,i] = remaining[i+1:,i]/U[i,i]
remaining[i+1:, i+1:] -= np.outer(Lsub[i+1:,i], U[i, i+1:])

i = i + 1

print(P@A-(Lsub+I)@U)


# ## Inspect the result

# In[117]:


P


# In[118]:


I+Lsub


# In[119]:


U


# ## Questions
# 
# Why do we switch to maintaining `Lsub` instead of all of `L`?
# 
# <details>
# Realize that <code>P</code> is a permutation of the "trailing" rows. Applying to <code>Lsub</code> avoids disturbing the diagonal.
# </details>
# 
# ---
# 
# Why is it mathematically correct to only apply the permutations to `Lsub` and not all of `L`?
# 
# <details>
# Realize that <code>P</code> is a permutation of the "trailing" rows. If we applied to <code>Lsub + I</code> instead, then the pivots would need to go into the row to which the 1 from the diagonal was permuted. Instead, it goes into <code>U[i,i]</code>, and leaving the diagonal of <code>L</code> undisturbed reflects that.
# </details>

# In[ ]:




