MAT 232 Chapter 1

680 days ago by kcrisman

Welcome to Linear Algebra!

Here is a really typical application.

import pylab A_image = pylab.mean(pylab.imread(DATA + 'Jenks.png'), 2) @interact def svd_image(i=(5,(1..50))): u,s,v = pylab.linalg.svd(A_image) A = sum(s[j]*pylab.outer(u[0:,j],v[j,0:]) for j in range(i)) g = graphics_array([[matrix_plot(A),matrix_plot(A_image)]]) show(g,axes=False, figsize=(8,10)) html('<h2>Compressed using %s eigenvalues</h2>'%i) 
       

Click to the left again to hide and once more to show the dynamic interactive window

Section 1.1

So, what the heck are solutions of systems of linear equations, anyway?  Let's see what can happen.

var('x1,x2') @interact def _(M = matrix(RR,[[1,5],[-2,-7]]), V = matrix(RR,[[7,-5]]),range=(5,(1,100)),show_sol=False): a, b, c, d = M[0,0],M[0,1],M[1,0],M[1,1] e, f = V[0,0],V[0,1] E1 = a*x1+b*x2==e E2 = c*x1+d*x2==f eq1 = solve(E1,x2)[0].rhs() eq2 = solve(E2,x2)[0].rhs() html("Solving:") html('Eq 1: $'+latex(E1)+'$') html('Eq 2: $'+latex(E2)+'$') P = plot(eq1,(x1,-range,range))+plot(eq2,(x1,-range,range),color='red') show(P) if show_sol: try: html(solve([E1,E2],[x1,x2])[0]) except: html('No solution?') 
       

Click to the left again to hide and once more to show the dynamic interactive window

var('x1,x2,x3') @interact def _(M = matrix(RR,[[1,5,2],[-2,-7,3],[0,1,1]]), V = matrix(RR,[[7,-5,0]]),range=(5,(1,100)),show_sol=False): a1, a2, a3, b1, b2, b3, c1, c2, c3 = M[0,0],M[0,1],M[0,2],M[1,0],M[1,1],M[1,2],M[2,0],M[2,1],M[2,2] e, f, g = V[0,0],V[0,1],V[0,2] E1 = a1*x1+a2*x2+a3*x3==e E2 = b1*x1+b2*x2+b3*x3==f E3 = c1*x1+c2*x2+c3*x3==g eq1 = solve(E1,x3)[0].rhs() eq2 = solve(E2,x3)[0].rhs() eq3 = solve(E3,x3)[0].rhs() html('Eq 1: $'+latex(E1)+'$') html('Eq 2: $'+latex(E2)+'$') html('Eq 3: $'+latex(E3)+'$') P = plot3d(eq1,(x1,-range,range),(x2,-range,range))+plot3d(eq2,(x1,-range,range),(x2,-range,range),color='red')+plot3d(eq3,(x1,-range,range),(x2,-range,range),color='green') #show(P,viewer='canvas3d') show(P) if show_sol: try: html(solve([E1,E2,E3],[x1,x2,x3])[0]) except: html('No solution?') 
       

Click to the left again to hide and once more to show the dynamic interactive window

Can we write these things as matrices?  Sure! Here is the absolute basic syntax.

M = matrix([[1,2],[3,8]]) V = matrix([[7],[25]]) 
       
M\V 
       
[3]
[2]
[3]
[2]

Section 1.2

So how do we write a matrix in Sage?

matrix(2 , [6,5,3,2,1,0,-4,8] ) 
       
[ 6  5  3  2]
[ 1  0 -4  8]
[ 6  5  3  2]
[ 1  0 -4  8]

This is probably the easiest way.  You put in a "list" (stuff in brackets) of the right length, tell Sage there are two rows, and go.

To make things easier, we give it a name.

M = matrix(2 , [6,5,3,2,1,0,-4,8] ) 
       

Echelon form?

M.echelon_form() 
       
[  1   0  -4   8]
[  0   5  27 -46]
[  1   0  -4   8]
[  0   5  27 -46]

Huh?  Well, this is an echelon form.  (Exactly which one it is has to do with MAT 335, Algebraic Structures, in a way I won't address here.)  If we want the reduced one, we use the following command.

M.rref() 
       
[    1     0    -4     8]
[    0     1  27/5 -46/5]
[    1     0    -4     8]
[    0     1  27/5 -46/5]

Whew!

Note that this system, if it were from an augmented matrix, does indeed correspond to having one free variable (the third).

matrix(2,[6,5,3,1,0,-4])\matrix(2,[2,8]) 
       
[    8]
[-46/5]
[    0]
[    8]
[-46/5]
[    0]
var('x1,x2,x3') solve([6*x1+5*x2+3*x3==2,x1-4*x3==8],[x1,x2,x3]) 
       
[[x1 == 4*r2 + 8, x2 == -27/5*r2 - 46/5, x3 == r2]]
[[x1 == 4*r2 + 8, x2 == -27/5*r2 - 46/5, x3 == r2]]

The "r1" is the "free variable.

Section 1.3

The main thing a computer can help us with today is visualizing vectors and their spans.

In Sage, there is no distinction between column and row vectors, and indeed they are seen as not intrinsically the same as matrices.  We only write them as the same in this class for convenience, really.

V = vector( [1,2] ) 
       
#V.plot() plot(V) 
       

The computer really helps us see what the span looks like, and especially how it can change "dimension".

@interact def _(V = matrix([[1],[3]]), W = matrix([[-2],[4]]) ): v = vector([V[0,0],V[1,0]]) w = vector([W[0,0],W[1,0]]) G = Graphics() G += v.plot() G += w.plot(color='green') for i in [-3..3]: for j in [-3..3]: G += line([(i*v[0]+j*w[0],i*v[1]+j*w[1]),(i*v[0]+(j+1)*w[0],i*v[1]+(j+1)*w[1])],alpha=.5,color='red') G += line([(j*v[0]+i*w[0],j*v[1]+i*w[1]),((j+1)*v[0]+i*w[0],(j+1)*v[1]+i*w[1])],alpha=.5,color='red') show(G) 
       

Click to the left again to hide and once more to show the dynamic interactive window

Notice that in this case I add an option so that it's clear that not just the integer linear combinations, but all linear combinations are part of the span.

var('s,t') @interact def _(V = matrix([[1],[5],[0]]), W = matrix([[0],[1],[1]]),show_plane=False ): v = vector([V[0,0],V[1,0],V[2,0]]) w = vector([W[0,0],W[1,0],W[2,0]]) G = Graphics() G += v.plot(thickness=5) G += w.plot(color='green',thickness=5) for i in [-3..3]: for j in [-3..3]: G += line([(i*v[0]+j*w[0],i*v[1]+j*w[1],i*v[2]+j*w[2]),(i*v[0]+(j+1)*w[0],i*v[1]+(j+1)*w[1],i*v[2]+(j+1)*w[2])],alpha=.5,color='red') G += line([(j*v[0]+i*w[0],j*v[1]+i*w[1],j*v[2]+i*w[2]),((j+1)*v[0]+i*w[0],(j+1)*v[1]+i*w[1],(j+1)*v[2]+i*w[2])],alpha=.5,color='red') if show_plane: G += parametric_plot3d(s*v+t*w,(s,-3,3),(t,-3,3),color='red',alpha=.5) show(G) 
       

Click to the left again to hide and once more to show the dynamic interactive window

Section 1.5

What does this whole parametric business mean?

var('s,t') V = vector([1/2,0,1]) W = vector([-1/2,1,0]) P = parametric_plot3d(s*W+t*V,(s,-5,5),(t,-5,5)) P += V.plot(thickness=5,color='red') P += W.plot(thickness=5,color='red') P += parametric_plot3d(vector([3,0,1])+s*W+t*V,(s,-5,5),(t,-5,5)) P += vector([3,0,1]).plot(thickness=5,color='green') show(P) 
       
var('s,t') V = vector([-2,1]) P = parametric_plot(t*V,(t,-2,2)) P += V.plot(width=5,color='red') show(P) 
       
var('s,t') V = vector([-2,1]) P = parametric_plot(t*V,(t,-2,2)) P += V.plot(width=5,color='red') P += parametric_plot(vector([1,1])+t*V,(t,-2,2)) P += vector([1,1]).plot(color='green') show(P) 
       

Section 1.8

Let's visualize a linear transformation.

@interact(layout=[['A','Myvect']]) def _(A = matrix([[1,-1],[0,1]]),Myvect=matrix([[0,-1]])): V = vector([1,3]) W = A*V V1 = vector([-1,2]) W1 = A*V1 L = line([V,V1],linestyle='--') LW = line([W,W1],linestyle='--') Myvect = vector(Myvect[0]) Wyvect = A*Myvect show(graphics_array([[plot(V)+plot(V1,color='red')+plot(Myvect,color='green')+plot(L),plot(W)+plot(W1,color='red')+plot(Wyvect,color='green')+plot(LW)]]),aspect_ratio=1) 
       

Click to the left again to hide and once more to show the dynamic interactive window

You get to pick the vector and the linear transformation!

@interact(layout=[['A','Myvect']]) def _(A = matrix([[1,-3],[3,5],[-1,7]]),Myvect=matrix([[2,-1]])): V = vector([1,3]) W = A*V V1 = vector([-1,2]) W1 = A*V1 Myvect = vector(Myvect[0]) Wyvect = A*Myvect show(plot(V)+plot(V1,color='red')+plot(Myvect,color='green')) show(plot(W,thickness=10)+plot(W1,color='red',thickness=10)+plot(Wyvect,color='green',thickness=10)) 
       

Click to the left again to hide and once more to show the dynamic interactive window

Section 1.9

Here is a fun activity for visualizing the geometry of linear transformations.  It's due to Jane Long, based on an activity of Tom Judson, both at Stephen F. Austin State in Nacogdoches, TX.

t = var('t') @interact def _(auto_update=False,A=matrix(RDF,[[1,0],[0,1]])): pll=A*vector((-0.5,0.5)) plr=A*vector((-0.3,0.5)) prl=A*vector((0.3,0.5)) prr=A*vector((0.5,0.5)) left_eye=line([pll,plr])+point(pll,size=5)+point(plr,size=5) right_eye=line([prl,prr],color='green')+point(prl,size=5,color='green')+point(prr,size=5,color='green') mouth=parametric_plot(A*vector([t, -0.15*sin(2*pi*t)-0.5]), (t, -0.5, 0),color='red')+parametric_plot(A*vector([t, -0.15*sin(2*pi*t)-0.5]), (t,0,0.5),color='orange') face=parametric_plot(A*vector([cos(t),sin(t)]), (t,0,pi/2),color='black')+parametric_plot(A*vector([cos(t),sin(t)]), (t,pi/2,pi),color='lavender')+parametric_plot(A*vector([cos(t),sin(t)]), (t,pi,3*pi/2),color='cyan')+parametric_plot(A*vector([cos(t),sin(t)]),(t,3*pi/2,2*pi),color='sienna') P=right_eye+left_eye+face+mouth html('smiley guy transformed by $A$') P.show(aspect_ratio=1) 
       

Click to the left again to hide and once more to show the dynamic interactive window