PREP Quickstart, Linear Algebra
system:sage


<h1 style="font-size: 2em;">Sage Quickstart for Linear Algebra</h1>
<p>This&nbsp;<a href="http://www.sagemath.org" target="_blank">Sage</a>&nbsp;worksheet was developed for the MAA PREP Workshop "Sage: Using Open-Source Mathematics Software with Undergraduates" (funding provided by NSF DUE 0817071).</p>
<p>Linear algebra underpins a lot of Sage's algorithms, so it is fast, robust and comprehensive.</p>
<h2><span style="color: #ff0000;">"Objects" (Matrices and Vectors)<br /></span></h2>

<p>Make a matrix, use tab-completion to see routines.</p>

{{{id=2|
A = matrix([[1,2,3],[4,5,6]); A
///
}}}

<p>Lots of other ways to make matrices:</p>

{{{id=1|
B = matrix(QQ, 3, 2, [1,2,3,4,5,6]); B
///
}}}

{{{id=5|
C = matrix(QQ, 3, [1,2,3,4,5,6]); C
///
}}}

{{{id=6|
D = matrix(CC, 20, range(400)); D
///
}}}

{{{id=7|
E = diagonal_matrix( range(0, 41, 4) ); E
///
}}}

<p>Vectors are rows or columns, whatever you please.</p>

{{{id=9|
row = vector( (3, -1, 4))
col = vector( QQ, [4, 5] )
row; col
///
}}}

{{{id=8|
F = matrix(QQ, 3, 2, range(6)); F
///
}}}

{{{id=11|
F*col
///
}}}

{{{id=12|
row*F
///
}}}

<p>"Vectors" over rings (and sometimes over fields) are "free module elements", but behave in most ways like regular vectors.</p>

{{{id=14|
ring_vec = vector(SR, [2, 12, -4, 9])
field_vec = vector( QQ, (2, 3, 14) )
ring_vec; field_vec
///
}}}

{{{id=13|
print type( ring_vec )
print type( field_vec )
///
}}}

<h2><span style="color: #ff0000;">Left-Handed or Right-handed?</span></h2>
<p>Sage "prefers" rows to columns.&nbsp; For example, a kernel is row vectors (on the left).</p>

{{{id=16|
G = matrix(QQ, 2, 3, [[1,2,3],[2,4,6]])
G.kernel()
///
}}}

{{{id=18|
G.left_kernel()
///
}}}

{{{id=19|
G.right_kernel()
///
}}}

<p>Note: kernels are <span style="color: #ff6600;"><strong>vector spaces</strong></span> and bases are "<strong><span style="color: #ff6600;">echelonized</span></strong>" (canonicalized).</p>
<p>Left/right variants for solving, eigen-stuff, etc.</p>

{{{id=23|
H = random_matrix(QQ, 5, 5, num_bound = 10, den_bound = 4)
///
}}}

{{{id=20|
H.eigenspaces_right()
///
[
(a0, Vector space of degree 5 and dimension 1 over Number Field in a0 with defining polynomial x^5 - 155/12*x^4 - 299/3*x^3 + 53221/48*x^2 - 16625/4*x - 2209885/216
User basis matrix:
[                                                                                                                                                               1 -3528699552/9644695239401*a0^4 + 49221408672/9644695239401*a0^3 + 380969816688/9644695239401*a0^2 - 4522844548884/9644695239401*a0 - 2654980101218/9644695239401 -1108505628/9644695239401*a0^4 - 2964355713/9644695239401*a0^3 + 215701081716/9644695239401*a0^2 + 9917630578119/38578780957604*a0 + 5158582615969/9644695239401 3551619924/9644695239401*a0^4 - 7608003465/9644695239401*a0^3 - 635333550861/9644695239401*a0^2 - 7504094424665/38578780957604*a0 - 7465093661617/38578780957604 768957048/9644695239401*a0^4 - 27261298542/9644695239401*a0^3 - 130343508492/9644695239401*a0^2 + 9260114163951/19289390478802*a0 + 19846456871992/9644695239401])
]
}}}

{{{id=22|
H.eigenspaces_left()
///
}}}

<h2><span style="color: #ff0000;">Matrix Manipulations</span></h2>

{{{id=24|
A = matrix(3, 2, range(6))
B = matrix(2, 3, range(0, 12, 2))
print A
print
print B
///
}}}

{{{id=26|
C = A.augment( B.transpose() ); C
///
}}}

{{{id=27|
D = B.stack( A.transpose() ); D
///
}}}

<p>Bring up: Sage <a href="http://wiki.sagemath.org/quickref?action=AttachFile&amp;do=get&amp;target=quickref-linalg.pdf">Linear Algebra Quick Reference</a></p>

{{{id=28|

///
}}}