sagemath / sage

Main repository of SageMath. Now open for Issues and Pull Requests.
https://www.sagemath.org
Other
1.21k stars 419 forks source link

Move ascii art to _ascii_art_ #18328

Open vbraun opened 9 years ago

vbraun commented 9 years ago

There are probably multiple places where _repr_() returns ascii art and not a string that can be composed with other strings. That then leads to issues like

sage: pi.n(100), CartanMatrix("E8~").dynkin_diagram()
(3.1415926535897932384626433833,         O 2
         |
         |
 O---O---O---O---O---O---O---O
 1   3   4   5   6   7   8   0   
 E8~)

The solution should be to move ascii art to the magic _ascii_art_ method.

Unfortunately, the IPython commandline defaults to plain text output. So you either have to switch

sage: %display ascii_art

or override _rich_repr_ to make ascii art the default for your object. Perhaps the default display preference on the commandline should be changed to ascii art.

Component: user interface

Issue created by migration from https://trac.sagemath.org/ticket/18328

vbraun commented 9 years ago
comment:1

PS: ascii art output would look like this

sage: %display ascii_art
sage: pi.n(100), CartanMatrix("E8~").dynkin_diagram()
(                                         O 2                      )
(                                         |                        )
(                                         |                        )
(                                 O---O---O---O---O---O---O---O    )
(                                 1   3   4   5   6   7   8   0    )
( 3.1415926535897932384626433833, E8~                              )
mezzarobba commented 9 years ago
comment:2

Related: #15565.

mantepse commented 9 years ago
comment:3

For the record, this is the decision Axiom (maybe better known here as FriCAS) made a few decades ago, output is ascii-art. The downside is, of course, that you cannot copy output into the clipboard and reuse it as input. However, especially for symbolic expressions involving sums, products, powers, fractions, the ascii-art is a great convenience!

Back to sage, as mentioned by Marc, also (small) matrices are always typeset as ascii-art, similar to Dynkin diagrams. Are there any others?

vbraun commented 9 years ago
comment:4

Note that we have sage_input (though objects with fancy representation need to explictly support it):

sage: polytopes.hypercube(3)
A 3-dimensional polyhedron in ZZ^3 defined as the convex hull of 8 vertices
sage: sage_input(_)
Polyhedron(base_ring=ZZ, vertices=[(-1, -1, -1), (-1, -1, 1), (-1, 1, -1), (-1, 1, 1), (1, -1, -1), (1, -1, 1), (1, 1, -1), (1, 1, 1)])
mantepse commented 9 years ago
comment:5

am I right in guessing that _repr_ contains ascii-art if it returns a string which contains a newline (i.e., \n)?

If so, it should be quite possible to collect at least a few "bad guys"...

vbraun commented 9 years ago
comment:6

The plain displayhook actually has support for ascii-art matrices

sage: %display plain
sage: pi.n(100), identity_matrix(2)
(
                                [1 0]
3.1415926535897932384626433833, [0 1]
)

but its hardcoded to apply to matrices only. The ascii art output is much more flexible.