{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "ChEn-3170: Computational Methods in Chemical Engineering Fall 2018 UMass Lowell; Prof. V. F. de Almeida **24Sep2018**\n", "\n", "# 05. Computational Linear Algebra Fundamentals\n", "$ \n", " \\newcommand{\\Amtrx}{\\boldsymbol{\\mathsf{A}}}\n", " \\newcommand{\\Bmtrx}{\\boldsymbol{\\mathsf{B}}}\n", " \\newcommand{\\Cmtrx}{\\boldsymbol{\\mathsf{C}}}\n", " \\newcommand{\\Dmtrx}{\\boldsymbol{\\mathsf{D}}}\n", " \\newcommand{\\Mmtrx}{\\boldsymbol{\\mathsf{M}}}\n", " \\newcommand{\\Imtrx}{\\boldsymbol{\\mathsf{I}}}\n", " \\newcommand{\\Pmtrx}{\\boldsymbol{\\mathsf{P}}}\n", " \\newcommand{\\Qmtrx}{\\boldsymbol{\\mathsf{Q}}}\n", " \\newcommand{\\Lmtrx}{\\boldsymbol{\\mathsf{L}}}\n", " \\newcommand{\\Umtrx}{\\boldsymbol{\\mathsf{U}}}\n", " \\newcommand{\\xvec}{\\boldsymbol{\\mathsf{x}}}\n", " \\newcommand{\\avec}{\\boldsymbol{\\mathsf{a}}}\n", " \\newcommand{\\bvec}{\\boldsymbol{\\mathsf{b}}}\n", " \\newcommand{\\cvec}{\\boldsymbol{\\mathsf{c}}}\n", " \\newcommand{\\rvec}{\\boldsymbol{\\mathsf{r}}}\n", " \\newcommand{\\norm}[1]{\\bigl\\lVert{#1}\\bigr\\rVert}\n", " \\DeclareMathOperator{\\rank}{rank}\n", "$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "## Table of Contents\n", "* [Theory](#theory)\n", "* [Matrix-vector and matrix-matrix product operations](#product)\n", "* [NumPy and SciPy Linear Algebra](#pylinalg)\n", " + [Matrix solve](#pysolve)\n", " + [$\\Lmtrx$ forward solve](#pyl)\n", " + [$\\Umtrx$ backward solve](#pyu)\n", " + [$\\Amtrx = \\Pmtrx\\,\\Lmtrx\\,\\Umtrx$ factorization](#[pyplu)\n", "* [Course Linear Algebra](#courselinalg)\n", " + [$\\Lmtrx$ forward solve](#l)\n", " + [$\\Umtrx$ forward solve](#u)\n", " + [$\\Amtrx = \\Lmtrx\\,\\Umtrx$ factorization](#lu)\n", " + [$\\Pmtrx\\,\\Amtrx = \\Lmtrx\\,\\Umtrx$ factorization](#plu)\n", " + [$\\Pmtrx\\,\\Amtrx\\,\\Qmtrx = \\Lmtrx\\,\\Umtrx$ factorization](#pqlu)\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Theory\n", "The course notes (OneNote [ChEn-3170-linalg](https://studentuml-my.sharepoint.com/:o:/g/personal/valmor_dealmeida_uml_edu/EhHdq8qIW_JNiMBu60tKHi8BV6Nv8jonllo__NOtX7JgkQ?e=jA5xj8)) cover basic elements of lineary system of algebraic equations. Their representation in row and column formats and the insight gained from them when searching for a solution of the system.\n", "\n", "Some basic theoretical aspects of solving for $\\xvec$ in the matrix equation $\\Amtrx\\,\\xvec = \\bvec$ are covered. $\\Amtrx$ is a matrix, $\\bvec$ and $\\xvec$ are vectors." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Matrix-vector and matrix-matrix product operations\n", "The following operations between vectors and matrices are obtained directly from the buil-in functions in the `numpy` package." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'''Import the NumPy package as usual'''\n", "\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Scalar product of two vectors: $\\avec \\cdot \\bvec$." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'''Vector scalar product or dot product of vectors'''\n", "\n", "a_vec = np.array( np.random.random(3) )\n", "b_vec = np.array( np.random.random(3) )\n", "\n", "a_vec_dot_b_vec = np.dot( a_vec, b_vec ) # clear linear algebra operation\n", "print('a.b =', a_vec_dot_b_vec)\n", "\n", "a_vec_x_b_vec = a_vec @ b_vec # consistent linear algebra multiplication\n", "print('a@b =', a_vec_x_b_vec )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Matrix vector product: $\\Amtrx\\,\\bvec$." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'''Matrix-vector product'''\n", "\n", "a_mtrx = np.array( [ [ 2., 1., 1.], # per course notes (p 04)\n", " [ 4., -6., 0.],\n", " [-2., 7., 2.]])\n", "\n", "b_vec = np.array( [5., -2., 9.]) # per course notes\n", "\n", "a_mtrx_x_b_vec = a_mtrx @ b_vec # linear algebra matrix-vector product\n", "\n", "print('A x b =', a_mtrx_x_b_vec)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Matrix-vector product: $\\Imtrx\\,\\bvec = \\bvec$. Note: $\\begin{pmatrix}\n", "1 & 0 & 0 \\\\\n", "0 & 1 & 0 \\\\\n", "0 & 0 & 1\n", "\\end{pmatrix} \\, \\begin{pmatrix} b_1\\\\b_2\\\\b_3 \\end{pmatrix} = \\begin{pmatrix} b_1\\\\b_2\\\\b_3 \\end{pmatrix} $." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'''Identity-matrix vector product'''\n", "\n", "i_mtrx = np.eye(3)\n", "\n", "i_mtrx_x_b_vec = i_mtrx @ b_vec # linear algebra matrix-vector product\n", "\n", "print('I x b =', i_mtrx_x_b_vec)\n", "print('b =', b_vec)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Matrix-matrix product: $\\Imtrx\\,\\Amtrx = \\Amtrx$. Note: $\\begin{pmatrix}\n", "1 & 0 & 0 \\\\\n", "0 & 1 & 0 \\\\\n", "0 & 0 & 1\n", "\\end{pmatrix} \\, \n", "\\begin{pmatrix} \n", "A_{1,1} & A_{1,2} & A_{1,3} \\\\\n", "A_{2,1} & A_{2,2} & A_{2,3} \\\\\n", "A_{3,1} & A_{3,2} & A_{3,3}\n", "\\end{pmatrix} = \n", "\\begin{pmatrix} \n", "A_{1,1} & A_{1,2} & A_{1,3} \\\\\n", "A_{2,1} & A_{2,2} & A_{2,3} \\\\\n", "A_{3,1} & A_{3,2} & A_{3,3}\n", "\\end{pmatrix}\n", "$." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'''Matrix-matrix product IA = A'''\n", "\n", "i_mtrx_x_a_mtrx = i_mtrx @ a_mtrx # linear algebra matrix-matrix product\n", "\n", "print('I x A =\\n', i_mtrx_x_a_mtrx)\n", "print('A =\\n', a_mtrx)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Matrix-matrix product: $\\Amtrx\\,\\Bmtrx = \\Cmtrx$. Note: \n", "$\\begin{pmatrix}\n", "A_{1,1} & A_{1,2} & A_{1,3} \\\\\n", "A_{2,1} & A_{2,2} & A_{2,3} \\\\\n", "A_{3,1} & A_{3,2} & A_{3,3}\n", "\\end{pmatrix} \\, \n", "\\begin{pmatrix} \n", "B_{1,1} & B_{1,2} & B_{1,3} \\\\\n", "B_{2,1} & B_{2,2} & B_{2,3} \\\\\n", "B_{3,1} & B_{3,2} & B_{3,3}\n", "\\end{pmatrix} = \n", "\\begin{pmatrix} \n", "C_{1,1} & C_{1,2} & C_{1,3} \\\\\n", "C_{2,1} & C_{2,2} & C_{2,3} \\\\\n", "C_{3,1} & C_{3,2} & C_{3,3}\n", "\\end{pmatrix}\n", "$ where each $C_{i,j}$ is a vector product of the $i$th row of $\\Amtrx$ and the $j$th column of $\\Bmtrx$, *i.e.* \n", "$C_{i,j} = \\sum\\limits_{k=1}^3 A_{i,k}\\, B_{k,j}$." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'''Matrix-matrix product AB = C'''\n", "\n", "b_mtrx = np.array( [[5. , 5. , 5.],\n", " [-2., -2., -2.],\n", " [9. , 9. , 9.]]\n", " )\n", "c_mtrx = a_mtrx @ b_mtrx # linear algebra matrix-matrix product\n", "\n", "print('A =\\n', a_mtrx)\n", "print('B =\\n', b_mtrx)\n", "print('C =\\n', c_mtrx)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "