force sage to print a matrix

725 views
Skip to first unread message

amps

unread,
Aug 21, 2009, 6:02:41 PM8/21/09
to sage-support
I have a program that outputs a matrix for certain values of n. for
n=5 it works fine but for n=6 it just outputs

29 x 29 dense matrix over Integer Ring

how can I force sage to output the actual matrix?

Simon King

unread,
Aug 21, 2009, 6:18:38 PM8/21/09
to sage-support
Perhaps
sage: M = random_matrix(ZZ,29,29)
sage: print M.str()
but there might be nicer (and more intuitive!) ways.

Cheers,
Simon

William Stein

unread,
Aug 21, 2009, 6:57:39 PM8/21/09
to sage-s...@googlegroups.com

I think that is the only way.  I can't think of any nicer way.  Any idea what it *should* be?

 -- William

Simon King

unread,
Aug 21, 2009, 7:49:52 PM8/21/09
to sage-support
On 22 Aug., 00:57, William Stein <wst...@gmail.com> wrote:
> On Fri, Aug 21, 2009 at 3:18 PM, Simon King <simon.k...@nuigalway.ie> wrote:
[...]
> >  sage: print M.str()
> > but there might be nicer (and more intuitive!) ways.
>
> I think that is the only way.  I can't think of any nicer way.  Any idea
> what it *should* be?

sage: M
29 x 29 dense matrix over Integer Ring
sage: print M
[ 100 201 892 ...

In other words: If the user types a *command* to display M, then one
can assume that (s)he really wants to see something (here: content of
M). But if M is addressed without a command, it might be better to not
confront the user with lengthy data.

It's just a version of "explicit is better than implicit" - namely
"using a command means more than not using a command".
Therefore I believe that __repr__() should not necessarily coincide
with __str__().

Why do I think that "print M.str()" is not intuitive? Since it is
*two* commands, str() and print (without print it looks ugly).
So, better might be a method with a descriptive name such as
"prettyprint", "display", "print_all", etc. that returns None (rather
than a string) but prints M to stdout.

One problem in that approach is that there is already show() and plot
() and so on. So, if there are too many display-related methods, it
might be difficult to tell them apart. Perhaps: show() uses graphic,
display() uses ascii art.

Cheers,
Simon

Robert Bradshaw

unread,
Aug 21, 2009, 8:34:57 PM8/21/09
to sage-support
On Fri, 21 Aug 2009, Simon King wrote:

>
> On 22 Aug., 00:57, William Stein <wst...@gmail.com> wrote:
>> On Fri, Aug 21, 2009 at 3:18 PM, Simon King <simon.k...@nuigalway.ie> wrote:
> [...]
>>>  sage: print M.str()
>>> but there might be nicer (and more intuitive!) ways.
>>
>> I think that is the only way.  I can't think of any nicer way.  Any idea
>> what it *should* be?
>
> sage: M
> 29 x 29 dense matrix over Integer Ring
> sage: print M
> [ 100 201 892 ...
>
> In other words: If the user types a *command* to display M, then one
> can assume that (s)he really wants to see something (here: content of
> M). But if M is addressed without a command, it might be better to not
> confront the user with lengthy data.
>
> It's just a version of "explicit is better than implicit" - namely
> "using a command means more than not using a command".
> Therefore I believe that __repr__() should not necessarily coincide
> with __str__().

One problem with this is that then one can't do "print M" and get what one
expects from a non-terminal line (e.g. inside a function, or anything but
the last line of a notebook cell).

> Why do I think that "print M.str()" is not intuitive? Since it is
> *two* commands, str() and print (without print it looks ugly).
> So, better might be a method with a descriptive name such as
> "prettyprint", "display", "print_all", etc. that returns None (rather
> than a string) but prints M to stdout.
>
> One problem in that approach is that there is already show() and plot
> () and so on. So, if there are too many display-related methods, it
> might be difficult to tell them apart. Perhaps: show() uses graphic,
> display() uses ascii art.

+1 to somethign like display().

Really, there should be a way to set a threshold for something like this.

- Robert

William Stein

unread,
Aug 21, 2009, 9:02:50 PM8/21/09
to sage-s...@googlegroups.com

I like Robert's suggestion.   How about make the compressed display helpful:

sage: M
29 x 29 dense matrix over Integer Ring (use config.matrix_print to print larger matrices)
sage: config.matrix_print?
tells me how to use it...
sage: config.matrix_print(50)
sage: M
[...]

The config object could allow for a wide range of print configuration and other options.  

 -- William

kcrisman

unread,
Aug 21, 2009, 10:26:17 PM8/21/09
to sage-support
This is not as nice, but I have this problem a lot, and just do
sage: M.rows()
which at least lets me look at the matrix, if that's all you want.
However, the suggestions above are great for a "real" solution.
- kcrisman

Jason Grout

unread,
Aug 21, 2009, 10:49:29 PM8/21/09
to sage-s...@googlegroups.com
Robert Bradshaw wrote:
> On Fri, 21 Aug 2009, Simon King wrote:
>
>> On 22 Aug., 00:57, William Stein <wst...@gmail.com> wrote:
>>> On Fri, Aug 21, 2009 at 3:18 PM, Simon King <simon.k...@nuigalway.ie> wrote:
>> [...]
>>>> sage: print M.str()
>>>> but there might be nicer (and more intuitive!) ways.
>>> I think that is the only way. I can't think of any nicer way. Any idea
>>> what it *should* be?
>> sage: M
>> 29 x 29 dense matrix over Integer Ring
>> sage: print M
>> [ 100 201 892 ...
>>
>> In other words: If the user types a *command* to display M, then one
>> can assume that (s)he really wants to see something (here: content of
>> M). But if M is addressed without a command, it might be better to not
>> confront the user with lengthy data.
>>
>> It's just a version of "explicit is better than implicit" - namely
>> "using a command means more than not using a command".
>> Therefore I believe that __repr__() should not necessarily coincide
>> with __str__().
>
> One problem with this is that then one can't do "print M" and get what one
> expects from a non-terminal line (e.g. inside a function, or anything but
> the last line of a notebook cell).


Can you elaborate? I'm not sure what you're trying to say---what would
one expect? I thought Simon was saying that print M would lead a user
to expect the matrix to be printed out.

I find print M.str() frustrating to remember, so I'm glad this may be
resolved now. I typically first try "M", and then my natural instinct
is to try "print M", thinking that if I tell Sage that I really want it
to print everything out, it will.

Thanks,

Jason

Simon King

unread,
Aug 22, 2009, 3:56:30 AM8/22/09
to sage-support
Hi Jason,

On 22 Aug., 04:49, Jason Grout <jason-s...@creativetrax.com> wrote:
> Robert Bradshaw wrote:
[...]
> > One problem with this is that then one can't do "print M" and get what one
> > expects from a non-terminal line (e.g. inside a function, or anything but
> > the last line of a notebook cell).
>
> Can you elaborate?  I'm not sure what you're trying to say---what would
> one expect?  I thought Simon was saying that print M would lead a user
> to expect the matrix to be printed out.

I think that means: Suppose you do want the output "29 x 29 dense
matrix over Integer Ring", but you are inside a function. If inside
the function you said "M", nothing would appear; if you said "print
M", then (according to my original suggestion) everything would be
shown, although this time you don't want it. You could do "print
M.__repr__()" inside the function, but that's not nice.

A configuration object would be a nice thing to have. And perhaps
*additionally* a display method that shows the matrix no matter what.

Cheers,
Simon

Jason Grout

unread,
Aug 22, 2009, 5:15:03 AM8/22/09
to sage-s...@googlegroups.com
Simon King wrote:
> Hi Jason,
>
> On 22 Aug., 04:49, Jason Grout <jason-s...@creativetrax.com> wrote:
>> Robert Bradshaw wrote:
> [...]
>>> One problem with this is that then one can't do "print M" and get what one
>>> expects from a non-terminal line (e.g. inside a function, or anything but
>>> the last line of a notebook cell).
>> Can you elaborate? I'm not sure what you're trying to say---what would
>> one expect? I thought Simon was saying that print M would lead a user
>> to expect the matrix to be printed out.
>
> I think that means: Suppose you do want the output "29 x 29 dense
> matrix over Integer Ring", but you are inside a function. If inside
> the function you said "M", nothing would appear; if you said "print
> M", then (according to my original suggestion) everything would be
> shown, although this time you don't want it. You could do "print
> M.__repr__()" inside the function, but that's not nice.
>

Of course, you'd probably do repr(M) instead, which is more standard
Python. But that's not something I'd expect someone to be able to guess
naturally. And I don't think using repr would work anyway, since that
is what is used for output when you just type M.

print M prints M.__str__()

M by itself prints out M.__repr__()

> A configuration object would be a nice thing to have. And perhaps
> *additionally* a display method that shows the matrix no matter what.
>

+1 on a function that does this, without having to set an attribute. +1
also on making sure that we don't keep adding more and more
special-purpose display functions without making them have some sort of
consistent scheme. +1 also on making the "display" function name
somehow indicate how it is different than "show" or "prettyprint" or
other similar-sounding functions.

Jason

--
Jason Grout

Simon King

unread,
Aug 22, 2009, 5:31:27 AM8/22/09
to sage-support
Hi Jason!
On 22 Aug., 11:15, Jason Grout <jason-s...@creativetrax.com> wrote:
> Simon King wrote:
[...]
> Of course, you'd probably do repr(M) instead, which is more standard
> Python. [...]

repr(M) returns a string, but doesn't print it. So, in order to
display repr(M) inside a function, you'd do
print repr(M)
which is not nice.

> +1 on a function that does this, without having to set an attribute.  +1
> also on making sure that we don't keep adding more and more
> special-purpose display functions without making them have some sort of
> consistent scheme. [...]

Yes, consistency should be taken serious.

Cheers,
Simon
Reply all
Reply to author
Forward
0 new messages