{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 4 Matrices"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# import all python add-ons etc that will be needed later on\n",
"%matplotlib inline\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from sympy import *\n",
"init_printing() # allows printing of SymPy results in typeset maths format\n",
"plt.rcParams.update({'font.size': 14}) # set font size for plots"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## 4 Basic properties of Matrices\n",
"The rest of this chapter describes the properties and uses of matrices. The determinant described in the previous sections can be considered as one of the many properties of a matrix. In several places, reference is made to eigenvalues and eigenvectors before they have been fully explained, which is done in Section 12.3. The meaning of these words is outlined below but how they are obtained is left unanswered for the moment.\n",
"The eigenvalue - eigenvector equation is met, for example, in solving problems in Quantum Mechanics and Chemical Kinetics and has the form,\n",
"\n",
"$$\\displaystyle \\mathrm{operator \\times function = constant \\times same\\; function}$$\n",
"\n",
"provided the function is not zero. In matrix form, the eigenvector - eigenvalue equation is\n",
"\n",
"$$\\displaystyle \\pmb{Ax}=\\lambda \\pmb{x}$$\n",
"\n",
"where $\\pmb A$ is an $n \\times n$ square matrix, $\\pmb x$ is one of possibly $n$, one-dimensional column matrices (column vectors) each of length $n$ and each one is called an *eigenvector*, $\\lambda$ represents one of $n$ numbers, and each $\\lambda$ is called an *eigenvalue*. What this equation shows us is that the operator (matrix $\\pmb A$) changes a vector $\\pmb x$ by stretching or contracting it by an amount $\\lambda$ but leaves it pointing in the same direction.\n",
"\n",
"### **(i) Addition and subtraction**\n",
"\n",
"To add or subtract two matrices they must have the same shape. To add or subtract a constant or another matrix their individual elements are added or subtracted. Similarly, to multiply or divide by a constant number (a scalar), each element is also multiplied or divided by this value. Multiplying two matrices together is more complicated and depends on the order with which this is done and the shapes of the two matrices; sometimes it is just not possible.\n",
"\n",
"The notation used, is that bold italic letters indicate a matrix and the numbers $\\pmb 0$ and $\\pmb 1$ for the null and unit matrix respectively.\n",
"\n",
"Suppose two matrices are $\\displaystyle \\pmb{A}=\\begin{bmatrix} 2&1&5\\\\-5 & 2 & 6\\end{bmatrix}$ and $\\displaystyle \\pmb{B}=\\begin{bmatrix} 1&10&8\\\\-3 & 3 & 9\\end{bmatrix}$ calculating \n",
"\n",
"$$\\displaystyle \\pmb{A}+\\pmb{B},\\; \\pmb{A}-\\pmb{B},\\;\\pmb{3A}+\\pmb{0.5B}\\quad\\text{etc.}$$\n",
"\n",
"is easy, for example, taking elements individually,\n",
"\n",
"$$\\displaystyle \\pmb{A}+\\pmb{B}=\\begin{bmatrix} 3&11&13\\\\-8 & 5 & 16\\end{bmatrix}$$\n",
"\n",
"and similarly for the other calculations.\n",
"\n",
"### **(ii) Division**\n",
"One matrix _cannot_ be divided by another; the inverse matrix of the divisor is formed instead and then these matrices are multiplied together. The inverse of a matrix $\\pmb M$ is always written as $\\pmb{M}^{-1}$. Generating the inverse of a matrix is difficult unless the matrix is small, and one would normally use Python/Sympy to do this.\n",
"\n",
"### **(iii) Multiplication**\n",
"Matrices can be multiplied together. The multiplication order is always important; $\\pmb{AB}$ is not necessarily the same as $\\pmb{BA}$. With three or more matrices $\\pmb{ABC}$, the multiplication sequence does not matter as long as the ordering is the same. This is the associatite property\n",
"\n",
"$$\\displaystyle \\pmb{ABC}\\equiv \\pmb{A}(\\pmb{BC})\\equiv (\\pmb{AB})\\pmb{C}$$\n",
"\n",
"If $\\pmb{BC}$ are multiplied first, then the result left multiplied by $\\pmb{A}$, this is the same as multiplying $\\pmb{AB}$ to form a new matrix, $\\pmb{D}$ for example, then performing the multiplication $\\pmb{DC}$. Habitually, we start at the right and multiply the $\\pmb{BC}$ pair first then left multiply by $\\pmb{A}$. The details are described in Section 5.\n",
"\n",
"There are a few special matrices and several types operations that can be performed on matrices and these are describe next.\n",
"\n",
"## 4.1 The zero or null matrix\n",
"As you would expect it is possible to have a matrix of zeros; adding this to another matrix changes nothing, but multiplying by it annihilates the other matrix leaving only the zero matrix as you would expect.\n",
"\n",
"$$\\displaystyle \\pmb{0}=\\begin{bmatrix} 0 & 0 & \\cdots \\\\0 & 0 \\cdots \\\\ \\vdots & \\vdots & \\ddots \\end{bmatrix} \\qquad \\pmb{A}+\\pmb{0} =\\pmb{A};\\; \\pmb{A0}=\\pmb{0};\\; \\pmb{0A}=\\pmb{0}$$\n",
"\n",
"## 4.2 The unit or identity matrix $\\pmb{1}$.\n",
"\n",
"This is a matrix of unit diagonals with all other elements being zero. The unit matrix is also called the _multiplicative identity_.\n",
"\n",
"$$\\displaystyle \\pmb{1}=\\begin{bmatrix} 1 & 0 & 0& \\cdots \\\\0 & 1 & 0 &\\cdots \\\\ 0 & 0 & 1 & 0\\\\ \\vdots & & &\\ddots\\end{bmatrix}\\qquad \\pmb{A1}=\\pmb{A};\\; \\pmb{1A}=\\pmb{A}$$ \n",
"\n",
"## 4.3 Diagonal matrix\n",
"\n",
"This is a matrix whose diagonals can take any value, but all other elements are zero. The unit matrix is a special case. In general\n",
"\n",
"$$\\displaystyle \\begin{bmatrix} a & 0 & 0& \\cdots \\\\0 & b & 0 &\\cdots \\\\ 0 & 0 & c & 0\\\\ \\vdots & & &\\ddots\\end{bmatrix}$$\n",
"\n",
"## 4.4 Trace or character of a matrix\n",
"The trace, spur, or character of any matrix $\\pmb M$, is the sum of its diagonal elements only, \n",
"\n",
"$$\\displaystyle Tr(\\pmb{M})=\\sum_{i=0}a_{i,i}$$\n",
"\n",
"for example \n",
"\n",
"$$\\displaystyle \\pmb{M}= \\begin{bmatrix} 1 & 0 & 2 \\\\ 3 & -1 & 0 \\\\ 0 & 4 & -1 \\end{bmatrix} \\qquad Tr(\\pmb{M})= -1$$\n",
"\n",
"By multiplying out the product of matrices, the trace of the product is found to be unchanged if they are permuted cyclically:\n",
"\n",
"$$Tr(\\pmb{ABC}) = Tr(\\pmb{CAB}) = Tr(\\pmb{BCA})$$\n",
"\n",
"This fact is very useful in group theory because the trace of a matrix forms a _representation_ of a molecule's symmetry. Because of this rule, the trace, and hence representation, is independent of the basis set used to describe the symmetry.\n",
"\n",
"## 4.5 Matrix transpose $\\pmb{M}^T$\n",
"\n",
"The transpose operation replaces rows and columns with one another, and so exchanges element $(1, 2)$ with $(2, 1)$, and $(1, 3)$ with $(3, 1)$ and so on. For example;\n",
"\n",
"$$\\displaystyle\\begin{bmatrix} a & b & c \\\\ d & e & f \\end{bmatrix}^T= \\begin{bmatrix} a & d \\\\b & e\\\\ c & f \\end{bmatrix}$$ \n",
"\n",
"and for a square matrix this operation leaves the diagonal unchanged, and is, in effect, a reflection about the diagonal,\n",
"\n",
"$$\\displaystyle\\begin{bmatrix} a & b & c \\\\ d & e & f \\\\g & h & i \\end{bmatrix}^{\\,T}= \\begin{bmatrix} a & d & g\\\\b & e & h\\\\ c & f & i\\end{bmatrix}$$ \n",
"\n",
"You can appreciate that two transposes reproduce the initial matrix $(\\pmb{M}^T)^T = \\pmb{M}$.\n",
"\n",
"A _symmetric_ matrix is equal to its transpose and therefore must also be square and have off-diagonal elements $a_{ij} = a_{ji}$. An _antisymmetric_ matrix satisfies the identity $\\pmb{A} = -\\pmb{A}^T$ and must therefore have zeros on its diagonal and have components $a_{ij} = -a_{ji}$; for example, such a matrix and its transpose is\n",
"\n",
"$$\\displaystyle\\begin{bmatrix} 0 & 2 & 3 \\\\ -2 & 0 & 4 \\\\-3 & -4 & 0 \\end{bmatrix}^{\\,T}= \\begin{bmatrix} 0 & -2 & 3\\\\2 & 0 & -4\\\\ 3 & 4 & 0\\end{bmatrix}=-\\begin{bmatrix} 0 & 2 & 3 \\\\ -2 & 0 & 4 \\\\-3 & -4 & 0 \\end{bmatrix}$$\n",
"\n",
"When two matrices are multiplied, then the transpose reorders the matrix multiplication because rows are converted into columns and vice versa. This means that\n",
"\n",
"$$\\displaystyle (\\pmb{AB})^T=\\pmb{B}^T\\pmb{A}^T$$\n",
"\n",
"Matrix multiplication is described in section 5 and if for example if $\\displaystyle \\pmb{A}=\\begin{bmatrix} a & b \\\\ c & d \\end{bmatrix},\\; \\pmb{B}=\\begin{bmatrix} 1 & 2 \\\\ 3 & 4 \\end{bmatrix}$, then \n",
"\n",
"$$\\displaystyle \\pmb{AB}^T = \\begin{bmatrix} a +3b & 2a+4b \\\\ c+3d & 2c+4d \\end{bmatrix}^T=\\begin{bmatrix} a +3b & c+3d \\\\ 2a+4b & 2c+4d \\end{bmatrix}$$\n",
"\n",
"Performing the transpose then multiplying in reverse order gives the same result.\n",
"\n",
"$$\\displaystyle \\pmb{b}^T\\pmb{A}^T = \\begin{bmatrix} 1 & 3 \\\\ 2& 4 \\end{bmatrix}\\begin{bmatrix} a & c \\\\ b& d \\end{bmatrix}=\\begin{bmatrix} a +3b & c+3d \\\\ 2a+4b & 2c+4d \\end{bmatrix}$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4.6 Complex conjugate of a matrix M*\n",
"This changes each matrix element with its complex conjugate, assuming there are complex numbers in the matrix; if not, it has no effect. \n",
"\n",
"The conjugate of a matrix $\\pmb{M}$ is labelled $\\pmb{M}^*$. In making a complex conjugate each $i$ is replaced by $-i$ where $i =\\sqrt{-1}$, for example $[3\\; i\\;4]^* =[3\\;-i \\;4]$.\n",
"Some properties are\n",
"\n",
"$$\\displaystyle \\begin{align}&(\\pmb{M}^*)^* = \\pmb{M}, \\\\&(\\pmb{AB})^* = \\pmb{A}^*\\pmb{B}^*, \\\\ &|\\pmb{M}^*|=|\\pmb{M}|^*\\end{align}$$\n",
"\n",
"where the last property describes the effect on the determinant of the matrix.\n",
"\n",
"## 4.7 Adjoint matrix, $\\pmb{M}^\\dagger$\n",
"\n",
"This grand sounding name simply means\n",
"\n",
"$\\qquad\\qquad$_'Form the complex conjugate, then transpose the matrix or vice versa'_\n",
"\n",
"The special symbol $\\dagger$ is conventionally used as a superscript, e.g. \n",
"\n",
"$$\\displaystyle \\pmb M=\\begin{bmatrix} 1&0&0\\\\0&0&w\\\\0&w^2&0 \\end{bmatrix},\\qquad \\pmb M^\\dagger=\\begin{bmatrix} 1&0&0\\\\0&0&w^{*2}\\\\0&w^*&0 \\end{bmatrix}$$\n",
"\n",
"where $w$ is complex. The effect of the adjoint operation is \n",
"\n",
"$$\\displaystyle \\pmb{M}^\\dagger = (\\pmb{M}^*)^T=(\\pmb{M}^T)^*$$\n",
"\n",
"Other properties are, where $A$ and $B$ are matrices and $\\lambda$ a (complex) number,\n",
"\n",
"$$\\displaystyle (\\pmb{A}^\\dagger)^\\dagger=\\pmb{A},\\qquad (\\pmb{A}^\\dagger)^{-1}=(\\pmb{A}^{-1})^\\dagger, \\quad (\\lambda\\pmb{A})^\\dagger=\\lambda^*\\pmb{A}^\\dagger$$\n",
"\n",
"$$\\displaystyle (\\pmb{A}+\\pmb{B})^\\dagger=\\pmb{A}^\\dagger+\\pmb{B}^\\dagger,\\qquad (\\pmb{A}\\pmb{B})^\\dagger=\\pmb{B}^\\dagger\\pmb{A}^\\dagger,\\quad |\\pmb A^\\dagger|=|\\pmb A|^*$$\n",
"\n",
"In quantum mechanics, the adjoint is always used to convert a 'ket' into a 'bra' and vice versa, see Chapter 8.\n",
"\n",
"(Warning: In pure mathematics, in particular, the adjoint is defined differently as the transposed matrix of cofactors and call our adjoint the conjugate transpose or the Hermitian adjoint.)\n",
"\n",
"## 4.8 Hermitian or self-adjoint matrix $\\pmb{M}^\\dagger=\\pmb{M}$\n",
"In these matrices the diagonals are real and the off-diagonals $a_{i,j} = a_{j,i}^*$. The Pauli spin matrices of quantum mechanics are Hermitian; for example\n",
"\n",
"$$\\displaystyle \\pmb{\\sigma}_2=\\begin{bmatrix} 0 & i \\\\ -i & 0\\end{bmatrix}$$\n",
"\n",
"Taking the transpose then the complex conjugate produces a matrix that is clearly the same as $\\pmb{\\sigma}_2$. \n",
"\n",
"$$\\displaystyle \\begin{bmatrix} 0 & i \\\\ -i & 0\\end{bmatrix}^T=\\begin{bmatrix} 0 & -i \\\\ i & 0\\end{bmatrix}\\quad\\text{then}\\quad\n",
"\\begin{bmatrix} 0 & -i \\\\ i & 0\\end{bmatrix}^*=\\begin{bmatrix} 0 & i \\\\ -i & 0\\end{bmatrix}$$\n",
"\n",
"If the Hermitian matrix is real, which means that it contains only real numbers, then it must be symmetric. \n",
"\n",
"In quantum mechanics, a matrix containing the integrals that evaluate to expectation values is always a real Hermitian matrix whose eigenvalues are real and eigenvectors orthogonal, but not necessarily normalized.\n",
"\n",
"An _anti-Hermitian_ matrix is defined as $\\pmb{M}^\\dagger = -\\pmb{M}$.\n",
"\n",
"## 4.9 Inverse of a square matrix, $\\pmb{M}^{-1}$\n",
"\n",
"One matrix cannot be divided into another or into a constant so the operation $1/\\pmb{M}$ is not allowed; instead the matrix inverse must be formed. Furthermore the inverse is defined only for a square matrix and then only if its determinant is not zero, $|\\pmb{M}| \\ne 0$. \n",
"\n",
"The inverse is another matrix labelled $\\pmb{M}^{-1}$, such that\n",
"\n",
"$$\\displaystyle \\pmb{MM}^{-1} =\\pmb{1}=\\pmb{M}^{-1}\\pmb{M}$$\n",
"\n",
"where $\\pmb{1}$ is the unit (diagonal) matrix. Formally, we can find the inverse matrix element by element with the $ij^{th}$ element given by\n",
"\n",
"$$\\displaystyle (\\pmb{M}^{-1})_{i,j}=\\frac{(-1)^{i+j}}{|\\pmb{M}|}|\\pmb{C}_{j,i}|$$\n",
"\n",
"where $| \\pmb{M} | \\ne 0$ is the determinant and $| \\pmb{C}_{j,i} |$ is the cofactor matrix of row $j$ and column $i$. Note the change in ordering of the indices. Cofactors were described in Section 2.2. Generally, it is best to use Python/Sympy to calculate the inverse, because, besides being tedious, the chance of making an error is very high. The matrix inverse is met again when solving equations.\n",
"\n",
"If the matrix is diagonal then its inverse comprises the reciprocal of each term.\n",
"\n",
"$$\\displaystyle\\begin{bmatrix} a & 0 & 0 \\\\ 0 & b & 0 \\\\0 & 0 & c \\end{bmatrix}^{-1}= \\begin{bmatrix} 1/a & 0 & 0\\\\0 & 1/b & 0\\\\ 0 & 0 & 1/c\\end{bmatrix}$$\n",
"\n",
"and this is used in the calculation of vibrational normal modes.\n",
"\n",
"The rotation matrix is often needed when using molecular point groups\n",
"\n",
"$$\\displaystyle \\pmb M_R=\\begin{bmatrix} \\cos(\\theta)&\\sin(\\theta) &0\\\\ -\\sin(\\theta)&\\cos(\\theta) &0\\\\0&0&1\\end{bmatrix}$$\n",
"\n",
"has the inverse\n",
"\n",
"$$\\displaystyle \\pmb M_R^{-1}=\\begin{bmatrix} \\cos(\\theta)&-\\sin(\\theta) &0\\\\ \\sin(\\theta)&\\cos(\\theta) &0\\\\0&0&1\\end{bmatrix}$$\n",
"\n",
"as can be seen by showing that $\\pmb M_R\\pmb M_R^{-1}=\\pmb 1$.\n",
"\n",
"## 4.10 Singular matrix, determinant $|\\pmb{M}|=0$\n",
"\n",
"The determinant is zero and the matrix has no inverse. This is not the same as the null matrix, $\\pmb{0}$. Odd sized $n \\times n$, anti-symmetric matrices are singular, see section 4.6.\n",
"\n",
"\n",
"## 4.11 Unitary matrix $\\pmb M^\\dagger = \\pmb M^{-1}$ or $\\pmb {M}^\\dagger \\pmb{M} = \\pmb{1}$ or $\\pmb {M} \\pmb{M}^\\dagger = \\pmb{1}$ and $ |\\pmb {M} | = 1$\n",
"\n",
"A matrix is unitary if its adjoint is equal to its inverse and all unitary matrices must be square.\n",
"\n",
"An example of a unitary matrix is $\\displaystyle \\quad\\begin{bmatrix} 0 & 1 & 0 \\\\ 0 & 0 & 1\\\\1 & 0 & 0 \\end{bmatrix}$,\n",
"\n",
"which has the determinant $0(0 - 1) - 1(0 - 1) + 0(0 - 0) = 1$, \n",
"\n",
"The rotation matrix is also unitary.\n",
"\n",
"$$\\displaystyle \\begin{bmatrix} \\cos(\\theta) & \\sin(\\theta) \\\\ -\\sin(\\theta) & \\cos(\\theta) \\end{bmatrix} \\qquad\\qquad\\text{(3)}$$\n",
"\n",
"whose determinant is unity because $\\sin^2(\\theta)+\\cos^2(\\theta)=1$. \n",
"\n",
"## 4.12 Orthogonal matrices\n",
"\n",
"Orthogonal matrices are always square and have the properties that if $\\pmb M$ is a matrix then\n",
"\n",
"$$ \\displaystyle \\pmb{M}^T = \\pmb{M}^{-1} \\text{ or equivalently}\\quad \\pmb{M}^T\\pmb{M} = \\pmb 1$$\n",
"\n",
"which means that the matrix transpose is its inverse and when the matrix and its transpose are multiplied together a unit diagonal matrix results. The orthogonal matrix has a determinant that is $\\pm 1$; the eigenvalues are all $+1$. However, a determinant of $\\pm 1$ does not mean that a matrix is orthogonal. The product of two orthogonal matrices is also an orthogonal matrix.\n",
"\n",
"The rotation matrix, equation 3, is orthogonal, as is the matrix $\\displaystyle \\frac{1}{\\sqrt{2}}\\begin{bmatrix} 1 & 1 \\\\ 1 & -1 \\end{bmatrix}$ which shows that a real orthogonal matrix is the same as a unitary matrix.\n",
"\n",
"In an orthogonal matrix, rows and columns form an _orthonormal basis_ and each row has a length of one. When an orthogonal matrix operates on (linearly transforms) a vector or a matrix representing an object, such as a molecule, the molecule is unchanged, meaning that relative bond angles and lengths remain the same but the coordinate axes moved so the object appears to have been rotated when viewed on the computer screen. Orthogonal matrices are used to represent reflections, rotations, and inversions in molecular group theory. Matrix decomposition methods make use of orthogonal matrices. One matrix method called _singular value decomposition_ is used in data analysis to separate out overlapping spectra into their constituent parts.\n",
"\n",
"## 4.13 Determinants\n",
"\n",
"Evaluating determinants was described in Section 2. The determinants of different types of square matrices have the following properties that do not involve evaluating the determinant.\n",
"\n",
"$$\\displaystyle \\qquad\\qquad\\begin{align} \\big|\\,\\pmb{ABC}\\,\\big|&=\\big|\\,\\pmb{A}\\,\\big|\\big|\\,\\pmb{B}\\,\\big|\\big|\\,\\pmb{C}\\,\\big|\\\\\n",
"\\\\ \\big|\\pmb{A}\\big|^T &= \\big|\\pmb{A}\\big| \\\\\n",
"\\big|\\pmb{A} \\big|^{-1} &= \\frac{1}{ \\big|\\pmb{A}\\big| }\\\\\n",
"\\big|a\\pmb{A}\\big| &= a^n\\big|\\pmb{A}\\big|\\end{align}\\qquad\\qquad\\qquad\\qquad\\qquad\\qquad\\text{(4)}$$ \n",
"\n",
"where $n$ is the length of the determinant's row or column."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5 Matrix multiplication\n",
"\n",
"The _shape_ of the matrices and their multiplication _order_ must both be respected. If multiplication is possible, and it not always is, then, in general, multiplication $\\pmb{AB}$ does not give the same result as $\\pmb{BA}$. The matrices\n",
"\n",
"$$\\displaystyle \\pmb{A}=\\begin{bmatrix} 2 & 1 & 5 \\\\ -5 & 6 & 2 \\end{bmatrix} \\; \\text{ and } \\pmb{B}=\\begin{bmatrix} 12 & 11 & 6 \\\\ -3 & 3 & 9 \\end{bmatrix} $$\n",
"\n",
"cannot be multiplied together, because multiplication is only possible if the number of columns of the left-hand matrix is equal to the number of rows of the right-hand one. The matrices are then _commensurate_ or _conformable_. The result is a matrix whose size is determined by the _number of rows_ of the left hand matrix and the _number of columns_ of the right hand matrix, e.g.\n",
"\n",
"$$\\displaystyle \\pmb{A} \\qquad \\pmb{B}\\qquad \\pmb{C}\\\\\n",
"(n\\times m)(m\\times r) \\to (n\\times r)\\\\ \n",
"\\qquad \\text{same number of columns in A as rows in B}$$\n",
"\n",
"The calculation below shows the multiplication $\\pmb{C} = \\pmb{AB}$; the arrows show how the top element in $\\pmb{C}$ is calculated as the product of element 1 of row 1 with element 1 of column 1. To this value is added the product of element 2 of row 1 and element 2 of column 1 and so on for all the elements in a row. This is why the number of rows and columns must be the same. You can also envisage the multiplication as the dot product of the each row with each column in turn.\n",
"\n",
"![Drawing](matrix-pics2.png) \n",
"\n",
"Multiplying two $2 \\times 2$ matrices produces\n",
"\n",
"$$\\displaystyle \\begin{align} \\begin{bmatrix} a_{11} & a_{12} \\\\ a_{21} & a_{22} \\end{bmatrix} \\times \\begin{bmatrix} b_{11} & b_{12} \\\\ b_{21} & b_{22} \\end{bmatrix} \n",
"&=\\begin{bmatrix}row(1)\\cdot col(1) & row(1)\\cdot col(2)\\\\ row(2)\\cdot col(1)& row(2)\\cdot col(2)\\end{bmatrix}\\\\\\\\\n",
"&=\\begin{bmatrix} a_{11}b_{11}+a_{12}b_{21} & a_{11}b_{12}+a_{12}b_{22} \\\\ a_{21}b_{11}+a_{22}b_{21} & a_{21}b_{12}+a_{22}b_{22} \\end{bmatrix}\\end{align}$$\n",
"\n",
"and it is useful to look at the pattern of elements where you can see the $\\pmb{A}$ matrix in the pattern of $a$'s and columns in $\\pmb{B}$ appear as rows in the sums. If the matrices are not square, multiplying a $n \\times m$ matrix by a $m \\times r$ one results in a $n \\times r$ matrix as shown above. The equation with which to calculate the (row - column) $ij^{th}$ element of any matrix multiplication is\n",
"\n",
"$$\\displaystyle C_{ij}=\\sum_{k=1}^m a_{ik}b_{kj} \\qquad\\tag{5}$$\n",
"\n",
"where the sum index $k$ runs from $1 \\to m$, the number of columns in matrix $\\pmb{A}$ or rows in $\\pmb{B}$ and $i$ takes values $1 \\to n$ and j from $1 \\to r$. Multiplying a $2 \\times 3$ matrix with a $3 \\times 2$ one, the number of columns in the left matrix is the same as the number of rows in the right-hand one and therefore the result is a $2 \\times 2$ square matrix,\n",
"\n",
"$$\\displaystyle\\begin{align} & \n",
"\\begin{bmatrix} a_{11} & a_{12} &a_{13} \\\\ a_{21} & a_{22} &a_{23} \\end{bmatrix} \\times \\begin{bmatrix} b_{11} & b_{12} \\\\ b_{21} & b_{22}\\\\ b_{31} & b_{32} \\end{bmatrix} =\\begin{bmatrix} P & Q \\\\ R & S \\end{bmatrix}\\qquad\\qquad\\text{(6)} \\\\\n",
"&\\quad\\quad \\uparrow \\qquad \\qquad\\qquad\\quad \\uparrow \\qquad\\qquad \\uparrow \\\\ \n",
"&\\text{ 3 cols 2 rows } \\qquad \\text{2 cols 3 rows }\\qquad \\text{2 by 2 matrix} \\end{align} $$\n",
"\n",
"where element $R=a_{21}b_{11} + a_{22}b_{21} + a_{23}b_{31}$ and element $S= a_{21}b_{12} + a_{22}b_{22} + a_{23}b_{32}$. A $2 \\times 3$ and a $3 \\times 1$ matrix produce a $2 \\times 1$ column matrix\n",
"\n",
"$$\\displaystyle \\qquad\\qquad\\begin{bmatrix} a_{11} & a_{12} &a_{13} \\\\ a_{21} & a_{22} &a_{23} \\end{bmatrix} \\times \\begin{bmatrix} b_{1} \\\\ b_{2}\\\\b_3 \\end{bmatrix} =\\begin{bmatrix} P \\\\ R \\end{bmatrix} \\qquad\\qquad \\qquad\\qquad\\text{(7)}$$\n",
"\n",
"where $P = a_{11}b_1 + a_{12}b_2 + a_{13}b_3$ and $R = a_{21}b_1 + a_{22}b_2 + a_{23}b_3$.\n",
"\n",
"Suppose there are three matrices $\\pmb{ABC}$, then the safest rule to follow is to left-multiply $\\pmb{C}$ by\n",
"$\\pmb{B}$ first, then to left-multiply the result by $\\pmb{A}$. The same rule is applied to several matrices; start at the right and work to the left. However, by the associative rule, Section 4.1, as long as the order $\\pmb{ABCD}$ is maintained, this product can be multiplied in any order.\n",
"\n",
"The diagrams in Fig. 7 show, diagrammatically, the result of multiplying differently shaped matrices. Only these multiplications are defines. The 'bra-ket' notation is shown also. \n",
"\n",
"![Drawing](matrices-fig7a.png) \n",
"_____\n",
"![Drawing](matrices-fig7b.png)\n",
"_____\n",
"![Drawing](matrices-fig7c.png) \n",
"\n",
"![Drawing](matrices-fig7d.png)\n",
"Figure 7. Pictorial representation of allowed matrix multiplication. Only these types of multiplications are possible. The lowest diagram shows the general case. The bra-ket notation used in quantum mechanics is shown also. The dot product is also called the *inner product*.\n",
"_______\n",
"\n",
"## 5.1 Matrix sum\n",
"\n",
"To sum the terms in a square matrix left-multiply by a unit row vector and right-multiply\n",
"by a unit column vector. For example\n",
"\n",
"$$\\displaystyle \\begin{bmatrix} 1 & 1 \\end{bmatrix} \\begin{bmatrix}a & b \\\\ c & b \\end{bmatrix}\\begin{bmatrix} 1 \\\\ 1 \\end{bmatrix} =a+b+c+d$$\n",
"\n",
"## 5.2 bra-ket notation\n",
"\n",
"Often the bra-ket notation is used in quantum mechanics. The _ket_ $|k\\rangle$ is a single column matrix (a column vector) $\\displaystyle | k\\rangle =\\begin{bmatrix} a \\\\ b \\\\ \\vdots \\end{bmatrix}$ and the _bra_ is the single row matrix (or vector) and is always the complex conjugate of the ket. $\\displaystyle \\langle j|=\\begin{bmatrix} a^* & b^* & \\cdots \\end{bmatrix}$. The $\\langle j |k\\rangle$ is a scalar number, and $|k\\rangle \\langle j|$ a square matrix.\n",
"\n",
"These objects are discussed in more detail in chapters 6 and are mentioned here as they provide a shorthand way of visualizing matrix multiplication; see Figure 7. There is no specific symbol for a scalar or a square matrix; they have to be represented by the bra-ket pair.\n",
"\n",
"## 5.3 Commutators\n",
"If matrices are not square then there is only one way to multiply them: the left-hand matrix's number of columns must equal the right-hand matrix's number of rows. When both matrices are square, to be multiplied they must be of the same size and we can then choose which is to be the left-hand and which the right-hand side of the multiplication, and now the _order_ of multiplication does matter as illustrated on the previous page. Sometimes the result is the same, i.e. $\\pmb{AB} = \\pmb{BA}$ then the matrices are said to *commute*, but generally they do not and, therefore, $\\pmb{AB} \\ne \\pmb{BA}$.\n",
"\n",
"The commutator of two square matrices $\\pmb{A}$ and $\\pmb{B}$ is also a matrix and is defined as\n",
"\n",
"$$\\displaystyle \\begin{bmatrix} \\pmb{A},\\pmb{B} \\end {bmatrix} = \\pmb{AB}-\\pmb{BA} \\qquad\\tag{8}$$\n",
"\n",
"If the result is the null matrix $\\pmb{0}$, which is full of zeros, $\\pmb{A}$ and $\\pmb{B}$ are said to *commute*: \n",
"\n",
"$$\\displaystyle [ \\pmb{A},\\pmb{B} ] = \\pmb{AB}-\\pmb{BA}=\\pmb{0}$$ \n",
"\n",
"Sometimes expressions such as $[\\pmb{C},[\\pmb{A,B}]]$ may be met. This means: work out the inside commutation first, then the commutation with each term that this produces: \n",
"\n",
"$$\\displaystyle [\\pmb{C},[\\pmb{A},\\pmb{B}]] = [\\pmb{C}, \\pmb{AB} - \\pmb{BA}] = [\\pmb{C}, \\pmb{AB}] - [\\pmb{C}, -\\pmb{BA}]$$\n",
"\n",
"Commutation is very important in quantum mechanics; only observables that commute can be observed simultaneously. Position and momentum, which do not commute, or those components of angular momentum, which also do not commute, have values that are restricted by an amount determined by the Heisenberg Uncertainty Principle when being observed simultaneously.\n",
"\n",
"Commutation is common of operators in general. The commutator relationship applies to any two operators $\\pmb{P}$ and $\\pmb{Q}$, which need not be matrices, but could be the differential operator such as $d/dx$ or $x$ itself. This parallel means that we can consider matrices as operators. The commutator will in general operate on a function, for example,\n",
"\n",
"$$\\displaystyle [\\pmb{P},\\pmb{Q}]f= \\pmb{PQ}f - \\pmb{QP}f$$\n",
"\n",
"and $f$ can be any normal function, $\\ln(x),\\, \\sin(x)$, and so on. In molecular group theory, the five types of operators that are the identity, rotation, reflection, inversion, and improper rotation (combined rotation and reflection) sometimes commute with one another although this depends on the point group. Section 6 describes these operations.\n",
"\n",
"\n",
"## 5.4 Integral powers of square matrices\n",
"\n",
"Only integral powers of matrices are defined and are calculated by repeated multiplication, for example,\n",
"\n",
"$$\\displaystyle \\pmb{M}^0=\\pmb{1}, \\quad \\pmb{M}^1=\\pmb{M}, \\quad \\pmb{M}^2=\\pmb{MM}, \\quad \\pmb{M}^3=\\pmb{MMM} $$\n",
"\n",
"and so on, for example \n",
"\n",
"$$\\displaystyle \\begin{bmatrix} 1 & 2 \\\\ 3 & 4 \\end{bmatrix}^2=\\begin{bmatrix} 1 & 2 \\\\ 3 & 4 \\end{bmatrix}\\begin{bmatrix} 1 & 2 \\\\ 3 & 4 \\end{bmatrix}=\\begin{bmatrix} 7 & 10 \\\\ 15 & 22 \\end{bmatrix}$$\n",
"\n",
"The *similarity transform*, see Section 13.4, can be used to obtain high integer powers of matrices, however, if the matrix is diagonal then taking the power is easy because the diagonal elements are raised to the power. The power need not be positive; therefore, the inverse of a diagonal matrix is easily obtained.\n",
"\n",
"$$\\displaystyle \\begin{bmatrix} a & 0 & 0 & \\cdots \\\\ 0 &b & 0 & \\cdots \\\\ 0& \\vdots &\\ddots\\end{bmatrix}^{\\,n}= \\begin{bmatrix} a^n & 0 & 0 & \\cdots \\\\ 0 &b^n & 0 & \\cdots \\\\0& \\vdots & \\ddots\\end{bmatrix}$$\n",
"\n",
"## 5.5 Functions of matrices\n",
"\n",
"In the study of the theory of nuclear magnetic resonance, NMR, a quantum mechanical description of nuclear spin is essential. To explore the rotation of the magnetization as used in inversion recovery, spin-echo, or a complicated two-dimensional experiment, such as COSY, requires the exponentiation of matrices (Levitt 2001). Exponentiation can only be performed by expanding the exponential and evaluating the terms in the series one by one as\n",
"\n",
"$$\\displaystyle e^{\\pmb{M}}=\\pmb{1} + \\pmb{M}+ \\frac{\\pmb{M}^2}{2!}+\\cdots; \\quad e^{-\\pmb{M}}=\\pmb{1} - \\pmb{M}+ \\frac{\\pmb{M}^2}{2!}-\\cdots$$\n",
"\n",
"and is $x$ is a variable $\\displaystyle e^{-x\\pmb{M}}=\\pmb{1} -x\\pmb{M}+ x^2\\frac{\\pmb{M}^2}{2!}-\\cdots$\n",
"\n",
"The matrix $\\pmb{1}$ is a unit diagonal matrix. Similar expressions are formed with trig and log functions according to their expansion formula and with Taylor or Maclaurin series for other functions. The familiar relationship $e^Ae^B = e^{A+B}$ is only true if the matrices $A$ and $B$ commute. In Section 13.2 and 13.3 a transformation is describe which enables the exponential of a matrix to be found.\n",
"\n",
"\n",
"## 5.6 Block diagonal matrices\n",
"\n",
"In many instances, a matrix can be blocked into smaller ones symmetrically disposed along the diagonal. The result of this is that the problem reduces to the lesser one of solving several matrices where each is much smaller than the whole and is therefore more easily solved. Why should we bother with this if the computer can diagonalize any matrix we give it to do? The reason is that eigenvalues can more easily be identified within the basis set by doing the calculation this way. Recall that the basis set you choose to use, for example in a quantum problem, determines the ordering of elements in a matrix. Why does this matter? It matters because when the spectrum from a molecule is observed, which measures only the difference in energy levels, we would like to know what quantum numbers give rise to what spectral lines. If the matrix is a block diagonal one, then this is made somewhat easier because we know what parts of the basis set elements are involved because each block when diagonalized contains only that part of the basis set that was in it in the first place. If the whole matrix is diagonalized blind, as it were, and without thinking about the problem beforehand, this information can be lost because all the elements and hence eigenvalues can be mixed up. The elements in the basis set can be ordered in any way you want, and different basis sets can be chosen for the same problem. By trying different ordering, it is sometimes possible to discover a block diagonal form for a matrix and so aid its solution. In the study of group theory, blocking matrices proves to be a powerful way of determining the irreducible representation; see Section 6. \n",
"\n",
"The following matrix has a $2 \\times 2$, a $3 \\times 3$, and a $1 \\times 1$ block.\n",
"\n",
"![Drawing](matrices-blockmatrix.png) \n",
"_________\n",
"\n",
"## 5.7 The special case of the 2 x 2 matrix\n",
"\n",
" $$\\displaystyle \\pmb{M}= \\begin{bmatrix} A & B \\\\C & D \\end{bmatrix}$$ \n",
"\n",
"### **(i) Determinant**\n",
"\n",
"The determinant is $\\displaystyle |\\pmb{M}|=AD-BC$\n",
"\n",
"### **(ii) Trace**\n",
"\n",
"The trace is $ Tr(\\pmb{M})=A+D$\n",
"\n",
"### **(iii) Inverse**\n",
"\n",
"The inverse is inverse $\\displaystyle \\pmb{M}^{-1}= \\frac{1}{|\\pmb{M}|}\\begin{bmatrix} D & -B \\\\-C & A \\end{bmatrix}$ \n",
"\n",
"### **(iv) Eigenvalues**\n",
"\n",
"The eigenvalues (see section 12.3) are solutions of the characteristic equation \n",
"\n",
"$$\\displaystyle \\lambda^2-\\lambda(A+D)-(AD-BC)=0$$\n",
"\n",
"or equivalently \n",
"\n",
"$$\\displaystyle \\lambda^2-\\lambda Tr(\\pmb{M})+|\\pmb{M}|=0$$\n",
"\n",
"which are\n",
"\n",
"$$\\displaystyle \\lambda_{1,2}= \\frac{A+D\\pm \\sqrt{(A+D)^2-4(AD-BC)}}{2}$$\n",
"\n",
"As a check $\\lambda_1+\\lambda_2=Tr(\\pmb{M})$ and $\\lambda_1\\lambda_2=|\\pmb{M}|$.\n",
"\n",
"The eigenvectors are $\\displaystyle v_1=k \\begin{bmatrix} B\\\\\\lambda_1-A \\end{bmatrix}$ and $\\displaystyle v_2=k\\begin{bmatrix} B\\\\\\lambda_2-A \\end{bmatrix}$ where $k$ is an arbitrary constant, for example to normalise the eigenvectors.\n",
"\n",
"## 5.8 Using matrices in Python and Sympy.\n",
"\n",
"The is a distinction between doing numerical and symbolic calculations. Python/numpy is used for numerical work and Sympy for algebraic/symbolic calculations. The notation is slightly different depending on whether you use Sympy or numpy.\n",
"\n",
"### **(i) Symbolic calculations using Sympy**"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\left[\\begin{matrix}a & b\\\\c & d\\end{matrix}\\right]$"
],
"text/plain": [
"⎡a b⎤\n",
"⎢ ⎥\n",
"⎣c d⎦"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"M, N, a, b, c, d = symbols('M, N, a, b, c, d') # define symbols to use \n",
"M = Matrix( [[a, b], [c, d]] ) # note double sets of brackets and capital M\n",
"M"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAEkAAAASCAYAAAAXOvPoAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy89olMNAAAACXBIWXMAABJ0AAASdAHeZh94AAADoElEQVR4nO3YW4hWVRQH8N+MV8piMrTGHCoDezCtlIqoB1MqQnzIh0gpLQzCriaRQZfVqgzqIaTShyArI6iHLnQTJMuKHgpNMyPoAtrFtLAeKorUpod9Pj1+zadWMw6Ff/hY39577bX3Xmet/17ntHV3dzuEfaO9vzfwX8DAvjSemV34Ei9ExIy+XKtp3XlYirkRsezf2uvrSJpUyQ/6eJ1W667tDWN97aSJleyVzf4NTMJv+Lg3jP3vIikzh2AcNkTEzt6w+RdOysxZmIYz0Imd+BxLI+LxHvQH4lrMxVhsU/jgASWStkTEtt7Y7AFiPAZhbWaehjswGUPxLhZExMaeJmbmZMzDORiBH7ChvUnpCCzHGLyDR/A8TsSyzFzYpD8Yr2ExduFhrMJdeBTH6p9Ug+MVp3TjMSWaz8cbmdlRn5CZbZm5GG9iCl7Hg1V7UnMkdWN0RGxtMnI7PsWVuL82tKRa+E7cGxHdlf4TeKvSOdik3eDBs3BuRKxrDGTmclyOa3Bfbc4i3IjnMCcifqnNGbZXJEXEz80Oqvq/xRYMr00+E1fhlYi4p+GgSv9tfFI1+yuSbqk7qMKSSo5vdGTmRCzEGsyqO4jik70iKTOPwnUKJ52MI+1N7vVFr6/kohab3V7JlpGUmZuUtDhQPB0Rl+3D3iDFAZsV2mhGIwCG1vpuUs54a0T83pPd3U7KzAlYiWPwPp5RDrpT4aTZ+LA294Jq/L0Wex6D7yLim1aHwhfKVX2g2LKf8VMwGC+3uNkaD2Rzre9C/KjwT4+oR9JT6MB5EbG6rpSZd1d/11TtoRiJdfU0q+lPxCisaHkcRMTUfY3/AzRSbVOL8YsruZLd5xiB9RHxRyuj7ZVyFyZgdQ8O6lAImz38sqv6jWxh97ZK9lelPbx5IDM7cTU+UzkJbZVsdQ7s4ZtGyI+p8rph+Gg8i9FK2q2HiNhRLXZcZk5v2sxCNN7TDjZpN262mZl5eG1PwxSOGoIbGqkYEb9iI0Zl5iXNxjJzbGYOaGt8KsnMVUqNsE6pEzpxkZKrM7AxIk6tGZiNJ7FD4a+tStE2Ht+jCydERD3/+wxVUfuTcqsehgF4UXHMDCX950fEQ03zpuElJapWKK8yHThdKYc66zfXpQovdSlV50lYoNQT7So+aiAilmM+vsZMzMFXSrXaje0Hy0EVxim31hpMxUdKel2hHHxKs4MgIl5VHu4KnK3cdtMVMr8Z2g59dNs//gTXLjEQkA2gwQAAAABJRU5ErkJggg==\n",
"text/latex": [
"$\\displaystyle a d - b c$"
],
"text/plain": [
"a⋅d - b⋅c"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"M.det() # determinant"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\left[\\begin{matrix}a c + a d & a d + b d\\\\a c + b c & b c + b d\\end{matrix}\\right]$"
],
"text/plain": [
"⎡a⋅c + a⋅d a⋅d + b⋅d⎤\n",
"⎢ ⎥\n",
"⎣a⋅c + b⋅c b⋅c + b⋅d⎦"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"N = Matrix([[d,a],[c,b] ])\n",
"\n",
"N*M # matrix multiply"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\left[\\begin{matrix}a d + b c & a^{2} + b^{2}\\\\2 c d & a c + b d\\end{matrix}\\right]$"
],
"text/plain": [
"⎡ 2 2 ⎤\n",
"⎢a⋅d + b⋅c a + b ⎥\n",
"⎢ ⎥\n",
"⎣ 2⋅c⋅d a⋅c + b⋅d⎦"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"M*N # matrix multiply "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\left[\\begin{matrix}a c - b c & - a^{2} + a d - b^{2} + b d\\\\a c + b c - 2 c d & - a c + b c\\end{matrix}\\right]$"
],
"text/plain": [
"⎡ 2 2 ⎤\n",
"⎢ a⋅c - b⋅c - a + a⋅d - b + b⋅d⎥\n",
"⎢ ⎥\n",
"⎣a⋅c + b⋅c - 2⋅c⋅d -a⋅c + b⋅c ⎦"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"N*M - M*N # M and N do not commute"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\left[\\begin{matrix}2\\\\3\\end{matrix}\\right]$"
],
"text/plain": [
"⎡2⎤\n",
"⎢ ⎥\n",
"⎣3⎦"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"V = Matrix([2,3]) # define vector column\n",
"V"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAABoAAAAPCAYAAAD6Ud/mAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy89olMNAAAACXBIWXMAABJ0AAASdAHeZh94AAABJ0lEQVR4nL3UPUtcQRSH8d+u+w0srey1thZLQYhiqX4BIwsWQiAcThFIlwTtFLQWLCNaplQQBEUklWUQEWx9yVrsdVl82bvewmlmBp7/PDNzmKm1Wi0f0RpPg8wcxCdMYhRDuMUJNrEZEf+r8vUu6SzWMYYD/MQORrCB7cysVeUbXcG/mMLvZzv/gkPMYLpY7N18rZ8aFeFvWIuIz1X4eu9Ip90V/X1VvlSUmQ3MF9O9qnw/J/quXeDdiNivyvcUZeYSlnGOuTJDL/5NUWYu4hfOMB4R1yWSnvyrosxsYhWnRehfiaSUfyHKzBX8wHERuiyR9MXXn4W+ahfzCBMRcVUi6ZvvPNjMXMAWHrSv4eYV/iIitqrw3V/QcNEPoPnGxv4Ui7+bfwTEc5vN3/BWuwAAAABJRU5ErkJggg==\n",
"text/latex": [
"$\\displaystyle 22$"
],
"text/plain": [
"22"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"W = Matrix ([5,4])\n",
"V.dot(W) # dot product is a scalar number"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\left[\\begin{matrix}22\\end{matrix}\\right]$"
],
"text/plain": [
"[22]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Transpose(V)*W # same as dot product "
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\left[\\begin{matrix}10 & 8\\\\15 & 12\\end{matrix}\\right]$"
],
"text/plain": [
"⎡10 8 ⎤\n",
"⎢ ⎥\n",
"⎣15 12⎦"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"V*transpose(W) # outer product is a matrix see figure 7"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### **(ii) Using numpy for numerical calculation. Note that the notation is different to that of Sympy** "
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a and b\n"
]
},
{
"data": {
"text/plain": [
"(array([[1, 3],\n",
" [5, 1]]),\n",
" array([[4, 1],\n",
" [2, 2]]))"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = np.array([[1, 3], [5, 1]]) # note that an array is now a treated as amatrix\n",
"b = np.array([[4, 1], [2, 2]])\n",
"print('a and b')\n",
"a,b"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[10, 7],\n",
" [22, 7]])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.matmul(a, b) # matrix multiply"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[10, 7],\n",
" [22, 7]])"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a @ b # equivalent to np.matmul(a,b) "
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 4, 3],\n",
" [10, 2]])"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a * b # this is NOT matrix multiply but element by element multiply "
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"v = np.array([2,3])"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"13"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v.dot(v) # dot product with itself is scalar"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"13"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.dot(v, v) # alternative way of doing dot product"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([17, 9])"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v @ a # will automatically make transpose"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([11, 13])"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a @ v # will automatically make transpose"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, -6],\n",
" [10, -1]])"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a @ b - b @ a # commute ? No!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}