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="LT">
13
<title>Sage and Linear Algebra Worksheet</title>
14
<subtitle>FCLA Section LT</subtitle>
15
16
<!-- header inclusion needs -xinclude switch on xsltproc -->
17
<frontmatter>
18
<xi:include href="../header.xml" />
19
</frontmatter>
20
21
<introduction>
22
<p>Sage has very capable linear transformations from <m>\mathbb{Q}^n</m> to <m>\mathbb{Q}^m</m>.</p>
23
</introduction>
24
25
<section>
26
<title>Creation via Symbolic Functions</title>
27
28
<p>Start with a symbolic function.</p>
29
30
<!-- [[1, 2, 1, 5], [1, 5, 4, 8], [0, -1, -1, -1]] -->
31
32
<sage><input>
33
var('x1 x2 x3 x4')
34
f(x1, x2, x3, x4) = (x1 + 2*x2 + x3 + 5*x4, x1 + 5*x2 + 4*x3 + 8*x4, -x2 - x3 - x4)
35
</input></sage>
36
37
<p>Then specify the domain and codomain. We need to be careful about how <c>T</c> prints, Sage likes rows.</p>
38
39
<sage><input>
40
T = linear_transformation(QQ^4, QQ^3, f)
41
T
42
</input></sage>
43
44
<p>At a most basic level, <c>T</c> behaves as a function.</p>
45
46
<sage><input>
47
u = random_vector(ZZ, 4, x=-9, y=9).change_ring(QQ)
48
u, T(u)
49
</input></sage>
50
51
<p>We can check Theorem LTTZZ, zero goes to zero.</p>
52
53
<sage><input>
54
z4 = zero_vector(QQ, 4)
55
z3 = zero_vector(QQ, 3)
56
z4, T(z4), T(z4) == z3
57
</input></sage>
58
59
</section>
60
61
<section>
62
<title>Creation via Matrices</title>
63
64
<p>We can also create a linear transformation from a matrix, as in Theorem MBLT, with one caveat. For a matrix <m>A</m>, the default is to create the function <m>T(\mathbf{v})=\mathbf{v}A</m>. The keywords option <c>side='right'</c> will indicate that we want to put the vector on the right of the matrix.</p>
65
66
<sage><input>
67
A = matrix(QQ, [[1, 2, 1, 5], [1, 5, 4, 8], [0, -1, -1, -1]])
68
S = linear_transformation(A, side='right')
69
</input></sage>
70
71
<p>Notice that we do not have to specify the domain or codomain, these are inferred from the size and type of the matrix. <c>S</c> is not new, it is exactly the linear transformation <c>T</c> above.</p>
72
73
<sage><input>
74
S == T
75
</input></sage>
76
77
<p>Again, notice how <c>S</c> prints <mdash /> the matrix representation is the transpose of what we like to use. This does not <em>change</em> the linear transformation as a function, it just changes how we think about it (we like linear combinations of columns, Sage likes linear combinations of rows).</p>
78
79
<sage><input>
80
A, S
81
</input></sage>
82
83
</section>
84
85
<section>
86
<title>Creation via Values on a Basis</title>
87
88
<p>Starting with a domain and a codomain, we can provide a list of the images of basis vectors for the domain.</p>
89
90
<sage><input>
91
v1 = vector(QQ, [1, 1, 0])
92
v2 = vector(QQ, [2, 5, -1])
93
v3 = vector(QQ, [1, 4, -1])
94
v4 = vector(QQ, [5, 8, -1])
95
R = linear_transformation(QQ^4, QQ^3, [v1, v2, v3, v4])
96
</input></sage>
97
98
<p>That's right <mdash /> same function again.</p>
99
100
<sage><input>
101
R == T
102
</input></sage>
103
104
<p>We can check how this construction works.</p>
105
106
<sage><input>
107
d3 = R.domain().basis()[2]
108
R(d3); R(d3) == v3
109
</input></sage>
110
111
<p>We can give the domain an alternate basis and create a different linear transformation, despite seemingly having the same construction. First we build the domain with a different user basis.</p>
112
113
<sage><input>
114
u1 = vector(QQ, [1, 0, 0, 0])
115
u2 = vector(QQ, [1, 1, 0, 0])
116
u3 = vector(QQ, [1, 1, 1, 0])
117
u4 = vector(QQ, [1, 1, 1, 1])
118
dom4 = (QQ^4).subspace_with_basis([u1, u2, u3, u4])
119
dom4
120
</input></sage>
121
122
<sage><input>
123
L = linear_transformation(dom4, QQ^3, [v1, v2, v3, v4])
124
L
125
</input></sage>
126
127
<p>Even though the matrix representation prints the same, this is not the same function, we will need ideas from Chapter R before we can understand the difference.</p>
128
129
<sage><input>
130
R(u3), L(u3)
131
</input></sage>
132
133
<p>This code should consistently return <c>False</c>.</p>
134
135
<sage><input>
136
v = random_vector(QQ, 4)
137
R(v) == L(v)
138
</input></sage>
139
140
</section>
141
142
<section>
143
<title>Basic Properties</title>
144
145
<p>Illustrations with <c>T</c>.</p>
146
147
<sage><input>
148
T.domain()
149
</input></sage>
150
151
152
<sage><input>
153
T.codomain()
154
</input></sage>
155
156
<p>A defining property, so always <c>True</c>.</p>
157
158
<sage><input>
159
u = random_vector(QQ, 4)
160
v = random_vector(QQ, 4)
161
u, v, T(u+v) == T(u) + T(v)
162
</input></sage>
163
164
<p>A defining property, so also always <c>True</c>.</p>
165
166
<sage><input>
167
alpha = (QQ).random_element()
168
u = random_vector(QQ, 4)
169
alpha, u, T(alpha*u) == alpha*T(u)
170
</input></sage>
171
172
<p>We can do <q>arithmetic</q> with linear transformations, though the addition seems to allow bad things to happen.</p>
173
174
<sage><input>
175
R+S
176
</input></sage>
177
178
<p>Scalar multiples also, and they are well-behaved.</p>
179
180
<sage><input>
181
12*T
182
</input></sage>
183
184
<p>The following is wrong. (In other words, there is a bug in Sage.)</p>
185
186
<sage><input>
187
P = R + L
188
P
189
</input></sage>
190
191
<p>As we can see<ellipsis /></p>
192
193
<sage><input>
194
Q = L + R
195
Q.is_equal_function(P)
196
</input></sage>
197
198
<p>The problem is that Sage is simply adding the matrices representing the linear transformations, without checking that they are defined using domains with the <em>same</em> basis. We will understand the subtlety better in Chapter R.</p>
199
200
</section>
201
202
<xi:include href="../legal.xml" />
203
204
</article>
205
</pretext>
206
207