📚 The CoCalc Library - books, templates and other resources
License: OTHER
<?xml version="1.0" encoding="UTF-8" ?>12<!-- Sage and Linear Algebra Worksheets -->3<!-- Robert A. Beezer -->4<!-- Copyright 2017-2019 License: CC BY-SA -->5<!-- See COPYING for more information -->67<pretext xmlns:xi="http://www.w3.org/2001/XInclude">89<xi:include href="../worksheetinfo.xml" />1011<article xml:id="LT">12<title>Sage and Linear Algebra Worksheet</title>13<subtitle>FCLA Section LT</subtitle>1415<!-- header inclusion needs -xinclude switch on xsltproc -->16<frontmatter>17<xi:include href="../header.xml" />18</frontmatter>1920<introduction>21<p>Sage has very capable linear transformations from <m>\mathbb{Q}^n</m> to <m>\mathbb{Q}^m</m>.</p>22</introduction>2324<section>25<title>Creation via Symbolic Functions</title>2627<p>Start with a symbolic function.</p>2829<!-- [[1, 2, 1, 5], [1, 5, 4, 8], [0, -1, -1, -1]] -->3031<sage><input>32var('x1 x2 x3 x4')33f(x1, x2, x3, x4) = (x1 + 2*x2 + x3 + 5*x4, x1 + 5*x2 + 4*x3 + 8*x4, -x2 - x3 - x4)34</input></sage>3536<p>Then specify the domain and codomain. We need to be careful about how <c>T</c> prints, Sage likes rows.</p>3738<sage><input>39T = linear_transformation(QQ^4, QQ^3, f)40T41</input></sage>4243<p>At a most basic level, <c>T</c> behaves as a function.</p>4445<sage><input>46u = random_vector(ZZ, 4, x=-9, y=9).change_ring(QQ)47u, T(u)48</input></sage>4950<p>We can check Theorem LTTZZ, zero goes to zero.</p>5152<sage><input>53z4 = zero_vector(QQ, 4)54z3 = zero_vector(QQ, 3)55z4, T(z4), T(z4) == z356</input></sage>5758</section>5960<section>61<title>Creation via Matrices</title>6263<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>6465<sage><input>66A = matrix(QQ, [[1, 2, 1, 5], [1, 5, 4, 8], [0, -1, -1, -1]])67S = linear_transformation(A, side='right')68</input></sage>6970<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>7172<sage><input>73S == T74</input></sage>7576<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>7778<sage><input>79A, S80</input></sage>8182</section>8384<section>85<title>Creation via Values on a Basis</title>8687<p>Starting with a domain and a codomain, we can provide a list of the images of basis vectors for the domain.</p>8889<sage><input>90v1 = vector(QQ, [1, 1, 0])91v2 = vector(QQ, [2, 5, -1])92v3 = vector(QQ, [1, 4, -1])93v4 = vector(QQ, [5, 8, -1])94R = linear_transformation(QQ^4, QQ^3, [v1, v2, v3, v4])95</input></sage>9697<p>That's right <mdash /> same function again.</p>9899<sage><input>100R == T101</input></sage>102103<p>We can check how this construction works.</p>104105<sage><input>106d3 = R.domain().basis()[2]107R(d3); R(d3) == v3108</input></sage>109110<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>111112<sage><input>113u1 = vector(QQ, [1, 0, 0, 0])114u2 = vector(QQ, [1, 1, 0, 0])115u3 = vector(QQ, [1, 1, 1, 0])116u4 = vector(QQ, [1, 1, 1, 1])117dom4 = (QQ^4).subspace_with_basis([u1, u2, u3, u4])118dom4119</input></sage>120121<sage><input>122L = linear_transformation(dom4, QQ^3, [v1, v2, v3, v4])123L124</input></sage>125126<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>127128<sage><input>129R(u3), L(u3)130</input></sage>131132<p>This code should consistently return <c>False</c>.</p>133134<sage><input>135v = random_vector(QQ, 4)136R(v) == L(v)137</input></sage>138139</section>140141<section>142<title>Basic Properties</title>143144<p>Illustrations with <c>T</c>.</p>145146<sage><input>147T.domain()148</input></sage>149150151<sage><input>152T.codomain()153</input></sage>154155<p>A defining property, so always <c>True</c>.</p>156157<sage><input>158u = random_vector(QQ, 4)159v = random_vector(QQ, 4)160u, v, T(u+v) == T(u) + T(v)161</input></sage>162163<p>A defining property, so also always <c>True</c>.</p>164165<sage><input>166alpha = (QQ).random_element()167u = random_vector(QQ, 4)168alpha, u, T(alpha*u) == alpha*T(u)169</input></sage>170171<p>We can do <q>arithmetic</q> with linear transformations, though the addition seems to allow bad things to happen.</p>172173<sage><input>174R+S175</input></sage>176177<p>Scalar multiples also, and they are well-behaved.</p>178179<sage><input>18012*T181</input></sage>182183<p>The following is wrong. (In other words, there is a bug in Sage.)</p>184185<sage><input>186P = R + L187P188</input></sage>189190<p>As we can see<ellipsis /></p>191192<sage><input>193Q = L + R194Q.is_equal_function(P)195</input></sage>196197<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>198199</section>200201<xi:include href="../legal.xml" />202203</article>204</pretext>205206207