# Ticket #8719: trac_8719-numpy-conversion.patch

File trac_8719-numpy-conversion.patch, 2.9 KB (added by jason, 3 years ago)
• ## sage/matrix/matrix1.pyx

```# HG changeset patch
# User Jason Grout <jason-sage@creativetrax.com>
# Date 1271724882 18000
# Node ID 338637719747111e3f30c1f1c3a1dee84d0e9d12
# Parent  40d5603e67a9a1b7d729c1565b7c1f3b996527c4
#8719: Make matrices convert to numpy arrays using numpy.array()

diff -r 40d5603e67a9 -r 338637719747 sage/matrix/matrix1.pyx```
 a sage: import numpy sage: sorted(numpy.typecodes.items()) [('All', '?bhilqpBHILQPfdgFDGSUVO'), ('AllFloat', 'fdgFDG'), ('AllInteger', 'bBhHiIlLqQpP'), ('Character', 'c'), ('Complex', 'FDG'), ('Float', 'fdg'), ('Integer', 'bhilqp'), ('UnsignedInteger', 'BHILQP')] Alternatively, numpy automatically calls this function (via the magic :meth:`__array__` method) to convert Sage matrices to numpy arrays:: sage: import numpy sage: b=numpy.array(a); b array([[ 0,  1,  2,  3], [ 4,  5,  6,  7], [ 8,  9, 10, 11]]) sage: b.dtype dtype('int64') sage: b.shape (3, 4) """ import numpy A = numpy.matrix(self.list(), dtype=dtype) return numpy.resize(A,(self.nrows(), self.ncols())) # Define the magic "__array__" function so that numpy.array(m) can convert # a matrix m to a numpy array. # See http://docs.scipy.org/doc/numpy/user/c-info.how-to-extend.html#converting-an-arbitrary-sequence-object __array__=numpy ################################################### # Construction functions
• ## sage/matrix/matrix_double_dense.pyx

`diff -r 40d5603e67a9 -r 338637719747 sage/matrix/matrix_double_dense.pyx`
 a array([[ 0.,  1.,  2.], [ 3.,  4.,  5.]]) Alternatively, numpy automatically calls this function (via the magic :meth:`__array__` method) to convert Sage matrices to numpy arrays:: sage: import numpy sage: m = matrix(RDF, 2, range(6)); m [0.0 1.0 2.0] [3.0 4.0 5.0] sage: numpy.array(m) array([[ 0.,  1.,  2.], [ 3.,  4.,  5.]]) sage: numpy.array(m).dtype dtype('float64') sage: m = matrix(CDF, 2, range(6)); m [  0 1.0 2.0] [3.0 4.0 5.0] sage: numpy.array(m) array([[ 0.+0.j,  1.+0.j,  2.+0.j], [ 3.+0.j,  4.+0.j,  5.+0.j]]) sage: numpy.array(m).dtype dtype('complex128') TESTS: sage: m = matrix(RDF,0,5,[]); m []