{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# LU Factorization\n",
"\n",
"The goal of this notebook is to give a brief explanation of LU Factorization roughly following TAK § 4.3.\n",
"\n",
"We will not explain how to compute the LU factorization. The method to compute the LU factorization is very close to Gaussian elimination. To learn how to do this see the description in TAK beginning on page 100."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Imports\n",
"\n",
"The following are standard imports."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import math as m\n",
"from mpmath import mp, iv"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We add the following import for this notebook (and going forward)."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from scipy import linalg"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Why use LU Factorization?\n",
"\n",
"Recall that the main motivation for Gaussian elimination was that it can be used to solve the equation $A x =b$. But what if you have several vectors $b_1, \\ldots, b_n$ and for each $i \\in \\{1, \\ldots, n\\}$ you want to find a solution vector $x_i$ satifying $A x_i = b_i$. Then you would need to repeat Gaussian elimination $n$ times.\n",
"But observe that the forward elimination phase of Gaussian elimination would be the same in each case. It turns out that we do not need to repeat this calculuation if we keep better track of what we are doing. This will save time as opposed to repeating Gaussian elimination several times."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## What is LU Factorization?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"An *LU factorization* of a matrix $A$ is obtained by writing $A$ as a product of two matrices,\n",
"$$A = LU,$$\n",
"where $L$ is a lower triangular matrix with ones on the diagonal and $U$ is an upper triangular matrix. The matrix $U$ is what you would obtain by applying forward elimination to $A$ without pivoting.\n",
"\n",
"For example, if \n",
"$$A=\\begin{bmatrix}\n",
" 2 & -1 & 4\\\\\n",
" 6 & -2 & 10\\\\\n",
" -2 & 3 & -11\n",
"\\end{bmatrix},$$\n",
"then $A=LU$ where\n",
"$$L=\\begin{bmatrix}\n",
" 1 & 0 & 0 \\\\\n",
" 3 & 1 & 0 \\\\\n",
" -1 & 2 & 1 \n",
"\\end{bmatrix}\n",
"\\quad \\text{and} \\quad\n",
"U = \\begin{bmatrix}\n",
" 2 & -1 & 4 \\\\\n",
" 0 & 1 & -2 \\\\\n",
" 0 & 0 & -3 \n",
"\\end{bmatrix}.$$"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A = np.array([[ 2, -1, 4],\n",
" [ 6, -2, 10],\n",
" [ -2, 3, -11]])\n",
"L = np.array([[1, 0, 0],\n",
" [3, 1, 0],\n",
" [-1, 2, 1]])\n",
"U = np.array([[2, -1, 4],\n",
" [0, 1, -2],\n",
" [0, 0, -3]])\n",
"(A == L@U).all()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Why is it useful?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We will see how knowing that $A=LU$ can help you solve the equation $Ax=b$. \n",
"\n",
"Observe we can solve the equation $LUx = b$. First consider the equation $Ly = b$. We can solve for $b$ in a straightforward way since $L$ is lower triangular (with ones on the diagonal).\n",
"\n",
"Once we've found $y$, we need to find $x$ to satisfy $Ux = y$. Since $U$ is upper triangular, this can be solved by back substitution. Once we find $x$, we are done since\n",
"$$A x = LUx = L y = b.$$\n",
"\n",
"Note that once we find the factorization $A=LU$, we can use this method to solve $Ax=b$ in linear time in the number of entries of $A$. This is the same as the amount of time it takes to examine all the entries of $A$, so this is as fast as you can hope to solve this equation (unless $A$ has a special form you can take advantage of)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## What about pivoting?\n",
"\n",
"LU factorization doesn't work if a pivot entry is one. It will also lead to numerical errors if there are very small pivot entries. The solution, as with Gaussian elimination, is to allow the algorithm to swap rows. Considering this results in a different factorization,\n",
"$$A = PLU$$\n",
"where $P$ is a permutation matrix and $L$ and $U$ are as before.\n",
"\n",
"A *permutation matrix* is a $P$ matrix with exactly one $1$ in every row and column, and with every other entry zero. For example\n",
"$$P=\\begin{bmatrix}\n",
"0 & 1 & 0 & 0 \\\\\n",
"0 & 0 & 0 & 1 \\\\\n",
"1 & 0 & 0 & 0 \\\\\n",
"0 & 0 & 1 & 0\n",
"\\end{bmatrix}.$$\n",
"Such a matrix is called a permutation matrix, because its action by multiplication on a column vector moves the numbers in the vector around. For example, the matrix above satisfies\n",
"$$P \\begin{bmatrix} 1 \\\\ 2 \\\\ 3 \\\\ 4\\end{bmatrix} = \\begin{bmatrix} 2 \\\\ 4 \\\\ 3 \\\\ 1\\end{bmatrix}.$$\n",
"\n",
"Note that for a permutation matrix $P$, we have \n",
"$$P^{-1}=P^T,$$\n",
"where $P^T$ denotes the transpose of $P$.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Using scipy\n",
"\n",
"The `scipy` package has a linear algebra module called `linalg`. This module has a LU factorization function called `lu`. This is really a $PLU$ factorization function. We will demonstrate its use using the matrix $A$ as above."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 2, -1, 4],\n",
" [ 6, -2, 10],\n",
" [ -2, 3, -11]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"P, L, U = linalg.lu(A)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here we check that it really is a factorization."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(np.abs(A - P@L@U) < 10**-8).all()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now lets look at the individual matrices. First $P$:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0., 0., 1.],\n",
" [1., 0., 0.],\n",
" [0., 1., 0.]])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"P"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can see that $P$ is a permuation matrix. Now $L$:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1. , 0. , 0. ],\n",
" [-0.33333333, 1. , 0. ],\n",
" [ 0.33333333, -0.14285714, 1. ]])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"L"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Observe that $L$ is lower triangular with ones on the diagonal. Now look at $U$."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 6. , -2. , 10. ],\n",
" [ 0. , 2.33333333, -7.66666667],\n",
" [ 0. , 0. , -0.42857143]])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"U"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Observe that $U$ is upper triangular."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solving $Ax=b$ using PLU Factorization\n",
"\n",
"We'll assume $A$ is an $n \\times n$ matrix for the discussion below."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 1: The permutation matrix.\n",
"\n",
"First we solve $Pz = b$ for $z$. Recalling that $P^{-1}=P^T$, we can do this:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"def permuation_solve(P, b):\n",
" Pt = P.transpose()\n",
" return Pt @ b"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 2: The lower triangular matrix\n",
"\n",
"Now we want to solve $Ly = z$ for $y$. Expanding this out, we want to solve:\n",
"$$\\begin{bmatrix}\n",
"1 & 0 & \\ldots \\\\\n",
"l_{1,0} & 1 & 0 & \\ldots \\\\\n",
"l_{2,0} & l_{2,1} & 1 & 0 & \\ldots \\\\\n",
"\\vdots & \\vdots & \\ddots & \\ddots & \\ddots \\\\\n",
"l_{n-1,0} & l_{n-1,1} & \\dots & l_{n-1,n-2} & 1\n",
"\\end{bmatrix}\n",
"\\begin{bmatrix} y_0 \\\\ y_1 \\\\ y_2 \\\\ \\vdots \\\\ y_{n-1}\\end{bmatrix} =\n",
"\\begin{bmatrix} z_0 \\\\ z_1 \\\\ z_2 \\\\ \\vdots \\\\ z_{n-1}\\end{bmatrix}.\n",
"$$\n",
"* The first row tells us that $y_0=z_0$.\n",
"* The second row tells us that $y_1 = z_1 - l_{1,0} y_0$.\n",
"* The third row tells us that $y_2 = z_2 - ( l_{2,0} y_0 + l_{2,1} y_1)$.\n",
"* In general, we can write $y_j$ as $z_j$ minus a dot product,\n",
"$$y_j = z_j - \\begin{bmatrix} l_{j,0} \\\\ \\vdots \\\\ l_{j,j-1} \\end{bmatrix} \\cdot \\begin{bmatrix} y_0 \\\\ \\vdots \\\\ y_{j-1} \\end{bmatrix}.$$"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"def lower_triangular_solve(L, z):\n",
" n = len(z)\n",
" y = np.zeros(n)\n",
" y[0] = z[0]\n",
" for j in range(1,n):\n",
" y[j] = z[j] - L[j,:j].dot(y[:j])\n",
" return y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 3: The upper triangular matrix\n",
"\n",
"Now we solve $Ux = y$ for $x$. This is the same as the back substitution step in Gaussian elimination, so we will not detail how it works. (See the `Gaussian elimination` notebook.)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"def upper_triangular_solve(U, y):\n",
" n = len(y)\n",
" x = np.zeros(n)\n",
" x[n-1] = y[n-1] / U[n-1,n-1]\n",
" for i in range(n-2, -1, -1):\n",
" x[i] = (y[i] - U[i,i+1:].dot(x[i+1:]))/U[i,i]\n",
" return x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 4: Putting the steps together\n",
"\n",
"We want to find $x$ such that $Ax=b$. We know that $A=PLU$. We can find $z$ such that $Pz=b$. We can find $y$ such that $Ly=z$. We can find $x$ such that $Ux=y$. Then we have\n",
"$$Ax = PLUx = PL y = Pz = b.$$"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"def plu_solve(P, L, U, b):\n",
" z = permuation_solve(P, b)\n",
" y = lower_triangular_solve(L, z)\n",
" x = upper_triangular_solve(U, y)\n",
" return x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We will check this with our matrix $A=PLU$ from above. Recall $A$ is as below."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 2, -1, 4],\n",
" [ 6, -2, 10],\n",
" [ -2, 3, -11]])"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let $b=\\begin{bmatrix} 1 \\\\ 2 \\\\ 3\\end{bmatrix}.$"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3])"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b = np.array([1,2,3])\n",
"b"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's check that our function works. We use our above method to solve for $x$."
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 2., -5., -2.])"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = plu_solve(P,L,U,b)\n",
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To see that it works we compute $Ax$ and see that it is the same as (or within round off error from) $b$."
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1., 2., 3.])"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A @ x"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}