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

sage: A.norm(p=’bogus’)<br />

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

...<br />

ValueError: matrix norm ’p’ must be +/- infinity, ’frob’ or an integer, not bogus<br />

sage: A.norm(p=632)<br />

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

...<br />

ValueError: matrix norm integer values <strong>of</strong> ’p’ must be -2, -1, 1 or 2, not 632<br />

numpy(dtype=None)<br />

This method returns a copy <strong>of</strong> the matrix as a numpy array. It uses the numpy C/api so is very fast.<br />

INPUT:<br />

•dtype - The desired data-type for the array. If not given, then the type will be determined as the<br />

minimum type required to hold the objects in the sequence.<br />

EXAMPLES:<br />

sage: m = matrix(RDF,[[1,2],[3,4]])<br />

sage: n = m.numpy()<br />

sage: import numpy<br />

sage: numpy.linalg.eig(n)<br />

(array([-0.37228132, 5.37228132]), array([[-0.82456484, -0.41597356],<br />

[ 0.56576746, -0.90937671]]))<br />

sage: m = matrix(RDF, 2, range(6)); m<br />

[0.0 1.0 2.0]<br />

[3.0 4.0 5.0]<br />

sage: m.numpy()<br />

array([[ 0., 1., 2.],<br />

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

Alternatively, numpy automatically calls this function (via the magic __array__() method) to convert<br />

<strong>Sage</strong> matrices to numpy arrays:<br />

sage: import numpy<br />

sage: m = matrix(RDF, 2, range(6)); m<br />

[0.0 1.0 2.0]<br />

[3.0 4.0 5.0]<br />

sage: numpy.array(m)<br />

array([[ 0., 1., 2.],<br />

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

sage: numpy.array(m).dtype<br />

dtype(’float64’)<br />

sage: m = matrix(CDF, 2, range(6)); m<br />

[0.0 1.0 2.0]<br />

[3.0 4.0 5.0]<br />

sage: numpy.array(m)<br />

array([[ 0.+0.j, 1.+0.j, 2.+0.j],<br />

[ 3.+0.j, 4.+0.j, 5.+0.j]])<br />

sage: numpy.array(m).dtype<br />

dtype(’complex128’)<br />

TESTS:<br />

sage: m = matrix(RDF,0,5,[]); m<br />

[]<br />

sage: m.numpy()<br />

array([], shape=(0, 5), dtype=float64)<br />

sage: m = matrix(RDF,5,0,[]); m<br />

380 Chapter 19. Dense matrices using a NumPy backend.

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

Saved successfully!

Ooh no, something went wrong!