{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

## Matrix Operators

\n", "
\n", "

\n", "This notebook goes with section 2.6 of the notes." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

## Setup Cell

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%autosave 0\n", "%matplotlib inline\n", "from sglib import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

## Visualize a Real, 2D Operation on a Vector

\n", "

\n", "You just need to change the input_vector and the matrix.\n", "The rest should work automatically. You can either supply\n", "a matrix of your own, or you can \"uncomment\" one of the\n", "ones already there (by removing the \"#\"). Make sure every\n", "matrix except the one you want is commented out.\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "input_vector = col(5,2) # Put your vector here.\n", "#matrix = Matrix([(2,0),(0,2)]) # Put your matrix here.\n", "\n", "#\n", "# Or, uncomment one of the matrices below\n", "#\n", "\n", "#matrix = Matrix([(-1,0),(0,-1)]) # Minus Operator\n", "#matrix = Matrix([(1,0),(0,-1)]) # X Reflect Operator\n", "#matrix = Matrix([(-1,0),(0,1)]) # Y Reflect Operator\n", "matrix = Matrix([(0,1),(1,0)]) # Not Operator\n", "\n", "# For the rotation operation, note that theta is in\n", "# radians, not degrees. So, for example, pi/2 would\n", "# give you 90 degrees, pi/4 would give you 45 degrees.\n", "from sympy import pi, sin, cos\n", "theta=pi/6\n", "a = cos(theta); b=-sin(theta); c=sin(theta); d=cos(theta)\n", "#matrix = Matrix([(a,b),(c,d)]) # Rotation Operator\n", "\n", "#\n", "# You probably won't want to change anything below here.\n", "# (Unless you want to change the colors below)\n", "#\n", "\n", "# In the next line, the matrix \"operates\" on the vector\n", "output_vector = matrix * input_vector\n", "\n", "# This line contains special code to print the math nicely\n", "Print(r'\$%s%s=%s\$'\n", " %( myltx(matrix), myltx(input_vector), myltx(output_vector) ))\n", "\n", "# The variable \"D\" results from a calculation that tells how\n", "# \"big\" we need to draw the plane and the vectors that go on it.\n", "D = max(1,abs(input_vector), abs(input_vector), \n", " abs(output_vector), abs(output_vector))\n", "D = float(D)*1.3\n", "\n", "# Draw a 2D plane and then draw the input and out vectors.\n", "# Here you can change the colors if you want to (but be sure\n", "# to change the colors in the \"title\" also).\n", "draw_plane(domain=[-D, D, -D, D])\n", "pylab.title('input vector blue\\n ouput vector green', fontsize=16)\n", "draw_vector(input_vector, color='blue', scale=D)\n", "draw_vector(output_vector, color='green', scale=D)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.5" } }, "nbformat": 4, "nbformat_minor": 1 }