Ticket #5174 (closed enhancement: fixed)

Opened 4 years ago

Last modified 3 years ago

_repr_ for large matrices should indicate how to see the entries

Reported by: jason Owned by: was
Priority: major Milestone: sage-4.3.1
Component: linear algebra Keywords:
Cc: Work issues:
Report Upstream: N/A Reviewers: Mike Hansen
Authors: John Palmieri Merged in: sage-4.3.1.alpha0
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

trac_5174-matrixrep.patch Download (13.9 KB) - added by jhpalmieri 3 years ago.
trac_5174-matrixrep-v2.patch Download (6.1 KB) - added by jhpalmieri 3 years ago.

Change History

comment:1 Changed 3 years ago by was

  • Report Upstream set to N/A

Idea:

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

comment:2 Changed 3 years ago by was

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 3 years ago by jhpalmieri

comment:3 Changed 3 years ago by jhpalmieri

  • Status changed from new to needs_review
  • Authors set to John Palmieri

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 3 years ago by 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.

comment:5 in reply to: ↑ 4 Changed 3 years ago by jhpalmieri

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 3 years ago by jhpalmieri

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 3 years ago by jhpalmieri

comment:7 Changed 3 years ago by mhansen

  • Status changed from needs_review to positive_review
  • Reviewers set to Mike Hansen

The second patch looks good to me.

comment:8 Changed 3 years ago by mhansen

  • Status changed from positive_review to closed
  • Resolution set to fixed
  • Merged in set to sage-4.3.1.alpha0
Note: See TracTickets for help on using tickets.