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

sage: import numpy<br />

sage: entries = numpy.array([1.2, 5.6]); entries<br />

array([ 1.2, 5.6])<br />

sage: A = diagonal_matrix(3, entries); A<br />

[1.2 0.0 0.0]<br />

[0.0 5.6 0.0]<br />

[0.0 0.0 0.0]<br />

sage: A.parent()<br />

Full MatrixSpace <strong>of</strong> 3 by 3 sparse matrices over Real Double Field<br />

sage: j = numpy.complex(0,1)<br />

sage: entries = numpy.array([2.0+j, 8.1, 3.4+2.6*j]); entries<br />

array([ 2.0+1.j , 8.1+0.j , 3.4+2.6j])<br />

sage: A = diagonal_matrix(entries); A<br />

[2.0 + 1.0*I 0.0 0.0]<br />

[ 0.0 8.1 0.0]<br />

[ 0.0 0.0 3.4 + 2.6*I]<br />

sage: A.parent()<br />

Full MatrixSpace <strong>of</strong> 3 by 3 sparse matrices over Complex Double Field<br />

sage: entries = numpy.array([4, 5, 6])<br />

sage: A = diagonal_matrix(entries); A<br />

[4 0 0]<br />

[0 5 0]<br />

[0 0 6]<br />

sage: A.parent()<br />

Full MatrixSpace <strong>of</strong> 3 by 3 sparse matrices over Integer Ring<br />

sage: entries = numpy.array([4.1, 5.2, 6.3])<br />

sage: A = diagonal_matrix(ZZ, entries); A<br />

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

...<br />

TypeError: Cannot convert non-integral float to integer<br />

By default returned matrices have a sparse implementation. This can be changed when using any <strong>of</strong> the formats.<br />

sage: A = diagonal_matrix([1,2,3], sparse=False)<br />

sage: A.parent()<br />

Full MatrixSpace <strong>of</strong> 3 by 3 dense matrices over Integer Ring<br />

An empty list <strong>and</strong> no ring specified defaults to the integers.<br />

sage: A = diagonal_matrix([])<br />

sage: A.parent()<br />

Full MatrixSpace <strong>of</strong> 0 by 0 sparse matrices over Integer Ring<br />

Giving the entries improperly may first complain about not having a length.<br />

sage: diagonal_matrix(QQ, 5, 10)<br />

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

...<br />

TypeError: unable to determine number <strong>of</strong> entries for diagonal matrix construction<br />

Giving too many entries will raise an error.<br />

sage: diagonal_matrix(QQ, 3, [1,2,3,4])<br />

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

...<br />

ValueError: number <strong>of</strong> diagonal matrix entries (4) exceeds the requested matrix size (3)<br />

30 Chapter 2. Matrix Constructor

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

Saved successfully!

Ooh no, something went wrong!