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:

Status badges

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)

trac_5174-matrixrep.patch (13.9 KB) - added by John Palmieri 13 years ago.
trac_5174-matrixrep-v2.patch (6.1 KB) - added by John Palmieri 13 years ago.

Download all attachments as: .zip

Change History (10)

comment:1 Changed 13 years ago by William Stein

Report Upstream: N/A

Idea:

sage: obj = random_matrix(ZZ,100)
sage: str(obj) + " ('print obj.str()' to see all entries)"

comment:2 Changed 13 years ago by William Stein

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 John Palmieri

Attachment: trac_5174-matrixrep.patch added

comment:3 Changed 13 years ago by John Palmieri

Authors: John Palmieri
Status: newneeds_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 Changed 13 years ago by William Stein

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 in reply to:  4 Changed 13 years ago by John Palmieri

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 John Palmieri

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 John Palmieri

comment:7 Changed 13 years ago by Mike Hansen

Reviewers: Mike Hansen
Status: needs_reviewpositive_review

The second patch looks good to me.

comment:8 Changed 13 years ago by Mike Hansen

Merged in: sage-4.3.1.alpha0
Resolution: fixed
Status: positive_reviewclosed
Note: See TracTickets for help on using tickets.