% Sage Linear Algebra Quick Reference
% (c) 2009 by Robert A. Beezer
% Licensed with the GNU Free Documentation License (GFDL)
%   http://www.gnu.org/copyleft/fdl.html
%
%  History
%
%    2009-05-10  Initial version based on Sage 3.4
%    2009-05-13  Added right paren to vector u in "vector operations" section
%
%
\documentclass[a4paper]{article}
\usepackage{graphicx}  
\usepackage[landscape]{geometry}
%\usepackage[pdftex]{color}
\usepackage[dvipdfm]{color}

\usepackage{quickrefcard}  

\usepackage{multicol}
\usepackage{amsmath}
\usepackage{amsfonts}

\begin{document}
\begin{multicols*}{3}
\begin{center}
\textbf{Sage Quick Reference: Linear Algebra}\\
Robert A. Beezer (modified by nu)\\
Sage Version 3.4\\
\url{http://wiki.sagemath.org/quickref}\\
GNU Free Document License, extend for your own use\\
Based on work by Peter Jipsen, William Stein 
\end{center}
% backup over center environment gap
\vspace{-2ex}
%*********************************************
\sect{Vector Constructions}

\CAUTION{First entry of a vector is numbered 0}\\
\EX{u = vector(QQ, [1, 3/2, -1])} length 3 over rationals\\
\EX{v = vector(QQ, {2:4, 95:4, 210:0})}                  \BreakLineAndIndent
211 entries, nonzero in entry 2 and entry 95, sparse

%*********************************************
\sect{Vector Operations}

\EX{u=vector(QQ,[1, 3/2, -1])},
\EX{v=vector(ZZ,[1, 8, -2])}

\EX{2*u - 3*v} linear combination\\
\EX{u.dot_product(v)}\\
\EX{u.cross_product(v)}  order: \sagecommand{u}$\times$\sagecommand{v}\\
\EX{u.inner_product(v)}  inner product matrix from parent\\
\EX{u.pairwise_product(v)} vector as a result\\
\EX{u.norm() == u.norm(2)} Euclidean norm\\
\EX{u.norm(1)} sum of entries\\
\EX{u.norm(Infinity)} maximum entry\\
\EX{A.gram_schmidt()} converts the rows of matrix \sagecommand{A}

%*********************************************
\sect{Matrix Constructions}

\CAUTION{Row, column numbering begins at $0$}

\EX{A = matrix(ZZ, [[1,2],[3,4],[5,6]])} \BreakLineAndIndent
 $3\times 2$ over the integers

\EX{B = matrix(QQ, 2, [1,2,3,4,5,6])}    \BreakLineAndIndent
2 rows from a list, so $2\times 3$ over rationals

\EX{C = matrix(CDF, 2, 2, [[5*I, 4*I], [I, 6]])} \BreakLineAndIndent
complex entries, 53-bit precision

\EX{Z = matrix(QQ, 2, 2, 0)} 
zero matrix 

\EX{D = matrix(QQ, 2, 2, 8)}  \BreakLineAndIndent
diagonal entries all $8$, other entries zero

\EX{I = identity_matrix(5)} 
$5\times 5$ identity matrix

\EX{J = jordan_block(-2,3)}   \BreakLineAndIndent
$3\times 3$ matrix, $-2$ on diagonal, $1$'s on super-diagonal

\EX{var('x y z'); K = matrix(SR,[[x,y+z],[0,x^2*z]])} \BreakLineAndIndent
symbolic expressions live in the ring \sagecommand{SR}

\EX{L=matrix(ZZ, 20, 80, {(5,9):30, (15,77):-6})}  \BreakLineAndIndent
$20\times 80$, two non-zero entries, sparse representation

%*********************************************
\sect{Matrix Multiplication}

\EX{u=vector(QQ,[1,2,3])}, \EX{v=vector(QQ,[1,2])}, \\
\EX{A = matrix(QQ, [[1,2,3],[4,5,6]])},\\
\EX{B = matrix(QQ, [[1,2],[3,4]])},

\EX{u*A},\quad
\EX{A*v},\quad
\EX{B*A},\quad
\EX{B^6},\quad
\EX{B^(-3)} all possible

\EX{B.iterates(v, 6)}  produces $vB^0, vB^1,\dots, vB^5$  \BreakLineAndIndent
\sagecommand{rows = False} moves \sagecommand{v} to right of matrix powers

\EX{f(x)=x^2+5*x+3} then \EX{f(B)} is possible

\EX{B.exp()} matrix exponential, i.e.\ $\sum_{k=0}^{\infty}\frac{B^k}{k!}$

%*********************************************
\sect{Matrix Spaces}

\EX{M = MatrixSpace(QQ, 3, 4)}  \BreakLineAndIndent
dimension 12 space of $3\times 4$ matrices

\EX{A = M([1,2,3,4,5,6,7,8,9,10,11,12])} \BreakLineAndIndent
is a $3\times 4$ matrix, an element of \sagecommand{M}

\EX{M.basis()}

\EX{M.dimension()}

\EX{M.zero_matrix()}

%*********************************************
\sect{Matrix Operations}

\EX{5*A+2*B} linear combination

\EX{A.inverse()}, also \EX{A^(-1)}, \EX{~A} \BreakLineAndIndent
\sagecommand{ZeroDivisionError} if singular

\EX{A.transpose()}

\EX{A.antitranspose()} transpose + reverse orderings

\EX{A.adjoint()}  matrix of cofactors

\EX{A.conjugate()} entry-by-entry complex conjugates

\EX{A.restrict(V)} restriction on invariant subspace \sagecommand{V}

%*********************************************
\sect{Row Operations}

Row Operations: (change matrix in place)\\
\CAUTION{first row is numbered 0}\\
\EX{A.rescale_row(i,a)} \sagecommand{a*}(row \sagecommand{i})\\
\EX{A.add_multiple_of_row(i,j,a)} \sagecommand{a*}(row \sagecommand{j}) + row \sagecommand{i}\\
\EX{A.swap_rows(i,j)}

Each has a column variant, \sagecommand{row}$\rightarrow$\sagecommand{col}\\
For a new matrix, use e.g.\ \EX{B = A.with_rescaled_row(i,a)}

%*********************************************
\sect{Echelon Form}

\EX{A.echelon_form()},
\EX{A.echelonize()},
\EX{A.hermite_form()}\\
\CAUTION{Base ring affects results}\\
\EX{A = matrix(ZZ,[[4,2,1],[6,3,2]])}\\
\EX{B = matrix(QQ,[[4,2,1],[6,3,2]])}\\
\begin{tabular}{ll}
\EX{A.echelon_form()} & \EX{B.echelon_form()}\\
$\left(\begin{array}{rrr}
2 & 1 & 0 \\
0 & 0 & 1
\end{array}\right)$
&
$\left(\begin{array}{rrr}
1 & \frac{1}{2} & 0 \\
0 & 0 & 1
\end{array}\right)$
\end{tabular}\\
\EX{A.pivots()} indices of columns spanning column space\\
\EX{A.pivot_rows()} indices of rows spanning row space

%*********************************************
\sect{Pieces of Matrices}

\CAUTION{row, column numbering begins at 0}

\EX{A.nrows()}, \EX{A.ncols()}

\EX{A[i,j]} entry in row \sagecommand{i} and column \sagecommand{j}  \BreakLineAndIndent
\CAUTION{OK: \sagecommand{A[2,3] = 8}, Error: \sagecommand{A[2][3] = 8}}

\EX{A[i]} 
row \sagecommand{i} as immutable Python tuple

\EX{A.row(i)} 
returns row \sagecommand{i} as Sage vector

\EX{A.column(j)} 
returns column \sagecommand{j} as Sage vector

\EX{A.list()} 
returns single Python list, row-major order

\EX{A.matrix_from_columns([8,2,8])} \BreakLineAndIndent
 new matrix from columns in list, repeats OK

\EX{A.matrix_from_rows([2,5,1])}\BreakLineAndIndent
new matrix from rows in list, out-of-order OK

\EX{A.matrix_from_rows_and_columns([2,4,2],[3,1])} \BreakLineAndIndent
common to the rows and the columns

\EX{A.rows()}  
all rows as a list of tuples

\EX{A.columns()}  
all columns as a list of tuples

\EX{A.submatrix(i,j,nr,nc)} \BreakLineAndIndent
start at entry \sagecommand{(i,j)}, 
use \sagecommand{nr} rows, \sagecommand{nc} cols

\EX{A[2:4,1:7]}, \EX{A[0:8:2,3::-1]} 
Python-style list slicing 

%*********************************************
\sect{Combining Matrices}

\EX{A.augment(B)}  \sagecommand{A} in first columns, \sagecommand{B} to the right\\
\EX{A.stack(B)}  \sagecommand{A} in top rows, \sagecommand{B} below\\
\EX{A.block_sum(B)}  Diagonal, \sagecommand{A} upper left, \sagecommand{B} lower right\\
\EX{A.tensor_product(B)}  Multiples of \sagecommand{B}, arranged as in \sagecommand{A}

%*********************************************
\sect{Scalar Functions on Matrices}

\EX{A.rank()}\\
\EX{A.nullity() == A.left_nullity()}\\
\EX{A.right_nullity()}\\
\EX{A.determinant() == A.det()}\\
\EX{A.permanent()}\\
\EX{A.trace()}\\
\EX{A.norm() == A.norm(2)} Euclidean norm\\
\EX{A.norm(1)} largest column sum\\
\EX{A.norm(Infinity)} largest row sum\\
\EX{A.norm('frob')} Frobenius norm

%*********************************************
\sect{MatrixProperties}

\EX{.is_zero()} (totally?), 
\EX{.is_one()} (identity matrix?),\\
\EX{.is_scalar()} (multiple of identity?), 
\EX{.is_square()},\\
\EX{.is_symmetric()}, 
\EX{.is_invertible()}, 
\EX{.is_nilpotent()}

%*********************************************
\sect{Eigenvalues}

\EX{A.charpoly('t')}  
no variable specified defaults to \sagecommand{x} \BreakLineAndIndent
\EX{A.characteristic_polynomial() == A.charpoly()}

\EX{A.fcp('t')}  
factored characteristic polynomial

\EX{A.minpoly()}  
the minimum polynomial  \BreakLineAndIndent
\EX{A.minimal_polynomial() == A.minpoly()}

\EX{A.eigenvalues()} 
unsorted list, with mutiplicities

\EX{A.eigenvectors_left()} 
vectors on left, \sagecommand{_right} too \BreakLineAndIndent
Returns a list of triples, one per eigenvalue: \BreakLineAndDoubleIndent
\sagecommand{e}: the eigenvalue          \BreakLineAndDoubleIndent
\sagecommand{V}: list of vectors, basis for eigenspace \BreakLineAndDoubleIndent
\sagecommand{n}: algebraic multiplicity  

\EX{A.eigenmatrix_right()} 
vectors on right, \sagecommand{_left} too \BreakLineAndIndent
Returns two matrices: \BreakLineAndDoubleIndent
\sagecommand{D}: diagonal matrix with eigenvalues \BreakLineAndDoubleIndent
\sagecommand{P}: eigenvectors as columns (rows for left version)\BreakLineAndIndent[\skipin\skipin\phantom{P:}]
 has zero columns if matrix not diagonalizable
                    
%*********************************************
\sect{Decompositions}
%

\Note{availability depends on base ring of matrix}

\EX{A.jordan_form(transformation=True)}  \BreakLineAndIndent
returns a pair of matrices:      \BreakLineAndDoubleIndent
\sagecommand{J}: matrix of Jordan blocks for eigenvalues \BreakLineAndDoubleIndent
\sagecommand{P}: nonsingular matrix \BreakLineAndDoubleIndent
 so \sagecommand{    A == P^(-1)*J*P} 

%
\EX{A.smith_form()} returns a triple of matrices:       \BreakLineAndIndent
\sagecommand{D}: elementary divisors on diagonal        \BreakLineAndIndent
\sagecommand{U}, \sagecommand{V}: with unit determinant \BreakLineAndIndent
so \sagecommand{    D == U*A*V}

%
\EX{A.LU()} returns a triple of matrices:    \BreakLineAndIndent
\sagecommand{P}: a permutation matrix        \BreakLineAndIndent
\sagecommand{L}: lower triangular matrix     \BreakLineAndIndent
\sagecommand{U}: upper triangular matrix     \BreakLineAndIndent
so \sagecommand{    P*A == L*U}

%
\EX{A.QR()} returns a pair of matrices:      \BreakLineAndIndent
\sagecommand{Q}: an orthogonal matrix        \BreakLineAndIndent
\sagecommand{R}: upper triangular matrix     \BreakLineAndIndent
so \sagecommand{    A == Q*R}

%
\EX{A.SVD()} returns a triple of matrices:   \BreakLineAndIndent
\sagecommand{U}: an orthogonal matrix        \BreakLineAndIndent
\sagecommand{S}: zero off the diagonal, same dimensions as \sagecommand{A} \BreakLineAndIndent
\sagecommand{V}: an orthogonal matrix        \BreakLineAndIndent
so \sagecommand{  A == U*S*(V-conjugate-transpose)}

%
\EX{A.symplectic_form()}\\
\EX{A.hessenberg_form()}\\
\EX{A.cholesky()}

%*********************************************
\sect{Solutions to Systems}

\EX{A.solve_right(B)} \sagecommand{_left} too \BreakLineAndIndent
 is solution to \sagecommand{A*X = B}, where \sagecommand{X} is a vector {\bf or} matrix

\EX{A = matrix(QQ, [[1,2],[3,4]])}\\
\EX{b = vector(QQ, [3,4])}\BreakLineAndIndent
 then \EX{A\b} returns the solution \EX{(-2, 5/2)}

%*********************************************
\sect{Vector Spaces}

\EX{U = VectorSpace(QQ, 4)}  
dimension 4, rationals as field

\EX{V = VectorSpace(RR, 4)}  
``field'' is 53-bit precision reals

\EX{W = VectorSpace(RealField(200), 4)} \BreakLineAndIndent
 ``field'' has 200 bit precision

\EX{X = CC^4} 
4-dimensional, 53-bit precision complexes

\EX{Y = VectorSpace(GF(7), 4)}  
finite          \BreakLineAndIndent
\EX{Y.finite()} returns \sagecommand{True}      \BreakLineAndIndent
\EX{len(Y.list())} returns $7^4=2401$ elements

%*********************************************
\sect{Vector Space Properties}

\EX{V.dimension()}

\EX{V.basis()}

\EX{V.echelonized_basis()}

\EX{V.has_user_basis()} 
with non-canonical basis?

\EX{V.is_subspace(W)} 
\sagecommand{True} if \sagecommand{W} is a subspace of \sagecommand{V}

\EX{V.is_full()} rank equals degree (as module)?

\EX{Y = GF(7)^4},\quad\EX{T = Y.subspaces(2)} \BreakLineAndIndent
\sagecommand{T} is a generator object 
for 2-D subspaces of \sagecommand{Y} \BreakLineAndIndent
\sagecommand{[U for U in T]} is list of 2850 2-D subspaces of \sagecommand{Y}

%*********************************************
\sect{Constructing Subspaces}

\EX{span([v1,v2,v3], QQ)} span of list of vectors over ring

For a matrix \sagecommand{A}, objects returned are  \BreakLineAndIndent
vector spaces when base ring is a field \BreakLineAndIndent
 modules when base ring is just a ring       \\
\EX{A.left_kernel() == A.kernel()}      
\sagecommand{right_} too                     \\
\EX{A.row_space() == A.row_module()}         \\
\EX{A.column_space() == A.column_module()}   

\EX{A.eigenspaces_right()} 
vectors on right, \sagecommand{_left} too                  \BreakLineAndIndent
Pairs, having eigenvalue with its right eigenspace

If \sagecommand{V} and \sagecommand{W} are subspaces\\
\EX{V.quotient(W)} quotient of \sagecommand{V} by subspace \sagecommand{W}\\
\EX{V.intersection(W)} intersection of \sagecommand{V} and \sagecommand{W}\\
\EX{V.direct_sum(W)} direct sum of \sagecommand{V} and \sagecommand{W}\\
\EX{V.subspace([v1,v2,v3])} specify basis vectors in a list

%*********************************************
\sect{Dense versus Sparse}

\Note{Algorithms may depend on representation}

Vectors and matrices have two representations \BreakLineAndIndent
Dense: lists, and lists of lists           \BreakLineAndIndent
Sparse: Python dictionaries

\EX{.is_dense()}, \EX{.is_sparse()}  to check

\EX{A.sparse_matrix()} returns sparse version of \sagecommand{A}

\EX{A.dense_rows()} returns dense row vectors of \sagecommand{A}

Some commands have  boolean \sagecommand{sparse} keyword

%*********************************************
\sect{Rings}

\Note{Many algorithms depend on the base ring}

\EX{<object>.base_ring(R)} for vectors, matrices,\dots \BreakLineAndIndent
to determine the ring in use

\EX{<object>.change_ring(R)} for vectors, matrices,\dots  \BreakLineAndIndent
to change to the ring (or field), \sagecommand{R}.

\EX{R.is_ring()},\quad\EX{R.is_field()}\\
\EX{R.is_integral_domain()},\quad\EX{R.is_exact()}

Some ring and fields                   \BreakLineAndIndent
\EX{ZZ}\quad integers, ring     \BreakLineAndIndent
\EX{QQ}\quad rationals, field   \BreakLineAndIndent
\EX{QQbar}\quad algebraic field, exact  \BreakLineAndIndent
\EX{RDF}\quad real double field, inexact \BreakLineAndIndent
\EX{RR}\quad 53-bit reals, inexact       \BreakLineAndIndent
\EX{RealField(400)}\quad 400-bit reals, inexact \BreakLineAndDoubleIndent
\sagecommand{CDF},
\sagecommand{CC},
\sagecommand{ComplexField(400)}\quad
 complexes, too \BreakLineAndIndent
\EX{RIF}\quad real interval field \BreakLineAndIndent
\EX{GF(2)}\quad mod 2, field, specialized implementations \BreakLineAndIndent
\EX{GF(p) == FiniteField(p)}\quad \sagecommand{p} prime, field \BreakLineAndIndent
\EX{Integers(6)}\quad integers mod 6, ring only \BreakLineAndIndent
\EX{CyclotomicField(7)}\quad rationals with 7$^{\rm th}$ root of unity \BreakLineAndIndent
\EX{QuadraticField(-5, 'x')}\quad rationals adjoin \sagecommand{x=}$\sqrt{-5}$\BreakLineAndIndent
\EX{SR}\quad ring of symbolic expressions

%*********************************************
\sect{Vector Spaces versus Modules}

A module is ``like'' a vector space over a ring, not a field\\
Many commands above apply to modules\\
Some ``vectors'' are really module elements

%*********************************************
\sect{More Help}

``tab-completion'' on partial commands\\
``tab-completion'' on \sagecommand{<object.>} for all relevant methods\\
\sagecommand{<command>?} for summary and examples\\
\sagecommand{<command>??} for complete source code
%
\end{multicols*}

\end{document}
