ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 03 Feb 2012 06:49:15 +0100Displaying matrices with . instead of 0https://ask.sagemath.org/question/7864/displaying-matrices-with-instead-of-0/In GAP it is possible (at least in some circumstances) to display a matrix with `.` instead of `0` and without brackets. This makes the structure of the matrix much easier to see. Is there a way to do this in Sage?
e.g. to show
<pre> 1 3 3 . 9
1 1 1 2 5
1 1 3 2 3
1 3 1 . 3
1 1 1 . 5
1 . . 1 3
1 1 3 . 3
1 3 1 . 3
1 1 1 . 3
1 3 . . .
1 . 1 . 2
</pre>
instead of
<pre>[ [ 1, 3, 3, 0, 9 ],
[ 1, 1, 1, 2, 5 ],
[ 1, 1, 3, 2, 3 ],
[ 1, 3, 1, 0, 3 ],
[ 1, 1, 1, 0, 5 ],
[ 1, 0, 0, 1, 3 ],
[ 1, 1, 3, 0, 3 ],
[ 1, 3, 1, 0, 3 ],
[ 1, 1, 1, 0, 3 ],
[ 1, 3, 0, 0, 0 ],
[ 1, 0, 1, 0, 2 ] ]</pre>Wed, 12 Jan 2011 08:57:06 +0100https://ask.sagemath.org/question/7864/displaying-matrices-with-instead-of-0/Answer by kcrisman for <p>In GAP it is possible (at least in some circumstances) to display a matrix with <code>.</code> instead of <code>0</code> and without brackets. This makes the structure of the matrix much easier to see. Is there a way to do this in Sage?</p>
<p>e.g. to show</p>
<pre> 1 3 3 . 9
1 1 1 2 5
1 1 3 2 3
1 3 1 . 3
1 1 1 . 5
1 . . 1 3
1 1 3 . 3
1 3 1 . 3
1 1 1 . 3
1 3 . . .
1 . 1 . 2
</pre>
<p>instead of </p>
<pre>[ [ 1, 3, 3, 0, 9 ],
[ 1, 1, 1, 2, 5 ],
[ 1, 1, 3, 2, 3 ],
[ 1, 3, 1, 0, 3 ],
[ 1, 1, 1, 0, 5 ],
[ 1, 0, 0, 1, 3 ],
[ 1, 1, 3, 0, 3 ],
[ 1, 3, 1, 0, 3 ],
[ 1, 1, 1, 0, 3 ],
[ 1, 3, 0, 0, 0 ],
[ 1, 0, 1, 0, 2 ] ]</pre>
https://ask.sagemath.org/question/7864/displaying-matrices-with-instead-of-0/?answer=11965#post-id-11965Huh, that's a great question. I don't believe so. However, you could $\LaTeX$ it...
sage: M = matrix([[1,2,3]])
sage: latex(M)
\left(\begin{array}{rrr}
1 & 2 & 3
\end{array}\right)
Though that's not what you want in terms of the dots. Here it is in Sage, straight from some examples in the GAP [reference manual](http://www.gap-system.org/Manuals/doc/htm/tut/CHAP003.htm):
sage: G = gap([[1,-1,1],[2,0,-1],[1,1,1]])
sage: G
[ [ 1, -1, 1 ], [ 2, 0, -1 ], [ 1, 1, 1 ] ]
sage: type(G)
<class 'sage.interfaces.gap.GapElement'>
sage: G.Display()
[ [ 1, -1, 1 ],
[ 2, 0, -1 ],
[ 1, 1, 1 ] ]
sage: H = gap('[[1,-1,1],[2,0,-1],[1,1,1]]*One(GF(5))')
sage: H
[ [ Z(5)^0, Z(5)^2, Z(5)^0 ], [ Z(5), 0*Z(5), Z(5)^2 ],
[ Z(5)^0, Z(5)^0, Z(5)^0 ] ]
sage: H.Display()
1 4 1
2 . 4
1 1 1
You'll note that this only happens in the finite field case, where of course 0 isn't in the group of units and so it's appropriate to display it differently. So I don't know whether we would want to do this.
For sparse Sage matrices, like (from Sage `matrix?`),
sage: m=matrix(QQ,2,3,{(1,1): 2}); m
[0 0 0]
[0 2 0]
I could imagine something of the kind as an optional way of doing it. But I don't use them, so you'd want to ask on sage-devel about that.
Wed, 12 Jan 2011 10:14:36 +0100https://ask.sagemath.org/question/7864/displaying-matrices-with-instead-of-0/?answer=11965#post-id-11965Answer by John Palmieri for <p>In GAP it is possible (at least in some circumstances) to display a matrix with <code>.</code> instead of <code>0</code> and without brackets. This makes the structure of the matrix much easier to see. Is there a way to do this in Sage?</p>
<p>e.g. to show</p>
<pre> 1 3 3 . 9
1 1 1 2 5
1 1 3 2 3
1 3 1 . 3
1 1 1 . 5
1 . . 1 3
1 1 3 . 3
1 3 1 . 3
1 1 1 . 3
1 3 . . .
1 . 1 . 2
</pre>
<p>instead of </p>
<pre>[ [ 1, 3, 3, 0, 9 ],
[ 1, 1, 1, 2, 5 ],
[ 1, 1, 3, 2, 3 ],
[ 1, 3, 1, 0, 3 ],
[ 1, 1, 1, 0, 5 ],
[ 1, 0, 0, 1, 3 ],
[ 1, 1, 3, 0, 3 ],
[ 1, 3, 1, 0, 3 ],
[ 1, 1, 1, 0, 3 ],
[ 1, 3, 0, 0, 0 ],
[ 1, 0, 1, 0, 2 ] ]</pre>
https://ask.sagemath.org/question/7864/displaying-matrices-with-instead-of-0/?answer=11966#post-id-11966How about
m = matrix([[0,0,1], [1,0,0]])
print m.str().replace(' 0', ' .').replace('[0', '[.')
Should we add a method to the matrix class which does something like this? What should it be called?
Wed, 12 Jan 2011 14:49:03 +0100https://ask.sagemath.org/question/7864/displaying-matrices-with-instead-of-0/?answer=11966#post-id-11966Comment by d3banjan for <p>How about</p>
<pre><code>m = matrix([[0,0,1], [1,0,0]])
print m.str().replace(' 0', ' .').replace('[0', '[.')
</code></pre>
<p>Should we add a method to the matrix class which does something like this? What should it be called?</p>
https://ask.sagemath.org/question/7864/displaying-matrices-with-instead-of-0/?comment=20356#post-id-20356why not instruct it to take in a dict object that goes as (0=".", 1=";" ) and so on?Fri, 03 Feb 2012 05:08:29 +0100https://ask.sagemath.org/question/7864/displaying-matrices-with-instead-of-0/?comment=20356#post-id-20356Comment by kcrisman for <p>How about</p>
<pre><code>m = matrix([[0,0,1], [1,0,0]])
print m.str().replace(' 0', ' .').replace('[0', '[.')
</code></pre>
<p>Should we add a method to the matrix class which does something like this? What should it be called?</p>
https://ask.sagemath.org/question/7864/displaying-matrices-with-instead-of-0/?comment=22287#post-id-22287Ooh, I like this direction. Can you open a ticket? Ivan is on Trac, and you should copy Jason Grout on it too.Thu, 13 Jan 2011 13:32:47 +0100https://ask.sagemath.org/question/7864/displaying-matrices-with-instead-of-0/?comment=22287#post-id-22287Comment by kcrisman for <p>How about</p>
<pre><code>m = matrix([[0,0,1], [1,0,0]])
print m.str().replace(' 0', ' .').replace('[0', '[.')
</code></pre>
<p>Should we add a method to the matrix class which does something like this? What should it be called?</p>
https://ask.sagemath.org/question/7864/displaying-matrices-with-instead-of-0/?comment=22292#post-id-22292Maybe m.print_with_dots()? zeros_are_dots()? Not sure... Then one followup would be whether we should enable this only for finite field matrices as in GAP (at least as I understand it in GAP). And whether we should enable blank spaces in sparse matrices. Etc.Wed, 12 Jan 2011 22:37:15 +0100https://ask.sagemath.org/question/7864/displaying-matrices-with-instead-of-0/?comment=22292#post-id-22292Comment by Ivan Andrus for <p>How about</p>
<pre><code>m = matrix([[0,0,1], [1,0,0]])
print m.str().replace(' 0', ' .').replace('[0', '[.')
</code></pre>
<p>Should we add a method to the matrix class which does something like this? What should it be called?</p>
https://ask.sagemath.org/question/7864/displaying-matrices-with-instead-of-0/?comment=20355#post-id-20355It can do that. It just has convenience parameters for what I thought would be the most common use cases. In fact `rep_mapping` can be either a map or a function.Fri, 03 Feb 2012 06:43:40 +0100https://ask.sagemath.org/question/7864/displaying-matrices-with-instead-of-0/?comment=20355#post-id-20355Comment by John Palmieri for <p>How about</p>
<pre><code>m = matrix([[0,0,1], [1,0,0]])
print m.str().replace(' 0', ' .').replace('[0', '[.')
</code></pre>
<p>Should we add a method to the matrix class which does something like this? What should it be called?</p>
https://ask.sagemath.org/question/7864/displaying-matrices-with-instead-of-0/?comment=22285#post-id-22285See http://trac.sagemath.org/sage_trac/ticket/10627.Thu, 13 Jan 2011 15:48:23 +0100https://ask.sagemath.org/question/7864/displaying-matrices-with-instead-of-0/?comment=22285#post-id-22285Comment by Ivan Andrus for <p>How about</p>
<pre><code>m = matrix([[0,0,1], [1,0,0]])
print m.str().replace(' 0', ' .').replace('[0', '[.')
</code></pre>
<p>Should we add a method to the matrix class which does something like this? What should it be called?</p>
https://ask.sagemath.org/question/7864/displaying-matrices-with-instead-of-0/?comment=22291#post-id-22291Considering that sometimes I convert a matrix to one over a finite field just so that I can display it like this, I would prefer if the method worked for any matrix.Thu, 13 Jan 2011 03:17:56 +0100https://ask.sagemath.org/question/7864/displaying-matrices-with-instead-of-0/?comment=22291#post-id-22291Comment by kcrisman for <p>How about</p>
<pre><code>m = matrix([[0,0,1], [1,0,0]])
print m.str().replace(' 0', ' .').replace('[0', '[.')
</code></pre>
<p>Should we add a method to the matrix class which does something like this? What should it be called?</p>
https://ask.sagemath.org/question/7864/displaying-matrices-with-instead-of-0/?comment=22290#post-id-22290Sounds like you need to open a ticket :) Open source is driven by developer interest, as you know. But in that case I vote for an option to leave zero spots blank as well, so it doesn't look so much like a Nethack screen.Thu, 13 Jan 2011 10:37:36 +0100https://ask.sagemath.org/question/7864/displaying-matrices-with-instead-of-0/?comment=22290#post-id-22290Comment by John Palmieri for <p>How about</p>
<pre><code>m = matrix([[0,0,1], [1,0,0]])
print m.str().replace(' 0', ' .').replace('[0', '[.')
</code></pre>
<p>Should we add a method to the matrix class which does something like this? What should it be called?</p>
https://ask.sagemath.org/question/7864/displaying-matrices-with-instead-of-0/?comment=22288#post-id-22288We can add some optional arguments to the "str" method, to be used like mat.str(zero="."), for example. What sort of replacements might we want? Replace "0" with something else? Replace "1" with "+" and "-1" with "-"?Thu, 13 Jan 2011 13:03:14 +0100https://ask.sagemath.org/question/7864/displaying-matrices-with-instead-of-0/?comment=22288#post-id-22288Answer by Ivan Andrus for <p>In GAP it is possible (at least in some circumstances) to display a matrix with <code>.</code> instead of <code>0</code> and without brackets. This makes the structure of the matrix much easier to see. Is there a way to do this in Sage?</p>
<p>e.g. to show</p>
<pre> 1 3 3 . 9
1 1 1 2 5
1 1 3 2 3
1 3 1 . 3
1 1 1 . 5
1 . . 1 3
1 1 3 . 3
1 3 1 . 3
1 1 1 . 3
1 3 . . .
1 . 1 . 2
</pre>
<p>instead of </p>
<pre>[ [ 1, 3, 3, 0, 9 ],
[ 1, 1, 1, 2, 5 ],
[ 1, 1, 3, 2, 3 ],
[ 1, 3, 1, 0, 3 ],
[ 1, 1, 1, 0, 5 ],
[ 1, 0, 0, 1, 3 ],
[ 1, 1, 3, 0, 3 ],
[ 1, 3, 1, 0, 3 ],
[ 1, 1, 1, 0, 3 ],
[ 1, 3, 0, 0, 0 ],
[ 1, 0, 1, 0, 2 ] ]</pre>
https://ask.sagemath.org/question/7864/displaying-matrices-with-instead-of-0/?answer=13239#post-id-13239Below is what I currently have in my `init.sage` to mimic GAP printing. It works great if there are few different values or they are all related, but not well at all if there are many different unrelated values.
def increment_string(s):
"""
Magically increment a string a la perl
Arguments:
- `str`:
"""
if s == "":
return "A"
elif s[-1] == 'Z':
return increment_string(s[:-1]) + 'A'
else:
return s[:-1] + chr(ord(s[-1]) + 1)
def pretty_print(M,maxlen=5,suppress_legend=False):
elements = [x for x in Set(M.list()) if len(repr(x)) > maxlen]
specials={M.base_ring().zero():'.'}
if len(elements) == 0:
print M.str(specials)
return
cur_label=""
for x in elements:
if specials.has_key(-x):
specials[x]='-'+specials[-x]
elif specials.has_key(conjugate(x)):
specials[x]='/'+specials[conjugate(x)]
elif specials.has_key(-conjugate(x)):
specials[x]='-/'+specials[-conjugate(x)]
else:
cur_label = increment_string(cur_label)
specials[x] = cur_label
s = M.str(specials)
if not suppress_legend:
# "Sort by value"
from operator import itemgetter
for key,val in sorted(specials.iteritems(), key=itemgetter(1)):
k = val[0]
if k != '-' and k != '/' and k != '.':
s += "\n"+val+" = "+repr(key)
print s
pp = pretty_print
Fri, 03 Feb 2012 06:49:15 +0100https://ask.sagemath.org/question/7864/displaying-matrices-with-instead-of-0/?answer=13239#post-id-13239