Opened 12 years ago
Closed 12 years ago
#8719 closed enhancement (fixed)
convert RDF/CDF matrices to numpy
Reported by: | Jason Grout | Owned by: | Jason Grout |
---|---|---|---|
Priority: | major | Milestone: | sage-4.4.2 |
Component: | linear algebra | Keywords: | |
Cc: | Rob Beezer | Merged in: | sage-4.4.2.alpha0 |
Authors: | Jason Grout | Reviewers: | Rob Beezer |
Report Upstream: | N/A | Work issues: | |
Branch: | Commit: | ||
Dependencies: | Stopgaps: |
Description (last modified by )
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)
Attachments (1)
Change History (11)
comment:1 Changed 12 years ago by
Status: | new → needs_review |
---|
comment:2 Changed 12 years ago by
Authors: | → Jason Grout |
---|
comment:3 Changed 12 years ago by
Description: | modified (diff) |
---|
comment:4 Changed 12 years ago by
Cc: | Rob Beezer added |
---|---|
Owner: | changed from jason, was to Jason Grout |
comment:5 Changed 12 years ago by
comment:6 Changed 12 years ago by
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
Changed 12 years ago by
Attachment: | trac_8719-numpy-conversion.patch added |
---|
comment:8 Changed 12 years ago by
comment:9 Changed 12 years ago by
Reviewers: | → Rob Beezer |
---|---|
Status: | needs_review → positive_review |
Looks good, builds and passes all tests, documentation builds without warnings.
The added documentation looks great.
Positive review.
comment:10 Changed 12 years ago by
Merged in: | → sage-4.4.2.alpha0 |
---|---|
Resolution: | → fixed |
Status: | positive_review → closed |
rbeezer: it seems like you could naturally review this. It just adds a numpy-specific magic method for conversion.