CoCalc Public FilesFall2020_Lecture_Books / cs410_2020_lec01n01.ipynbOpen with one click!
Authors: Ross Beveridge, V K, Yongxin Liu
Views : 133
License: Apache License 2.0
Description: First CS410 SageMath Notebook for Fall 2020
Compute Environment: Ubuntu 20.04 (Default)

First 410 Notebook Illustrating SageMath on CoCalc

This is a tiny step down the path we will take this Fall using SageMath through Jupyter - and hosted by CoCalc - to illustrate key concepts during the course of CS410. Today, the very basics of formulas and linear algebra.

Ross Beveridge, August 25, 2020

In [33]:
%display latex latex.matrix_delimiters(left='|', right='|') latex.vector_delimiters(left='[', right=']')

To begin, you can designate variables that will be treated as symbolic as opposed to values.

In [34]:
var('a','b','c','d','x','y')
(a,b,c,d,x,y)\left(a, b, c, d, x, y\right)

Next you can create vectors and matrices which are symbolic (not numeric - yet)

In [39]:
mm = matrix([[a,b],[c,d]]) uu = vector([x,y]) mm,uu
(abcd,[x,y])\left(\left|\begin{array}{rr} a & b \\ c & d \end{array}\right|, \left[x,\,y\right]\right)

But notice all we have done so far is to implicitly create a sequence of the two objects, the matrix and the vector, and print them back in the basic Python/Jupyter/SageMath Read-Eval-Pring Loop. What if we want to create better formed equations?

In [40]:
pretty_print("m = ", mm, " and", " u = ", uu)
mx=abcdxxandxxux=[x,y]\verb|m|\phantom{\verb!x!}\verb|=| \left|\begin{array}{rr} a & b \\ c & d \end{array}\right| \phantom{\verb!xx!}\verb|and| \phantom{\verb!xx!}\verb|u|\phantom{\verb!x!}\verb|=| \left[x,\,y\right]

Next complication is that a matrix times a vector becomes somewhat an issue deep in the particulars of any given language. Conceptually, it may at times be simplest to realize that a vector is - when doing matrix multiplication - just a one dimensional matrix. So below we arrive at the simple case of a 2x2 matrix times a 2x1 vector/matrix

In [37]:
um = matrix(uu).transpose() pretty_print(LatexExpr("M = "), mm, ", ", LatexExpr("U = "), um)
M=abcd,U=xyM = \left|\begin{array}{rr} a & b \\ c & d \end{array}\right| \verb|,| U = \left|\begin{array}{r} x \\ y \end{array}\right|
In [38]:
vm = mm * um pretty_print(vm, " = ", mm, "*", um)
ax+bycx+dyx=abcd*xy\left|\begin{array}{r} a x + b y \\ c x + d y \end{array}\right| \phantom{\verb!x!}\verb|=| \left|\begin{array}{rr} a & b \\ c & d \end{array}\right| \verb|*| \left|\begin{array}{r} x \\ y \end{array}\right|

And while it may not seem like much, notice that SageMath carried out the symbolic computation of multiplying a matrix and a column vector. Put simply, anyone spending much of their life working with linear algebra in contexts such as computer graphics, should master a tool such as SageMath (Maple, Mathematic, ..). The reason is that true understanding comes from personal experience working back and forth between multiple ways of conceptualizing a problem, and hence any tool that does the routine part for you is helpful.