📚 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="VR">12<title>Sage and Linear Algebra Worksheet</title>13<subtitle>FCLA Section VR</subtitle>1415<!-- header inclusion needs -xinclude switch on xsltproc -->16<frontmatter>17<xi:include href="../header.xml" />18</frontmatter>1920<section>21<title>Vector Representations</title>2223<p>It is easy to form vector representations of vectors in <m>\mathbb{C}^n</m>.</p>2425<p>We get a nonstandard basis quickly from the columns of a nonsingular matrix. The keyword <c>algorithm='unimodular'</c> requests a matrix with determinant <m>1</m>.</p>2627<sage><input>28n = 629A = random_matrix(QQ, n, algorithm='unimodular', upper_bound=9)30A31</input></sage>3233<p>The columns of <c>A</c> become the <q>user basis</q> of a vector space.</p>3435<sage><input>36B = A.columns()37V = (QQ^n).subspace_with_basis(B)38V39</input></sage>404142<sage><input>43u = random_vector(QQ, n)44u45</input></sage>4647<p>Now, we get values of the invertible linear transformation <m>\rho_B</m> with the Sage method <c>.coordinate_vector()</c> method of the vector space.</p>4849<sage><input>50c = V.coordinate_vector(u)51c52</input></sage>5354<p>The inverse linear transformation is also available as the <c>.linear_combination_of_basis()</c> method of the vector space.</p>5556<sage><input>57round_trip = V.linear_combination_of_basis(c)58round_trip59</input></sage>6061<p>And the automated check:</p>6263<sage><input>64u == round_trip65</input></sage>6667<p>Notice that this is something we could do <q>by hand</q> with just reduced row-echelon form. The coordinitization of <c>u</c> relative to the basis <c>B</c> is just a (unique) solution to a linear system.</p>6869<sage><input>70aug = column_matrix(B + [u])71aug.rref()72</input></sage>7374<p>The following stanza will always return <c>True</c> as we <q>coordinatize</q> and then use the coordinates to form a linear combination of the basis.</p>7576<sage><input>77w = random_vector(QQ, n)78x = V.coordinate_vector(w)79y = V.linear_combination_of_basis(x)80y == w81</input></sage>82</section>8384<!-- Might be better to compute something of interest in abstract vector space by coordinatizing -->85<section>86<title>Abstract Vector Spaces</title>8788<p>Sage does not implement abstract vector spaces. It presumes we have <q>nice</q> standard bases available and can apply an intermediate coordinatization ourselves.</p>8990<!-- [[1, 1, 2, 5], [-2, -1, -3, -8], [2, 1, 4, 7], [-2, 1, 3, -7]] -->91<!-- (-33/14, 1/2, 1, 1) -->92<exercise>93<statement>94<p>In <m>P_3</m>, the vector space of polynomials with degree at most <m>3</m>, find the vector representation of <m>p = x^{3} + x^{2} + \frac{1}{2} \, x - \frac{33}{14}</m> relative to the basis for <m>P_3</m>: <md>95<mrow>B = \{&965x^{3} + 2x^{2} + x + 1,\,97-8x^{3} - 3x^{2} - x - 2,</mrow>98<mrow>& 7x^{3} + 4x^{2} + x + 2,\,99-7x^{3} + 3x^{2} + x - 2\}</mrow>100</md>.</p>101102<p>Hint: Coordinatize with respect to the basis <m>\left\{1, x, x^2, x^3\right\}</m>.</p>103</statement>104</exercise>105106107<sage><input>108A = matrix(QQ, [[1, -2, 2, -2], [1, -1, 1, 1], [2, -3, 4, 3], [5, -8, 7, -7]])109B = A.columns()110B111</input></sage>112113<p><c>B</c> is a basis, since <c>A</c> is nonsingular.</p>114115<sage><input>116A.is_singular()117</input></sage>118119<p>Now coordinatize <c>p</c>.</p>120121<sage><input>122p = vector(QQ, [-33/14, 1/2, 1, 1])123p124</input></sage>125126<p>We'll get a coordinatization old-style.</p>127128<sage><input>129aug = column_matrix(B + [p])130r = aug.rref()131r132</input></sage>133134<p>Let's check to see if this is right and we can recover <c>p</c>.</p>135136<sage><input>137soln = r.column(4)138round_trip = sum([soln[i]*B[i] for i in range(4)])139round_trip, round_trip == p140</input></sage>141</section>142143<xi:include href="../legal.xml" />144145</article>146</pretext>147148149