Math 1062 -- Feb 13, 2012 -- Linear Algebra (part 1)

92 days ago by William_Stein

Math 1062 -- Feb 13, 2012 -- Linear Algebra (part 1)

 

  1. Questions?  Reminder: the "short" take-home midterm will be assigned Wed and due Friday at midnight. 
  2. Warm Up: http://aleph.sagemath.org, persistent URL's, and embedding Sage (including interacts) in your own web page.  See Beezer's examples, the official directions (22 easy steps?), and this simple complete example.
  3. Warm Up for today: Fast 2x2 matrix multiplication?  Talk for a moment about this sage-support thread.
  4. General overview of linear algebra in Sage.
 
       

Making a Matrix

[1,2; 2,3; pi,e^2] 
       
A = matrix([[1,2], [2,3], [pi, e^2]]); A.base_ring() 
       
Symbolic Ring
Symbolic Ring
K = GF(7); K 
       
Finite Field of size 7
Finite Field of size 7
list(K) 
       
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]
RDF 
       
Real Double Field
Real Double Field
S.<chris2, william2> = RR[] 
       
random_matrix(S, 2) 
       
[         -0.821876689273126*chris2^2 -
0.337570915811116*chris2*william2 - 0.445009814043026*william2^2 -
0.933733757961343*chris2 - 0.328938818266539 -0.901307772747002*chris2^2
- 0.343913151855248*chris2*william2 - 0.955029600171170*william2^2 -
0.613473476443475*chris2 + 0.817179944585997*william2]
[0.692008619961593*chris2^2 - 0.666991104945709*chris2*william2 -
0.0950703419495393*william2^2 + 0.261926742846123*chris2 -
0.801579599176361*william2                  0.143659682615950*chris2^2 -
0.221007018170083*william2^2 - 0.162863097602838*chris2 -
0.617597598526627*william2 - 0.553651224948270]
[         -0.821876689273126*chris2^2 - 0.337570915811116*chris2*william2 - 0.445009814043026*william2^2 - 0.933733757961343*chris2 - 0.328938818266539 -0.901307772747002*chris2^2 - 0.343913151855248*chris2*william2 - 0.955029600171170*william2^2 - 0.613473476443475*chris2 + 0.817179944585997*william2]
[0.692008619961593*chris2^2 - 0.666991104945709*chris2*william2 - 0.0950703419495393*william2^2 + 0.261926742846123*chris2 - 0.801579599176361*william2                  0.143659682615950*chris2^2 - 0.221007018170083*william2^2 - 0.162863097602838*chris2 - 0.617597598526627*william2 - 0.553651224948270]
(chris2 + william2)^3 
       
chris2^3 + 3.00000000000000*chris2^2*william2 +
3.00000000000000*chris2*william2^2 + william2^3
chris2^3 + 3.00000000000000*chris2^2*william2 + 3.00000000000000*chris2*william2^2 + william2^3
S = RR['chris, william']; S 
       
Multivariate Polynomial Ring in chris, william over Real Field with 53
bits of precision
Multivariate Polynomial Ring in chris, william over Real Field with 53 bits of precision
S.0 
       
chris
chris
S.1 
       
william
william
S.gens() 
       
(chris, william)
(chris, william)
S.gen(0) 
       
chris
chris
chris 
       
Traceback (click to the left of this block for traceback)
...
NameError: name 'chris' is not defined
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_92.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("Y2hyaXM="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmp1kumAa/___code___.py", line 2, in <module>
    exec compile(u'chris
  File "", line 1, in <module>
    
NameError: name 'chris' is not defined
A = matrix(GF(7), 3, 2, [1,2,3]*2); A 
       
[1 2]
[3 1]
[2 3]
[1 2]
[3 1]
[2 3]
A.base_ring() 
       
Finite Field of size 7
Finite Field of size 7
A = matrix(8, [1..8^2]) A 
       
[ 1  2  3  4  5  6  7  8]
[ 9 10 11 12 13 14 15 16]
[17 18 19 20 21 22 23 24]
[25 26 27 28 29 30 31 32]
[33 34 35 36 37 38 39 40]
[41 42 43 44 45 46 47 48]
[49 50 51 52 53 54 55 56]
[57 58 59 60 61 62 63 64]
[ 1  2  3  4  5  6  7  8]
[ 9 10 11 12 13 14 15 16]
[17 18 19 20 21 22 23 24]
[25 26 27 28 29 30 31 32]
[33 34 35 36 37 38 39 40]
[41 42 43 44 45 46 47 48]
[49 50 51 52 53 54 55 56]
[57 58 59 60 61 62 63 64]
A.base_ring() 
       
Integer Ring
Integer Ring
v = A[1]; type(v) 
       
<type 'sage.modules.vector_integer_dense.Vector_integer_dense'>
<type 'sage.modules.vector_integer_dense.Vector_integer_dense'>
A.column(1) 
       
(2, 2, 1)
(2, 2, 1)
A.row(1) 
       
(4/5, 2, 7)
(4/5, 2, 7)
v.dot_product(v) 
       
1341/25
1341/25
3.5 * v 
       
(2.80000000000000, 7.00000000000000, 24.5000000000000)
(2.80000000000000, 7.00000000000000, 24.5000000000000)
       
[  1   2   3]
[4/5   2   7]
[  1   1   1]
[  1   2   3]
[4/5   2   7]
[  1   1   1]
       
[ 1  2  3  4  5  6  7  8]
[ 9 10 11 12 13 14 15 16]
[17 18 19 20 21 22 23 24]
[25 26 27 28 29 30 31 32]
[33 34 35 36 37 38 39 40]
[41 42 43 44 45 46 47 48]
[49 50 51 52 53 54 55 56]
[57 58 59 60 61 62 63 64]
[ 1  2  3  4  5  6  7  8]
[ 9 10 11 12 13 14 15 16]
[17 18 19 20 21 22 23 24]
[25 26 27 28 29 30 31 32]
[33 34 35 36 37 38 39 40]
[41 42 43 44 45 46 47 48]
[49 50 51 52 53 54 55 56]
[57 58 59 60 61 62 63 64]
A[:,1::2] 
       
[ 2  4  6  8]
[10 12 14 16]
[18 20 22 24]
[26 28 30 32]
[34 36 38 40]
[42 44 46 48]
[50 52 54 56]
[58 60 62 64]
[ 2  4  6  8]
[10 12 14 16]
[18 20 22 24]
[26 28 30 32]
[34 36 38 40]
[42 44 46 48]
[50 52 54 56]
[58 60 62 64]
A[0,1] 
       
2
2
A[0,1] = 2012 
       
A.set_immutable() 
       
0,1,2 
       
(0, 1, 2)
(0, 1, 2)
A[0,1] = 92090834 
       
Traceback (click to the left of this block for traceback)
...
ValueError: matrix is immutable; please change a copy instead (i.e., use
copy(M) to change a copy of M).
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_142.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("QVswLDFdID0gOTIwOTA4MzQ="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpHwLvwE/___code___.py", line 3, in <module>
    exec compile(u'A[_sage_const_0 ,_sage_const_1 ] = _sage_const_92090834 
  File "", line 1, in <module>
    
  File "matrix0.pyx", line 1339, in sage.matrix.matrix0.Matrix.__setitem__ (sage/matrix/matrix0.c:5846)
  File "matrix0.pyx", line 404, in sage.matrix.matrix0.Matrix.check_mutability (sage/matrix/matrix0.c:3884)
ValueError: matrix is immutable; please change a copy instead (i.e., use copy(M) to change a copy of M).
v = A[0]; v 
       
(1, 2012, 3, 4, 5, 6, 7, 8)
(1, 2012, 3, 4, 5, 6, 7, 8)
v[1] = 193939 
       
Traceback (click to the left of this block for traceback)
...
ValueError: vector is immutable; please change a copy instead (use
copy())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_139.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("dlsxXSA9IDE5MzkzOQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmpv4ZPc_/___code___.py", line 3, in <module>
    exec compile(u'v[_sage_const_1 ] = _sage_const_193939 
  File "", line 1, in <module>
    
  File "vector_integer_dense.pyx", line 157, in sage.modules.vector_integer_dense.Vector_integer_dense.__setitem__ (sage/modules/vector_integer_dense.c:3008)
ValueError: vector is immutable; please change a copy instead (use copy())
(A[0])[1] = 193939 
       
Traceback (click to the left of this block for traceback)
...
ValueError: vector is immutable; please change a copy instead (use
copy())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_136.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("QVswXVsxXSA9IDE5MzkzOQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/private/var/folders/4z/h5m3q9b94zs641jlbxj79sv00000gn/T/tmp3B0USk/___code___.py", line 3, in <module>
    exec compile(u'A[_sage_const_0 ][_sage_const_1 ] = _sage_const_193939 
  File "", line 1, in <module>
    
  File "vector_integer_dense.pyx", line 157, in sage.modules.vector_integer_dense.Vector_integer_dense.__setitem__ (sage/modules/vector_integer_dense.c:3008)
ValueError: vector is immutable; please change a copy instead (use copy())
       
[   1 2012    3    4    5    6    7    8]
[   9   10   11   12   13   14   15   16]
[  17   18   19   20   21   22   23   24]
[  25   26   27   28   29   30   31   32]
[  33   34   35   36   37   38   39   40]
[  41   42   43   44   45   46   47   48]
[  49   50   51   52   53   54   55   56]
[  57   58   59   60   61   62   63   64]
[   1 2012    3    4    5    6    7    8]
[   9   10   11   12   13   14   15   16]
[  17   18   19   20   21   22   23   24]
[  25   26   27   28   29   30   31   32]
[  33   34   35   36   37   38   39   40]
[  41   42   43   44   45   46   47   48]
[  49   50   51   52   53   54   55   56]
[  57   58   59   60   61   62   63   64]
A*A 
       
[10061/2515  6039/2012 12079/2012]
[      47/5 27666/2515      117/5]
[      14/5  6037/2012         11]
[10061/2515  6039/2012 12079/2012]
[      47/5 27666/2515      117/5]
[      14/5  6037/2012         11]
A. 
       
A.echelon_form() 
       
[1 0 0]
[0 1 0]
[0 0 1]
[1 0 0]
[0 1 0]
[0 0 1]
show(A) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} 1 & \frac{1}{2012} & 3 \\ \frac{4}{5} & 2 & 7 \\ 1 & 1 & 1 \end{array}\right)
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} 1 & \frac{1}{2012} & 3 \\ \frac{4}{5} & 2 & 7 \\ 1 & 1 & 1 \end{array}\right)
latex(A) 
       
\left(\begin{array}{rrr}
1 & \frac{1}{2012} & 3 \\
\frac{4}{5} & 2 & 7 \\
1 & 1 & 1
\end{array}\right)
\left(\begin{array}{rrr}
1 & \frac{1}{2012} & 3 \\
\frac{4}{5} & 2 & 7 \\
1 & 1 & 1
\end{array}\right)
A.charpoly() 
       
x^3 - 4*x^2 - 12576/2515*x + 17297/2012
x^3 - 4*x^2 - 12576/2515*x + 17297/2012
A.det() 
       
-17297/2012
-17297/2012
A.solve_right(vector([1,2,3])) 
       
(34185/17297, 116696/86485, -28166/86485)
(34185/17297, 116696/86485, -28166/86485)
A.solve_left(vector([1,2,3])) 
       
(-38228/86485, 8046/17297, 92529/86485)
(-38228/86485, 8046/17297, 92529/86485)
show(A.transpose()) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} 1 & \frac{4}{5} & 1 \\ \frac{1}{2012} & 2 & 1 \\ 3 & 7 & 1 \end{array}\right)
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} 1 & \frac{4}{5} & 1 \\ \frac{1}{2012} & 2 & 1 \\ 3 & 7 & 1 \end{array}\right)

Very powerful!

A = random_matrix(ZZ, 500) time v = A.solve_right(vector([1..500])) 
       
Time: CPU 2.68 s, Wall: 1.86 s
Time: CPU 2.68 s, Wall: 1.86 s
len(str(v)) 
       
1340022
1340022
 
       

Explain some of the examples in the docstring below:

matrix?