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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<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 />

... [-4, 7, -3, -8],<br />

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

sage: P, L, U = C.LU(format=’plu’)<br />

sage: perm, M = C.LU(format=’compact’)<br />

sage: (L - identity_matrix(4)) + U == M<br />

True<br />

sage: p = [perm[i]+1 for i in range(len(perm))]<br />

sage: PP = Permutation(p).to_matrix()<br />

sage: PP == P<br />

True<br />

For a nonsingular matrix, <strong>and</strong> the ‘nonzero’ pivot strategy there is no need to permute rows, so the permutation<br />

matrix will be the identity. Furthermore, it can be shown that then the L <strong>and</strong> U matrices are uniquely<br />

determined by requiring L to have ones on the diagonal.<br />

sage: D = matrix(QQ, [[ 1, 0, 2, 0, -2, -1],<br />

... [ 3, -2, 3, -1, 0, 6],<br />

... [-4, 2, -3, 1, -1, -8],<br />

... [-2, 2, -3, 2, 1, 0],<br />

... [ 0, -1, -1, 0, 2, 5],<br />

... [-1, 2, -4, -1, 5, -3]])<br />

sage: P, L, U = D.LU(pivot=’nonzero’)<br />

sage: P<br />

[1 0 0 0 0 0]<br />

[0 1 0 0 0 0]<br />

[0 0 1 0 0 0]<br />

[0 0 0 1 0 0]<br />

[0 0 0 0 1 0]<br />

[0 0 0 0 0 1]<br />

sage: L<br />

[ 1 0 0 0 0 0]<br />

[ 3 1 0 0 0 0]<br />

[ -4 -1 1 0 0 0]<br />

[ -2 -1 -1 1 0 0]<br />

[ 0 1/2 1/4 1/2 1 0]<br />

[ -1 -1 -5/2 -2 -6 1]<br />

sage: U<br />

[ 1 0 2 0 -2 -1]<br />

[ 0 -2 -3 -1 6 9]<br />

[ 0 0 2 0 -3 -3]<br />

[ 0 0 0 1 0 4]<br />

[ 0 0 0 0 -1/4 -3/4]<br />

[ 0 0 0 0 0 1]<br />

sage: D == L*U<br />

True<br />

The base ring <strong>of</strong> the matrix may be any field, or a ring which has a fraction field implemented in <strong>Sage</strong>. The<br />

ring needs to be exact (there is a numerical LU decomposition for matrices over RDF <strong>and</strong> CDF). <strong>Matrices</strong><br />

returned are over the original field, or the fraction field <strong>of</strong> the ring. If the field is not ordered (i.e. the<br />

absolute value function is not implemented), then the pivot strategy needs to be ‘nonzero’.<br />

sage: A = matrix(RealField(100), 3, 3, range(9))<br />

sage: P, L, U = A.LU()<br />

Traceback (most recent call last):<br />

...<br />

TypeError: base ring <strong>of</strong> the matrix must be exact, not Real Field with 100 bits <strong>of</strong> precision<br />

sage: A = matrix(Integers(6), 3, 2, range(6))<br />

sage: A.LU()<br />

123

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

Saved successfully!

Ooh no, something went wrong!