Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132939 views
License: OTHER
1
<?xml version="1.0" encoding="UTF-8" ?>
2
3
<!-- Sage and Linear Algebra Worksheets -->
4
<!-- Robert A. Beezer -->
5
<!-- Copyright 2017-2019 License: CC BY-SA -->
6
<!-- See COPYING for more information -->
7
8
<pretext xmlns:xi="http://www.w3.org/2001/XInclude">
9
10
<xi:include href="../worksheetinfo.xml" />
11
12
<article xml:id="EE">
13
<title>Sage and Linear Algebra Worksheet</title>
14
<subtitle>FCLA Section EE</subtitle>
15
16
<!-- header inclusion needs -xinclude switch on xsltproc -->
17
<frontmatter>
18
<xi:include href="../header.xml" />
19
</frontmatter>
20
21
<section>
22
<title>Eigenvalues and Eigenvectors</title>
23
24
<p>A <m>6\times 6</m> matrix with <q>nice</q> eigenvalues.</p>
25
26
<sage><input>
27
A = matrix(QQ, [
28
[-31, -23, -16, 12, 120, -17],
29
[ -3, 7, 0, -12, 60, -21],
30
[-28, -14, -9, -4, 152, -30],
31
[-36, -20, -16, -1, 192, -32],
32
[ -9, -5, -4, 0, 47, -8],
33
[ -1, 1, 0, -4, 20, -3]
34
])
35
A
36
</input></sage>
37
38
39
<sage><input>
40
p = A.characteristic_polynomial()
41
p
42
</input></sage>
43
44
45
<sage><input>
46
p.factor()
47
</input></sage>
48
49
<p>Eigenvalues are the roots of the characteristic polynomial (Theorem EMRCP), which should be obvious from the factored version, including their (algebraic) multiplicities. Of course, it can be very easy to get these in Sage.</p>
50
51
<sage><input>
52
A.eigenvalues()
53
</input></sage>
54
55
<exercise>
56
<statement>
57
<p>Create the singular matrices <m>A-\lambda I_6</m> for each eigenvalue (we will choose to do two with <q>random</q> choices for the eigenvalue). Row-reducing these matrices will exhibit their nonzero nullity.</p>
58
59
</statement>
60
</exercise>
61
62
<sage><input>
63
(A-( )*identity_matrix(6)).rref()
64
</input></sage>
65
66
<sage><input>
67
(A-( )*identity_matrix(6)).rref()
68
</input></sage>
69
70
71
<exercise>
72
<statement>
73
<p>Examine the eigenspace for the eigenvalue <m>\lambda=3</m>, using Sage's right kernel command and the pivot basis.</p>
74
75
<p>Use a basis for the eigenspace to create eigenvectors of <c>A</c> for the eigenvalue <m>\lambda=3</m> at will.</p>
76
</statement>
77
</exercise>
78
79
<sage><input>
80
E6 = (A-3*identity_matrix(6)).right_kernel(basis='pivot')
81
E6
82
</input></sage>
83
84
85
<sage><input>
86
B = E6.basis()
87
B
88
</input></sage>
89
90
<sage><input>
91
v = *B[0] + *B[1]
92
v
93
</input></sage>
94
95
<p>We can check this. Compare <m>Av</m> with <m>3v</m>.</p>
96
97
<sage><input>
98
A*v
99
</input></sage>
100
101
<sage><input>
102
3*v
103
</input></sage>
104
105
<p>Here's an easy check:</p>
106
107
<sage><input>
108
A*v - 3*v
109
</input></sage>
110
111
<p>Can you make more eigenvectors?</p>
112
</section>
113
114
<section>
115
<title>Eigenspaces, Eigenmatrices</title>
116
117
<p>Continuing with <c>A</c> from above, we can get eigen-stuff quickly from Sage, once we understand what is really happening (according to the definitions).</p>
118
119
<p>As always we want the <q>right</q> versions of the relevant commands. Eigenspaces are in the second parts of pairs, where the first part of each pair is the eigenvalue. Notice that they are vector spaces (with bases, etc). The basis vectors are Sage's version of a basis, with vectors from an echelonized matrix, typically with lots of zeros and ones in the first part of the vectors.</p>
120
121
<sage><input>
122
A.eigenspaces_right()
123
</input></sage>
124
125
<p>The <c>eigenmatrix</c> commands return pair of matrices. The first is a diagonal matrix with the eigenvalues on the diagonal. The second is a square matrix with linearly independent eigenvectors in the columns, and the order of the eigenvectors is the same as the order of the eigenvalues. That is, the eigenvector in column <c>i</c> of the second matrix is a basis vector for the eigenspace of the eigenvalue in column <c>i</c> of the first matrix.</p>
126
127
<sage><input>
128
A.eigenmatrix_right()
129
</input></sage>
130
131
<p>Sometimes the dimension of an eigenspace (the geometric multiplicity) is strictly less than the number of times the eigenvalue appears as a root of the characteristic polynomial. This is the case with <c>C</c> next, but was not the case with <c>A</c> above.</p>
132
133
<sage><input>
134
C = matrix(QQ, [
135
[128, 20, 44, -50, 236, -18, -330, -565],
136
[ -23, -16, -5, 6, -40, 27, 62, 128],
137
[ -44, -12, -15, 16, -78, 20, 110, 207],
138
[ -2, 10, -4, 3, -10, -23, 20, -9],
139
[ -61, 5, -25, 27, -116, -26, 153, 225],
140
[ -12, -12, -1, 2, -20, 24, 34, 82],
141
[ -23, -3, -8, 9, -42, 2, 57, 99],
142
[ 13, 6, 3, -4, 23, -12, -35, -72]
143
])
144
C
145
</input></sage>
146
147
<sage><input>
148
C.eigenmatrix_right()
149
</input></sage>
150
151
</section>
152
153
<section>
154
<title>Fancy Footwork</title>
155
156
<p>A totally random matrix is unlikely to have a characteristic polynomial that factors if we restrict ourseves to the rationals. But we can find all the roots over <m>\overline{Q}</m>, the set of all algebraic numbers. (This is the set of all real roots of all possible polynomials with integer coefficients.)</p>
157
158
<sage><input>
159
D = random_matrix(QQ, 10)
160
D
161
</input></sage>
162
163
<sage><input>
164
p = D.characteristic_polynomial()
165
p.factor()
166
</input></sage>
167
168
<sage><input>
169
p.roots(ring=QQbar, multiplicities=False)
170
</input></sage>
171
172
<p>If we make a <q>block diagonal</q> matrix, then the characteristic polynomial will definitely factor some</p>
173
174
<sage><input>
175
E = block_diagonal_matrix( [random_matrix(QQ, 5), random_matrix(QQ, 5)])
176
E
177
</input></sage>
178
179
<sage><input>
180
p = E.charpoly()
181
p.factor()
182
</input></sage>
183
184
<p>Finally a large example, illustrating how fast Sage is at making characteristic polynomials and at factoring.</p>
185
186
<sage><input>
187
F = block_diagonal_matrix( [random_matrix(QQ, 50), random_matrix(QQ, 50)])
188
p = F.charpoly()
189
p.factor()
190
</input></sage>
191
192
<p>This is such a common operation, that Sage has a shorthand method for the <term>Factored Characteristic Polynomial</term>, namely <c>.fcp()</c>.</p>
193
194
<sage><input>
195
F.fcp()
196
</input></sage>
197
198
<p>Nothing short of amazing!</p>
199
200
</section>
201
202
<section>
203
<title>Numerical Matrices</title>
204
205
<p>If we use <c>CDF</c> (Complex Double Field) for the number system of the entries of our matrix, we get (good) approximate values for eigenvalues. (If we are OK with the approximate nature, these routines are very, very fast.)</p>
206
207
<sage><input>
208
G = random_matrix(QQ, 300)
209
H = G.change_ring(CDF)
210
H.eigenvalues()
211
</input></sage>
212
213
</section>
214
215
<xi:include href="../legal.xml" />
216
217
</article>
218
</pretext>
219
220