show() method for all sage objects

135 views
Skip to first unread message

Kwankyu Lee

unread,
May 18, 2019, 7:29:58 PM5/18/19
to sage-devel
Hi,

How about adding show() method to the class of sage objects, so that we can do

mat.transpose().show()

instead of

show(mat.transpose())

which is cumbersome to add to and remove from mat.transpose()? This is useful in Jupyter notebook.


Kwankyu

Volker Braun

unread,
May 19, 2019, 4:48:23 AM5/19/19
to sage-devel
If you find yourself typing show() all the time you probably want to change the displayhook to typeset stuff, i.e.  you want "%display typeset" in the notebook or on the command line:

sage: m = random_matrix(ZZ, 3)
sage: m
[12 -5  6]
[ 1 -1  1]
[-1  2  1]
sage: %display unicode_art
sage: m
⎛12 -5  6⎞
⎜ 1 -1  1⎟
⎝-1  2  1⎠

In general, I would lean more on the side of reasonable defaults (e.g. why is %display typeset not the default in the notebook, its easier for power users to turn it off than for novices to turn it on) than a proliferation of more display-related methods.

Kwankyu Lee

unread,
May 19, 2019, 5:13:32 AM5/19/19
to sage-devel


On Sunday, May 19, 2019 at 5:48:23 PM UTC+9, Volker Braun wrote:
If you find yourself typing show() all the time you probably want to change the displayhook to typeset stuff, i.e.  you want "%display typeset" in the notebook or on the command line:

sage: m = random_matrix(ZZ, 3)
sage: m
[12 -5  6]
[ 1 -1  1]
[-1  2  1]
sage: %display unicode_art
sage: m
⎛12 -5  6⎞
⎜ 1 -1  1⎟
⎝-1  2  1⎠

I had thought of even proposing show_unicode_art() method for sage objects :-)
 
In general, I would lean more on the side of reasonable defaults (e.g. why is %display typeset not the default in the notebook, its easier for power users to turn it off than for novices to turn it on) than a proliferation of more display-related methods.

I would prefer the current default %display plain in the notebook though I am a novice in this regard.

Eric Gourgoulhon

unread,
May 19, 2019, 8:50:22 AM5/19/19
to sage-devel
Le dimanche 19 mai 2019 10:48:23 UTC+2, Volker Braun a écrit :

In general, I would lean more on the side of reasonable defaults (e.g. why is %display typeset not the default in the notebook, its easier for power users to turn it off than for novices to turn it on) than a proliferation of more display-related methods.


A big +1 to this.
LaTeX is arguably the best way to display mathematics and one may wonder why "%display latex" is not the default in the Jupyter notebook. If one would like the plain text view of an object X instead, one can use print(X).
So, in the Jupyter notebook, instead of having (as of today)

X.show() --> LaTeX typeset
X  --> plain text

we could have

X  --> LaTeX typeset
print(X)  --> plain text

By the way, a proper way to implement this could be to simply add the following method to SageObject:

    def _repr_latex_(self):
        return '$' + str(latex(self)) + '$'

Then the LaTeX display is taken in charge by the *native* Jupyter rich output mechanism (there is no need of "%display latex", which is a magic implemented at the Sage level) . Moreover, this would fix the long standing issue of pdf export reported in
I've discussed this point with Fernando Perez at the CIRM meeting in February and he recommends that we use Jupyter's _repr_latex_.  See
for more details on customizing Jupyter rich output.

Eric.

Kwankyu Lee

unread,
May 20, 2019, 1:55:30 AM5/20/19
to sage-devel


On Sunday, May 19, 2019 at 9:50:22 PM UTC+9, Eric Gourgoulhon wrote:
LaTeX is arguably the best way to display mathematics

I agree with this, but sage objects are not mathematics.
 
If one would like the plain text view of an object X instead, one can use print(X).
So, in the Jupyter notebook, instead of having

X.show() --> LaTeX typeset
X  --> plain text
 
+1 as default.
 
X  --> LaTeX typeset
print(X)  --> plain text

-1 as default. Moreover many sage objects currently are displayed broken if typeset. 

Eric Gourgoulhon

unread,
May 21, 2019, 10:29:00 AM5/21/19
to sage-devel
Le lundi 20 mai 2019 07:55:30 UTC+2, Kwankyu Lee a écrit :
. Moreover many sage objects currently are displayed broken if typeset. 

In that case, a solution could be to introduce a new class, TypesetSageObject, say, to implement _repr_latex_() in it and to make Sage objects that can be correctly typeset inheritate from it.
For instance, this could be done for all manifold objects and for symbolic expressions (class Expression).

Eric.

Simon King

unread,
May 22, 2019, 3:15:10 AM5/22/19
to sage-...@googlegroups.com
Hi Eric,
A lot of Sage objects are written in Cython. So, a potential problem of
that approach would be multiple inheritance.

Best regards,
Simon

Eric Gourgoulhon

unread,
May 22, 2019, 4:49:33 AM5/22/19
to sage-devel
Hi Simon,

Le mercredi 22 mai 2019 09:15:10 UTC+2, Simon King a écrit :

A lot of Sage objects are written in Cython. So, a potential problem of
that approach would be multiple inheritance.



Ah yes, that's right...

According to
if _repr_latex_() returns None then it falls back to repr(). So a strategy could be to implement _repr_latex_ in SageObject (I guess all Cython objects are inhereting from it) as follows:

    def _repr_latex_(self):
       
from sage.misc.latex import latex
       
if not hasattr(self, '_latex_'):
           
return None

       
return '$' + str(latex(self)) + '$'

This assumes that if an object has a _latex_ method, it works properly...
I've tested the code above and it fully solves https://trac.sagemath.org/ticket/23330

Best regards,
Eric.


Eric Gourgoulhon

unread,
May 22, 2019, 5:13:44 AM5/22/19
to sage-devel
Le mercredi 22 mai 2019 10:49:33 UTC+2, Eric Gourgoulhon a écrit :
I've tested the code above and it fully solves https://trac.sagemath.org/ticket/23330


PS: if someone would like to have a look, I've pushed the code to the trac branch:

Simon King

unread,
May 22, 2019, 10:11:50 AM5/22/19
to sage-...@googlegroups.com
Hi Eric,

On 2019-05-22, Eric Gourgoulhon <egourg...@gmail.com> wrote:
> According to
> https://ipython.readthedocs.io/en/stable/config/integrating.html
> if _repr_latex_() returns None then it falls back to repr(). So a strategy
> could be to implement _repr_latex_ in SageObject (I guess all Cython
> objects are inhereting from it) as follows:
>
> def _repr_latex_(self):
> from sage.misc.latex import latex
> if not hasattr(self, '_latex_'):
> return None
> return '$' + str(latex(self)) + '$'

That sounds reasonable to me, and is basically what we do with Python's
magical (double underscore) methods (namely: provide a reasonable
default that shouldn't be overridden).

Best regards,
Simon

Reply all
Reply to author
Forward
0 new messages