Opened 14 years ago
Closed 13 years ago
#5174 closed enhancement (fixed)
_repr_ for large matrices should indicate how to see the entries
Reported by: | Jason Grout | Owned by: | William Stein |
---|---|---|---|
Priority: | major | Milestone: | sage-4.3.1 |
Component: | linear algebra | Keywords: | |
Cc: | Merged in: | sage-4.3.1.alpha0 | |
Authors: | John Palmieri | Reviewers: | Mike Hansen |
Report Upstream: | N/A | Work issues: | |
Branch: | Commit: | ||
Dependencies: | Stopgaps: |
Description
[09:30] <jason-> on the other hand, I always get frustrated trying to see a 30x30 matrix in Sage [09:30] <wstein> print a.str() [09:30] <jason-> yeah, I never remember it. [09:30] <wstein> That will show you any matrix in sage of any size. [09:31] <wstein> Well then the output of a._repr_() should mention it so you can remember it. make a ticket. [09:31] <jason-> good point. [09:31] <jason-> I think I usually end up doing a.rows() [09:32] <jason-> or list(a) [09:32] <jason-> so you're saying print a should do: [09:32] <jason-> 30 x 30 dense matrix over Integer Ring (to see the entries, do print a.str()) [09:33] <wstein> Yep. [09:33] <jason-> 30 x 30 dense matrix over Integer Ring (to see the entries, type "print a.str()") [09:33] <wstein> Of course it's possibly confusing since "a.str()" is really "yourvar.str()" [09:33] <wstein> but hopefully people can understand that.
Attachments (2)
Change History (10)
comment:1 Changed 13 years ago by
Report Upstream: | → N/A |
---|
comment:2 Changed 13 years ago by
Change this function in matrix/matrix0.pyx:
def __repr__(self): if self._nrows < max_rows and self._ncols < max_cols: return self.str() if self.is_sparse(): s = 'sparse' else: s = 'dense' return "%s x %s %s matrix over %s"%(self._nrows, self._ncols, s, self.base_ring())
Changed 13 years ago by
Attachment: | trac_5174-matrixrep.patch added |
---|
comment:3 Changed 13 years ago by
Authors: | → John Palmieri |
---|---|
Status: | new → needs_review |
Here's a patch. The docstring to the __repr__
method for matrices illustrates what the patch does:
EXAMPLES:: sage: A = matrix([[1,2], [3,4], [5,6]]) sage: A.__repr__() '[1 2]\n[3 4]\n[5 6]' sage: print A [1 2] [3 4] [5 6] If the matrix is too big, don't print all of the elements:: sage: A = random_matrix(ZZ, 100) sage: A.__repr__() "100 x 100 dense matrix over Integer Ring (type 'print A.str()' to see all of the entries)" sage: print A 100 x 100 dense matrix over Integer Ring (type 'print A.str()' to see all of the entries) If there are several names for the same matrix, write it as "obj":: sage: B = A; print B 100 x 100 dense matrix over Integer Ring (type 'print obj.str()' to see all of the entries)
I actually think that this looks a little funny in some situations; for example, in chain_complex.py, the old version did this:
sage: C.differential() {0: 40 x 40 dense matrix over Integer Ring, 1: []}
while the new version does this:
sage: C.differential() {0: 40 x 40 dense matrix over Integer Ring (type 'print obj.str()' to see all of the entries), 1: []}
It would be pretty easy to change the print representation so if there were no name attached to the object (e.g., if it were produced by another method, as in this example), then the extra message about "print ..." would be omitted. Opinions?
comment:4 follow-up: 5 Changed 13 years ago by
It would be pretty easy to change the print representation so
if there were no name attached to the object (e.g., if it were
produced by another method, as in this example), then the extra message about "print ..." would be omitted. Opinions?
How do you do that? I would have no clue.
comment:5 Changed 13 years ago by
Replying to was:
It would be pretty easy to change the print representation so
if there were no name attached to the object (e.g., if it were
produced by another method, as in this example), then the extra message about "print ..." would be omitted. Opinions?
How do you do that? I would have no clue.
A web search led me to a code snippet (by Georg Brandl, the author of Sphinx) which more or less searches through the locals()
dictionary for the object, making a list of the variable names bound to it. (See the part of the patch for the file sageinspect.py.) If you exclude the variable names starting with "_", then if the resulting list is empty, you don't print the message.
comment:6 Changed 13 years ago by
Here's a new version of the patch. This is similar to the old one, but it behaves like this:
sage: C.differential() {0: 40 x 40 dense matrix over Integer Ring, 1: []}
Changed 13 years ago by
Attachment: | trac_5174-matrixrep-v2.patch added |
---|
comment:7 Changed 13 years ago by
Reviewers: | → Mike Hansen |
---|---|
Status: | needs_review → positive_review |
The second patch looks good to me.
comment:8 Changed 13 years ago by
Merged in: | → sage-4.3.1.alpha0 |
---|---|
Resolution: | → fixed |
Status: | positive_review → closed |
Idea: