{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from IPython.display import display, HTML\n",
"display(HTML(\"\"))"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Lecture 4: Conditioning. Floating point arithmetic and stability. Systems of linear equations. "
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"## Introductory example\n",
"Consider solving a linear systems of equations $$Ax = f$$ with\n",
"\n",
"$$A_{ij} = \\frac{1}{i + j + 1}, \\quad i,j = 0, \\ldots, n-1.$$\n",
"\n",
"For example, at $n=5$:\n",
"\n",
"$$\n",
"A=\\begin{bmatrix}1&{\\frac {1}{2}}&{\\frac {1}{3}}&{\\frac {1}{4}}&{\\frac {1}{5}}\\\\{\\frac {1}{2}}&{\\frac {1}{3}}&{\\frac {1}{4}}&{\\frac {1}{5}}&{\\frac {1}{6}}\\\\{\\frac {1}{3}}&{\\frac {1}{4}}&{\\frac {1}{5}}&{\\frac {1}{6}}&{\\frac {1}{7}}\\\\{\\frac {1}{4}}&{\\frac {1}{5}}&{\\frac {1}{6}}&{\\frac {1}{7}}&{\\frac {1}{8}}\\\\{\\frac {1}{5}}&{\\frac {1}{6}}&{\\frac {1}{7}}&{\\frac {1}{8}}&{\\frac {1}{9}}\\end{bmatrix}.\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def hilbert_fl(n):\n",
" return np.array([[1.0/(i + j + 1) for i in range(n)] for j in range(n)])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Lets try to solve a small system numerically"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"import numpy as np\n",
"n = 3\n",
"A = hilbert_fl(n)\n",
"f = np.random.randn(n)\n",
"x = np.linalg.solve(A, f) # we have not discussed yet what this function is doing but apparently it solves a system of equations"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2.7277615519878704e-15\n"
]
}
],
"source": [
"err = np.linalg.norm(A @ x - f) / np.linalg.norm(f)\n",
"print(err)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### So far so good, what about a larger system?"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"n = 15\n",
"A = hilbert_fl(n)\n",
"f = np.random.randint(-3,3,n)\n",
"x = np.linalg.solve(A, f)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.5838742081211422\n"
]
}
],
"source": [
"err = np.linalg.norm(A @ x - f) / np.linalg.norm(f)\n",
"print(err)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"The error grows with increase of $n$, and we have to find out why. It is not a problem with the algorithm!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Lets solve the system exactly symbolically "
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [],
"source": [
"from sympy import Rational\n",
"from sympy.matrices import Matrix\n",
"def hilbert_sym(n):\n",
" return Matrix([[Rational(1, i+j+1) for i in range(n)] for j in range(n)])"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1. , 0.5 ],\n",
" [0.5 , 0.33333333]])"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"hilbert_fl(2)"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\left[\\begin{matrix}1 & \\frac{1}{2}\\\\\\frac{1}{2} & \\frac{1}{3}\\end{matrix}\\right]$"
],
"text/plain": [
"Matrix([\n",
"[ 1, 1/2],\n",
"[1/2, 1/3]])"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"hilbert_sym(2)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"n = 12"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [],
"source": [
"A_sym = hilbert_sym(n)\n",
"A_sym_inv = A_sym.inv(method=\"LU\")"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\left[\\begin{array}{cccccccccccc}144 & -10296 & 240240 & -2702700 & 17297280 & -68612544 & 176432256 & -299304720 & 332560800 & -232792560 & 93117024 & -16224936\\\\-10296 & 981552 & -25765740 & 309188880 & -2061259200 & 8409937536 & -22076086032 & 38044955520 & -42800574960 & 30263032800 & -12206089896 & 2141691552\\\\240240 & -25765740 & 721440720 & -9018009000 & 61837776000 & -257554337040 & 686811565440 & -1198416098880 & 1361836476000 & -970938969000 & 394350596640 & -69604975440\\\\-2702700 & 309188880 & -9018009000 & 115945830000 & -811620810000 & 3434057827200 & -9271956133440 & 16342037712000 & -18725251545000 & 13443770340000 & -5492740453200 & 974469656160\\\\17297280 & -2061259200 & 61837776000 & -811620810000 & 5771525760000 & -24725216355840 & 67432408243200 & -119841609888000 & 138278780640000 & -99868008240000 & 41012462050560 & -7308522421200\\\\-68612544 & 8409937536 & -257554337040 & 3434057827200 & -24725216355840 & 106992754412544 & -294230074634496 & 526565596677120 & -611192210428800 & 443680271274240 & -183018111900624 & 32742180446976\\\\176432256 & -22076086032 & 686811565440 & -9271956133440 & 67432408243200 & -294230074634496 & 814790975910912 & -1466861305029120 & 1711338189200640 & -1247850762958800 & 516757021837056 & -92769511266432\\\\-299304720 & 38044955520 & -1198416098880 & 16342037712000 & -119841609888000 & 526565596677120 & -1466861305029120 & 2654320456719360 & -3110531785218000 & 2276990587872000 & -946216088737920 & 170392979877120\\\\332560800 & -42800574960 & 1361836476000 & -18725251545000 & 138278780640000 & -611192210428800 & 1711338189200640 & -3110531785218000 & 3659449159080000 & -2688113888460000 & 1120519052452800 & -202341663604080\\\\-232792560 & 30263032800 & -970938969000 & 13443770340000 & -99868008240000 & 443680271274240 & -1247850762958800 & 2276990587872000 & -2688113888460000 & 1980715496760000 & -827939077645680 & 149882713780800\\\\93117024 & -12206089896 & 394350596640 & -5492740453200 & 41012462050560 & -183018111900624 & 516757021837056 & -946216088737920 & 1120519052452800 & -827939077645680 & 346945899203904 & -62950739787936\\\\-16224936 & 2141691552 & -69604975440 & 974469656160 & -7308522421200 & 32742180446976 & -92769511266432 & 170392979877120 & -202341663604080 & 149882713780800 & -62950739787936 & 11445589052352\\end{array}\\right]$"
],
"text/plain": [
"Matrix([\n",
"[ 144, -10296, 240240, -2702700, 17297280, -68612544, 176432256, -299304720, 332560800, -232792560, 93117024, -16224936],\n",
"[ -10296, 981552, -25765740, 309188880, -2061259200, 8409937536, -22076086032, 38044955520, -42800574960, 30263032800, -12206089896, 2141691552],\n",
"[ 240240, -25765740, 721440720, -9018009000, 61837776000, -257554337040, 686811565440, -1198416098880, 1361836476000, -970938969000, 394350596640, -69604975440],\n",
"[ -2702700, 309188880, -9018009000, 115945830000, -811620810000, 3434057827200, -9271956133440, 16342037712000, -18725251545000, 13443770340000, -5492740453200, 974469656160],\n",
"[ 17297280, -2061259200, 61837776000, -811620810000, 5771525760000, -24725216355840, 67432408243200, -119841609888000, 138278780640000, -99868008240000, 41012462050560, -7308522421200],\n",
"[ -68612544, 8409937536, -257554337040, 3434057827200, -24725216355840, 106992754412544, -294230074634496, 526565596677120, -611192210428800, 443680271274240, -183018111900624, 32742180446976],\n",
"[ 176432256, -22076086032, 686811565440, -9271956133440, 67432408243200, -294230074634496, 814790975910912, -1466861305029120, 1711338189200640, -1247850762958800, 516757021837056, -92769511266432],\n",
"[-299304720, 38044955520, -1198416098880, 16342037712000, -119841609888000, 526565596677120, -1466861305029120, 2654320456719360, -3110531785218000, 2276990587872000, -946216088737920, 170392979877120],\n",
"[ 332560800, -42800574960, 1361836476000, -18725251545000, 138278780640000, -611192210428800, 1711338189200640, -3110531785218000, 3659449159080000, -2688113888460000, 1120519052452800, -202341663604080],\n",
"[-232792560, 30263032800, -970938969000, 13443770340000, -99868008240000, 443680271274240, -1247850762958800, 2276990587872000, -2688113888460000, 1980715496760000, -827939077645680, 149882713780800],\n",
"[ 93117024, -12206089896, 394350596640, -5492740453200, 41012462050560, -183018111900624, 516757021837056, -946216088737920, 1120519052452800, -827939077645680, 346945899203904, -62950739787936],\n",
"[ -16224936, 2141691552, -69604975440, 974469656160, -7308522421200, 32742180446976, -92769511266432, 170392979877120, -202341663604080, 149882713780800, -62950739787936, 11445589052352]])"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_sym_inv"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\left[\\begin{matrix}225 & -25200 & 928200\\\\-25200 & 3763200 & -155937600\\\\928200 & -155937600 & 6892441920\\end{matrix}\\right]$"
],
"text/plain": [
"Matrix([\n",
"[ 225, -25200, 928200],\n",
"[-25200, 3763200, -155937600],\n",
"[928200, -155937600, 6892441920]])"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_sym_inv[:3,:3]"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
"x_sym = A_sym_inv @ f"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([178587533700, -36343113031680, 1840126802426640], dtype=object)"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x_sym[:3]"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([2, -2, -1, 2, 2, -2, 1, -2, -1, -2, 0, -2, -2, -1, -2],\n",
" dtype=object)"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_sym @ x_sym"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 2, -2, -1, 2, 2, -2, 1, -2, -1, -2, 0, -2, -2, -1, -2])"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Thats cool: we found an exact solution. How does it relate to the one we found numerically above?"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1.68481673e+09, -2.49810169e+11, 9.23256364e+12, -1.47865831e+14,\n",
" 1.26548177e+15, -6.35346650e+15, 1.90893727e+16, -3.10903988e+16,\n",
" 8.47002490e+15, 7.74591773e+16, -1.83635048e+17, 2.14030465e+17,\n",
" -1.44209897e+17, 5.37157771e+16, -8.60260839e+15])"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([178587533700, -36343113031680, 1840126802426640,\n",
" -40763720391095520, 494478649301036400, -3692652241725326880,\n",
" 18178212566190156480, -61465382245798399440, 146065938596015157720,\n",
" -246060765797279312880, 292261743908214880800,\n",
" -239289867888435655200, 128509153948247763600,\n",
" -40739658302071890000, 5777758685388081600], dtype=object)"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x_sym"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1494.0414789748738"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.linalg.norm(x - x_sym.astype(np.float64))/np.linalg.norm(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So we have two errors: error on the solution, $|x-x_0|/|x_0|$ and error on the residual, $|A x-f|/|f|$. Note that we can easily estimate the second type of error but estimating the first one is tricky: you may think you have to know the true solution $x_0$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lets try to further increase the system size $n$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Whats going on? Why dows np.linalg.solve fail so miserably?"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\left[\\begin{array}{ccccccccccccccc}1 & \\frac{1}{2} & \\frac{1}{3} & \\frac{1}{4} & \\frac{1}{5} & \\frac{1}{6} & \\frac{1}{7} & \\frac{1}{8} & \\frac{1}{9} & \\frac{1}{10} & \\frac{1}{11} & \\frac{1}{12} & \\frac{1}{13} & \\frac{1}{14} & \\frac{1}{15}\\\\\\frac{1}{2} & \\frac{1}{3} & \\frac{1}{4} & \\frac{1}{5} & \\frac{1}{6} & \\frac{1}{7} & \\frac{1}{8} & \\frac{1}{9} & \\frac{1}{10} & \\frac{1}{11} & \\frac{1}{12} & \\frac{1}{13} & \\frac{1}{14} & \\frac{1}{15} & \\frac{1}{16}\\\\\\frac{1}{3} & \\frac{1}{4} & \\frac{1}{5} & \\frac{1}{6} & \\frac{1}{7} & \\frac{1}{8} & \\frac{1}{9} & \\frac{1}{10} & \\frac{1}{11} & \\frac{1}{12} & \\frac{1}{13} & \\frac{1}{14} & \\frac{1}{15} & \\frac{1}{16} & \\frac{1}{17}\\\\\\frac{1}{4} & \\frac{1}{5} & \\frac{1}{6} & \\frac{1}{7} & \\frac{1}{8} & \\frac{1}{9} & \\frac{1}{10} & \\frac{1}{11} & \\frac{1}{12} & \\frac{1}{13} & \\frac{1}{14} & \\frac{1}{15} & \\frac{1}{16} & \\frac{1}{17} & \\frac{1}{18}\\\\\\frac{1}{5} & \\frac{1}{6} & \\frac{1}{7} & \\frac{1}{8} & \\frac{1}{9} & \\frac{1}{10} & \\frac{1}{11} & \\frac{1}{12} & \\frac{1}{13} & \\frac{1}{14} & \\frac{1}{15} & \\frac{1}{16} & \\frac{1}{17} & \\frac{1}{18} & \\frac{1}{19}\\\\\\frac{1}{6} & \\frac{1}{7} & \\frac{1}{8} & \\frac{1}{9} & \\frac{1}{10} & \\frac{1}{11} & \\frac{1}{12} & \\frac{1}{13} & \\frac{1}{14} & \\frac{1}{15} & \\frac{1}{16} & \\frac{1}{17} & \\frac{1}{18} & \\frac{1}{19} & \\frac{1}{20}\\\\\\frac{1}{7} & \\frac{1}{8} & \\frac{1}{9} & \\frac{1}{10} & \\frac{1}{11} & \\frac{1}{12} & \\frac{1}{13} & \\frac{1}{14} & \\frac{1}{15} & \\frac{1}{16} & \\frac{1}{17} & \\frac{1}{18} & \\frac{1}{19} & \\frac{1}{20} & \\frac{1}{21}\\\\\\frac{1}{8} & \\frac{1}{9} & \\frac{1}{10} & \\frac{1}{11} & \\frac{1}{12} & \\frac{1}{13} & \\frac{1}{14} & \\frac{1}{15} & \\frac{1}{16} & \\frac{1}{17} & \\frac{1}{18} & \\frac{1}{19} & \\frac{1}{20} & \\frac{1}{21} & \\frac{1}{22}\\\\\\frac{1}{9} & \\frac{1}{10} & \\frac{1}{11} & \\frac{1}{12} & \\frac{1}{13} & \\frac{1}{14} & \\frac{1}{15} & \\frac{1}{16} & \\frac{1}{17} & \\frac{1}{18} & \\frac{1}{19} & \\frac{1}{20} & \\frac{1}{21} & \\frac{1}{22} & \\frac{1}{23}\\\\\\frac{1}{10} & \\frac{1}{11} & \\frac{1}{12} & \\frac{1}{13} & \\frac{1}{14} & \\frac{1}{15} & \\frac{1}{16} & \\frac{1}{17} & \\frac{1}{18} & \\frac{1}{19} & \\frac{1}{20} & \\frac{1}{21} & \\frac{1}{22} & \\frac{1}{23} & \\frac{1}{24}\\\\\\frac{1}{11} & \\frac{1}{12} & \\frac{1}{13} & \\frac{1}{14} & \\frac{1}{15} & \\frac{1}{16} & \\frac{1}{17} & \\frac{1}{18} & \\frac{1}{19} & \\frac{1}{20} & \\frac{1}{21} & \\frac{1}{22} & \\frac{1}{23} & \\frac{1}{24} & \\frac{1}{25}\\\\\\frac{1}{12} & \\frac{1}{13} & \\frac{1}{14} & \\frac{1}{15} & \\frac{1}{16} & \\frac{1}{17} & \\frac{1}{18} & \\frac{1}{19} & \\frac{1}{20} & \\frac{1}{21} & \\frac{1}{22} & \\frac{1}{23} & \\frac{1}{24} & \\frac{1}{25} & \\frac{1}{26}\\\\\\frac{1}{13} & \\frac{1}{14} & \\frac{1}{15} & \\frac{1}{16} & \\frac{1}{17} & \\frac{1}{18} & \\frac{1}{19} & \\frac{1}{20} & \\frac{1}{21} & \\frac{1}{22} & \\frac{1}{23} & \\frac{1}{24} & \\frac{1}{25} & \\frac{1}{26} & \\frac{1}{27}\\\\\\frac{1}{14} & \\frac{1}{15} & \\frac{1}{16} & \\frac{1}{17} & \\frac{1}{18} & \\frac{1}{19} & \\frac{1}{20} & \\frac{1}{21} & \\frac{1}{22} & \\frac{1}{23} & \\frac{1}{24} & \\frac{1}{25} & \\frac{1}{26} & \\frac{1}{27} & \\frac{1}{28}\\\\\\frac{1}{15} & \\frac{1}{16} & \\frac{1}{17} & \\frac{1}{18} & \\frac{1}{19} & \\frac{1}{20} & \\frac{1}{21} & \\frac{1}{22} & \\frac{1}{23} & \\frac{1}{24} & \\frac{1}{25} & \\frac{1}{26} & \\frac{1}{27} & \\frac{1}{28} & \\frac{1}{29}\\end{array}\\right]$"
],
"text/plain": [
"Matrix([\n",
"[ 1, 1/2, 1/3, 1/4, 1/5, 1/6, 1/7, 1/8, 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15],\n",
"[ 1/2, 1/3, 1/4, 1/5, 1/6, 1/7, 1/8, 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16],\n",
"[ 1/3, 1/4, 1/5, 1/6, 1/7, 1/8, 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17],\n",
"[ 1/4, 1/5, 1/6, 1/7, 1/8, 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18],\n",
"[ 1/5, 1/6, 1/7, 1/8, 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18, 1/19],\n",
"[ 1/6, 1/7, 1/8, 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18, 1/19, 1/20],\n",
"[ 1/7, 1/8, 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18, 1/19, 1/20, 1/21],\n",
"[ 1/8, 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18, 1/19, 1/20, 1/21, 1/22],\n",
"[ 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18, 1/19, 1/20, 1/21, 1/22, 1/23],\n",
"[1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18, 1/19, 1/20, 1/21, 1/22, 1/23, 1/24],\n",
"[1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18, 1/19, 1/20, 1/21, 1/22, 1/23, 1/24, 1/25],\n",
"[1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18, 1/19, 1/20, 1/21, 1/22, 1/23, 1/24, 1/25, 1/26],\n",
"[1/13, 1/14, 1/15, 1/16, 1/17, 1/18, 1/19, 1/20, 1/21, 1/22, 1/23, 1/24, 1/25, 1/26, 1/27],\n",
"[1/14, 1/15, 1/16, 1/17, 1/18, 1/19, 1/20, 1/21, 1/22, 1/23, 1/24, 1/25, 1/26, 1/27, 1/28],\n",
"[1/15, 1/16, 1/17, 1/18, 1/19, 1/20, 1/21, 1/22, 1/23, 1/24, 1/25, 1/26, 1/27, 1/28, 1/29]])"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_sym"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1. , 0.5 , 0.33333333, 0.25 , 0.2 ,\n",
" 0.16666667, 0.14285714, 0.125 , 0.11111111, 0.1 ,\n",
" 0.09090909, 0.08333333, 0.07692308, 0.07142857, 0.06666667],\n",
" [0.5 , 0.33333333, 0.25 , 0.2 , 0.16666667,\n",
" 0.14285714, 0.125 , 0.11111111, 0.1 , 0.09090909,\n",
" 0.08333333, 0.07692308, 0.07142857, 0.06666667, 0.0625 ],\n",
" [0.33333333, 0.25 , 0.2 , 0.16666667, 0.14285714,\n",
" 0.125 , 0.11111111, 0.1 , 0.09090909, 0.08333333,\n",
" 0.07692308, 0.07142857, 0.06666667, 0.0625 , 0.05882353],\n",
" [0.25 , 0.2 , 0.16666667, 0.14285714, 0.125 ,\n",
" 0.11111111, 0.1 , 0.09090909, 0.08333333, 0.07692308,\n",
" 0.07142857, 0.06666667, 0.0625 , 0.05882353, 0.05555556],\n",
" [0.2 , 0.16666667, 0.14285714, 0.125 , 0.11111111,\n",
" 0.1 , 0.09090909, 0.08333333, 0.07692308, 0.07142857,\n",
" 0.06666667, 0.0625 , 0.05882353, 0.05555556, 0.05263158],\n",
" [0.16666667, 0.14285714, 0.125 , 0.11111111, 0.1 ,\n",
" 0.09090909, 0.08333333, 0.07692308, 0.07142857, 0.06666667,\n",
" 0.0625 , 0.05882353, 0.05555556, 0.05263158, 0.05 ],\n",
" [0.14285714, 0.125 , 0.11111111, 0.1 , 0.09090909,\n",
" 0.08333333, 0.07692308, 0.07142857, 0.06666667, 0.0625 ,\n",
" 0.05882353, 0.05555556, 0.05263158, 0.05 , 0.04761905],\n",
" [0.125 , 0.11111111, 0.1 , 0.09090909, 0.08333333,\n",
" 0.07692308, 0.07142857, 0.06666667, 0.0625 , 0.05882353,\n",
" 0.05555556, 0.05263158, 0.05 , 0.04761905, 0.04545455],\n",
" [0.11111111, 0.1 , 0.09090909, 0.08333333, 0.07692308,\n",
" 0.07142857, 0.06666667, 0.0625 , 0.05882353, 0.05555556,\n",
" 0.05263158, 0.05 , 0.04761905, 0.04545455, 0.04347826],\n",
" [0.1 , 0.09090909, 0.08333333, 0.07692308, 0.07142857,\n",
" 0.06666667, 0.0625 , 0.05882353, 0.05555556, 0.05263158,\n",
" 0.05 , 0.04761905, 0.04545455, 0.04347826, 0.04166667],\n",
" [0.09090909, 0.08333333, 0.07692308, 0.07142857, 0.06666667,\n",
" 0.0625 , 0.05882353, 0.05555556, 0.05263158, 0.05 ,\n",
" 0.04761905, 0.04545455, 0.04347826, 0.04166667, 0.04 ],\n",
" [0.08333333, 0.07692308, 0.07142857, 0.06666667, 0.0625 ,\n",
" 0.05882353, 0.05555556, 0.05263158, 0.05 , 0.04761905,\n",
" 0.04545455, 0.04347826, 0.04166667, 0.04 , 0.03846154],\n",
" [0.07692308, 0.07142857, 0.06666667, 0.0625 , 0.05882353,\n",
" 0.05555556, 0.05263158, 0.05 , 0.04761905, 0.04545455,\n",
" 0.04347826, 0.04166667, 0.04 , 0.03846154, 0.03703704],\n",
" [0.07142857, 0.06666667, 0.0625 , 0.05882353, 0.05555556,\n",
" 0.05263158, 0.05 , 0.04761905, 0.04545455, 0.04347826,\n",
" 0.04166667, 0.04 , 0.03846154, 0.03703704, 0.03571429],\n",
" [0.06666667, 0.0625 , 0.05882353, 0.05555556, 0.05263158,\n",
" 0.05 , 0.04761905, 0.04545455, 0.04347826, 0.04166667,\n",
" 0.04 , 0.03846154, 0.03703704, 0.03571429, 0.03448276]])"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Condition number of a problem"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Conditioning of a problem"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"- Conditioning of a problem is defined without reference to any particular algorithm."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"- A well-conditioned problem $f(x)$: all small perturbations of $x$ lead to small changes in $f(x)$."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"- An ill-conditioned problem $f(x)$: some small perturbation of $x$ leads to large changes in $f(x)$."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"The absolute condition number of a problem $f(x)$ is:\n",
"\n",
"$$\\kappa = \\lim _{\\varepsilon \\rightarrow 0}\\,\\sup _{\\|\\delta x\\|\\,\\leq \\,\\varepsilon }{\\frac {\\|\\delta f(x)\\|}{\\|\\delta x\\|}}$$\n",
"\n",
"and the relative condition number is:\n",
"\n",
"$$\\kappa = \\lim _{\\varepsilon \\rightarrow 0}\\,\\sup _{\\|\\delta x\\|\\,\\leq \\,\\varepsilon }{\\frac {\\|\\delta f(x)\\|/\\|f(x)\\|}{\\|\\delta x\\|/\\|x\\|}}.$$\n",
"\n",
"Relative $\\kappa$ is normally more important (its at least dimensionless)."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"If the function $f(x)$ is differentiable:\n",
"$$\n",
"\\kappa = \\frac{\\Vert J(x) \\Vert}{\\Vert f(x)\\Vert / \\Vert x \\Vert},\n",
"$$\n",
"where $J$ is Jacobian matrix: $J _{ij}={\\frac {\\partial f_{i}}{\\partial x_{j}}}$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Well- and ill-conditioned problems"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"- A problem is well-conditioned if $\\kappa$ is small ($\\lesssim 10^2$) and ill-conditioned if $\\kappa$ is large ($\\gtrsim 10^{6}$)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Consider a problem of computing $f(x)=\\sqrt{x}$:\n",
"$$\\kappa = \\frac{\\Vert J(x) \\Vert}{\\Vert f(x)\\Vert / \\Vert x \\Vert} = \\frac{1/(2\\sqrt{x})}{\\sqrt{x}/x}=\\frac12.$$\n",
"This problem is well-conditioned by any standard."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"Consider computing $f(x)=\\tan x$ for $x=10^{100}$. \n",
"The problem is ill-conditioned. Why?\n",
"\n",
"[Surely You're Joking, Mr. Feynman](https://edisciplinas.usp.br/pluginfile.php/4420924/mod_resource/content/1/LuckyNumbers_Feynman.pdf)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Condition numbers in linear algebra computations"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"- In matrix computations, the basic operations are computing $f(x) = A x$ and computing $g(x) = A^{-1}x$ (that is, solving a linear system)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- They have the condition numbers\n",
"$\\kappa_f(x) = \\Vert A \\Vert \\frac{\\Vert x \\Vert}{\\Vert A x \\Vert}$ and $\\kappa_g(x)=\\Vert A^{-1} \\Vert \\frac{\\Vert A x \\Vert}{\\Vert x \\Vert}$"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"- The worst-case bounds (independent on $x$):\n",
"$$\n",
"\\kappa_{f,g}(x) \\leq \\kappa(A),\n",
"$$\n",
"where the right-hand-side is known as a condition number of a matrix $A$: \n",
"$$\\kappa(A) = \\Vert A \\Vert \\Vert A^{-1} \\Vert.$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Note, that the condition number is different for different norms. Which are worst-case vectors $x$ for $f(x)$ and $g(x)$ and $L_2$ norm?"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Condition number of a linear system\n",
"Now consider the perturbation of a system of linear equations:\n",
"from\n",
"$$ A x = f$$\n",
"to\n",
"$$ (A + \\Delta A) (x + \\Delta x) = f + \\Delta f.$$"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"Perturbation to the solution:\n",
"\n",
"$$\n",
"\\frac{\\Vert \\Delta x \\Vert}{\\Vert x \\Vert} \\leq \\frac{\\mathrm{cond}(A)}{1 - \\mathrm{cond}(A)\\frac{\\|\\Delta A\\|}{\\|A\\|}} \\Big(\\frac{\\Vert\\Delta A\\Vert}{\\Vert A \\Vert} + \\frac{\\Vert \\Delta f \\Vert}{ \\Vert f \\Vert}\\Big)\n",
"$$\n",
"\n",
"The crucial role is played by the condition number $\\kappa(A) = \\Vert A \\Vert \\Vert A^{-1} \\Vert$."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"The larger the condition number, the less number of digits we can recover."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Note, that if $\\Delta A = 0$, then\n",
"\n",
"$$ \\frac{\\Vert \\Delta x \\Vert}{\\Vert x \\Vert} \\leq \\mathrm{cond}(A) \\frac{\\|\\Delta f\\|}{\\|f\\|} $$"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"The condition number in spectral norm is equal to the ratio of the largest singular value and the smallest singular value.\n",
"\n",
"$$ \\mathrm{cond}_2 (A) = \\|A\\|_2 \\|A^{-1}\\|_2 = \\frac{\\sigma_{\\max}}{\\sigma_{\\min}} $$"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Hilbert matrix (again)"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"2.495951750009794e+17"
]
},
"execution_count": 87,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n = 15\n",
"A = hilbert_fl(n)\n",
"np.linalg.cond(A)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"The Hilbert matrix is very poorly conditioned, indeed. This implies that solution of the equation $Ax = b$ for x is very sensitive to the small changes in $A$ and $b$. Here it is time to recall how exactly are real numbers represented in a CPU."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Floating point\n",
"The numbers in computer memory are typically represented as floating point numbers \n",
"\n",
"A floating point number is represented as \n",
"\n",
"$$\\textrm{number} = \\textrm{significand} \\times \\textrm{base}^{\\textrm{exponent}},$$\n",
"\n",
"where *significand* is integer, *base* is positive integer and *exponent* is integer (can be negative), i.e.\n",
"\n",
"$$ 1.2 = 12 \\cdot 10^{-1}.$$"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## IEEE 754\n",
"In modern computers, the floating point representation is controlled by [IEEE 754 standard](https://en.wikipedia.org/wiki/IEEE_floating_point) which was published in 1985. Before that point different computers behaved differently with floating point numbers. \n",
"\n",
"IEEE 754 has:\n",
"- Floating point representation (as described above), $(-1)^s \\times c \\times b^q$.\n",
"- Two infinities, $+\\infty$ and $-\\infty$\n",
"- Rules for assigning kinds of NaN\n",
"- Rules for rounding\n",
"- Rules for $\\frac{0}{0}, \\frac{1}{-0}, \\ldots$"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## The two most common format, single & double\n",
"\n",
"The two most common formats, called binary32 and binary64 (called also single and double formats). Recently, the format binary16 plays important role in learning deep neural networks.\n",
"\n",
"| Name | Common Name | Base | Digits | Emin | Emax |\n",
"|------|----------|----------|-------|------|------|\n",
"|binary16| half precision | 2 | 11 | -14 | + 15 |\n",
"|binary32| single precision | 2 | 24 | -126 | + 127 | \n",
"|binary64| double precision | 2 | 53 | -1022 | +1023 | \n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"For all real $x$ (in an admissible range, such as $[1.1\\cdot 10^{-38}, 3.4\\cdot 10^{38}]$ for single precision) there exists floating point number $\\tilde x$ such that $|x-\\tilde x| \\leq |x|\\epsilon_{machine}$\n",
"- For single precision: $$\\epsilon_{machine} = 2^{-24}\\approx 5.96\\cdot 10^{-8}$$\n",
"- For double precision: $$\\epsilon_{machine} = 2^{-53}\\approx 1.11\\cdot 10^{-16}$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For single precision, we have $8388608$ numbers in $(1,2)$ which is quite dense. But we still have the same $8388608$ numbers in $(2^{126}, 2^{127})$ -- much larger interval! At each arithmetic operation, the result will typically not fit into the grid and will be replaced by some proxy from the grid. This can be disastrous for the final result if accumulates as a result of careless programming."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Accuracy and memory\n",
"\n",
"The relative accuracy of single precision is $10^{-7}-10^{-8}$, while for double precision is $10^{-14}-10^{-16}$.\n",
"\n",
"- A float16 takes 2 bytes, float32 takes 4 bytes, float64, or double precision, takes 8 bytes\n",
"\n",
"- These are the only two floating point-types supported in hardware (float32 and float64).\n",
"\n",
"- Normally, double precision is for Scientific Computing and float is for GPU/Data Science."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Accuracy demos"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0.5 0.6 0.7 0.8]\n",
"0.7999999999999999\n"
]
}
],
"source": [
"z = np.arange(0.5, 0.9, 0.1)\n",
"print(z)\n",
"print(z[-1])"
]
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4.3670121385730454e-07\n"
]
}
],
"source": [
"a = np.float32(6.0)\n",
"b = np.sqrt(a)\n",
"print(b ** 2 - a)"
]
},
{
"cell_type": "code",
"execution_count": 102,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.0\n"
]
}
],
"source": [
"a = np.array(0.4147369269524216, dtype=np.float32)\n",
"b = np.exp(a)\n",
"print(np.log(b) - a)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Loss of significance\n",
"\n",
"- Many operations lead to the [loss of significance](https://en.wikipedia.org/wiki/Loss_of_significance)\n",
"- For example, it is a bad idea to subtract two big numbers that are close, the difference will have fewer correct digits\n",
"- This is related to algorithms and their properties (stability)"
]
},
{
"cell_type": "code",
"execution_count": 103,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.0\n",
"0.0\n",
"2.0\n"
]
}
],
"source": [
"import math\n",
"test_list = [1, 1e20, 1, -1e20]\n",
"print(np.sum(test_list))\n",
"print(1 + 1e20 + 1 - 1e20)\n",
"print(math.fsum(test_list))"
]
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2.0\n"
]
}
],
"source": [
"print(1e20 - 1e20 + 1 + 1)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Some disasters attibuted to bad numerical computing\n",
"- [Vancouver Stock Exchange Index computation error](https://www5.in.tum.de/~huckle/Vancouv.pdf): in January 1982 the index was initialized at 1000 and subsequently updated and truncated to three decimal places on each trade. Such a thing was done about 3000 times each day. The accumulated truncations led to an erroneous loss of around 25 points per month. On 25 Nov 1983, the error was corrected, raising the value of the index from its Friday closing figure of 524 to 1098.\n",
"- [The Patriot Missile failure](https://www-users.cse.umn.edu/~arnold/disasters/patriot.html), happened In Dharan, Saudi Arabia, on February 25, 1991, resulted in 28 deaths, is ultimately attributable to poor handling of rounding errors.\n",
"- [The explosion of the Ariane 5 rocket](https://www-users.cse.umn.edu/~arnold/disasters/ariane.html) just after lift-off on its maiden voyage off French Guiana, on June 4, 1996, was ultimately the consequence of a simple overflow.\n",
"- [The sinking of the Sleipner A offshore platform](https://www-users.cse.umn.edu/~arnold/disasters/sleipner.html) in Gandsfjorden near Stavanger, Norway, on August 23, 1991, resulted in a loss of nearly one billion dollars. It was found to be the result of inaccurate finite element analysis. "
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Stability of algorithms\n",
"\n",
"- Let $x$ be an object (for example, a vector) \n",
"- Let $f(x)$ be the function (functional) you want to evaluate \n",
"- You have a numerical algorithm working in finite precision arithmetics will deliver an approximation $alg(x)$ to the correct result $f(x)$"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"The algorithm is called forward stable, if $$\\Vert alg(x) - f(x) \\Vert \\leq \\varepsilon $$ \n",
"\n",
"The algorithm is called backward stable, if for any $x$ there is a close vector $x + \\delta x$ such that\n",
"\n",
"$$alg(x) = f(x + \\delta x)$$\n",
"\n",
"and $\\Vert \\delta x \\Vert$ is small.\n",
"\n",
"The algorithm is called stable if it gives approximately correct answers for approximately correct problems: for each $x$ there exists $\\tilde x$ such that $\\Vert x-\\tilde x\\Vert = \\mathcal{O}(\\epsilon_{machine})$ such that\n",
"$$\n",
"\\Vert alg(x) - f(\\tilde x) \\Vert = \\mathcal{O}(\\epsilon_{machine}) \n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The best algorithms usually compute exact answers for slightly perturbed data. If the output is not too sentitive to input data (characterized by the condition number), then this is sufficient in practice."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Examples of instability - 1\n",
"\n",
"How to compute the following function in numerically stable manner?\n",
"\n",
"$$\\log(1 - \\tanh^2(u))$$"
]
},
{
"cell_type": "code",
"execution_count": 107,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Original function: -inf\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\Zenon\\AppData\\Local\\Temp\\ipykernel_21092\\1730504921.py:2: RuntimeWarning: divide by zero encountered in log\n",
" print(\"Original function:\", np.log(1 - np.tanh(u)**2))\n"
]
}
],
"source": [
"u = 30\n",
"print(\"Original function:\", np.log(1 - np.tanh(u)**2))"
]
},
{
"cell_type": "code",
"execution_count": 109,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Use more numerically stable form: -58.61370563888011\n"
]
}
],
"source": [
"print(\"Use more numerically stable form:\", np.log(4) - 2 * np.log(np.exp(-u) + np.exp(u)))"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Examples of instability - 2\n",
"How to compute the following function in numerically stable manner?\n",
"$$SoftMax(x)_j = \\dfrac{e^{x_j}}{\\sum\\limits_{i=1}^n e^{x_i}}$$"
]
},
{
"cell_type": "code",
"execution_count": 118,
"metadata": {},
"outputs": [],
"source": [
"n = 5\n",
"x = np.random.randn(n)\n",
"x[0] = 10"
]
},
{
"cell_type": "code",
"execution_count": 119,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[9.99869717e-01 4.25877087e-05 1.71219662e-05 4.14169156e-05\n",
" 2.91559416e-05]\n"
]
}
],
"source": [
"print(np.exp(x) / np.sum(np.exp(x)))"
]
},
{
"cell_type": "code",
"execution_count": 120,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([10. , -0.06381458, -0.97501805, -0.09169088, -0.44272154])"
]
},
"execution_count": 120,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 121,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[9.99869717e-01 4.25877087e-05 1.71219662e-05 4.14169156e-05\n",
" 2.91559416e-05]\n"
]
}
],
"source": [
"print(np.exp(x - np.max(x)) / np.sum(np.exp(x - np.max(x))))"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Linear systems "
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"## Scales of linear systems\n",
"\n",
"In different applications, the typical size of the linear systems can be different. \n",
"\n",
"- Small: $n \\leq 10^4$ (full matrix can be stored in memory, dense matrix)\n",
"- Medium: $n = 10^4 - 10^6$ (typically, sparse or structured matrix)\n",
"- Large: $n = 10^8 - 10^9$ (typically sparse matrix + parallel computations)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We will talk only about approaches to small systems today."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Main questions about linear systems\n",
"\n",
"1. What is the accuracy we get from the solution (due to rounding errors)?\n",
"2. How we compute the solution? (using the Cramer's rule with dets is good only for $2 \\times 2$ matrices)\n",
"3. What is the complexity of the solution of linear systems?"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Solving $Ax=b$ via QR factorization\n",
"- Factor $A$ into $QR$: $A = QR$\n",
"- $Ax=b$ is equivalent to $QRx=b$ that is $Rx = y$ where $y = Q^*b$\n",
"- Solve triangular system $Rx=y$"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"- The algorithm is stable. It means that the solution $\\tilde x$ computed by this algorithm satisfies:\n",
"$$\n",
"\\frac{\\Vert\\tilde x - x\\Vert}{\\Vert x\\Vert} = \\mathcal{O}(\\kappa(A)\\epsilon_{machine})\n",
"$$\n",
"- Solving linear system with this method takes $2n^3$ flops [dominated by complexity of QR]"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Solving $Ax=b$ via Gaussian elimination\n",
"- Gaussian elimination proceeds via the computation of one of the most important matrix decompositions: LU-decomposition.\n",
"\n",
"- LU-decomposition of the square matrix $A$ is the representation\n",
"\n",
"$$A = LU,$$\n",
"\n",
"where \n",
"- $L$ is lower triangular (elements strictly above the diagonal are zero)\n",
"- $U$ is upper triangular matrix (elements strictly below the diagonal are zero)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"$$\n",
"\\begin{bmatrix}a_{11}&a_{12}&a_{13}\\\\a_{21}&a_{22}&a_{23}\\\\a_{31}&a_{32}&a_{33}\\end{bmatrix}=\\begin{bmatrix}\\ell _{11}&0&0\\\\\\ell _{21}&\\ell _{22}&0\\\\\\ell _{31}&\\ell _{32}&\\ell _{33}\\end{bmatrix}\\begin{bmatrix}u_{11}&u_{12}&u_{13}\\\\0&u_{22}&u_{23}\\\\0&0&u_{33}\\end{bmatrix}\n",
"$$\n",
"\n",
"This factorization is non-unique, so it is typical to require that the matrix $L$ has ones on the diagonal."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"LU decomposition is useful to solve a linear system since\n",
"\n",
"$$ A^{-1} f = (L U)^{-1} f = U^{-1} L^{-1} f, $$\n",
"\n",
"and this reduces to the solution of two linear systems forward step\n",
"\n",
"$$ L y = f, $$\n",
"\n",
"and backward step\n",
"\n",
"$$ U x = y. $$"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Complexity of the Gaussian elimination/LU decomposition\n",
"\n",
"- The complexity is dominated by LU decomposition, which takes $\\frac23 n^3$: several times faster than QR \n",
"- In many cases, when solution with multiple right hand sides is required, computing LU-decomposition once is a good idea\n",
"- Once the decomposition is found then solving linear systems with $L$ and $U$ costs only $\\mathcal{O}(n^2)$ operations."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Computing LU decomposition\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## When LU fails\n",
"\n",
"- What happens, if pivots in the Gaussian elimination are really small or zero?. \n",
"\n",
"- There is classical $2 \\times 2$ example of a matrix with a bad LU decomposition. \n",
"\n",
"- The matrix we look at is \n",
"\n",
"$$\n",
" A = \\begin{pmatrix}\n",
" \\varepsilon & 1 \\\\\n",
" 1 & 1 \n",
" \\end{pmatrix}\n",
"$$\n",
"\n",
"- If $\\varepsilon$ is sufficiently small, we might fail."
]
},
{
"cell_type": "code",
"execution_count": 170,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"L:\n",
" [[1.e+00 0.e+00]\n",
" [1.e+18 1.e+00]]\n",
"U:\n",
" [[ 1.e-18 1.e+00]\n",
" [ 0.e+00 -1.e+18]]\n",
"A:\n",
" [[1.e-18 1.e+00]\n",
" [1.e+00 1.e+00]]\n",
"L * U - A:\n",
" [[ 0.00000000e+00 0.00000000e+00]\n",
" [-1.11022302e-16 -1.00000000e+00]]\n"
]
}
],
"source": [
"import numpy as np\n",
"eps = 1e-18\n",
"A = np.array([[eps, 1],[1.0, 1]])\n",
"A0 = A.copy()\n",
"n = A.shape[0]\n",
"L = np.zeros((n, n))\n",
"U = np.zeros((n, n))\n",
"for k in range(n): \n",
" L[k, k] = 1\n",
" for i in range(k+1, n):\n",
" L[i, k] = A[i, k] / A[k, k]\n",
" for j in range(k+1, n):\n",
" A[i, j] = A[i, j] - L[i, k] * A[k, j]\n",
" for j in range(k, n):\n",
" U[k, j] = A[k, j]\n",
"print('L:\\n', L)\n",
"print('U:\\n', U)\n",
"print('A:\\n', A0)\n",
"print('L * U - A:\\n', np.dot(L, U) - A0)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## The concept of pivoting\n",
"\n",
"- We can do pivoting, i.e. permute rows and columns to maximize $A_{kk}$ that we divide over. \n",
"\n",
"- The simplest but effective strategy is the row pivoting: at each step, select the index that is maximal in modulus, and put it onto the diagonal. \n",
"\n",
"- It gives us the decomposition \n",
"\n",
"$$A = P L U,$$\n",
"\n",
"where $P$ is a permutation matrix.\n",
"\n",
"Pivoting ensures $| L_{ij}|<1,$ but the elements of $U$ can grow, up to $2^n$. Theoretically, gaussian elimination is unstable, though (in practice, this is almost never encountered)."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Linear equations: Summary\n",
"- Linear systems can be solved by Gaussian elimination, complexity is $\\mathcal{O}(n^3)$.\n",
"- Linear systems can be solved by LU-decomposition, complexity is $\\mathcal{O}(n^3)$ for the decomposition, $\\mathcal{O}(n^2)$ for each solve\n",
"- Linear least squares can be solved by normal equations (bad)\n",
"- Linear least squares can be solved by QR-decomposition (good)\n",
"- Without structure, we can solve up to $10^4$ linear systems on a laptop (memory restrictions)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We will discuss iterative methods for linear systems in what follows."
]
}
],
"metadata": {
"celltoolbar": "Slideshow",
"kernelspec": {
"display_name": "Python 3.10.8 64-bit",
"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.10.8"
},
"vscode": {
"interpreter": {
"hash": "b0fa6594d8f4cbf19f97940f81e996739fb7646882a419484c72d19e05852a7e"
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}