Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132936 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="B">
13
<title>Sage and Linear Algebra Worksheet</title>
14
<subtitle>FCLA Section B</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>Bases</title>
23
24
<p>Five <q>random</q> vectors, each with 4 entries, collected into a set <c>S</c>.</p>
25
26
<sage><input>
27
v1 = vector(QQ, [-4, -2, 3, -11])
28
v2 = vector(QQ, [-2, 7, 3, 9])
29
v3 = vector(QQ, [ 6, -4, -7, 5])
30
v4 = vector(QQ, [-1, 0, 3, -4])
31
v5 = vector(QQ, [-4, 5, -5, 11])
32
S = [v1, v2, v3, v4, v5]
33
</input></sage>
34
35
<p>Consider the subspace spanned by these five vectors. We will make these vectors the <em>rows</em> of a matrix and row-reduce to see a basis for the space (subspace, or row space, take your pick). This is an application of Theorem BRS.</p>
36
37
<sage><input>
38
A = matrix(S)
39
A
40
</input></sage>
41
42
<sage><input>A.rref()</input></sage>
43
44
<p>Sage does this semi-automatically, tossing zero rows for us.</p>
45
46
<sage><input>
47
W = span(S)
48
B = W.basis()
49
B
50
</input></sage>
51
52
<exercise>
53
<statement>
54
<p>Construct a <em>random</em> vector, <c>w</c>, in this subspace by choosing scalars for a linear combination of the vectors we used to build <c>W</c> as a span originally.</p>
55
56
<p>Then use the three <em>basis</em> vectors in <c>B</c> to recreate the vector <c>w</c>. Question: how many ways can you do this? By Theorem VRRB there should always be exactly one way to create <c>w</c> using a linear combination of a basis of <c>W</c>.</p>
57
</statement>
58
</exercise>
59
60
<sage><input>
61
w = *v1 + *v2 + *v3 + *v4 + *v5
62
w
63
</input></sage>
64
65
<sage><input>w in W</input></sage>
66
67
<sage><input>*B[0] + *B[1] + *B[2]</input></sage>
68
</section>
69
70
<section>
71
<title>Nonsingular Matrices</title>
72
73
<p>We will obtain a basis of <m>\mathbb{C}^{10}</m> from the columns of a <m>10\times 10</m> nonsingular matrix.</p>
74
75
<sage><input>
76
entries = [[ 1, 1, 1, -1, -2, 4, 2, -3, 1, -6],
77
[-2, -1, -2, 2, 4, -7, -4, 5, -1, 7],
78
[ 1, -1, 2, -2, -5, 8, 5, -3, 4, -4],
79
[-1, -2, 0, 1, 0, -5, 0, -3, -5, 6],
80
[ 0, -2, 1, -1, -2, 3, 2, 3, 3, 7],
81
[ 1, 0, 1, -1, -2, 4, 2, 0, 2, 0],
82
[-1, 0, -1, 1, 3, -1, -2, 7, 5, 1],
83
[ 1, 1, 1, -1, -2, 8, 3, 2, 8, -6],
84
[ 0, 2, -1, 1, 2, -1, -2, 2, 2, -6],
85
[ 1, 3, 0, 0, 1, 3, 0, 0, 3, -8]]
86
M = matrix(QQ, entries)
87
M
88
</input></sage>
89
90
<sage><input>
91
not M.is_singular()
92
</input></sage>
93
94
<p>A totally random vector with 10 entries:</p>
95
96
<sage><input>
97
v = random_vector(ZZ, 10, x=-9, y=9)
98
v
99
</input></sage>
100
101
<exercise>
102
<statement>
103
<p>By Theorem CNMB, the columns of the matrix are a basis of <m>\mathbb{C}^{10}</m>. So the vector <c>v</c> should be a linear combination of the columns of the matrix. Verify this fact in three ways.<ol>
104
<li>First, the old-fashioned way, thus exposing Theorem NMUS.</li>
105
106
<li>Then, the modern way, with an inverse, since a nonsingular matrix is invertible, thus exposing Theorem SNCM.</li>
107
108
<li>Finally, the Sage way, as described below.</li>
109
</ol></p>
110
</statement>
111
</exercise>
112
113
<sage><input>
114
aug = M.augment(v)
115
aug.rref()
116
</input></sage>
117
118
<sage><input>M.inverse()*v</input></sage>
119
120
<p>The Sage way: first create a space with a <term>user basis</term>.</p>
121
122
<sage><input>
123
X = (QQ^10).subspace_with_basis(M.columns())
124
X
125
</input></sage>
126
127
<p>Sage still carries an <term>echelonized basis</term>, in addition to the <term>user-installed</term> basis.</p>
128
129
<sage><input>
130
X.basis()
131
</input></sage>
132
133
<sage><input>
134
X.echelonized_basis()
135
</input></sage>
136
137
<p>Now ask for a coordinatization, relative to the basis in <c>X</c>, thus exposing Theorem VRRB.</p>
138
139
<sage><input>
140
X.coordinates(v)
141
</input></sage>
142
</section>
143
144
<section>
145
<title>Orthonormal Bases</title>
146
147
<p>A particularly simple orthonormal basis of <m>\mathbb{C}^3</m>, collected into the set <c>S</c>.</p>
148
149
<sage><input>
150
v1 = vector(QQ, [1/3, 2/3, 2/3])
151
v2 = vector(QQ, [2/3, -2/3, 1/3])
152
v3 = vector(QQ, [2/3, 1/3, -2/3])
153
S = [v1, v2, v3]
154
</input></sage>
155
156
<exercise>
157
<statement>
158
<p>If these vectors are an orthonormal basis, then as the columns of a matrix they should create an orthonormal basis.</p>
159
</statement>
160
</exercise>
161
162
<sage><input>
163
Q = column_matrix(S)
164
Q
165
</input></sage>
166
167
<sage><input>Q.conjugate_transpose()*Q</input></sage>
168
169
<sage><input>Q.is_unitary()</input></sage>
170
171
<exercise>
172
<statement>
173
<p>Build a random vector of size <m>3</m> and find our ways to express the vector as a (unique) linear combination of the basis vectors. Which method is most efficient?</p>
174
</statement>
175
</exercise>
176
177
<p>A totally random vector with 3 entries.</p>
178
179
<sage><input>
180
v = random_vector(ZZ, 3, x=-9, y=9)
181
v
182
</input></sage>
183
184
<p>First, the old-fashioned way, thus exposing Theorem NMUS.</p>
185
186
<sage><input>
187
aug = Q.augment(v)
188
aug.rref()
189
</input></sage>
190
191
<p>Now, the modern way, with an inverse, since a nonsingular matrix is invertible, thus exposing Theorem SNCM.</p>
192
193
<sage><input>Q.inverse()*v</input></sage>
194
195
<p>The Sage way. Create a space with a <q>user basis</q> and ask for a coordinatization, thus exposing Theorem VRRB.</p>
196
197
<sage><input>
198
X = (QQ^3).subspace_with_basis(Q.columns())
199
X.coordinates(v)
200
</input></sage>
201
202
<p>Finally, exploiting the orthonormal basis, and computing scalars for the linear combination with an inner product, thus exposing Theorem COB. (Sage's <c>.inner_product()</c> does not conjugate the entries of either vector, so we use the more careful <c>.hermitian_inner_product()</c> vector method instead.)</p>
203
204
<sage><input>
205
a1 = v1.hermitian_inner_product(v)
206
a2 = v2.hermitian_inner_product(v)
207
a3 = v3.hermitian_inner_product(v)
208
a1, a2, a3
209
</input></sage>
210
</section>
211
212
<xi:include href="../legal.xml" />
213
214
</article>
215
</pretext>
216
217