08.02.2015 Views

Sage Reference Manual: Matrices and Spaces of Matrices - Mirrors

Sage Reference Manual: Matrices and Spaces of Matrices - Mirrors

Sage Reference Manual: Matrices and Spaces of Matrices - Mirrors

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Sage</strong> <strong>Reference</strong> <strong>Manual</strong>: <strong>Matrices</strong> <strong>and</strong> <strong>Spaces</strong> <strong>of</strong> <strong>Matrices</strong>, Release 6.1.1<br />

A rectangular matrix. Note that the orthonormal keyword is ignored in these cases.<br />

sage: A = matrix(RDF, [[-0.978325, -0.751994, 0.925305, -0.200512, 0.420458],<br />

... [-0.474877, -0.983403, 0.089836, 0.132218, 0.672965]])<br />

sage: G, M = A.gram_schmidt(orthonormal=False)<br />

sage: G.round(6).zero_at(10^-6)<br />

[-0.607223 -0.466745 0.574315 -0.124453 0.260968]<br />

[ 0.123203 -0.617909 -0.530578 0.289773 0.487368]<br />

sage: M.round(6).zero_at(10^-6)<br />

[1.611147 0.0]<br />

[0.958116 0.867778]<br />

sage: (A-M*G).zero_at(10^-12)<br />

[0.0 0.0 0.0 0.0 0.0]<br />

[0.0 0.0 0.0 0.0 0.0]<br />

sage: (G*G.transpose()).round(6).zero_at(10^-6)<br />

[1.0 0.0]<br />

[0.0 1.0]<br />

Even though a set <strong>of</strong> vectors may be linearly dependent, no effort is made to decide when a zero vector is<br />

really the result <strong>of</strong> a relation <strong>of</strong> linear dependence. So in this regard, input matrices are treated as being <strong>of</strong><br />

full rank. Try one <strong>of</strong> the base rings that provide exact results if you need exact results.<br />

sage: entries = [[1,1,2], [2,1,3], [3,1,4]]<br />

sage: A = matrix(QQ, entries)<br />

sage: A.rank()<br />

2<br />

sage: B = matrix(RDF, entries)<br />

sage: G, M = B.gram_schmidt()<br />

sage: G.round(6) # r<strong>and</strong>om signs<br />

[-0.408248 -0.408248 -0.816497]<br />

[ 0.707107 -0.707107 -0.0]<br />

[ -0.57735 -0.57735 0.57735]<br />

sage: M.round(10) # r<strong>and</strong>om<br />

[-2.4494897428 0.0 0.0]<br />

[-3.6742346142 0.7071067812 0.0]<br />

[-4.8989794856 1.4142135624 0.0]<br />

sage: (A - M*G).zero_at(1e-14)<br />

[0.0 0.0 0.0]<br />

[0.0 0.0 0.0]<br />

[0.0 0.0 0.0]<br />

sage: (G*G.transpose()).zero_at(1e-14)<br />

[1.0 0.0 0.0]<br />

[0.0 1.0 0.0]<br />

[0.0 0.0 1.0]<br />

Exact Rings, Orthonormalization:<br />

To scale a vector to unit length requires taking a square root, which <strong>of</strong>ten takes us outside the base ring.<br />

For the integers <strong>and</strong> the rationals, the field <strong>of</strong> algebraic numbers (QQbar) is big enough to contain what<br />

we need, but the price is that the computations are very slow, hence mostly <strong>of</strong> value for small cases or<br />

instruction. Now we need to use the orthonormal keyword.<br />

sage: A = matrix(QQbar, [[6, -8, 1],<br />

... [4, 1, 3],<br />

... [6, 3, 3],<br />

... [7, 1, -5],<br />

... [7, -3, 5]])<br />

sage: G, M = A.gram_schmidt(orthonormal=True)<br />

sage: G<br />

[ 0.5970223141259934 -0.7960297521679913 0.09950371902099891]<br />

175

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!