📚 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="B">12<title>Sage and Linear Algebra Worksheet</title>13<subtitle>FCLA Section B</subtitle>1415<!-- header inclusion needs -xinclude switch on xsltproc -->16<frontmatter>17<xi:include href="../header.xml" />18</frontmatter>1920<section>21<title>Bases</title>2223<p>Five <q>random</q> vectors, each with 4 entries, collected into a set <c>S</c>.</p>2425<sage><input>26v1 = vector(QQ, [-4, -2, 3, -11])27v2 = vector(QQ, [-2, 7, 3, 9])28v3 = vector(QQ, [ 6, -4, -7, 5])29v4 = vector(QQ, [-1, 0, 3, -4])30v5 = vector(QQ, [-4, 5, -5, 11])31S = [v1, v2, v3, v4, v5]32</input></sage>3334<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>3536<sage><input>37A = matrix(S)38A39</input></sage>4041<sage><input>A.rref()</input></sage>4243<p>Sage does this semi-automatically, tossing zero rows for us.</p>4445<sage><input>46W = span(S)47B = W.basis()48B49</input></sage>5051<exercise>52<statement>53<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>5455<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>56</statement>57</exercise>5859<sage><input>60w = *v1 + *v2 + *v3 + *v4 + *v561w62</input></sage>6364<sage><input>w in W</input></sage>6566<sage><input>*B[0] + *B[1] + *B[2]</input></sage>67</section>6869<section>70<title>Nonsingular Matrices</title>7172<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>7374<sage><input>75entries = [[ 1, 1, 1, -1, -2, 4, 2, -3, 1, -6],76[-2, -1, -2, 2, 4, -7, -4, 5, -1, 7],77[ 1, -1, 2, -2, -5, 8, 5, -3, 4, -4],78[-1, -2, 0, 1, 0, -5, 0, -3, -5, 6],79[ 0, -2, 1, -1, -2, 3, 2, 3, 3, 7],80[ 1, 0, 1, -1, -2, 4, 2, 0, 2, 0],81[-1, 0, -1, 1, 3, -1, -2, 7, 5, 1],82[ 1, 1, 1, -1, -2, 8, 3, 2, 8, -6],83[ 0, 2, -1, 1, 2, -1, -2, 2, 2, -6],84[ 1, 3, 0, 0, 1, 3, 0, 0, 3, -8]]85M = matrix(QQ, entries)86M87</input></sage>8889<sage><input>90not M.is_singular()91</input></sage>9293<p>A totally random vector with 10 entries:</p>9495<sage><input>96v = random_vector(ZZ, 10, x=-9, y=9)97v98</input></sage>99100<exercise>101<statement>102<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>103<li>First, the old-fashioned way, thus exposing Theorem NMUS.</li>104105<li>Then, the modern way, with an inverse, since a nonsingular matrix is invertible, thus exposing Theorem SNCM.</li>106107<li>Finally, the Sage way, as described below.</li>108</ol></p>109</statement>110</exercise>111112<sage><input>113aug = M.augment(v)114aug.rref()115</input></sage>116117<sage><input>M.inverse()*v</input></sage>118119<p>The Sage way: first create a space with a <term>user basis</term>.</p>120121<sage><input>122X = (QQ^10).subspace_with_basis(M.columns())123X124</input></sage>125126<p>Sage still carries an <term>echelonized basis</term>, in addition to the <term>user-installed</term> basis.</p>127128<sage><input>129X.basis()130</input></sage>131132<sage><input>133X.echelonized_basis()134</input></sage>135136<p>Now ask for a coordinatization, relative to the basis in <c>X</c>, thus exposing Theorem VRRB.</p>137138<sage><input>139X.coordinates(v)140</input></sage>141</section>142143<section>144<title>Orthonormal Bases</title>145146<p>A particularly simple orthonormal basis of <m>\mathbb{C}^3</m>, collected into the set <c>S</c>.</p>147148<sage><input>149v1 = vector(QQ, [1/3, 2/3, 2/3])150v2 = vector(QQ, [2/3, -2/3, 1/3])151v3 = vector(QQ, [2/3, 1/3, -2/3])152S = [v1, v2, v3]153</input></sage>154155<exercise>156<statement>157<p>If these vectors are an orthonormal basis, then as the columns of a matrix they should create an orthonormal basis.</p>158</statement>159</exercise>160161<sage><input>162Q = column_matrix(S)163Q164</input></sage>165166<sage><input>Q.conjugate_transpose()*Q</input></sage>167168<sage><input>Q.is_unitary()</input></sage>169170<exercise>171<statement>172<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>173</statement>174</exercise>175176<p>A totally random vector with 3 entries.</p>177178<sage><input>179v = random_vector(ZZ, 3, x=-9, y=9)180v181</input></sage>182183<p>First, the old-fashioned way, thus exposing Theorem NMUS.</p>184185<sage><input>186aug = Q.augment(v)187aug.rref()188</input></sage>189190<p>Now, the modern way, with an inverse, since a nonsingular matrix is invertible, thus exposing Theorem SNCM.</p>191192<sage><input>Q.inverse()*v</input></sage>193194<p>The Sage way. Create a space with a <q>user basis</q> and ask for a coordinatization, thus exposing Theorem VRRB.</p>195196<sage><input>197X = (QQ^3).subspace_with_basis(Q.columns())198X.coordinates(v)199</input></sage>200201<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>202203<sage><input>204a1 = v1.hermitian_inner_product(v)205a2 = v2.hermitian_inner_product(v)206a3 = v3.hermitian_inner_product(v)207a1, a2, a3208</input></sage>209</section>210211<xi:include href="../legal.xml" />212213</article>214</pretext>215216217