This Sage worksheet was developed for the MAA PREP Workshop "Sage: Using Open-Source Mathematics Software with Undergraduates" (funding provided by NSF DUE 0817071).
Linear algebra underpins a lot of Sage's algorithms, so it is fast, robust and comprehensive.
Make a matrix, use tab-completion to see routines.
{{{id=2| A = matrix([[1,2,3],[4,5,6]); A /// }}}Lots of other ways to make matrices:
{{{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 /// }}}Vectors are rows or columns, whatever you please.
{{{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 /// }}}"Vectors" over rings (and sometimes over fields) are "free module elements", but behave in most ways like regular vectors.
{{{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 ) /// }}}Sage "prefers" rows to columns. For example, a kernel is row vectors (on the left).
{{{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() /// }}}Note: kernels are vector spaces and bases are "echelonized" (canonicalized).
Left/right variants for solving, eigen-stuff, etc.
{{{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() /// }}}Bring up: Sage Linear Algebra Quick Reference
{{{id=28| /// }}}