Sage: Ticket #10627: add options for printing matrices
https://trac.sagemath.org/ticket/10627
<p>
On <a class="ext-link" href="http://ask.sagemath.org/question/303/displaying-matrices-with-instead-of-0"><span class="icon"></span>ask.sagemath.org</a>, it has been suggested that we should be able to print matrices using <code>.</code> instead of <code>0</code>. We could also use a space instead of <code>0</code>. Perhaps we could print <code>+</code> instead of <code>1</code> and <code>-</code> instead of <code>-1</code> for a (1,-1)-matrix. All of these could be implemented by passing optional arguments to the <code>str</code> method for matrices, for example
</p>
<pre class="wiki"> sage: mat = random_matrix(ZZ, 50)
sage: mat.str(zero='.')
</pre><p>
or
</p>
<pre class="wiki"> sage: mat.str(use_dot=True)
</pre><p>
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.
</p>
en-usSagehttps://trac.sagemath.org/chrome/site/logo_sagemath_trac.png
https://trac.sagemath.org/ticket/10627
Trac 1.1.6iandrusSat, 15 Jan 2011 13:36:08 GMT
https://trac.sagemath.org/ticket/10627#comment:1
https://trac.sagemath.org/ticket/10627#comment:1
<p>
Perhaps we could pass a map for how to display things like:
</p>
<pre class="wiki"> sage: mat = random_matrix(ZZ, 50)
sage: mat.str({0:'.',1:'+',-1:'-'})
</pre><p>
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.
</p>
<p>
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.
</p>
TicketjasonSat, 15 Jan 2011 20:30:23 GMT
https://trac.sagemath.org/ticket/10627#comment:2
https://trac.sagemath.org/ticket/10627#comment:2
<p>
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.
</p>
TicketrbeezerFri, 21 Jan 2011 05:46:11 GMT
https://trac.sagemath.org/ticket/10627#comment:3
https://trac.sagemath.org/ticket/10627#comment:3
<p>
I find things like <code>use_dot=True</code> get complicated fast. As the options and possibilities increase, the keyword management becomes a morass.
</p>
<p>
I'd argue for
</p>
<pre class="wiki">mat.str(zero=' ', plus_one='+', minus_one='-')
</pre><p>
and think those three could be a good start.
</p>
<p>
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.
</p>
<p>
Then the code just checks for keys in the dictionary before setting an entry.
</p>
<p>
This would be nice for adjacency/incidence matrices of graphs.
</p>
<p>
$0.02 worth,
Rob
</p>
TicketjasonFri, 21 Jan 2011 14:11:53 GMT
https://trac.sagemath.org/ticket/10627#comment:4
https://trac.sagemath.org/ticket/10627#comment:4
<p>
How about a keyword that is a dictionary *or* a function? Then I could do something like:
</p>
<pre class="wiki">def print_entry(x):
if x>0:
return '+'
elif x<0:
return '-'
else: return '0'
mat.str(print_entries=print_entry)
</pre>
TicketiandrusFri, 21 Jan 2011 17:23:57 GMTauthor set
https://trac.sagemath.org/ticket/10627#comment:5
https://trac.sagemath.org/ticket/10627#comment:5
<ul>
<li><strong>author</strong>
set to <em>Ivan Andrus</em>
</li>
</ul>
<p>
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?
</p>
TicketjasonFri, 21 Jan 2011 17:32:41 GMT
https://trac.sagemath.org/ticket/10627#comment:6
https://trac.sagemath.org/ticket/10627#comment:6
<p>
Thanks!
</p>
<p>
Wow, that is a <em>huge</em> patch! Can you explain the whitespace changes?
</p>
TicketiandrusFri, 21 Jan 2011 17:36:38 GMT
https://trac.sagemath.org/ticket/10627#comment:7
https://trac.sagemath.org/ticket/10627#comment:7
<p>
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.
</p>
TicketjasonFri, 21 Jan 2011 17:42:48 GMT
https://trac.sagemath.org/ticket/10627#comment:8
https://trac.sagemath.org/ticket/10627#comment:8
<p>
It would probably be better to make a separate ticket to clean up whitespace, if you want to do that.
</p>
TicketiandrusFri, 21 Jan 2011 20:47:03 GMTattachment set
https://trac.sagemath.org/ticket/10627
https://trac.sagemath.org/ticket/10627
<ul>
<li><strong>attachment</strong>
set to <em>trac_10627-pretty-matrices.patch</em>
</li>
</ul>
TicketiandrusFri, 21 Jan 2011 20:48:22 GMT
https://trac.sagemath.org/ticket/10627#comment:9
https://trac.sagemath.org/ticket/10627#comment:9
<p>
There are still a few whitespace changes, but I've removed all unnecessary hunks.
</p>
TicketrbeezerTue, 25 Jan 2011 23:44:28 GMTstatus changed
https://trac.sagemath.org/ticket/10627#comment:10
https://trac.sagemath.org/ticket/10627#comment:10
<ul>
<li><strong>status</strong>
changed from <em>new</em> to <em>needs_review</em>
</li>
</ul>
TicketrbeezerTue, 25 Jan 2011 23:46:34 GMTstatus changed; reviewer set
https://trac.sagemath.org/ticket/10627#comment:11
https://trac.sagemath.org/ticket/10627#comment:11
<ul>
<li><strong>status</strong>
changed from <em>needs_review</em> to <em>positive_review</em>
</li>
<li><strong>reviewer</strong>
set to <em>Rob Beezer</em>
</li>
</ul>
<p>
Looks good. As mentioned, if there is additional behavior desired, that could be easily added through one of the mechanisms made available here.
</p>
<p>
Positive review.
</p>
TicketjdemeyerWed, 26 Jan 2011 22:28:55 GMTstatus changed; resolution, merged set
https://trac.sagemath.org/ticket/10627#comment:12
https://trac.sagemath.org/ticket/10627#comment:12
<ul>
<li><strong>status</strong>
changed from <em>positive_review</em> to <em>closed</em>
</li>
<li><strong>resolution</strong>
set to <em>fixed</em>
</li>
<li><strong>merged</strong>
set to <em>sage-4.6.2.alpha3</em>
</li>
</ul>
Ticket