Tutorial: Matrix Algebra system:sage

Creating Vectors and Matrices

You create vectors using the vector command.  Type vector? for details.

{{{id=27| vector([1,2,3]) /// }}}

Consider $x^{y\sqrt{2}}$.

{{{id=162| plot(x^-2, x, -5, 5).show(ymax=10) /// }}} {{{id=161| %latex Consider $x^y$ + $z^w$. /// }}} {{{id=30| vector([1, e + e, pi + sqrt(2)]) /// }}}

Hello this is $2^3 x + y^\pi$.

{{{id=31| vector(1/n for n in [1..10]) /// }}}

Problem:  Create the vector $(1^2, 2^2, 3^2, \ldots, 100^2)$.

{{{id=39| vector(n*n for n in [1..100]) /// }}} {{{id=33| /// }}}

Problem: Create the vector $(\pi, e, e^{2\pi i})$.

{{{id=36| /// }}} {{{id=32| /// }}}

You create matrices using the matrix command.  Type matrix? for details.

{{{id=1| matrix([[1,2,3], [4,5,6]]) /// }}} {{{id=45| matrix(2, 2, [2/3, -3/17, sqrt(2)+I, cos(x)]) /// }}} {{{id=46| matrix(3, 3, [i/j for i in [1..3] for j in [1..3]]) /// }}}

Giving RDF as the first argument makes all entries be interpreted as elements of the Real Double Field.

{{{id=40| matrix(RDF, 2, 3, [1,2,3/2, 2/3,1,-1]) /// }}}

Other important fields: CDF (complex double field), QQ (rational numbers), SR (symbolic expressions), RealField(prec) and ComplexField(prec) for real and complex numbers to a given numbers of bits of precision.

{{{id=86| matrix(RealField(100), 2, 2, [1,2,3,4]) /// }}}

Problem: Make a $2\times 2$ matrix whose entries are 200-bit precision complex numbers.

{{{id=88| /// }}} {{{id=87| /// }}}

Problem: Create the $5\times 5$ matrix whose $(i,j)$ entry is $i^2 + j^2$ for $0 \leq i,j \leq 4$.

{{{id=90| /// }}} {{{id=44| /// }}}

You can create a random $n\times m$ matrix with entries from a given field by typing random_matrix(field, n, m):

{{{id=82| random_matrix(RDF,2,4) /// }}} {{{id=83| random_matrix(QQ, 3, 4, num_bound=100, den_bound=100) /// }}}

Use field.random_element? to figure out additional options that determine how the random numbers are chosen.

{{{id=80| /// }}} {{{id=48| /// }}}

Problem: Make a random $5\times 7$ matrix with rational number entries whose numerators and denominators are bounded by 1000.

{{{id=50| /// }}} {{{id=42| /// }}}

Problem: Create a random $10\times 10$ matrix with real double precision entries between $0$ and $1$.

{{{id=4| /// }}} {{{id=53| /// }}} {{{id=52| /// }}} {{{id=3| /// }}}

Computing Reduced Row Echelon Form

You can read about reduced row echelon form in Wikipedia.

Problem: Create a matrix m and type m.<tab key>.  Notice that there are a lot of functions for matrices, including echelon_form().

{{{id=54| /// }}} {{{id=63| /// }}}

We compute some echelon forms.

{{{id=65| m = matrix(2, 2, [2/3, -3/17, sqrt(2)+I, cos(x)]) m.echelon_form() /// }}} {{{id=68| matrix(3, 3, [i/j for i in [1..3] for j in [1..3]]).echelon_form() /// }}} {{{id=69| m = matrix(2, 2, [2/3, -3/17, sqrt(2)+I, cos(x)]) m.echelon_form() /// }}} {{{id=10| matrix(3, 3, [i/j for i in [1..3] for j in [1..3]]).echelon_form() /// }}}

WARNING: Right now if you create a matrix over the integers, then echelon_form() returns the echelon form over the integers, which may be different than the echelon forms over the rationals.  Always give QQ as the first input the matrix constructor to avoid this issue.

{{{id=59| m = matrix(2, [1,2, 3,4]) m.base_ring() /// }}}

Notice the 2 in the lower right below -- echelon form over the integers is like usual echelon form, but one isn't allowed to divide.

{{{id=58| m.echelon_form() /// }}} {{{id=70| matrix(QQ, 2, [1,2,3,4]).echelon_form() /// }}} {{{id=57| /// }}}

Problem: Compute the echelon form of the matrix $$m = \left(\begin{array}{rrrr}
\frac{1}{2} & -1 & 0 & -2 \\
-1 & 1 & -1 & 2 \\
-2 & -\frac{1}{2} & -2 & -2
\end{array}\right).$$

{{{id=74| /// }}} {{{id=71| /// }}}

Problem: Compute the echelon form of the $10\times 10$ matrix $m$ whose entries are $1,2,\ldots,100$, i.e., whose first row is $1,2,\ldots,10$, whose seconds row is $11,12,\ldots,20$, etc.

{{{id=75| /// }}} {{{id=78| /// }}}

Problem: Can Sage "quickly" compute the echelon form of a matrix over the rationals of size $10\times 10$? What about $100\times 100$, $10\times 50$, $200\times 201$, $500\times 501$, $1000\times 1001$?

{{{id=93| time random_matrix(QQ, 5, 10).echelon_form() /// }}} {{{id=100| /// }}} {{{id=98| /// }}} {{{id=97| /// }}} {{{id=96| /// }}} {{{id=95| /// }}} {{{id=9| /// }}} {{{id=8| /// }}}

Computing Determinants, Characteristic Polynomials, and Minimal Polynomials

Here are examples of computing each.

{{{id=101| m = matrix(2,[1,sqrt(2), 3, -5/2]) /// }}} {{{id=91| m.det() /// }}} {{{id=102| m.charpoly() /// }}} {{{id=7| m.minpoly() # known bug when entries symbolic: http://trac.sagemath.org/sage_trac/ticket/5639 /// }}} {{{id=103| m = matrix(4,[1..16]) factor(m.minpoly()) /// }}} {{{id=16| factor(m.charpoly()) /// }}} {{{id=105| /// }}} {{{id=104| /// }}}

Problem: Compute the determinant, characteristic polynomial, and minimal polynomial of the matrix $$m = \left(\begin{array}{rrrr}
\frac{1}{2} & -1 & 0 & -2 \\
-1 & 1 & -1 & 2 \\
-2 & -\frac{1}{2} & -2 & -2
\end{array}\right).$$

{{{id=108| /// }}} {{{id=107| /// }}} {{{id=15| /// }}} {{{id=14| /// }}}

Eigenvalues and Eigenvectors

The eigenvalues and eigenvectors_right commands find the eigenvalues and eigenvectors of a matrix.  See Wikipedia if you don't know about eigenvalues and eigenvectors.

{{{id=144| m = matrix(QQ, 3, [7/3,0,-1/3, -1/3,2,1/3, -2/3,0,8/3]) show(m) /// }}} {{{id=145| m.eigenvalues() /// }}} {{{id=140| /// }}}

The eigenvectors_right command returns triples $(\alpha, [v_1,\ldots,v_k], k])$ where the $v_i$ are a basis of eigenvectors with corresponding eigenvalue $\alpha$.

{{{id=109| E = m.eigenvectors_right() show(E) /// }}} {{{id=13| E[0] /// }}} {{{id=21| E[1][1][0] /// }}} {{{id=151| E[1][1][1] /// }}} {{{id=152| /// }}} {{{id=147| /// }}}

Problem: Find the eigenvalues and eigenvectors of the matrix $$m=\left(\begin{array}{rrr}
2 & -\frac{7}{2} & 6 \\
0 & 0 & 3 \\
0 & -1 & 4
\end{array}\right).$$

{{{id=154| /// }}} {{{id=153| /// }}}

Problem: Find the eigenvalues and eigenvectors of the $10\times 10$ matrix $m$ whose entries are $1,2,\ldots,100$.

{{{id=155| /// }}} {{{id=146| /// }}} {{{id=20| /// }}} {{{id=19| /// }}}

Solving Systems of Linear Equations

You can use the solve command for small linear (or nonlinear) systems, and the backslash (or solve_right) command for big linear systems or matrix equations.

{{{id=130| var('x,y') solve([2*x + 3*y == 1, 2/3*x - 5*y == 2], x,y) /// }}} {{{id=129| matrix(2,[2,3, 2/3,-5]) \ vector([1,2]) /// }}} {{{id=131| matrix(2,[2,3, 2/3,-5]).solve_right( vector([1,2]) ) /// }}} {{{id=128| A = matrix(QQ,2, [2,3, 2/3,-5]); B = matrix(QQ,2, [1,2,3,4]) X = A \ B; X /// }}} {{{id=18| A * X == B /// }}} {{{id=134| /// }}}

Problem:  Solve the linear system $Ax = v$, where  $$A = \left(\begin{array}{rrrr}
\frac{1}{2} & -1 & 0 & -2 \\
-1 & 1 & -1 & 2 \\
-2 & -\frac{1}{2} & -2 & -2

\end{array}\right) \qquad\text{and}\qquad v =\left(\begin{array}{r}
-1 \\
1 \\
\frac{1}{2}

\end{array}\right)$$

{{{id=136| /// }}} {{{id=133| /// }}}

Problem:  Create a linear system of equations (with matrices) that has no solution and ask Sage to solve it.  What happens?

{{{id=138| /// }}} {{{id=137| /// }}} {{{id=25| /// }}}

Plotting Matrices

{{{id=112| m = matrix(3, [0,-5,5, 1,2, -1, -1,-2,-2]); m /// }}} {{{id=111| m.plot() /// }}} {{{id=115| list_plot3d(m) /// }}} {{{id=123| @interact def f(n=(2..100)): show(random_matrix(RDF, n).plot()) /// }}} {{{id=110| /// }}}

In Sage, the notation a % b means "the remainder upon dividing a by b", e.g., 7%5 is 2.

{{{id=114| @interact def f(n=(2..50), grid=True): m = matrix(n, [((i+j))%n for i in [0..n-1] for j in [0..n-1]]) show(m.plot(cmap='summer'), gridlines=grid) /// }}}

Problem: Make an interactive "multiplication table modulo n" by copying the code above and changing (i+j)%n to (i*j)%n.

{{{id=157| /// }}} {{{id=126| /// }}} {{{id=124| /// }}} {{{id=0| /// }}} {{{id=125| /// }}}