2030 lines
58 KiB
Plaintext
2030 lines
58 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- dom:TITLE: Data analysis and Machine Learning Lectures: Linear Algebra methods -->\n",
|
|
"# Data analysis and Machine Learning Lectures: Linear Algebra methods \n",
|
|
"<!-- dom:AUTHOR: Morten Hjorth-Jensen at Department of Physics, University of Oslo & Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University -->\n",
|
|
"<!-- Author: --> \n",
|
|
"**Morten Hjorth-Jensen**, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University\n",
|
|
"\n",
|
|
"Date: **Jan 27, 2018**\n",
|
|
"\n",
|
|
"Copyright 1999-2018, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Important Matrix and vector handling packages\n",
|
|
"\n",
|
|
"The Numerical Recipes codes have been rewritten in Fortran 90/95 and\n",
|
|
"C/C++ by us. The original source codes are taken from the widely used\n",
|
|
"software package LAPACK, which follows two other popular packages\n",
|
|
"developed in the 1970s, namely EISPACK and LINPACK.\n",
|
|
"\n",
|
|
" * LINPACK: package for linear equations and least square problems.\n",
|
|
"\n",
|
|
" * LAPACK:package for solving symmetric, unsymmetric and generalized eigenvalue problems. From LAPACK's website <http://www.netlib.org> it is possible to download for free all source codes from this library. Both C/C++ and Fortran versions are available.\n",
|
|
"\n",
|
|
" * BLAS (I, II and III): (Basic Linear Algebra Subprograms) are routines that provide standard building blocks for performing basic vector and matrix operations. Blas I is vector operations, II vector-matrix operations and III matrix-matrix operations. Highly parallelized and efficient codes, all available for download from <http://www.netlib.org>.\n",
|
|
"\n",
|
|
"**Add python material on linear algebra and array handling, text on numpy etc**\n",
|
|
"\n",
|
|
"## Basic Matrix Features\n",
|
|
"\n",
|
|
"**Matrix properties reminder.**"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mathbf{A} =\n",
|
|
" \\begin{bmatrix} a_{11} & a_{12} & a_{13} & a_{14} \\\\\n",
|
|
" a_{21} & a_{22} & a_{23} & a_{24} \\\\\n",
|
|
" a_{31} & a_{32} & a_{33} & a_{34} \\\\\n",
|
|
" a_{41} & a_{42} & a_{43} & a_{44}\n",
|
|
" \\end{bmatrix}\\qquad\n",
|
|
"\\mathbf{I} =\n",
|
|
" \\begin{bmatrix} 1 & 0 & 0 & 0 \\\\\n",
|
|
" 0 & 1 & 0 & 0 \\\\\n",
|
|
" 0 & 0 & 1 & 0 \\\\\n",
|
|
" 0 & 0 & 0 & 1\n",
|
|
" \\end{bmatrix}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Basic Matrix Features\n",
|
|
"The inverse of a matrix is defined by"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mathbf{A}^{-1} \\cdot \\mathbf{A} = I\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Basic Matrix Features\n",
|
|
"\n",
|
|
"**Matrix Properties Reminder.**\n",
|
|
"\n",
|
|
"\n",
|
|
"<table border=\"1\">\n",
|
|
"<thead>\n",
|
|
"<tr><th align=\"center\"> Relations </th> <th align=\"center\"> Name </th> <th align=\"center\"> matrix elements </th> </tr>\n",
|
|
"</thead>\n",
|
|
"<tbody>\n",
|
|
"<tr><td align=\"center\"> $A = A^{T}$ </td> <td align=\"center\"> symmetric </td> <td align=\"center\"> $a_{ij} = a_{ji}$ </td> </tr>\n",
|
|
"<tr><td align=\"center\"> $A = \\left (A^{T} \\right )^{-1}$ </td> <td align=\"center\"> real orthogonal </td> <td align=\"center\"> $\\sum_k a_{ik} a_{jk} = \\sum_k a_{ki} a_{kj} = \\delta_{ij}$ </td> </tr>\n",
|
|
"<tr><td align=\"center\"> $A = A^{ * }$ </td> <td align=\"center\"> real matrix </td> <td align=\"center\"> $a_{ij} = a_{ij}^{ * }$ </td> </tr>\n",
|
|
"<tr><td align=\"center\"> $A = A^{\\dagger}$ </td> <td align=\"center\"> hermitian </td> <td align=\"center\"> $a_{ij} = a_{ji}^{ * }$ </td> </tr>\n",
|
|
"<tr><td align=\"center\"> $A = \\left (A^{\\dagger} \\right )^{-1}$ </td> <td align=\"center\"> unitary </td> <td align=\"center\"> $\\sum_k a_{ik} a_{jk}^{ * } = \\sum_k a_{ki}^{ * } a_{kj} = \\delta_{ij}$ </td> </tr>\n",
|
|
"</tbody>\n",
|
|
"</table>\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Some famous Matrices\n",
|
|
"\n",
|
|
" * Diagonal if $a_{ij}=0$ for $i\\ne j$\n",
|
|
"\n",
|
|
" * Upper triangular if $a_{ij}=0$ for $i > j$\n",
|
|
"\n",
|
|
" * Lower triangular if $a_{ij}=0$ for $i < j$\n",
|
|
"\n",
|
|
" * Upper Hessenberg if $a_{ij}=0$ for $i > j+1$\n",
|
|
"\n",
|
|
" * Lower Hessenberg if $a_{ij}=0$ for $i < j+1$\n",
|
|
"\n",
|
|
" * Tridiagonal if $a_{ij}=0$ for $|i -j| > 1$\n",
|
|
"\n",
|
|
" * Lower banded with bandwidth $p$: $a_{ij}=0$ for $i > j+p$\n",
|
|
"\n",
|
|
" * Upper banded with bandwidth $p$: $a_{ij}=0$ for $i < j+p$\n",
|
|
"\n",
|
|
" * Banded, block upper triangular, block lower triangular....\n",
|
|
"\n",
|
|
"## Basic Matrix Features\n",
|
|
"\n",
|
|
"**Some Equivalent Statements.**\n",
|
|
"\n",
|
|
"For an $N\\times N$ matrix $\\mathbf{A}$ the following properties are all equivalent\n",
|
|
"\n",
|
|
" * If the inverse of $\\mathbf{A}$ exists, $\\mathbf{A}$ is nonsingular.\n",
|
|
"\n",
|
|
" * The equation $\\mathbf{Ax}=0$ implies $\\mathbf{x}=0$.\n",
|
|
"\n",
|
|
" * The rows of $\\mathbf{A}$ form a basis of $R^N$.\n",
|
|
"\n",
|
|
" * The columns of $\\mathbf{A}$ form a basis of $R^N$.\n",
|
|
"\n",
|
|
" * $\\mathbf{A}$ is a product of elementary matrices.\n",
|
|
"\n",
|
|
" * $0$ is not eigenvalue of $\\mathbf{A}$.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Matrix Handling in C/C++, Static and Dynamical allocation\n",
|
|
"\n",
|
|
"**Static.**\n",
|
|
"\n",
|
|
"We have an $N\\times N$ matrix A with $N=100$\n",
|
|
"In C/C++ this would be defined as"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
" int N = 100;\n",
|
|
" double A[100][100];\n",
|
|
" // initialize all elements to zero\n",
|
|
" for(i=0 ; i < N ; i++) {\n",
|
|
" for(j=0 ; j < N ; j++) {\n",
|
|
" A[i][j] = 0.0;\n",
|
|
" \n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Note the way the matrix is organized, row-major order.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Matrix Handling in C/C++\n",
|
|
"\n",
|
|
"**Row Major Order, Addition.**\n",
|
|
"\n",
|
|
"We have $N\\times N$ matrices A, B and C and we wish to\n",
|
|
"evaluate $A=B+C$."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mathbf{A}= \\mathbf{B}\\pm\\mathbf{C} \\Longrightarrow a_{ij} = b_{ij}\\pm c_{ij},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"In C/C++ this would be coded like"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
" for(i=0 ; i < N ; i++) {\n",
|
|
" for(j=0 ; j < N ; j++) {\n",
|
|
" a[i][j] = b[i][j]+c[i][j]\n",
|
|
" \n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Matrix Handling in C/C++\n",
|
|
"\n",
|
|
"**Row Major Order, Multiplication.**\n",
|
|
"\n",
|
|
"We have $N\\times N$ matrices A, B and C and we wish to\n",
|
|
"evaluate $A=BC$."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mathbf{A}=\\mathbf{BC} \\Longrightarrow a_{ij} = \\sum_{k=1}^{n} b_{ik}c_{kj},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"In C/C++ this would be coded like"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
" for(i=0 ; i < N ; i++) {\n",
|
|
" for(j=0 ; j < N ; j++) {\n",
|
|
" for(k=0 ; k < N ; k++) {\n",
|
|
" a[i][j]+=b[i][k]*c[k][j];\n",
|
|
" \n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Dynamic memory allocation in C/C++\n",
|
|
"\n",
|
|
"At least three possibilities in this course\n",
|
|
"\n",
|
|
" * Do it yourself\n",
|
|
"\n",
|
|
" * Use the functions provided in the library package lib.cpp\n",
|
|
"\n",
|
|
" * Use Armadillo <http://arma.sourceforgenet> (a C++ linear algebra library, discussion both here and at lab). \n",
|
|
"\n",
|
|
"## Matrix Handling in C/C++, Dynamic Allocation\n",
|
|
"\n",
|
|
"**Do it yourself.**"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
" int N;\n",
|
|
" double ** A;\n",
|
|
" A = new double*[N]\n",
|
|
" for ( i = 0; i < N; i++)\n",
|
|
" A[i] = new double[N];\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Always free space when you don't need an array anymore."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
" for ( i = 0; i < N; i++)\n",
|
|
" delete[] A[i];\n",
|
|
" delete[] A;\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Armadillo, recommended!!\n",
|
|
"\n",
|
|
" * Armadillo is a C++ linear algebra library (matrix maths) aiming towards a good balance between speed and ease of use. The syntax is deliberately similar to Matlab.\n",
|
|
"\n",
|
|
" * Integer, floating point and complex numbers are supported, as well as a subset of trigonometric and statistics functions. Various matrix decompositions are provided through optional integration with LAPACK, or one of its high performance drop-in replacements (such as the multi-threaded MKL or ACML libraries).\n",
|
|
"\n",
|
|
" * A delayed evaluation approach is employed (at compile-time) to combine several operations into one and reduce (or eliminate) the need for temporaries. This is accomplished through recursive templates and template meta-programming.\n",
|
|
"\n",
|
|
" * Useful for conversion of research code into production environments, or if C++ has been decided as the language of choice, due to speed and/or integration capabilities.\n",
|
|
"\n",
|
|
" * The library is open-source software, and is distributed under a license that is useful in both open-source and commercial/proprietary contexts.\n",
|
|
"\n",
|
|
"## Armadillo, simple examples"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
" #include <iostream>\n",
|
|
" #include <armadillo>\n",
|
|
" \n",
|
|
" using namespace std;\n",
|
|
" using namespace arma;\n",
|
|
" \n",
|
|
" int main(int argc, char** argv)\n",
|
|
" {\n",
|
|
" mat A = randu<mat>(5,5);\n",
|
|
" mat B = randu<mat>(5,5);\n",
|
|
" \n",
|
|
" cout << A*B << endl;\n",
|
|
" \n",
|
|
" return 0;\n",
|
|
" \n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Armadillo, how to compile and install\n",
|
|
"\n",
|
|
"For people using Ubuntu, Debian, Linux Mint, simply go to the synaptic package manager and install\n",
|
|
"armadillo from there.\n",
|
|
"You may have to install Lapack as well.\n",
|
|
"For Mac and Windows users, follow the instructions from the webpage\n",
|
|
"<http://arma.sourceforge.net>.\n",
|
|
"To compile, use for example (linux/ubuntu)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
" c++ -O2 -o program.x program.cpp -larmadillo -llapack -lblas\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"where the `-l` option indicates the library you wish to link to.\n",
|
|
"\n",
|
|
"For OS X users you may have to declare the paths to the include files and the libraries as"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
" c++ -O2 -o program.x program.cpp -L/usr/local/lib -I/usr/local/include -larmadillo -llapack -lblas\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Armadillo, simple examples"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
" #include <iostream>\n",
|
|
" #include \"armadillo\"\n",
|
|
" using namespace arma;\n",
|
|
" using namespace std;\n",
|
|
" \n",
|
|
" int main(int argc, char** argv)\n",
|
|
" {\n",
|
|
" // directly specify the matrix size (elements are uninitialised)\n",
|
|
" mat A(2,3);\n",
|
|
" // .n_rows = number of rows (read only)\n",
|
|
" // .n_cols = number of columns (read only)\n",
|
|
" cout << \"A.n_rows = \" << A.n_rows << endl;\n",
|
|
" cout << \"A.n_cols = \" << A.n_cols << endl;\n",
|
|
" // directly access an element (indexing starts at 0)\n",
|
|
" A(1,2) = 456.0;\n",
|
|
" A.print(\"A:\");\n",
|
|
" // scalars are treated as a 1x1 matrix,\n",
|
|
" // hence the code below will set A to have a size of 1x1\n",
|
|
" A = 5.0;\n",
|
|
" A.print(\"A:\");\n",
|
|
" // if you want a matrix with all elements set to a particular value\n",
|
|
" // the .fill() member function can be used\n",
|
|
" A.set_size(3,3);\n",
|
|
" A.fill(5.0); A.print(\"A:\");\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Armadillo, simple examples"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
" mat B;\n",
|
|
" \n",
|
|
" // endr indicates \"end of row\"\n",
|
|
" B << 0.555950 << 0.274690 << 0.540605 << 0.798938 << endr\n",
|
|
" << 0.108929 << 0.830123 << 0.891726 << 0.895283 << endr\n",
|
|
" << 0.948014 << 0.973234 << 0.216504 << 0.883152 << endr\n",
|
|
" << 0.023787 << 0.675382 << 0.231751 << 0.450332 << endr;\n",
|
|
" \n",
|
|
" // print to the cout stream\n",
|
|
" // with an optional string before the contents of the matrix\n",
|
|
" B.print(\"B:\");\n",
|
|
" \n",
|
|
" // the << operator can also be used to print the matrix\n",
|
|
" // to an arbitrary stream (cout in this case)\n",
|
|
" cout << \"B:\" << endl << B << endl;\n",
|
|
" // save to disk\n",
|
|
" B.save(\"B.txt\", raw_ascii);\n",
|
|
" // load from disk\n",
|
|
" mat C;\n",
|
|
" C.load(\"B.txt\");\n",
|
|
" C += 2.0 * B;\n",
|
|
" C.print(\"C:\");\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Armadillo, simple examples"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
" // submatrix types:\n",
|
|
" //\n",
|
|
" // .submat(first_row, first_column, last_row, last_column)\n",
|
|
" // .row(row_number)\n",
|
|
" // .col(column_number)\n",
|
|
" // .cols(first_column, last_column)\n",
|
|
" // .rows(first_row, last_row)\n",
|
|
" \n",
|
|
" cout << \"C.submat(0,0,3,1) =\" << endl;\n",
|
|
" cout << C.submat(0,0,3,1) << endl;\n",
|
|
" \n",
|
|
" // generate the identity matrix\n",
|
|
" mat D = eye<mat>(4,4);\n",
|
|
" \n",
|
|
" D.submat(0,0,3,1) = C.cols(1,2);\n",
|
|
" D.print(\"D:\");\n",
|
|
" \n",
|
|
" // transpose\n",
|
|
" cout << \"trans(B) =\" << endl;\n",
|
|
" cout << trans(B) << endl;\n",
|
|
" \n",
|
|
" // maximum from each column (traverse along rows)\n",
|
|
" cout << \"max(B) =\" << endl;\n",
|
|
" cout << max(B) << endl;\n",
|
|
" \n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Armadillo, simple examples"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
" // maximum from each row (traverse along columns)\n",
|
|
" cout << \"max(B,1) =\" << endl;\n",
|
|
" cout << max(B,1) << endl;\n",
|
|
" // maximum value in B\n",
|
|
" cout << \"max(max(B)) = \" << max(max(B)) << endl;\n",
|
|
" // sum of each column (traverse along rows)\n",
|
|
" cout << \"sum(B) =\" << endl;\n",
|
|
" cout << sum(B) << endl;\n",
|
|
" // sum of each row (traverse along columns)\n",
|
|
" cout << \"sum(B,1) =\" << endl;\n",
|
|
" cout << sum(B,1) << endl;\n",
|
|
" // sum of all elements\n",
|
|
" cout << \"sum(sum(B)) = \" << sum(sum(B)) << endl;\n",
|
|
" cout << \"accu(B) = \" << accu(B) << endl;\n",
|
|
" // trace = sum along diagonal\n",
|
|
" cout << \"trace(B) = \" << trace(B) << endl;\n",
|
|
" // random matrix -- values are uniformly distributed in the [0,1] interval\n",
|
|
" mat E = randu<mat>(4,4);\n",
|
|
" E.print(\"E:\");\n",
|
|
" \n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Armadillo, simple examples"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
" // row vectors are treated like a matrix with one row\n",
|
|
" rowvec r;\n",
|
|
" r << 0.59499 << 0.88807 << 0.88532 << 0.19968;\n",
|
|
" r.print(\"r:\");\n",
|
|
" \n",
|
|
" // column vectors are treated like a matrix with one column\n",
|
|
" colvec q;\n",
|
|
" q << 0.81114 << 0.06256 << 0.95989 << 0.73628;\n",
|
|
" q.print(\"q:\");\n",
|
|
" \n",
|
|
" // dot or inner product\n",
|
|
" cout << \"as_scalar(r*q) = \" << as_scalar(r*q) << endl;\n",
|
|
" \n",
|
|
" // outer product\n",
|
|
" cout << \"q*r =\" << endl;\n",
|
|
" cout << q*r << endl;\n",
|
|
" \n",
|
|
" \n",
|
|
" // sum of three matrices (no temporary matrices are created)\n",
|
|
" mat F = B + C + D;\n",
|
|
" F.print(\"F:\");\n",
|
|
" \n",
|
|
" return 0;\n",
|
|
" \n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Armadillo, simple examples"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
" #include <iostream>\n",
|
|
" #include \"armadillo\"\n",
|
|
" using namespace arma;\n",
|
|
" using namespace std;\n",
|
|
" \n",
|
|
" int main(int argc, char** argv)\n",
|
|
" {\n",
|
|
" cout << \"Armadillo version: \" << arma_version::as_string() << endl;\n",
|
|
" \n",
|
|
" mat A;\n",
|
|
" \n",
|
|
" A << 0.165300 << 0.454037 << 0.995795 << 0.124098 << 0.047084 << endr\n",
|
|
" << 0.688782 << 0.036549 << 0.552848 << 0.937664 << 0.866401 << endr\n",
|
|
" << 0.348740 << 0.479388 << 0.506228 << 0.145673 << 0.491547 << endr\n",
|
|
" << 0.148678 << 0.682258 << 0.571154 << 0.874724 << 0.444632 << endr\n",
|
|
" << 0.245726 << 0.595218 << 0.409327 << 0.367827 << 0.385736 << endr;\n",
|
|
" \n",
|
|
" A.print(\"A =\");\n",
|
|
" \n",
|
|
" // determinant\n",
|
|
" cout << \"det(A) = \" << det(A) << endl;\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Armadillo, simple examples"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
" // inverse\n",
|
|
" cout << \"inv(A) = \" << endl << inv(A) << endl;\n",
|
|
" double k = 1.23;\n",
|
|
" \n",
|
|
" mat B = randu<mat>(5,5);\n",
|
|
" mat C = randu<mat>(5,5);\n",
|
|
" \n",
|
|
" rowvec r = randu<rowvec>(5);\n",
|
|
" colvec q = randu<colvec>(5);\n",
|
|
" \n",
|
|
" \n",
|
|
" // examples of some expressions\n",
|
|
" // for which optimised implementations exist\n",
|
|
" // optimised implementation of a trinary expression\n",
|
|
" // that results in a scalar\n",
|
|
" cout << \"as_scalar( r*inv(diagmat(B))*q ) = \";\n",
|
|
" cout << as_scalar( r*inv(diagmat(B))*q ) << endl;\n",
|
|
" \n",
|
|
" // example of an expression which is optimised\n",
|
|
" // as a call to the dgemm() function in BLAS:\n",
|
|
" cout << \"k*trans(B)*C = \" << endl << k*trans(B)*C;\n",
|
|
" \n",
|
|
" return 0;\n",
|
|
" \n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Gaussian Elimination\n",
|
|
"\n",
|
|
"We start with the linear set of equations"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mathbf{A}\\mathbf{x} = \\mathbf{w}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"We assume also that the matrix $\\mathbf{A}$ is non-singular and that the\n",
|
|
"matrix elements along the diagonal satisfy $a_{ii} \\ne 0$. Simple $4\\times 4 $ example"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\begin{bmatrix}\n",
|
|
" a_{11}& a_{12} &a_{13}& a_{14}\\\\\n",
|
|
" a_{21}& a_{22} &a_{23}& a_{24}\\\\\n",
|
|
" a_{31}& a_{32} &a_{33}& a_{34}\\\\\n",
|
|
" a_{41}& a_{42} &a_{43}& a_{44}\\\\\n",
|
|
" \\end{bmatrix} \\begin{bmatrix}\n",
|
|
" x_1\\\\\n",
|
|
" x_2\\\\\n",
|
|
" x_3 \\\\\n",
|
|
" x_4 \\\\\n",
|
|
" \\end{bmatrix}\n",
|
|
" =\\begin{bmatrix}\n",
|
|
" w_1\\\\\n",
|
|
" w_2\\\\\n",
|
|
" w_3 \\\\\n",
|
|
" w_4\\\\\n",
|
|
" \\end{bmatrix}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Gaussian Elimination\n",
|
|
"or"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"a_{11}x_1 +a_{12}x_2 +a_{13}x_3 + a_{14}x_4=w_1 \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"a_{21}x_1 + a_{22}x_2 + a_{23}x_3 + a_{24}x_4=w_2 \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"a_{31}x_1 + a_{32}x_2 + a_{33}x_3 + a_{34}x_4=w_3 \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"a_{41}x_1 + a_{42}x_2 + a_{43}x_3 + a_{44}x_4=w_4. \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Gaussian Elimination\n",
|
|
"\n",
|
|
"The basic idea of Gaussian elimination is to use the first equation to eliminate the first unknown $x_1$\n",
|
|
"from the remaining $n-1$ equations. Then we use the new second equation to eliminate the second unknown\n",
|
|
"$x_2$ from the remaining $n-2$ equations. With $n-1$ such eliminations\n",
|
|
"we obtain a so-called upper triangular set of equations of the form"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"b_{11}x_1 +b_{12}x_2 +b_{13}x_3 + b_{14}x_4=y_1 \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"b_{22}x_2 + b_{23}x_3 + b_{24}x_4=y_2 \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"b_{33}x_3 + b_{34}x_4=y_3 \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- Equation labels as ordinary links -->\n",
|
|
"<div id=\"eq:gaussbacksub\"></div>\n",
|
|
"\n",
|
|
"$$\n",
|
|
"b_{44}x_4=y_4. \\nonumber\n",
|
|
"\\label{eq:gaussbacksub} \\tag{1}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"We can solve this system of equations recursively starting from $x_n$ (in our case $x_4$) and proceed with\n",
|
|
"what is called a backward substitution. \n",
|
|
"\n",
|
|
"## Gaussian Elimination\n",
|
|
"This process can be expressed mathematically as"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- Equation labels as ordinary links -->\n",
|
|
"<div id=\"_auto1\"></div>\n",
|
|
"\n",
|
|
"$$\n",
|
|
"\\begin{equation}\n",
|
|
" x_m = \\frac{1}{b_{mm}}\\left(y_m-\\sum_{k=m+1}^nb_{mk}x_k\\right)\\quad m=n-1,n-2,\\dots,1.\n",
|
|
"\\label{_auto1} \\tag{2}\n",
|
|
"\\end{equation}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"To arrive at such an upper triangular system of equations, we start by eliminating\n",
|
|
"the unknown $x_1$ for $j=2,n$. We achieve this by multiplying the first equation by $a_{j1}/a_{11}$ and then subtract\n",
|
|
"the result from the $j$th equation. We assume obviously that $a_{11}\\ne 0$ and that\n",
|
|
"$\\mathbf{A}$ is not singular.\n",
|
|
"\n",
|
|
"## Gaussian Elimination\n",
|
|
"\n",
|
|
"Our actual $4\\times 4$ example reads after the first operation"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\begin{bmatrix}\n",
|
|
" a_{11}& a_{12} &a_{13}& a_{14}\\\\\n",
|
|
" 0& (a_{22}-\\frac{a_{21}a_{12}}{a_{11}}) &(a_{23}-\\frac{a_{21}a_{13}}{a_{11}}) & (a_{24}-\\frac{a_{21}a_{14}}{a_{11}})\\\\\n",
|
|
"0& (a_{32}-\\frac{a_{31}a_{12}}{a_{11}})& (a_{33}-\\frac{a_{31}a_{13}}{a_{11}})& (a_{34}-\\frac{a_{31}a_{14}}{a_{11}})\\\\\n",
|
|
"0&(a_{42}-\\frac{a_{41}a_{12}}{a_{11}}) &(a_{43}-\\frac{a_{41}a_{13}}{a_{11}}) & (a_{44}-\\frac{a_{41}a_{14}}{a_{11}}) \\\\\n",
|
|
" \\end{bmatrix} \\begin{bmatrix}\n",
|
|
" x_1\\\\\n",
|
|
" x_2\\\\\n",
|
|
" x_3 \\\\\n",
|
|
" x_4 \\\\\n",
|
|
" \\end{bmatrix} \n",
|
|
" =\\begin{bmatrix}\n",
|
|
" y_1\\\\\n",
|
|
" w_2^{(2)}\\\\\n",
|
|
" w_3^{(2)} \\\\\n",
|
|
" w_4^{(2)}\\\\\n",
|
|
" \\end{bmatrix},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"or"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"b_{11}x_1 +b_{12}x_2 +b_{13}x_3 + b_{14}x_4=y_1 \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"a^{(2)}_{22}x_2 + a^{(2)}_{23}x_3 + a^{(2)}_{24}x_4=w^{(2)}_2 \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"a^{(2)}_{32}x_2 + a^{(2)}_{33}x_3 + a^{(2)}_{34}x_4=w^{(2)}_3 \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"a^{(2)}_{42}x_2 + a^{(2)}_{43}x_3 + a^{(2)}_{44}x_4=w^{(2)}_4, \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- Equation labels as ordinary links -->\n",
|
|
"<div id=\"_auto2\"></div>\n",
|
|
"\n",
|
|
"$$\n",
|
|
"\\begin{equation} \n",
|
|
"\\label{_auto2} \\tag{3}\n",
|
|
"\\end{equation}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Gaussian Elimination\n",
|
|
"\n",
|
|
"The new coefficients are"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- Equation labels as ordinary links -->\n",
|
|
"<div id=\"_auto3\"></div>\n",
|
|
"\n",
|
|
"$$\n",
|
|
"\\begin{equation}\n",
|
|
" b_{1k} = a_{1k}^{(1)} \\quad k=1,\\dots,n,\n",
|
|
"\\label{_auto3} \\tag{4}\n",
|
|
"\\end{equation}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"where each $a_{1k}^{(1)}$ is equal to the original $a_{1k}$ element. The other coefficients are"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- Equation labels as ordinary links -->\n",
|
|
"<div id=\"_auto4\"></div>\n",
|
|
"\n",
|
|
"$$\n",
|
|
"\\begin{equation}\n",
|
|
"a_{jk}^{(2)} = a_{jk}^{(1)}-\\frac{a_{j1}^{(1)}a_{1k}^{(1)}}{a_{11}^{(1)}} \\quad j,k=2,\\dots,n,\n",
|
|
"\\label{_auto4} \\tag{5}\n",
|
|
"\\end{equation}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"with a new right-hand side given by"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- Equation labels as ordinary links -->\n",
|
|
"<div id=\"_auto5\"></div>\n",
|
|
"\n",
|
|
"$$\n",
|
|
"\\begin{equation}\n",
|
|
"y_{1}=w_1^{(1)}, \\quad w_j^{(2)} =w_j^{(1)}-\\frac{a_{j1}^{(1)}w_1^{(1)}}{a_{11}^{(1)}} \\quad j=2,\\dots,n.\n",
|
|
"\\label{_auto5} \\tag{6}\n",
|
|
"\\end{equation}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"We have also set $w_1^{(1)}=w_1$, the original vector element.\n",
|
|
"We see that the system of unknowns $x_1,\\dots,x_n$ is transformed into an $(n-1)\\times (n-1)$ problem.\n",
|
|
"\n",
|
|
"## Gaussian Elimination\n",
|
|
"\n",
|
|
"This step is called forward substitution.\n",
|
|
"Proceeding with these substitutions, we obtain the\n",
|
|
"general expressions for the new coefficients"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- Equation labels as ordinary links -->\n",
|
|
"<div id=\"_auto6\"></div>\n",
|
|
"\n",
|
|
"$$\n",
|
|
"\\begin{equation}\n",
|
|
" a_{jk}^{(m+1)} = a_{jk}^{(m)}-\\frac{a_{jm}^{(m)}a_{mk}^{(m)}}{a_{mm}^{(m)}} \\quad j,k=m+1,\\dots,n,\n",
|
|
"\\label{_auto6} \\tag{7}\n",
|
|
"\\end{equation}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"with $m=1,\\dots,n-1$ and a\n",
|
|
"right-hand side given by"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- Equation labels as ordinary links -->\n",
|
|
"<div id=\"_auto7\"></div>\n",
|
|
"\n",
|
|
"$$\n",
|
|
"\\begin{equation}\n",
|
|
" w_j^{(m+1)} =w_j^{(m)}-\\frac{a_{jm}^{(m)}w_m^{(m)}}{a_{mm}^{(m)}}\\quad j=m+1,\\dots,n.\n",
|
|
"\\label{_auto7} \\tag{8}\n",
|
|
"\\end{equation}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"This set of $n-1$ elimations leads us to an equations which is solved by back substitution.\n",
|
|
"If the arithmetics is exact and the matrix $\\mathbf{A}$ is not singular, then the computed answer will be exact.\n",
|
|
"\n",
|
|
"Even though the matrix elements along the diagonal are not zero,\n",
|
|
"numerically small numbers may appear and subsequent divisions may lead to large numbers, which, if added\n",
|
|
"to a small number may yield losses of precision. Suppose for example that our first division in $(a_{22}-a_{21}a_{12}/a_{11})$\n",
|
|
"results in $-10^{-7}$ and that $a_{22}$ is one.\n",
|
|
"one. We are then\n",
|
|
"adding $10^7+1$. With single precision this results in $10^7$.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Linear Algebra Methods\n",
|
|
"\n",
|
|
" * Gaussian elimination, $O(2/3n^3)$ flops, general matrix\n",
|
|
"\n",
|
|
" * LU decomposition, upper triangular and lower tridiagonal matrices, $O(2/3n^3)$ flops, general matrix. Get easily the inverse, determinant and can solve linear equations with back-substitution only, $O(n^2)$ flops\n",
|
|
"\n",
|
|
" * Cholesky decomposition. Real symmetric or hermitian positive definite matrix, $O(1/3n^3)$ flops.\n",
|
|
"\n",
|
|
" * Tridiagonal linear systems, important for differential equations. Normally positive definite and non-singular. $O(8n)$ flops for symmetric. Special case of banded matrices.\n",
|
|
"\n",
|
|
" * Singular value decomposition\n",
|
|
"\n",
|
|
" * the QR method will be discussed in chapter 7 in connection with eigenvalue systems. $O(4/3n^3)$ flops.\n",
|
|
"\n",
|
|
"## LU Decomposition\n",
|
|
"\n",
|
|
"The LU decomposition method means that we can rewrite\n",
|
|
"this matrix as the product of two matrices $\\mathbf{L}$ and $\\mathbf{U}$\n",
|
|
"where"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\begin{bmatrix}\n",
|
|
" a_{11} & a_{12} & a_{13} & a_{14} \\\\\n",
|
|
" a_{21} & a_{22} & a_{23} & a_{24} \\\\\n",
|
|
" a_{31} & a_{32} & a_{33} & a_{34} \\\\\n",
|
|
" a_{41} & a_{42} & a_{43} & a_{44}\n",
|
|
" \\end{bmatrix}\n",
|
|
" = \\begin{bmatrix}\n",
|
|
" 1 & 0 & 0 & 0 \\\\\n",
|
|
" l_{21} & 1 & 0 & 0 \\\\\n",
|
|
" l_{31} & l_{32} & 1 & 0 \\\\\n",
|
|
" l_{41} & l_{42} & l_{43} & 1\n",
|
|
" \\end{bmatrix}\n",
|
|
" \\begin{bmatrix}\n",
|
|
" u_{11} & u_{12} & u_{13} & u_{14} \\\\\n",
|
|
" 0 & u_{22} & u_{23} & u_{24} \\\\\n",
|
|
" 0 & 0 & u_{33} & u_{34} \\\\\n",
|
|
" 0 & 0 & 0 & u_{44}\n",
|
|
" \\end{bmatrix}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## LU Decomposition\n",
|
|
"\n",
|
|
"LU decomposition forms the backbone of other algorithms in linear algebra, such as the\n",
|
|
"solution of linear equations given by"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"a_{11}x_1 +a_{12}x_2 +a_{13}x_3 + a_{14}x_4=w_1 \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"a_{21}x_1 + a_{22}x_2 + a_{23}x_3 + a_{24}x_4=w_2 \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"a_{31}x_1 + a_{32}x_2 + a_{33}x_3 + a_{34}x_4=w_3 \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"a_{41}x_1 + a_{42}x_2 + a_{43}x_3 + a_{44}x_4=w_4. \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"The above set of equations is conveniently solved by using LU decomposition as an intermediate step.\n",
|
|
"\n",
|
|
"The matrix $\\mathbf{A}\\in \\mathbb{R}^{n\\times n}$ has an LU factorization if the determinant\n",
|
|
"is different from zero. If the LU factorization exists and $\\mathbf{A}$ is non-singular, then the LU factorization\n",
|
|
"is unique and the determinant is given by"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"det\\{\\mathbf{A}\\}=det\\{\\mathbf{LU}\\}= det\\{\\mathbf{L}\\}det\\{\\mathbf{U}\\}=u_{11}u_{22}\\dots u_{nn}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## LU Decomposition, why?\n",
|
|
"\n",
|
|
"There are at least three main advantages with LU decomposition compared with standard Gaussian elimination:\n",
|
|
"\n",
|
|
" * It is straightforward to compute the determinant of a matrix\n",
|
|
"\n",
|
|
" * If we have to solve sets of linear equations with the same matrix but with different vectors $\\mathbf{y}$, the number of FLOPS is of the order $n^3$.\n",
|
|
"\n",
|
|
" * The inverse is such an operation \n",
|
|
"\n",
|
|
"## LU Decomposition, linear equations\n",
|
|
"\n",
|
|
"With the LU decomposition it is rather\n",
|
|
"simple to solve a system of linear equations"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"a_{11}x_1 +a_{12}x_2 +a_{13}x_3 + a_{14}x_4=w_1 \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"a_{21}x_1 + a_{22}x_2 + a_{23}x_3 + a_{24}x_4=w_2 \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"a_{31}x_1 + a_{32}x_2 + a_{33}x_3 + a_{34}x_4=w_3 \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"a_{41}x_1 + a_{42}x_2 + a_{43}x_3 + a_{44}x_4=w_4. \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"This can be written in matrix form as"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mathbf{Ax}=\\mathbf{w}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"where $\\mathbf{A}$ and $\\mathbf{w}$ are known and we have to solve for\n",
|
|
"$\\mathbf{x}$. Using the LU dcomposition we write"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mathbf{A} \\mathbf{x} \\equiv \\mathbf{L} \\mathbf{U} \\mathbf{x} =\\mathbf{w}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## LU Decomposition, linear equations\n",
|
|
"\n",
|
|
"The previous equation can be calculated in two steps"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mathbf{L} \\mathbf{y} = \\mathbf{w};\\qquad \\mathbf{Ux}=\\mathbf{y}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"To show that this is correct we use to the LU decomposition\n",
|
|
"to rewrite our system of linear equations as"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mathbf{LUx}=\\mathbf{w},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"and since the determinat of $\\mathbf{L}$ is equal to 1 (by construction\n",
|
|
"since the diagonals of $\\mathbf{L}$ equal 1) we can use the inverse of\n",
|
|
"$\\mathbf{L}$ to obtain"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mathbf{Ux}=\\mathbf{L^{-1}w}=\\mathbf{y},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"which yields the intermediate step"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mathbf{L^{-1}w}=\\mathbf{y}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"and as soon as we have $\\mathbf{y}$ we can obtain $\\mathbf{x}$\n",
|
|
"through $\\mathbf{Ux}=\\mathbf{y}$.\n",
|
|
"\n",
|
|
"## LU Decomposition, why?\n",
|
|
"\n",
|
|
"For our four-dimentional example this takes the form"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"y_1=w_1 \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"l_{21}y_1 + y_2=w_2\\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"l_{31}y_1 + l_{32}y_2 + y_3 =w_3\\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"l_{41}y_1 + l_{42}y_2 + l_{43}y_3 + y_4=w_4. \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"and"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"u_{11}x_1 +u_{12}x_2 +u_{13}x_3 + u_{14}x_4=y_1 \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"u_{22}x_2 + u_{23}x_3 + u_{24}x_4=y_2\\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"u_{33}x_3 + u_{34}x_4=y_3\\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"u_{44}x_4=y_4 \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"This example shows the basis for the algorithm\n",
|
|
"needed to solve the set of $n$ linear equations.\n",
|
|
"\n",
|
|
"## LU Decomposition, linear equations\n",
|
|
"\n",
|
|
"The algorithm goes as follows\n",
|
|
"\n",
|
|
" * Set up the matrix $\\bf A$ and the vector $\\bf w$ with their correct dimensions. This determines the dimensionality of the unknown vector $\\bf x$.\n",
|
|
"\n",
|
|
" * Then LU decompose the matrix $\\bf A$ through a call to the function `ludcmp(double a, int n, int indx, double &d)`. This functions returns the LU decomposed matrix $\\bf A$, its determinant and the vector indx which keeps track of the number of interchanges of rows. If the determinant is zero, the solution is malconditioned.\n",
|
|
"\n",
|
|
" * Thereafter you call the function `lubksb(double a, int n, int indx, double w)` which uses the LU decomposed matrix $\\bf A$ and the vector $\\bf w$ and returns $\\bf x$ in the same place as $\\bf w$. Upon exit the original content in $\\bf w$ is destroyed. If you wish to keep this information, you should make a backup of it in your calling function.\n",
|
|
"\n",
|
|
"## LU Decomposition, the inverse of a matrix\n",
|
|
"\n",
|
|
"If the inverse exists then"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mathbf{A}^{-1}\\mathbf{A}=\\mathbf{I},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"the identity matrix. With an LU decomposed matrix we can rewrite the last equation as"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mathbf{LU}\\mathbf{A}^{-1}=\\mathbf{I}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## LU Decomposition, the inverse of a matrix\n",
|
|
"\n",
|
|
"If we assume that the first column (that is column 1) of the inverse matrix\n",
|
|
"can be written as a vector with unknown entries"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mathbf{A}_1^{-1}= \\begin{bmatrix}\n",
|
|
" a_{11}^{-1} \\\\\n",
|
|
" a_{21}^{-1} \\\\\n",
|
|
" \\dots \\\\\n",
|
|
" a_{n1}^{-1} \\\\\n",
|
|
" \\end{bmatrix},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"then we have a linear set of equations"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mathbf{LU}\\begin{bmatrix}\n",
|
|
" a_{11}^{-1} \\\\\n",
|
|
" a_{21}^{-1} \\\\\n",
|
|
" \\dots \\\\\n",
|
|
" a_{n1}^{-1} \\\\\n",
|
|
" \\end{bmatrix} =\\begin{bmatrix}\n",
|
|
" 1 \\\\\n",
|
|
" 0 \\\\\n",
|
|
" \\dots \\\\\n",
|
|
" 0 \\\\\n",
|
|
" \\end{bmatrix}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## LU Decomposition, the inverse\n",
|
|
"\n",
|
|
"In a similar way we can compute the unknow entries of the second column,"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mathbf{LU}\\begin{bmatrix}\n",
|
|
" a_{12}^{-1} \\\\\n",
|
|
" a_{22}^{-1} \\\\\n",
|
|
" \\dots \\\\\n",
|
|
" a_{n2}^{-1} \\\\\n",
|
|
" \\end{bmatrix}=\\begin{bmatrix}\n",
|
|
" 0 \\\\\n",
|
|
" 1 \\\\\n",
|
|
" \\dots \\\\\n",
|
|
" 0 \\\\\n",
|
|
" \\end{bmatrix},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"and continue till we have solved all $n$ sets of linear equations.\n",
|
|
"\n",
|
|
"\n",
|
|
"## [Using Armadillo to perform an LU decomposition](https://github.com/CompPhysics/ComputationalPhysicsMSU/blob/master/doc/Programs/CppQtCodesLectures/MatrixTest/main.cpp)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
" #include <iostream>\n",
|
|
" #include \"armadillo\"\n",
|
|
" using namespace arma;\n",
|
|
" using namespace std;\n",
|
|
" \n",
|
|
" int main()\n",
|
|
" {\n",
|
|
" mat A = randu<mat>(5,5);\n",
|
|
" vec b = randu<vec>(5);\n",
|
|
" \n",
|
|
" A.print(\"A =\");\n",
|
|
" b.print(\"b=\");\n",
|
|
" // solve Ax = b\n",
|
|
" vec x = solve(A,b);\n",
|
|
" // print x\n",
|
|
" x.print(\"x=\");\n",
|
|
" // find LU decomp of A, if needed, P is the permutation matrix\n",
|
|
" mat L, U;\n",
|
|
" lu(L,U,A);\n",
|
|
" // print l\n",
|
|
" L.print(\" L= \");\n",
|
|
" // print U\n",
|
|
" U.print(\" U= \");\n",
|
|
" //Check that A = LU\n",
|
|
" (A-L*U).print(\"Test of LU decomposition\");\n",
|
|
" return 0;\n",
|
|
" }\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Iterative methods, Chapter 6\n",
|
|
" * Direct solvers such as Gauss elimination and LU decomposition discussed in connection with project 1.\n",
|
|
"\n",
|
|
" * Iterative solvers such as Basic iterative solvers, Jacobi, Gauss-Seidel, Successive over-relaxation. These methods are easy to parallelize, as we will se later. Much used in solutions of partial differential equations.\n",
|
|
"\n",
|
|
" * Other iterative methods such as Krylov subspace methods with Generalized minimum residual (GMRES) and Conjugate gradient etc will not be discussed.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Iterative methods, Jacobi's method\n",
|
|
"It is a simple method for solving"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mathbf{A}\\mathbf{x}=\\mathbf{b},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"where $\\mathbf{A}$ is a matrix and $\\mathbf{x}$ and $\\mathbf{b}$ are vectors. The vector $\\mathbf{x}$ is \n",
|
|
"the unknown.\n",
|
|
"\n",
|
|
"It is an iterative scheme where we start with a guess for the unknown, and \n",
|
|
"after $k+1$ iterations we have"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mathbf{x}^{(k+1)}= \\mathbf{D}^{-1}(\\mathbf{b}-(\\mathbf{L}+\\mathbf{U})\\mathbf{x}^{(k)}),\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"with $\\mathbf{A}=\\mathbf{D}+\\mathbf{U}+\\mathbf{L}$ and\n",
|
|
"$\\mathbf{D}$ being a diagonal matrix, $\\mathbf{U}$ an upper triangular matrix and $\\mathbf{L}$ a lower triangular\n",
|
|
"matrix.\n",
|
|
"\n",
|
|
"If the matrix $\\mathbf{A}$ is positive definite or diagonally dominant, one can show that this method will always converge to the exact solution.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Iterative methods, Jacobi's method\n",
|
|
"We can demonstrate Jacobi's method by this $4\\times 4$ matrix problem. We assume a guess\n",
|
|
"for the vector elements $x_i^{(0)}$, a guess which represents our first iteration. The new\n",
|
|
"values are obtained by substitution"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"x_1^{(1)} =(b_1-a_{12}x_2^{(0)} -a_{13}x_3^{(0)} - a_{14}x_4^{(0)})/a_{11} \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"x_2^{(1)} =(b_2-a_{21}x_1^{(0)} - a_{23}x_3^{(0)} - a_{24}x_4^{(0)})/a_{22} \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"x_3^{(1)} =(b_3- a_{31}x_1^{(0)} -a_{32}x_2^{(0)} -a_{34}x_4^{(0)})/a_{33} \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"x_4^{(1)}=(b_4-a_{41}x_1^{(0)} -a_{42}x_2^{(0)} - a_{43}x_3^{(0)})/a_{44}, \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"which after $k+1$ iterations reads"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"x_1^{(k+1)} =(b_1-a_{12}x_2^{(k)} -a_{13}x_3^{(k)} - a_{14}x_4^{(k)})/a_{11} \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"x_2^{(k+1)} =(b_2-a_{21}x_1^{(k)} - a_{23}x_3^{(k)} - a_{24}x_4^{(k)})/a_{22} \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"x_3^{(k+1)} =(b_3- a_{31}x_1^{(k)} -a_{32}x_2^{(k)} -a_{34}x_4^{(k)})/a_{33} \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"x_4^{(k+1)}=(b_4-a_{41}x_1^{(k)} -a_{42}x_2^{(k)} - a_{43}x_3^{(k)})/a_{44}, \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Iterative methods, Jacobi's method\n",
|
|
"We can generalize the above equations to"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"x_i^{(k+1)}=(b_i-\\sum_{j=1, j\\ne i}^{n}a_{ij}x_j^{(k)})/a_{ii}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"or in an even more compact form as"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mathbf{x}^{(k+1)}= \\mathbf{D}^{-1}(\\mathbf{b}-(\\mathbf{L}+\\mathbf{U})\\mathbf{x}^{(k)}),\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"with $\\mathbf{A}=\\mathbf{D}+\\mathbf{U}+\\mathbf{L}$ and\n",
|
|
"$\\mathbf{D}$ being a diagonal matrix, $\\mathbf{U}$ an upper triangular matrix and $\\mathbf{L}$ a lower triangular\n",
|
|
"matrix.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Iterative methods, Gauss-Seidel's method\n",
|
|
"Our $4\\times 4$ matrix problem"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"x_1^{(k+1)} =(b_1-a_{12}x_2^{(k)} -a_{13}x_3^{(k)} - a_{14}x_4^{(k)})/a_{11} \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"x_2^{(k+1)} =(b_2-a_{21}x_1^{(k)} - a_{23}x_3^{(k)} - a_{24}x_4^{(k)})/a_{22} \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"x_3^{(k+1)} =(b_3- a_{31}x_1^{(k)} -a_{32}x_2^{(k)} -a_{34}x_4^{(k)})/a_{33} \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"x_4^{(k+1)}=(b_4-a_{41}x_1^{(k)} -a_{42}x_2^{(k)} - a_{43}x_3^{(k)})/a_{44}, \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"can be rewritten as"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"x_1^{(k+1)} =(b_1-a_{12}x_2^{(k)} -a_{13}x_3^{(k)} - a_{14}x_4^{(k)})/a_{11} \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"x_2^{(k+1)} =(b_2-a_{21}x_1^{(k+1)} - a_{23}x_3^{(k)} - a_{24}x_4^{(k)})/a_{22} \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"x_3^{(k+1)} =(b_3- a_{31}x_1^{(k+1)} -a_{32}x_2^{(k+1)} -a_{34}x_4^{(k)})/a_{33} \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"x_4^{(k+1)}=(b_4-a_{41}x_1^{(k+1)} -a_{42}x_2^{(k+1)} - a_{43}x_3^{(k+1)})/a_{44}, \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"which allows us to utilize the preceding solution (forward substitution). This improves normally the convergence\n",
|
|
"behavior and leads to the Gauss-Seidel method!\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Iterative methods, Gauss-Seidel's method\n",
|
|
"We can generalize"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"x_1^{(k+1)} =(b_1-a_{12}x_2^{(k)} -a_{13}x_3^{(k)} - a_{14}x_4^{(k)})/a_{11} \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"x_2^{(k+1)} =(b_2-a_{21}x_1^{(k+1)} - a_{23}x_3^{(k)} - a_{24}x_4^{(k)})/a_{22} \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"x_3^{(k+1)} =(b_3- a_{31}x_1^{(k+1)} -a_{32}x_2^{(k+1)} -a_{34}x_4^{(k)})/a_{33} \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"x_4^{(k+1)}=(b_4-a_{41}x_1^{(k+1)} -a_{42}x_2^{(k+1)} - a_{43}x_3^{(k+1)})/a_{44}, \\nonumber\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"to the following form"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"x^{(k+1)}_i = \\frac{1}{a_{ii}} \\left(b_i - \\sum_{j > i}a_{ij}x^{(k)}_j - \\sum_{j < i}a_{ij}x^{(k+1)}_j \\right),\\quad i=1,2,\\ldots,n.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"The procedure is generally continued until the changes made by an iteration are below some tolerance.\n",
|
|
"\n",
|
|
"The convergence properties of the Jacobi method and the \n",
|
|
"Gauss-Seidel method are dependent on the matrix $\\mathbf{A}$. These methods converge when\n",
|
|
"the matrix is symmetric positive-definite, or is strictly or irreducibly diagonally dominant.\n",
|
|
"Both methods sometimes converge even if these conditions are not satisfied.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Iterative methods, Successive over-relaxation\n",
|
|
"Given a square system of n linear equations with unknown $\\mathbf x$:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mathbf{A}\\mathbf x = \\mathbf b\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"where"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mathbf{A}=\\begin{bmatrix} a_{11} & a_{12} & \\cdots & a_{1n} \\\\ a_{21} & a_{22} & \\cdots & a_{2n} \\\\ \\vdots & \\vdots & \\ddots & \\vdots \\\\a_{n1} & a_{n2} & \\cdots & a_{nn} \\end{bmatrix}, \\qquad \\mathbf{x} = \\begin{bmatrix} x_{1} \\\\ x_2 \\\\ \\vdots \\\\ x_n \\end{bmatrix} , \\qquad \\mathbf{b} = \\begin{bmatrix} b_{1} \\\\ b_2 \\\\ \\vdots \\\\ b_n \\end{bmatrix}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Iterative methods, Successive over-relaxation\n",
|
|
"Then A can be decomposed into a diagonal component D, and strictly lower and upper triangular components L and U:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mathbf{A} =\\mathbf{D} + \\mathbf{L} + \\mathbf{U},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"where"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"D = \\begin{bmatrix} a_{11} & 0 & \\cdots & 0 \\\\ 0 & a_{22} & \\cdots & 0 \\\\ \\vdots & \\vdots & \\ddots & \\vdots \\\\0 & 0 & \\cdots & a_{nn} \\end{bmatrix}, \\quad L = \\begin{bmatrix} 0 & 0 & \\cdots & 0 \\\\ a_{21} & 0 & \\cdots & 0 \\\\ \\vdots & \\vdots & \\ddots & \\vdots \\\\a_{n1} & a_{n2} & \\cdots & 0 \\end{bmatrix}, \\quad U = \\begin{bmatrix} 0 & a_{12} & \\cdots & a_{1n} \\\\ 0 & 0 & \\cdots & a_{2n} \\\\ \\vdots & \\vdots & \\ddots & \\vdots \\\\0 & 0 & \\cdots & 0 \\end{bmatrix}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"The system of linear equations may be rewritten as:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"(D+\\omega L) \\mathbf{x} = \\omega \\mathbf{b} - [\\omega U + (\\omega-1) D ] \\mathbf{x}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"for a constant $\\omega > 1$.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Iterative methods, Successive over-relaxation\n",
|
|
"The method of successive over-relaxation is an iterative technique that solves the left hand side of this expression for $x$, using previous value for $x$ on the right hand side. Analytically, this may be written as:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mathbf{x}^{(k+1)} = (D+\\omega L)^{-1} \\big(\\omega \\mathbf{b} - [\\omega U + (\\omega-1) D ] \\mathbf{x}^{(k)}\\big).\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"However, by taking advantage of the triangular form of $(D+\\omega L)$, the elements of $x^{(k+1)}$ can be computed sequentially using forward substitution:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"x^{(k+1)}_i = (1-\\omega)x^{(k)}_i + \\frac{\\omega}{a_{ii}} \\left(b_i - \\sum_{j > i} a_{ij}x^{(k)}_j - \\sum_{j < i} a_{ij}x^{(k+1)}_j \\right),\\quad i=1,2,\\ldots,n.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"The choice of relaxation factor is not necessarily easy, and depends upon the properties of the coefficient matrix. For symmetric, positive-definite matrices it can be proven that $0 < \\omega < 2$ will lead to convergence, but we are generally interested in faster convergence rather than just convergence."
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|