Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert RDF/CDF matrices to numpy #8719

Closed
jasongrout opened this issue Apr 20, 2010 · 10 comments
Closed

convert RDF/CDF matrices to numpy #8719

jasongrout opened this issue Apr 20, 2010 · 10 comments

Comments

@jasongrout
Copy link
Member

This patch makes the following work:

            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')

            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)

CC: @rbeezer

Component: linear algebra

Author: Jason Grout

Reviewer: Rob Beezer

Merged: sage-4.4.2.alpha0

Issue created by migration from https://trac.sagemath.org/ticket/8719

@jasongrout
Copy link
Member Author

Author: Jason Grout

@jasongrout

This comment has been minimized.

@jasongrout
Copy link
Member Author

comment:5

rbeezer: it seems like you could naturally review this. It just adds a numpy-specific magic method for conversion.

@rbeezer
Copy link
Mannequin

rbeezer mannequin commented May 4, 2010

comment:6

Hi Jason,

So you have defined a new method "__array__" for a Sage matrix object. When somebody calls numpy.array(a) for a Sage matrix a then this __array__ method gets called (and this is in effect just the numpy() method of a Sage matrix)? In other words, the numpy.array() method has an expanded reportoire and now "knows" how to deal with a Sage matrix object?

If so, could you say so? The line __array__=numpy all by itself looks really mysterious with no documentation. And the new doctests say "you could use numpy" - when Sage is really doing the heavy-lifting? It sounds like numpy is doing the work, when this is not a standard behavior of numpy.

So I'm suggesting maybe this seemingly circular arrrangement (modify Sage matrices, so numpy can deal with them, using a Sage function to convert to a numpy array) could be better explained so nobody messes it up or gets too confused. With the added code and the added doctests all together, I think I was able to figure this out - otherwise it would have been a head-scratcher.

Rob

@jasongrout
Copy link
Member Author

Attachment: trac_8719-numpy-conversion.patch.gz

@jasongrout
Copy link
Member Author

comment:7

I updated the docs.

@rbeezer
Copy link
Mannequin

rbeezer mannequin commented May 4, 2010

comment:8

Replying to @jasongrout:

I updated the docs.

Looks good! I'll finish this tomorrow night.

@rbeezer
Copy link
Mannequin

rbeezer mannequin commented May 5, 2010

comment:9

Looks good, builds and passes all tests, documentation builds without warnings.

The added documentation looks great.

Positive review.

@rbeezer
Copy link
Mannequin

rbeezer mannequin commented May 5, 2010

Reviewer: Rob Beezer

@sagetrac-mvngu
Copy link
Mannequin

sagetrac-mvngu mannequin commented May 8, 2010

Merged: sage-4.4.2.alpha0

@sagetrac-mvngu sagetrac-mvngu mannequin removed the s: positive review label May 8, 2010
@sagetrac-mvngu sagetrac-mvngu mannequin closed this as completed May 8, 2010
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants