Ask Your Question
1

Any way to make Sage display large matrices all in 1 line?

asked 2019-01-07 13:27:00 +0100

Stockfish3709 gravatar image

Hi all,

Currently I have this code that generates large matrices.

B = BooleanPolynomialRing(25,'x', order = 'degneglex')
from sage.rings.polynomial.toy_variety import coefficient_matrix
from brial import *
v = BooleanPolynomialVector()
l = [B.random_element(degree = 3, terms = 4) for j in range(25)]
_ = [v.append(e) for e in l]

from sage.rings.polynomial.toy_variety import coefficient_matrix
print l
%time A, v = Sequence(l,B).coefficient_matrix()
print A
print v

The matrix generated is big, so big that it takes 2 lines to display all of it. It is hard to visualise. Is there some way of making each row of the matrix inline together such that they are not separated into different lines?

edit retag flag offensive close merge delete

Comments

Are you working in the Sage REPL (read-eval-print loop), also known as the command-line interface (in a terminal), or in the Jupyter Notebook, or in the SageNB notebook, or in SageTeX, or other?

slelievre gravatar imageslelievre ( 2019-01-07 13:55:13 +0100 )edit

Hi, thanks for the reply. My laptop in my office has Sage in it and is running on SageNB. However, I also use CoCalc online by starting a new Sage worksheet. Not sure if it makes a difference, but all the better if I can get both formats to display matrices correctly.

Stockfish3709 gravatar imageStockfish3709 ( 2019-01-07 14:35:09 +0100 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2019-01-07 14:34:38 +0100

slelievre gravatar image

updated 2019-01-07 14:36:35 +0100

Here is a silly answer, along with hopefully less silly ones.

Silly answer: just make the font size small enough so lines fit in the screen's width:

Imgur

Less silly: the visualize_structure method gives a graphical view of the nonzero positions:

sage: A.visualize_structure()

Imgur

Listing nonzero positions in each row is also a sensible (and copy-pasteable) way to visualize the matrix:

sage: dict((i, A.nonzero_positions_in_row(i)) for i in range(A.nrows()))
{0: [0, 7, 14, 67],
 1: [13, 85, 86, 93],
 2: [4, 11, 12, 38],
 3: [8, 16, 40, 75],
 4: [3, 32, 39, 78],
 5: [22, 47, 58, 79],
 6: [5, 26, 57, 68],
 7: [44, 65, 81, 87],
 8: [37, 88, 90, 91],
 9: [6, 46, 52, 84],
 10: [36, 43, 61, 95],
 11: [9, 20, 49, 92],
 12: [33, 34, 45, 71],
 13: [27, 50, 56, 69],
 14: [25, 41, 55, 72],
 15: [1, 19, 23, 30],
 16: [17, 64, 73, 80],
 17: [24, 48, 60, 74],
 18: [54, 70, 76, 83],
 19: [10, 18, 21, 51],
 20: [29, 35, 94, 97],
 21: [31, 42, 62, 96],
 22: [15, 53, 77, 98],
 23: [2, 59, 82, 89],
 24: [3, 28, 63, 66]}

One could also skip the whitespace by custom printing the matrix as follows:

sage: for row in A:
....:     print(''.join([str(a) for a in row]))
....:
100000010000001000000000000000000000000000000000000000000000000000010000000000000000000000000000000
000000000000010000000000000000000000000000000000000000000000000000000000000000000000011000000100000
000010000001100000000000000000000000001000000000000000000000000000000000000000000000000000000000000
000000001000000010000000000000000000000010000000000000000000000000000000000100000000000000000000000
000100000000000000000000000000001000000100000000000000000000000000000000000000100000000000000000000
000000000000000000000010000000000000000000000001000000000010000000000000000000010000000000000000000
000001000000000000000000001000000000000000000000000000000100000000001000000000000000000000000000000
000000000000000000000000000000000000000000001000000000000000000001000000000000000100000100000000000
000000000000000000000000000000000000010000000000000000000000000000000000000000000000000010110000000
000000100000000000000000000000000000000000000010000010000000000000000000000000000000100000000000000
000000000000000000000000000000000000100000010000000000000000010000000000000000000000000000000001000
000000000100000000001000000000000000000000000000010000000000000000000000000000000000000000001000000
000000000000000000000000000000000110000000000100000000000000000000000001000000000000000000000000000
000000000000000000000000000100000000000000000000001000001000000000000100000000000000000000000000000
000000000000000000000000010000000000000001000000000000010000000000000000100000000000000000000000000
010000000000000000010001000000100000000000000000000000000000000000000000000000000000000000000000000
000000000000000001000000000000000000000000000000000000000000000010000000010000001000000000000000000
000000000000000000000000100000000000000000000000100000000000100000000000001000000000000000000000000
000000000000000000000000000000000000000000000000000000100000000000000010000010000001000000000000000
000000000010000000100100000000000000000000000000000100000000000000000000000000000000000000000000000
000000000000000000000000000001000001000000000000000000000000000000000000000000000000000000000010010
000000000000000000000000000000010000000000100000000000000000001000000000000000000000000000000000100
000000000000000100000000000000000000000000000000000001000000000000000000000001000000000000000000001
001000000000000000000000000000000000000000000000000000000001000000000000000000000010000001000000000
000100000000000000000000000010000000000000000000000000000000000100100000000000000000000000000000000

or, replacing zeros by dots:

sage: for row in A:
....:     print(''.join([str(a) if a else '.' for a in row]))
....:
1......1......1....................................................1...............................
.............1.......................................................................11......1.....
....1......11.........................1............................................................
........1.......1.......................1..................................1.......................
...1............................1......1......................................1....................
......................1........................1..........1....................1...................
.....1....................1..............................1..........1..............................
............................................1....................1...............1.....1...........
.....................................1..................................................1.11.......
......1.......................................1.....1...............................1..............
....................................1......1.................1.................................1...
.........1..........1............................1..........................................1......
.................................11..........1.........................1...........................
...........................1......................1.....1............1.............................
.........................1...............1.............1................1..........................
.1.................1...1......1....................................................................
.................1..............................................1........1......1..................
........................1.......................1...........1.............1........................
......................................................1...............1.....1......1...............
..........1.......1..1.............................1...............................................
.............................1.....1..........................................................1..1.
...............................1..........1...................1.................................1..
...............1.....................................1.......................1....................1
..1........................................................1......................1......1.........
...1........................1..................................1..1................................

The above can be obtained in a simpler way, starting from the matrix's str and using replace once:

sage: print(A.str().replace(' ', ''))
[100000010000001000000000000000000000000000000000000000000000000000010000000000000000000000000000000]
[000000000000010000000000000000000000000000000000000000000000000000000000000000000000011000000100000]
[000010000001100000000000000000000000001000000000000000000000000000000000000000000000000000000000000]
[000000001000000010000000000000000000000010000000000000000000000000000000000100000000000000000000000]
[000100000000000000000000000000001000000100000000000000000000000000000000000000100000000000000000000]
[000000000000000000000010000000000000000000000001000000000010000000000000000000010000000000000000000]
[000001000000000000000000001000000000000000000000000000000100000000001000000000000000000000000000000]
[000000000000000000000000000000000000000000001000000000000000000001000000000000000100000100000000000]
[000000000000000000000000000000000000010000000000000000000000000000000000000000000000000010110000000]
[000000100000000000000000000000000000000000000010000010000000000000000000000000000000100000000000000]
[000000000000000000000000000000000000100000010000000000000000010000000000000000000000000000000001000]
[000000000100000000001000000000000000000000000000010000000000000000000000000000000000000000001000000]
[000000000000000000000000000000000110000000000100000000000000000000000001000000000000000000000000000]
[000000000000000000000000000100000000000000000000001000001000000000000100000000000000000000000000000]
[000000000000000000000000010000000000000001000000000000010000000000000000100000000000000000000000000]
[010000000000000000010001000000100000000000000000000000000000000000000000000000000000000000000000000]
[000000000000000001000000000000000000000000000000000000000000000010000000010000001000000000000000000]
[000000000000000000000000100000000000000000000000100000000000100000000000001000000000000000000000000]
[000000000000000000000000000000000000000000000000000000100000000000000010000010000001000000000000000]
[000000000010000000100100000000000000000000000000000100000000000000000000000000000000000000000000000]
[000000000000000000000000000001000001000000000000000000000000000000000000000000000000000000000010010]
[000000000000000000000000000000010000000000100000000000000000001000000000000000000000000000000000100]
[000000000000000100000000000000000000000000000000000001000000000000000000000001000000000000000000001]
[001000000000000000000000000000000000000000000000000000000001000000000000000000000010000001000000000]
[000100000000000000000000000010000000000000000000000000000000000100100000000000000000000000000000000]

or twice:

sage: print(A.str().replace(' ', '').replace('0', '.'))
[1......1......1....................................................1...............................]
[.............1.......................................................................11......1.....]
[....1......11.........................1............................................................]
[........1.......1.......................1..................................1.......................]
[...1............................1......1......................................1....................]
[......................1........................1..........1....................1...................]
[.....1....................1..............................1..........1..............................]
[............................................1....................1...............1.....1...........]
[.....................................1..................................................1.11.......]
[......1.......................................1.....1...............................1..............]
[....................................1......1.................1.................................1...]
[.........1..........1............................1..........................................1......]
[.................................11..........1.........................1...........................]
[...........................1......................1.....1............1.............................]
[.........................1...............1.............1................1..........................]
[.1.................1...1......1....................................................................]
[.................1..............................................1........1......1..................]
[........................1.......................1...........1.............1........................]
[......................................................1...............1.....1......1...............]
[..........1.......1..1.............................1...............................................]
[.............................1.....1..........................................................1..1.]
[...............................1..........1...................1.................................1..]
[...............1.....................................1.......................1....................1]
[..1........................................................1......................1......1.........]
[...1........................1..................................1..1................................]

(one could remove the brackets using .replace('[', '').replace(']', '')).

edit flag offensive delete link more
1

answered 2019-01-07 19:43:32 +0100

tmonteil gravatar image

updated 2019-01-07 20:31:14 +0100

I do not know about cocalc, but on sagenb and jupyter you can simply do:

show(A)

Or, if you want to see dots on the nonzero indices:

plot(A)

Or, if you want to see the matrix in a pdf file:

view(A)
edit flag offensive delete link more

Comments

Slight improvement: matrix_plot(A) displays the dots in a frame, as would plot(A, axes=False, frame=True).

slelievre gravatar imageslelievre ( 2019-01-08 05:18:08 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2019-01-07 13:27:00 +0100

Seen: 864 times

Last updated: Jan 07 '19