# 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]) pretty_print(html('<h2>Compressed using %s eigenvalues</h2>'%i))

## 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() pretty_print(html("Solving:")) pretty_print(html('$'+latex(E1)+'$')) pretty_print(html('$'+latex(E2)+'$')) P = plot(eq1,(x1,-range,range))+plot(eq2,(x1,-range,range),color='red') show(P,figsize=5) if show_sol: try: pretty_print(html(solve([E1,E2],[x1,x2])[0])) except: pretty_print(html('No solution?'))

range
show_sol

## 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() pretty_print(html('$'+latex(E1)+'$')) pretty_print(html('$'+latex(E2)+'$')) pretty_print(html('$'+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: pretty_print(html(solve([E1,E2,E3],[x1,x2,x3])[0])) except: pretty_print(html('No solution?'))

range
show_sol

## 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]) \ vector([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*r1 + 8, x2 == -27/5*r1 - 46/5, x3 == r1]] [[x1 == 4*r1 + 8, x2 == -27/5*r1 - 46/5, x3 == r1]]

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)

## Section 1.5

What does this whole parametric business mean?

var('s,t') V = vector([1,-2]) P = parametric_plot(t*V,(t,-2,2)) P += V.plot(width=5,color='red') show(P,figsize=4)
var('s,t') V = vector([1,-2]) 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)
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([2,1,0])+s*W+t*V,(s,-5,5),(t,-5,5)) 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') P += vector([2,1,0]).plot(thickness=5,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!

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

Notice how in the sample given, all the vectors get smashed onto one plane.