{ "metadata": { "name": "", "signature": "sha256:d9d23a333ea98b5147b9b834deb489d4fb34fe7c497a5fe205c341cf24321b7b" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "from sympy import *\n", "init_printing(use_latex='mathjax')\n", "x, y, z = symbols('x,y,z')\n", "r, theta = symbols('r,theta', positive=True)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Matrices\n", "\n", "The SymPy `Matrix` object helps us with small problems in linear algebra." ] }, { "cell_type": "code", "collapsed": false, "input": [ "rot = Matrix([[r*cos(theta), -r*sin(theta)],\n", " [r*sin(theta), r*cos(theta)]])\n", "rot" ], "language": "python", "metadata": {}, "outputs": [ { "latex": [ "$$\\left[\\begin{matrix}r \\cos{\\left (\\theta \\right )} & - r \\sin{\\left (\\theta \\right )}\\\\r \\sin{\\left (\\theta \\right )} & r \\cos{\\left (\\theta \\right )}\\end{matrix}\\right]$$" ], "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "\u23a1r\u22c5cos(\u03b8) -r\u22c5sin(\u03b8)\u23a4\n", "\u23a2 \u23a5\n", "\u23a3r\u22c5sin(\u03b8) r\u22c5cos(\u03b8) \u23a6" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Standard methods" ] }, { "cell_type": "code", "collapsed": false, "input": [ "rot.det()" ], "language": "python", "metadata": {}, "outputs": [ { "latex": [ "$$r^{2} \\sin^{2}{\\left (\\theta \\right )} + r^{2} \\cos^{2}{\\left (\\theta \\right )}$$" ], "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ " 2 2 2 2 \n", "r \u22c5sin (\u03b8) + r \u22c5cos (\u03b8)" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "rot.inv()" ], "language": "python", "metadata": {}, "outputs": [ { "latex": [ "$$\\left[\\begin{matrix}- \\frac{\\sin^{2}{\\left (\\theta \\right )}}{r \\cos{\\left (\\theta \\right )}} + \\frac{1}{r \\cos{\\left (\\theta \\right )}} & \\frac{1}{r} \\sin{\\left (\\theta \\right )}\\\\- \\frac{1}{r} \\sin{\\left (\\theta \\right )} & \\frac{1}{r} \\cos{\\left (\\theta \\right )}\\end{matrix}\\right]$$" ], "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "\u23a1 2 \u23a4\n", "\u23a2 sin (\u03b8) 1 sin(\u03b8)\u23a5\n", "\u23a2- \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 + \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 \u2500\u2500\u2500\u2500\u2500\u2500\u23a5\n", "\u23a2 r\u22c5cos(\u03b8) r\u22c5cos(\u03b8) r \u23a5\n", "\u23a2 \u23a5\n", "\u23a2 -sin(\u03b8) cos(\u03b8)\u23a5\n", "\u23a2 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 \u2500\u2500\u2500\u2500\u2500\u2500\u23a5\n", "\u23a3 r r \u23a6" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "rot.singular_values()" ], "language": "python", "metadata": {}, "outputs": [ { "latex": [ "$$\\begin{bmatrix}r, & r\\end{bmatrix}$$" ], "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "[r, r]" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise\n", "\n", "Find the inverse of the following Matrix:\n", "\n", "$$ \\left[\\begin{matrix}1 & x\\\\y & 1\\end{matrix}\\right] $$" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Create a matrix and use the `.inv` method to find the inverse\n", "\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Operators\n", "\n", "The standard SymPy operators work on matrices" ] }, { "cell_type": "code", "collapsed": false, "input": [ "rot * 2" ], "language": "python", "metadata": {}, "outputs": [ { "latex": [ "$$\\left[\\begin{matrix}2 r \\cos{\\left (\\theta \\right )} & - 2 r \\sin{\\left (\\theta \\right )}\\\\2 r \\sin{\\left (\\theta \\right )} & 2 r \\cos{\\left (\\theta \\right )}\\end{matrix}\\right]$$" ], "metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": [ "\u23a12\u22c5r\u22c5cos(\u03b8) -2\u22c5r\u22c5sin(\u03b8)\u23a4\n", "\u23a2 \u23a5\n", "\u23a32\u22c5r\u22c5sin(\u03b8) 2\u22c5r\u22c5cos(\u03b8) \u23a6" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "rot * rot" ], "language": "python", "metadata": {}, "outputs": [ { "latex": [ "$$\\left[\\begin{matrix}- r^{2} \\sin^{2}{\\left (\\theta \\right )} + r^{2} \\cos^{2}{\\left (\\theta \\right )} & - 2 r^{2} \\sin{\\left (\\theta \\right )} \\cos{\\left (\\theta \\right )}\\\\2 r^{2} \\sin{\\left (\\theta \\right )} \\cos{\\left (\\theta \\right )} & - r^{2} \\sin^{2}{\\left (\\theta \\right )} + r^{2} \\cos^{2}{\\left (\\theta \\right )}\\end{matrix}\\right]$$" ], "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "\u23a1 2 2 2 2 2 \u23a4\n", "\u23a2- r \u22c5sin (\u03b8) + r \u22c5cos (\u03b8) -2\u22c5r \u22c5sin(\u03b8)\u22c5cos(\u03b8) \u23a5\n", "\u23a2 \u23a5\n", "\u23a2 2 2 2 2 2 \u23a5\n", "\u23a3 2\u22c5r \u22c5sin(\u03b8)\u22c5cos(\u03b8) - r \u22c5sin (\u03b8) + r \u22c5cos (\u03b8)\u23a6" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "v = Matrix([[x], [y]])\n", "v" ], "language": "python", "metadata": {}, "outputs": [ { "latex": [ "$$\\left[\\begin{matrix}x\\\\y\\end{matrix}\\right]$$" ], "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "\u23a1x\u23a4\n", "\u23a2 \u23a5\n", "\u23a3y\u23a6" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "rot * v" ], "language": "python", "metadata": {}, "outputs": [ { "latex": [ "$$\\left[\\begin{matrix}r x \\cos{\\left (\\theta \\right )} - r y \\sin{\\left (\\theta \\right )}\\\\r x \\sin{\\left (\\theta \\right )} + r y \\cos{\\left (\\theta \\right )}\\end{matrix}\\right]$$" ], "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "\u23a1r\u22c5x\u22c5cos(\u03b8) - r\u22c5y\u22c5sin(\u03b8)\u23a4\n", "\u23a2 \u23a5\n", "\u23a3r\u22c5x\u22c5sin(\u03b8) + r\u22c5y\u22c5cos(\u03b8)\u23a6" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise\n", "\n", "In the last exercise you found the inverse of the following matrix" ] }, { "cell_type": "code", "collapsed": false, "input": [ "M = Matrix([[1, x], [y, 1]])\n", "M" ], "language": "python", "metadata": {}, "outputs": [ { "latex": [ "$$\\left[\\begin{matrix}1 & x\\\\y & 1\\end{matrix}\\right]$$" ], "metadata": {}, "output_type": "pyout", "prompt_number": 12, "text": [ "\u23a11 x\u23a4\n", "\u23a2 \u23a5\n", "\u23a3y 1\u23a6" ] } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "M.inv()" ], "language": "python", "metadata": {}, "outputs": [ { "latex": [ "$$\\left[\\begin{matrix}\\frac{x y}{- x y + 1} + 1 & - \\frac{x}{- x y + 1}\\\\- \\frac{y}{- x y + 1} & \\frac{1}{- x y + 1}\\end{matrix}\\right]$$" ], "metadata": {}, "output_type": "pyout", "prompt_number": 13, "text": [ "\u23a1 x\u22c5y -x \u23a4\n", "\u23a2\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 + 1 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u23a5\n", "\u23a2-x\u22c5y + 1 -x\u22c5y + 1\u23a5\n", "\u23a2 \u23a5\n", "\u23a2 -y 1 \u23a5\n", "\u23a2 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u23a5\n", "\u23a3 -x\u22c5y + 1 -x\u22c5y + 1\u23a6" ] } ], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now verify that this is the true inverse by multiplying the matrix times its inverse. Do you get the identity matrix back?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Multiply `M` by its inverse. Do you get back the identity matrix?\n", "\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise\n", "\n", "What are the eigenvectors and eigenvalues of `M`?" ] }, { "cell_type": "code", "collapsed": true, "input": [ "# Find the methods to compute eigenvectors and eigenvalues. Use these methods on `M`\n", "\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### NumPy-like Item access" ] }, { "cell_type": "code", "collapsed": false, "input": [ "rot[0, 0]" ], "language": "python", "metadata": {}, "outputs": [ { "latex": [ "$$r \\cos{\\left (\\theta \\right )}$$" ], "metadata": {}, "output_type": "pyout", "prompt_number": 16, "text": [ "r\u22c5cos(\u03b8)" ] } ], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "rot[:, 0]" ], "language": "python", "metadata": {}, "outputs": [ { "latex": [ "$$\\left[\\begin{matrix}r \\cos{\\left (\\theta \\right )}\\\\r \\sin{\\left (\\theta \\right )}\\end{matrix}\\right]$$" ], "metadata": {}, "output_type": "pyout", "prompt_number": 17, "text": [ "\u23a1r\u22c5cos(\u03b8)\u23a4\n", "\u23a2 \u23a5\n", "\u23a3r\u22c5sin(\u03b8)\u23a6" ] } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "rot[1, :]" ], "language": "python", "metadata": {}, "outputs": [ { "latex": [ "$$\\left[\\begin{matrix}r \\sin{\\left (\\theta \\right )} & r \\cos{\\left (\\theta \\right )}\\end{matrix}\\right]$$" ], "metadata": {}, "output_type": "pyout", "prompt_number": 18, "text": [ "[r\u22c5sin(\u03b8) r\u22c5cos(\u03b8)]" ] } ], "prompt_number": 18 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Mutation\n", "\n", "We can change elements in the matrix." ] }, { "cell_type": "code", "collapsed": false, "input": [ "rot[0, 0] += 1\n", "rot" ], "language": "python", "metadata": {}, "outputs": [ { "latex": [ "$$\\left[\\begin{matrix}r \\cos{\\left (\\theta \\right )} + 1 & - r \\sin{\\left (\\theta \\right )}\\\\r \\sin{\\left (\\theta \\right )} & r \\cos{\\left (\\theta \\right )}\\end{matrix}\\right]$$" ], "metadata": {}, "output_type": "pyout", "prompt_number": 19, "text": [ "\u23a1r\u22c5cos(\u03b8) + 1 -r\u22c5sin(\u03b8)\u23a4\n", "\u23a2 \u23a5\n", "\u23a3 r\u22c5sin(\u03b8) r\u22c5cos(\u03b8) \u23a6" ] } ], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": [ "simplify(rot.det())" ], "language": "python", "metadata": {}, "outputs": [ { "latex": [ "$$r \\left(r + \\cos{\\left (\\theta \\right )}\\right)$$" ], "metadata": {}, "output_type": "pyout", "prompt_number": 20, "text": [ "r\u22c5(r + cos(\u03b8))" ] } ], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [ "rot.singular_values()" ], "language": "python", "metadata": {}, "outputs": [ { "latex": [ "$$\\begin{bmatrix}\\sqrt{r^{2} + r \\cos{\\left (\\theta \\right )} + \\frac{1}{2} \\sqrt{4 r^{2} + 4 r \\cos{\\left (\\theta \\right )} + 1} + \\frac{1}{2}}, & \\sqrt{r^{2} + r \\cos{\\left (\\theta \\right )} - \\frac{1}{2} \\sqrt{4 r^{2} + 4 r \\cos{\\left (\\theta \\right )} + 1} + \\frac{1}{2}}\\end{bmatrix}$$" ], "metadata": {}, "output_type": "pyout", "prompt_number": 21, "text": [ "\u23a1 ________________________________________________ _______________\n", "\u23a2 \u2571 _______________________ \u2571 \n", "\u23a2 \u2571 \u2571 2 \u2571 \n", "\u23a2 \u2571 2 \u2572\u2571 4\u22c5r + 4\u22c5r\u22c5cos(\u03b8) + 1 1 \u2571 2 \n", "\u23a2 \u2571 r + r\u22c5cos(\u03b8) + \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 + \u2500 , \u2571 r + r\u22c5cos(\u03b8) \n", "\u23a3\u2572\u2571 2 2 \u2572\u2571 \n", "\n", "_________________________________\u23a4\n", " _______________________ \u23a5\n", " \u2571 2 \u23a5\n", " \u2572\u2571 4\u22c5r + 4\u22c5r\u22c5cos(\u03b8) + 1 1 \u23a5\n", "- \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 + \u2500 \u23a5\n", " 2 2 \u23a6" ] } ], "prompt_number": 21 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise\n", "\n", "Play around with your matrix `M`, manipulating elements in a NumPy like way. Then try the various methods that we've talked about (or others). See what sort of answers you get." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Play with matrices\n", "\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 22 } ], "metadata": {} } ] }