On Apr 16, 12:41 pm, Bastian Weber <[email protected]>
wrote:
> what is the proper way to convert a sage matrix to a numpy 2d-array?
>
Hi, well, basically, I don't think you have to convert it except you
need it for some special applications. Sage can do arithmetic over the
matrices. However, this short session should answer all your
questions:
sage: m = matrix(RDF, 2, range(4)); m
[0.0 1.0]
[2.0 3.0]
sage: type(m)
<type 'sage.matrix.matrix_real_double_dense.Matrix_real_double_dense'>
sage: n = m.numpy()
sage: type(n)
<type 'numpy.ndarray'>
# and back again:
sage: type(matrix(n))
<type 'sage.matrix.matrix_real_double_dense.Matrix_real_double_dense'>
sage: n
array([[ 0., 1.],
[ 2., 3.]])
sage: n*n
array([[ 0., 1.],
[ 4., 9.]])
# n*n is not the matrix product!
# but it is for sage matrices:
sage: m*m
[ 2.0 3.0]
[ 6.0 11.0]
# you need "dot" from numpy
sage: from numpy import dot
sage: dot(n,n)
array([[ 2., 3.],
[ 6., 11.]])
last note: RDF = real-double-field, i.e. the "pseudo" field of double
values the cpu and numpy uses by default.
H
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org