Opened 10 years ago

Closed 10 years ago

#10627 closed enhancement (fixed)

add options for printing matrices

Reported by: jhpalmieri Owned by: jason, was
Priority: minor Milestone: sage-4.6.2
Component: linear algebra Keywords: matrix repr
Cc: kcrisman, iandrus, jason Merged in: sage-4.6.2.alpha3
Authors: Ivan Andrus Reviewers: Rob Beezer
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Status badges

Description

On ask.sagemath.org, it has been suggested that we should be able to print matrices using . instead of 0. We could also use a space instead of 0. Perhaps we could print + instead of 1 and - instead of -1 for a (1,-1)-matrix. All of these could be implemented by passing optional arguments to the str method for matrices, for example

    sage: mat = random_matrix(ZZ, 50)
    sage: mat.str(zero='.')

or

    sage: mat.str(use_dot=True)

We need to figure out what options to allow for right now (although we can add more later) and what syntax to use for them.

Attachments (1)

trac_10627-pretty-matrices.patch (7.0 KB) - added by iandrus 10 years ago.

Download all attachments as: .zip

Change History (13)

comment:1 Changed 10 years ago by iandrus

Perhaps we could pass a map for how to display things like:

    sage: mat = random_matrix(ZZ, 50)
    sage: mat.str({0:'.',1:'+',-1:'-'})

Then we could map things arbitrarily. It might not as easy to remember or use, but it would allow the user to do anything they wanted. I can't think when it might be useful except perhaps to make matrices of group elements more understandable or for a mathematical puzzle in which the numbers are encoded as symbols.

Actually it might be useful to show some oft repeated but complicated elements as a single letter like GAP does when displaying character tables. For that to be really useful it would be best if it were automatic, but that's probably too much to ask.

comment:2 Changed 10 years ago by jason

This ticket would be very useful to me. In my research, we deal with completing matrices, so we often represent matrices using +, -, and 0 (or ".") to represent positive, negative, or zero entries. Having an easy way to print out such a matrix would be nice.

comment:3 Changed 10 years ago by rbeezer

I find things like use_dot=True get complicated fast. As the options and possibilities increase, the keyword management becomes a morass.

I'd argue for

mat.str(zero=' ', plus_one='+', minus_one='-')

and think those three could be a good start.

How about having our cake and eating it too? One argument, and it is a dictionary, then use it for translations. Keywords, with or without a dictionary, create over-riding entries of the translation dictionary.

Then the code just checks for keys in the dictionary before setting an entry.

This would be nice for adjacency/incidence matrices of graphs.

$0.02 worth, Rob

comment:4 Changed 10 years ago by jason

How about a keyword that is a dictionary *or* a function? Then I could do something like:

def print_entry(x):
   if x>0:
      return '+'
   elif x<0:
      return '-'
   else: return '0'

mat.str(print_entries=print_entry)

comment:5 Changed 10 years ago by iandrus

  • Authors set to Ivan Andrus

A patch against 4.6.1.rc0 (sorry, I haven't updated yet). I patched the generic Matrix class and the mod2 Matrix class. Are there any others that need to be changed?

comment:6 Changed 10 years ago by jason

Thanks!

Wow, that is a huge patch! Can you explain the whitespace changes?

comment:7 Changed 10 years ago by iandrus

Oops. I have my editor set up to delete trailing whitespace automatically and I forgot to trim the patch. I can remove all purely whitespace changes if you wish.

comment:8 Changed 10 years ago by jason

It would probably be better to make a separate ticket to clean up whitespace, if you want to do that.

Changed 10 years ago by iandrus

comment:9 Changed 10 years ago by iandrus

There are still a few whitespace changes, but I've removed all unnecessary hunks.

comment:10 Changed 10 years ago by rbeezer

  • Status changed from new to needs_review

comment:11 Changed 10 years ago by rbeezer

  • Reviewers set to Rob Beezer
  • Status changed from needs_review to positive_review

Looks good. As mentioned, if there is additional behavior desired, that could be easily added through one of the mechanisms made available here.

Positive review.

comment:12 Changed 10 years ago by jdemeyer

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