http://localhost:8000/home/admin/21/print
A is a 3 3 nonsingular matrix.
1 of 14
06/13/2011 11:13 AM
http://localhost:8000/home/admin/21/print
b is a 3-slot vector.
A = matrix([[2,1,1/3],[-1,6,2],[1/2,1,8]])
A
2
B
@ 1
1
3
1
6
1
1
2
C
2A
8
b = vector([14/3, 2, -6])
b
( 14
3 ; 2; 6)
Solve Ax = b.
Solve commands ("right" is location of solution vector).
A.solve_right(b)
(2; 1; 1)
print A.det()
print A.det() == 0
299/3
False
A.inverse()
B
@
6
13
27
299
12
299
1
13
95
598
9
598
1 C
23
A
3
23
(2; 1; 1)
Can "divide" by a matrix
b/A
770
( 299
;
44
20
897 ; 23 )
b/A.transpose()
2 of 14
06/13/2011 11:13 AM
http://localhost:8000/home/admin/21/print
(2; 1; 1)
Augment and row-reduce
R = A.augment(matrix(b).transpose())
R
1
3
1
6
1
2
B
@ 1
1
2
14
3
2
8
R.echelon_form()
1
@0
0
0
1
0
0
0
1
C
2A
6
1
2
1A
1
Properties of Matrices
B is a 6 5 matrix of rank 4 over the rationals Q
B = matrix(QQ,[
[10,0,3,8,7],
[-16,-1,-4,-10,-13],
[-6,1,-3,-6,-6],
[0,2,-2,-3,-2],
[3,0,1,2,3],
[-1,-1,1,1,0]
])
B
10
B
B 16
B
B 6
B
B
0
B
B
@
3
1
0
1
1
2
0
1
3
4
3
2
1
1
8
10
6
3
2
1
B.right_kernel()
RowSpanQ 1
23
1
2
B.right_kernel().basis()
3 of 14
1
7
C
13 C
C
6 C
C
2 C
C
C
3A
0
1
21
06/13/2011 11:13 AM
http://localhost:8000/home/admin/21/print
[(1; 23 ; 21 ; 1; 21 )]
B.left_kernel().basis()
B.row_space()
1 0 0 0
B
B0 1 0 0
RowSpanQ B
@0 0 1 0
0 0 0 1
B.column_space()
1 0 0 0
B
B0 1 0 0
RowSpanQ B
@0 0 1 0
0 0 0 1
print
print
print
print
print
print
print
1
2
C
3 C
C
1A
2
41
41
41
0
41
3
4
49
C
C
C
A
"Rank", B.rank()
"Right Nullity", B.right_nullity()
"Columns", B.ncols()
"Rank", B.rank()
"Left Nullity", B.left_nullity()
"Rows", B.nrows()
Rank 4
Right Nullity 1
Columns 5
Rank 4
Left Nullity 2
Rows 6
4 of 14
06/13/2011 11:13 AM
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
Linear Algebra in
BSage -- Sage
http://localhost:8000/home/admin/21/print
7
8
73
4
3
2
5
10
89
2
3
4
5
7
3
5
25
( 52 ; 4;
3
5
2
5
45
45
10
2
31
6
1
1
94
1
5
4
1
7
2
9
21
1
4
4
1
8
25
1
35
2
3 1
10 ; 5 ;
3
59
53
65
3
8
8
4
9
5
53
32
9
10
8
25
9
1
34
5
0
9
2
7
65
5
1
4
3
1
3
4
1
2
7
2
10
9
7
8
4
2
2
59
43
1
25
8
7
10
5
1
1
6
1
5
3
1
51
5
87
1
0
4
5
5
2
2
2
2
5
10
4; 51 ; 23 ; 54 ; 61 ; 81 ; 0; 10; 0; 0; 76 ; 1; 2; 25 ; 6; 1;
5 of 14
06/13/2011 11:13 AM
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
B
@
8
9
7
4
9
2
3
8
1
3
1
4
http://localhost:8000/home/admin/21/print
3
5
4
5
8
2
10
41
67
1
2
1
3
1
2
98
32
61
45
0
3
0
89
25
2
71
41
4
7
91
1
3
1
4
21
95
47
41
9
25
1
9
2
3
7
9
5
2
10
7
1
21
10
1
2
2
5
1
5
2
7
2
10
7
0
21
8
5
5
6
34
76
0
6
56
1
1
51
9
10
9
7
6
31
58
1
4
7
3
5
1
4
4
5
91
1
1
5
1
2
3
2
10
0
7
10
2
0
1
76
1
7
6
51
0
0
0
2
7
21
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
A
C.det()
187558143411975607125910504689634916293080048459801797630721278449241962022353955141284605117745
668930993248348814342449533378685145998341315304441572331718197554296221857483527
C.trace()
15937
1260
C.norm()
40:7778652883
EigenStu
D is a 4 4 matrix, not diagonalizable
D = matrix(QQ, [
[-2,1,-2,-4],
[12,1,4,9],
[6,5,-2,-4],
[3,-4,5,10]
])
show(D)
2
B
B 12
B
@ 6
3
6 of 14
1
1
5
4
2
4
2
5
1
4
C
9C
C
4 A
10
06/13/2011 11:13 AM
http://localhost:8000/home/admin/21/print
1
-t + 1
5
-4
-2
-4]
4
9]
-t - 2
-4]
5 -t + 10]
t4 7 t3 + 18 t2 20 t + 8
p.find_root(-10, 10)
1:9999948114
show(p.factor())
(t 2)3 (t 1)
Easier, but less instructive: : :
q(t) = D.charpoly('t')
q
D.eigenvalues()
[1; 2; 2; 2]
diag,
print
print
print
print
print
evecs = D.eigenmatrix_right()
"Diagonal matrix with eigenvalues"
diag
"Matrix with eigenvectors as columns"
evecs
7 of 14
06/13/2011 11:13 AM
http://localhost:8000/home/admin/21/print
1
0 0 0
0
1C
2 1 0 C
C
@0 2 1AA
0 0 2
1
B0 1
B 0
B
@ @0A
0
J, P = D.jordan_form(transformation = True)
print J
print
print P
[1|0 0 0]
[-+-----]
[0|2 1 0]
[0|0 2 1]
[0|0 0 2]
[ 1
5
[ -3 -10
[ -3
5
[ 0 -10
-8
21
2
11
1]
0]
0]
1]
1
B
B0
B
@0
0
0
2
0
0
1
0 0
C
1 0C
C
2 1A
0 2
8 of 14
06/13/2011 11:13 AM
http://localhost:8000/home/admin/21/print
75
19
130
19
155
19
1
B 3
B
B
@ 3
0
20
29
70
87
50
87
65
87
10
15
14
0C
C
C
5 A
14
5
7
Orthogonal?
Q.transpose()*Q
19
B 0
B
B
@ 0
0
4350
19
0
0
1
0
0C
C
C
0A
0
0
175
87
25
14
Resulting representation?
Q^(-1)*D*Q
1
B
B0
B
@0
0
20
19
2
0
0
1
535
133
698 C
609
C
C
1A
2915
1653
1
2
0
Decompositions
LU, QR, SVD, Jordan Canonical Form, Smith Normal Form, Cholesky
Decomposition, : : :
Convert D to a matrix E over the reals (double precision), R, to obtain QR
decomposition
E = D.change_ring(RDF)
E
2:0
B
B 12:0
B
@ 6:0
3:0
1:0
1:0
5:0
4:0
2:0
4:0
2:0
5:0
1
4:0
C
9:0 C
C
4:0 A
10:0
9 of 14
06/13/2011 11:13 AM
http://localhost:8000/home/admin/21/print
print ortho
print
print "Upper Triangular"
print triangular
print
Orthogonal
[ -0.14396315015 -0.206755085285
0.83647166171 0.486664263392]
[ 0.863778900898
0.11873886424 0.366775046298 -0.324442842262]
[ 0.431889450449 -0.661782341253 -0.372388950068 0.486664263392]
[ 0.215944725225 0.710772502024 -0.164674510583 0.648885684523]
Upper Triangular
[ 13.8924439894
[
-0.0
[
-0.0
[
-0.0
2.0154841021
3.95898662912
8.78175215913]
-6.24001793541
5.76589282015
11.6505245045]
0.0 -0.284437791007 -0.202100535715]
-0.0
-0.0 -0.324442842262]
Checks
(ortho.transpose()*ortho - identity_matrix(4)).norm()
5:57101754397 1016
(ortho*triangular - E).norm()
5:59659361322 1015
Vector Spaces
Sage is so much more than numerical computation.
Can work naturally with vector spaces and modules over a variety of elds and
rings.
F.<a> = FiniteField(3^2)
F
F32
V is a 3-dimensional vector space over F
V=F^3
V
F332
10 of 14
06/13/2011 11:13 AM
http://localhost:8000/home/admin/21/print
V.list()
all_subs[42]
= (32 )2 + (32 )1 + 1
11 of 14
06/13/2011 11:13 AM
http://localhost:8000/home/admin/21/print
Accuracy
Octave and Matlab emphasize numerical results - everything is a oating point
number.
Their rational form is deceptive. Example by William Stein:
octave:1> format rat;
octave:2> a = [-86/17,40/29,-68/43,-20/11;-24/17,-1/38,-2/25,49/17]
a=
-86/17
40/29
-68/43
-20/11
-24/17
-1/38
-2/25
49/17
octave:3> rref(a)
ans =
1
0 155/2122 -725/384
0
1 -152/173 -6553/795
and in Matlab:
>> format rat;
>> a = [-86/17,40/29,-68/43,-20/11;-24/17,-1/38,-2/25,49/17]
a=
-86/17
-24/17
40/29
-1/38
-68/43
-2/25
-20/11
49/17
>> rref(a)
ans =
1
0
0
1
13/178
-152/173
-725/384
-1426/173
Now in Sage:
F = matrix(2,[-86/17, 40/29, -68/43, -20/11, -24/17, -1/38,
-2/25, 49/17])
show(F.echelon_form())
12 of 14
06/13/2011 11:13 AM
http://localhost:8000/home/admin/21/print
Speed
Matrices with symbolic entries
var('x y')
n=6
entries = [x^i-y^j+i+j for i in range(1,n+1) for j in
range(1,n+1)]
G = matrix(SR, n, entries)
G
xy+2
B 2
Bx y+3
B 3
Bx y+4
B
B x4 y + 5
B
B 5
@ x y+6
x6 y + 7
G.det()
y 2 + x + 3
x2 y 2 + 4
x3 y 2 + 5
x4 y 2 + 6
x5 y 2 + 7
x6 y 2 + 8
y 3 + x + 4
x2 y 3 + 5
x3 y 3 + 6
x4 y 3 + 7
x5 y 3 + 8
x6 y 3 + 9
y 4 + x + 5
x2 y 4 + 6
x3 y 4 + 7
x4 y 4 + 8
x5 y 4 + 9
x6 y 4 + 10
y 5 + x + 6
x2 y 5 + 7
x3 y 5 + 8
x4 y 5 + 9
x5 y 5 + 10
x6 y 5 + 11
0
G.det().simplify_full()
0
time G.det()
0
Time: CPU 0.00 s, Wall: 0.00 s
Reals, 53-bit precision
H = random_matrix(RR, 10)
time H.det()
3:07894731367045
13 of 14
06/13/2011 11:13 AM
http://localhost:8000/home/admin/21/print
J = random_matrix(RealField(200), 10)
time J.det()
9:1432891763792026925313110748493443825766112531483542162293
Time: CPU 0.14 s, Wall: 0.14 s
Reals, Interval Arithmetic
K = (1/17.0)*random_matrix(RIF, 10, bound = 10)
time K.det()
Traceback (click to the left of this block for traceback)
...
TypeError: random_element() got an unexpected keyword argument
'bound'
Reals, Double Precision
L = random_matrix(RDF, 300)
timeit("L.det()")
5 loops, best of 3: 61.3 ms per loop
Rationals (Exact)
M = random_matrix(QQ, 800, num_bound = 10, den_bound = 10)
time M.det()
14 of 14
06/13/2011 11:13 AM