Skip to content

Instantly share code, notes, and snippets.

@DianSano
Created March 8, 2022 08:35
Show Gist options
  • Select an option

  • Save DianSano/7a953614b4719b25f0f587364c97d874 to your computer and use it in GitHub Desktop.

Select an option

Save DianSano/7a953614b4719b25f0f587364c97d874 to your computer and use it in GitHub Desktop.
SPL_LU_doolittle.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "SPL_LU_doolittle.ipynb",
"provenance": [],
"authorship_tag": "ABX9TyNFZdugVwG2k/2a3dLFt/z8",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/DianSano/7a953614b4719b25f0f587364c97d874/spl_lu_doolittle.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "raRVZTciqk-q",
"outputId": "549a086d-212e-4548-d623-3e83234de9d9"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[ 1. 0. 0. ]\n",
" [ 1. 1. 0. ]\n",
" [ 2. -4.5 1. ]] [[ 1. 4. 1.]\n",
" [ 0. 2. -2.]\n",
" [ 0. 0. -9.]]\n",
"[ 5. 1. -2.]\n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"a = np.array([[1, 4, 1],\n",
" [1, 6, -1],\n",
" [2, -1, 2]], float)\n",
"#the b matrix constant terms of the equations \n",
"b = np.array([7, 13, 5], float)\n",
"\n",
"def doolittle(A):\n",
" #source https://johnfoster.pge.utexas.edu/numerical-methods-book/LinearAlgebra_LU.html\n",
" n = a.shape[0]\n",
" U = np.zeros((n, n), dtype=np.double)\n",
" L = np.eye(n, dtype=np.double)\n",
" # Note: @ numpy operator for matrix multiplication\n",
" for k in range(n):\n",
" U[k, k:] = a[k, k:] - L[k,:k] @ U[:k,k:]\n",
" L[(k+1):,k] = (a[(k+1):,k] - L[(k+1):,:] @ U[:,k]) / U[k, k]\n",
" return L, U\n",
"\n",
"L, U = doolittle(a)\n",
"print(L, U)\n",
"\n",
"\n",
"def forward_substitution(L, b):\n",
" #source https://johnfoster.pge.utexas.edu/numerical-methods-book/LinearAlgebra_LU.html\n",
" #Get number of rows\n",
" n = L.shape[0]\n",
" #Allocating space for the solution vector\n",
" y = np.zeros_like(b, dtype=np.double);\n",
" #Here we perform the forward-substitution. \n",
" #Initializing with the first row.\n",
" y[0] = b[0] / L[0, 0]\n",
" #Looping over rows in reverse (from the bottom up),\n",
" #starting with the second to last row, because the \n",
" #last row solve was completed in the last step.\n",
" for i in range(1, n):\n",
" y[i] = (b[i] - np.dot(L[i,:i], y[:i])) / L[i,i] \n",
" return y\n",
"\n",
"\n",
"def back_substitution(U, y):\n",
" #source https://johnfoster.pge.utexas.edu/numerical-methods-book/LinearAlgebra_LU.html\n",
" #Number of rows\n",
" n = U.shape[0]\n",
" #Allocating space for the solution vector\n",
" x = np.zeros_like(y, dtype=np.double);\n",
" #Here we perform the back-substitution. \n",
" #Initializing with the last row.\n",
" x[-1] = y[-1] / U[-1, -1]\n",
" #Looping over rows in reverse (from the bottom up), \n",
" #starting with the second to last row, because the \n",
" #last row solve was completed in the last step.\n",
" for i in range(n-2, -1, -1):\n",
" x[i] = (y[i] - np.dot(U[i,i:], x[i:])) / U[i,i]\n",
" return x\n",
"\n",
"y = forward_substitution(L, b)\n",
"x = back_substitution(U, y)\n",
"\n",
"print(x)\n"
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment