{ "cells": [ { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "# Lecture 21: Matrices and Linear Algebra\n", "### Phys 503, J.R. Gladden, University of Mississippi" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "### Matrices in science\n", "Matrices are convenient mathematical constructs to represent various types of physical quantities. A *vector* is an example of a 1D matrix and is used to represent a quantity that requires both a magnitude and direction. Other quantities might require a magnitude, direction, and orientation - such as moment of intertia or elastic stiffness. *Tensors* are used here and can be represented in a 2D matrix. See the lecture slides for many more details.\n", "\n", "Matrices are also integral to representing and solving systems of linear equations - a field known in mathematics as linear algebra.\n" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "### Matrix objects in Numpy\n", "Numpy defines a \"matrix\" object that is similar to a 2D array, but adds certain capabilities for manipulating and combining matrices.\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [ "array([[ 1., 0., 0.],\n", " [ 0., 1., 0.],\n", " [ 0., 0., 1.]])" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "from numpy import linalg\n", "\n", "#2D numpy arrays can represent matrices\n", "\n", "Aa=np.array([[1,3,5],[2,5,1],[2,3,8]])\n", "\n", "#Numpy also has a 'matrix' object that is very similar to 2D arrays,\n", "#but has a more userfriendly interface\n", "\n", "A=np.matrix('[1 3 5; 2 5 1; 2 3 8]')\n", "\n", "#Returns the diagonal elements of the matrix\n", "\n", "A.diagonal()\n", "\n", "#Returns the trace (sum of diag. elements)\n", "A.trace()\n", "\n", "#Takes inverse of the matrix\n", "A.I\n", "\n", "# Matrix multiply using simple * operator\n", "# Should return identity matrix (matrix equivalent of 1)\n", "A.I*A\n", "\n", "#NxN Identity matrix given by eye(N), same syntax as Matlab\n", "np.eye(3)\n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [ "matrix([[1, 3, 5],\n", " [2, 5, 1],\n", " [2, 3, 8]])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "### Solving systems of equations example\n", "$$ x + 3y + 5z = 10 \\\\ 2x + 5y + z = 8 \\\\ 2x + 3y + 8z = 3$$" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[-9.28]\n", " [ 5.16]\n", " [ 0.76]]\n" ] } ], "source": [ "# Solutions to systems of equations\n", "# System\n", "#x + 3y + 5z = 10\n", "#2x + 5y + z = 8\n", "#2x + 3y + 8z = 3\n", "#\n", "#Form is A*X = b, \n", "\n", "b=np.matrix('[10; 8; 3]')\n", "A=np.matrix('[1 3 5; 2 5 1; 2 3 8]')\n", "\n", "# Solution can be found by 1/A * b\n", "A.I * b\n", "\n", "#Better to use the solve function in linalg\n", "X=linalg.solve(A,b)\n", "print(X)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [ "matrix([[-9.28],\n", " [ 5.16],\n", " [ 0.76]])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.I*b" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "### Eigenvalue Problems\n", "Eigenvalue (or \"proper\" value) problems are ones that can be cast in the form $$ \\matrix{A} \\matrix{X} = \\lambda \\matrix{X} $$\n", "Where $X$ is the eigenvector and $\\lambda$ are the eigenvalues.\n", "\n", "numpy.linalg provides tools for doing this." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('Eigenvalues: ', array([ 10.5540456 , -0.5873064 , 4.03326081]))\n", "Eigenvectors: \n", "[[-0.51686204 -0.94195144 0.11527992]\n", " [-0.32845853 0.31778071 -0.81936883]\n", " [-0.79054957 0.10836468 0.56155611]]\n" ] } ], "source": [ "#Eigenvalues\n", "lambs,vs = linalg.eig(A)\n", "print(\"Eigenvalues: \",lambs)\n", "print(\"Eigenvectors: \")\n", "print(vs)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [ "linalg.eig??\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.13" } }, "nbformat": 4, "nbformat_minor": 0 }